forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpSelectionValidator.cs
More file actions
557 lines (460 loc) · 24.1 KB
/
Copy pathCSharpSelectionValidator.cs
File metadata and controls
557 lines (460 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.LanguageService;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ExtractMethod;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.ExtractMethod;
internal sealed partial class CSharpSelectionValidator(
SemanticDocument document,
TextSpan textSpan,
bool localFunction) : SelectionValidator<CSharpSelectionResult, StatementSyntax>(document, textSpan)
{
private readonly bool _localFunction = localFunction;
public override async Task<(CSharpSelectionResult, OperationStatus)> GetValidSelectionAsync(CancellationToken cancellationToken)
{
if (!ContainsValidSelection)
return (null, OperationStatus.FailedWithUnknownReason);
var text = SemanticDocument.Text;
var root = SemanticDocument.Root;
var model = SemanticDocument.SemanticModel;
var doc = SemanticDocument;
// go through pipe line and calculate information about the user selection
var selectionInfo = GetInitialSelectionInfo(root, text);
selectionInfo = AssignInitialFinalTokens(selectionInfo, root, cancellationToken);
selectionInfo = AdjustFinalTokensBasedOnContext(selectionInfo, model, cancellationToken);
selectionInfo = AssignFinalSpan(selectionInfo, text);
selectionInfo = ApplySpecialCases(selectionInfo, text, SemanticDocument.SyntaxTree.Options, _localFunction);
selectionInfo = CheckErrorCasesAndAppendDescriptions(selectionInfo, root);
// there was a fatal error that we couldn't even do negative preview, return error result
if (selectionInfo.Status.Failed)
return (null, selectionInfo.Status);
var controlFlowSpan = GetControlFlowSpan(selectionInfo);
if (!selectionInfo.SelectionInExpression)
{
var statementRange = GetStatementRangeContainedInSpan<StatementSyntax>(root, controlFlowSpan, cancellationToken);
if (statementRange == null)
{
selectionInfo = selectionInfo.WithStatus(s => s.With(succeeded: false, CSharpFeaturesResources.Cannot_determine_valid_range_of_statements_to_extract));
return (null, selectionInfo.Status);
}
var isFinalSpanSemanticallyValid = IsFinalSpanSemanticallyValidSpan(model, controlFlowSpan, statementRange.Value, cancellationToken);
if (!isFinalSpanSemanticallyValid)
{
// check control flow only if we are extracting statement level, not expression
// level. you can not have goto that moves control out of scope in expression level
// (even in lambda)
selectionInfo = selectionInfo.WithStatus(s => s.With(succeeded: true, FeaturesResources.Not_all_code_paths_return));
}
}
var selectionChanged = selectionInfo.FirstTokenInOriginalSpan != selectionInfo.FirstTokenInFinalSpan || selectionInfo.LastTokenInOriginalSpan != selectionInfo.LastTokenInFinalSpan;
var result = await CSharpSelectionResult.CreateAsync(
selectionInfo.OriginalSpan,
selectionInfo.FinalSpan,
selectionInfo.SelectionInExpression,
doc,
selectionInfo.FirstTokenInFinalSpan,
selectionInfo.LastTokenInFinalSpan,
selectionChanged,
cancellationToken).ConfigureAwait(false);
return (result, selectionInfo.Status);
}
private SelectionInfo ApplySpecialCases(SelectionInfo selectionInfo, SourceText text, ParseOptions options, bool localFunction)
{
if (selectionInfo.Status.Failed)
return selectionInfo;
// If we're under a global statement (and not inside an inner lambda/local-function) then there are restrictions
// on if we can extract a method vs a local function.
if (IsCodeInGlobalLevel())
{
// Cannot extract a method from a top-level statement in normal code
if (!localFunction && options is { Kind: SourceCodeKind.Regular })
return selectionInfo.WithStatus(s => s.With(succeeded: false, CSharpFeaturesResources.Selection_cannot_include_top_level_statements));
// Cannot extract a local function from a global statement in script code
if (localFunction && options is { Kind: SourceCodeKind.Script })
return selectionInfo.WithStatus(s => s.With(succeeded: false, CSharpFeaturesResources.Selection_cannot_include_global_statements));
}
if (_localFunction)
{
foreach (var ancestor in selectionInfo.CommonRootFromOriginalSpan.AncestorsAndSelf())
{
if (ancestor.Kind() is SyntaxKind.BaseConstructorInitializer or SyntaxKind.ThisConstructorInitializer)
return selectionInfo.WithStatus(s => s.With(succeeded: false, CSharpFeaturesResources.Selection_cannot_be_in_constructor_initializer));
if (ancestor is AnonymousFunctionExpressionSyntax)
break;
}
}
if (!selectionInfo.SelectionInExpression)
return selectionInfo;
var expressionNode = selectionInfo.FirstTokenInFinalSpan.GetCommonRoot(selectionInfo.LastTokenInFinalSpan);
if (expressionNode is not AssignmentExpressionSyntax assign)
return selectionInfo;
// make sure there is a visible token at right side expression
if (assign.Right.GetLastToken().Kind() == SyntaxKind.None)
return selectionInfo;
return AssignFinalSpan(selectionInfo
.With(s => s.FirstTokenInFinalSpan = assign.Right.GetFirstToken(includeZeroWidth: true))
.With(s => s.LastTokenInFinalSpan = assign.Right.GetLastToken(includeZeroWidth: true)), text);
bool IsCodeInGlobalLevel()
{
for (var current = selectionInfo.CommonRootFromOriginalSpan; current != null; current = current.Parent)
{
if (current is CompilationUnitSyntax)
return true;
if (current is GlobalStatementSyntax)
return true;
if (current is AnonymousFunctionExpressionSyntax or LocalFunctionStatementSyntax or MemberDeclarationSyntax)
return false;
}
throw ExceptionUtilities.Unreachable();
}
}
private static TextSpan GetControlFlowSpan(SelectionInfo selectionInfo)
=> TextSpan.FromBounds(selectionInfo.FirstTokenInFinalSpan.SpanStart, selectionInfo.LastTokenInFinalSpan.Span.End);
private static SelectionInfo AdjustFinalTokensBasedOnContext(
SelectionInfo selectionInfo,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
if (selectionInfo.Status.Failed)
return selectionInfo;
// don't need to adjust anything if it is multi-statements case
if (!selectionInfo.SelectionInExpression && !selectionInfo.SelectionInSingleStatement)
{
return selectionInfo;
}
// get the node that covers the selection
var node = selectionInfo.FirstTokenInFinalSpan.GetCommonRoot(selectionInfo.LastTokenInFinalSpan);
var validNode = Check(semanticModel, node, cancellationToken);
if (validNode)
{
return selectionInfo;
}
var firstValidNode = node.GetAncestors<SyntaxNode>().FirstOrDefault(n => Check(semanticModel, n, cancellationToken));
if (firstValidNode == null)
{
// couldn't find any valid node
return selectionInfo.WithStatus(s => new OperationStatus(succeeded: false, CSharpFeaturesResources.Selection_does_not_contain_a_valid_node))
.With(s => s.FirstTokenInFinalSpan = default)
.With(s => s.LastTokenInFinalSpan = default);
}
firstValidNode = (firstValidNode.Parent is ExpressionStatementSyntax) ? firstValidNode.Parent : firstValidNode;
return selectionInfo.With(s => s.SelectionInExpression = firstValidNode is ExpressionSyntax)
.With(s => s.SelectionInSingleStatement = firstValidNode is StatementSyntax)
.With(s => s.FirstTokenInFinalSpan = firstValidNode.GetFirstToken(includeZeroWidth: true))
.With(s => s.LastTokenInFinalSpan = firstValidNode.GetLastToken(includeZeroWidth: true));
}
private SelectionInfo GetInitialSelectionInfo(SyntaxNode root, SourceText text)
{
var adjustedSpan = GetAdjustedSpan(text, OriginalSpan);
var firstTokenInSelection = root.FindTokenOnRightOfPosition(adjustedSpan.Start, includeSkipped: false);
var lastTokenInSelection = root.FindTokenOnLeftOfPosition(adjustedSpan.End, includeSkipped: false);
if (firstTokenInSelection.Kind() == SyntaxKind.None || lastTokenInSelection.Kind() == SyntaxKind.None)
{
return new SelectionInfo { Status = new OperationStatus(succeeded: false, FeaturesResources.Invalid_selection), OriginalSpan = adjustedSpan };
}
if (firstTokenInSelection.SpanStart > lastTokenInSelection.Span.End)
{
return new SelectionInfo
{
Status = new OperationStatus(succeeded: false, FeaturesResources.Selection_does_not_contain_a_valid_token),
OriginalSpan = adjustedSpan,
FirstTokenInOriginalSpan = firstTokenInSelection,
LastTokenInOriginalSpan = lastTokenInSelection
};
}
if (!UnderValidContext(firstTokenInSelection) || !UnderValidContext(lastTokenInSelection))
{
return new SelectionInfo
{
Status = new OperationStatus(succeeded: false, FeaturesResources.No_valid_selection_to_perform_extraction),
OriginalSpan = adjustedSpan,
FirstTokenInOriginalSpan = firstTokenInSelection,
LastTokenInOriginalSpan = lastTokenInSelection
};
}
var commonRoot = firstTokenInSelection.GetCommonRoot(lastTokenInSelection);
if (commonRoot == null)
{
return new SelectionInfo
{
Status = new OperationStatus(succeeded: false, FeaturesResources.No_common_root_node_for_extraction),
OriginalSpan = adjustedSpan,
FirstTokenInOriginalSpan = firstTokenInSelection,
LastTokenInOriginalSpan = lastTokenInSelection
};
}
if (!commonRoot.ContainedInValidType())
{
return new SelectionInfo
{
Status = new OperationStatus(succeeded: false, FeaturesResources.Selection_not_contained_inside_a_type),
OriginalSpan = adjustedSpan,
FirstTokenInOriginalSpan = firstTokenInSelection,
LastTokenInOriginalSpan = lastTokenInSelection
};
}
var selectionInExpression = commonRoot is ExpressionSyntax;
if (!selectionInExpression && !commonRoot.UnderValidContext())
{
return new SelectionInfo
{
Status = new OperationStatus(succeeded: false, FeaturesResources.No_valid_selection_to_perform_extraction),
OriginalSpan = adjustedSpan,
FirstTokenInOriginalSpan = firstTokenInSelection,
LastTokenInOriginalSpan = lastTokenInSelection
};
}
return new SelectionInfo
{
Status = OperationStatus.SucceededStatus,
OriginalSpan = adjustedSpan,
CommonRootFromOriginalSpan = commonRoot,
SelectionInExpression = selectionInExpression,
FirstTokenInOriginalSpan = firstTokenInSelection,
LastTokenInOriginalSpan = lastTokenInSelection
};
}
private static bool UnderValidContext(SyntaxToken token)
=> token.GetAncestors<SyntaxNode>().Any(n => CheckTopLevel(n, token.Span));
private static bool CheckTopLevel(SyntaxNode node, TextSpan span)
{
switch (node)
{
case BlockSyntax block:
return ContainsInBlockBody(block, span);
case ArrowExpressionClauseSyntax expressionBodiedMember:
return ContainsInExpressionBodiedMemberBody(expressionBodiedMember, span);
case FieldDeclarationSyntax field:
{
foreach (var variable in field.Declaration.Variables)
{
if (variable.Initializer != null && variable.Initializer.Span.Contains(span))
{
return true;
}
}
break;
}
case GlobalStatementSyntax:
return true;
case ConstructorInitializerSyntax constructorInitializer:
return constructorInitializer.ContainsInArgument(span);
case PrimaryConstructorBaseTypeSyntax primaryConstructorBaseType:
return primaryConstructorBaseType.ArgumentList.Arguments.FullSpan.Contains(span);
}
return false;
}
private static bool ContainsInBlockBody(BlockSyntax block, TextSpan textSpan)
{
if (block == null)
{
return false;
}
var blockSpan = TextSpan.FromBounds(block.OpenBraceToken.Span.End, block.CloseBraceToken.SpanStart);
return blockSpan.Contains(textSpan);
}
private static bool ContainsInExpressionBodiedMemberBody(ArrowExpressionClauseSyntax expressionBodiedMember, TextSpan textSpan)
{
if (expressionBodiedMember == null)
{
return false;
}
var expressionBodiedMemberBody = TextSpan.FromBounds(expressionBodiedMember.Expression.SpanStart, expressionBodiedMember.Expression.Span.End);
return expressionBodiedMemberBody.Contains(textSpan);
}
private static SelectionInfo CheckErrorCasesAndAppendDescriptions(
SelectionInfo selectionInfo,
SyntaxNode root)
{
if (selectionInfo.Status.Failed)
return selectionInfo;
if (selectionInfo.FirstTokenInFinalSpan.IsMissing || selectionInfo.LastTokenInFinalSpan.IsMissing)
{
selectionInfo = selectionInfo.WithStatus(s => s.With(succeeded: false, CSharpFeaturesResources.Contains_invalid_selection));
}
// get the node that covers the selection
var commonNode = selectionInfo.FirstTokenInFinalSpan.GetCommonRoot(selectionInfo.LastTokenInFinalSpan);
if ((selectionInfo.SelectionInExpression || selectionInfo.SelectionInSingleStatement) && commonNode.HasDiagnostics())
{
selectionInfo = selectionInfo.WithStatus(s => s.With(succeeded: false, CSharpFeaturesResources.The_selection_contains_syntactic_errors));
}
var tokens = root.DescendantTokens(selectionInfo.FinalSpan);
if (tokens.ContainPreprocessorCrossOver(selectionInfo.FinalSpan))
{
selectionInfo = selectionInfo.WithStatus(s => s.With(succeeded: true, CSharpFeaturesResources.Selection_can_not_cross_over_preprocessor_directives));
}
// TODO : check whether this can be handled by control flow analysis engine
if (tokens.Any(t => t.Kind() == SyntaxKind.YieldKeyword))
{
selectionInfo = selectionInfo.WithStatus(s => s.With(succeeded: true, CSharpFeaturesResources.Selection_can_not_contain_a_yield_statement));
}
// TODO : check behavior of control flow analysis engine around exception and exception handling.
if (tokens.ContainArgumentlessThrowWithoutEnclosingCatch(selectionInfo.FinalSpan))
{
selectionInfo = selectionInfo.WithStatus(s => s.With(succeeded: true, CSharpFeaturesResources.Selection_can_not_contain_throw_statement));
}
if (selectionInfo.SelectionInExpression && commonNode.PartOfConstantInitializerExpression())
{
selectionInfo = selectionInfo.WithStatus(s => s.With(succeeded: false, CSharpFeaturesResources.Selection_can_not_be_part_of_constant_initializer_expression));
}
if (commonNode.IsUnsafeContext())
{
selectionInfo = selectionInfo.WithStatus(s => s.With(s.Succeeded, CSharpFeaturesResources.The_selected_code_is_inside_an_unsafe_context));
}
// For now patterns are being blanket disabled for extract method. This issue covers designing extractions for them
// and re-enabling this.
// https://github.com/dotnet/roslyn/issues/9244
if (commonNode.Kind() == SyntaxKind.IsPatternExpression)
{
selectionInfo = selectionInfo.WithStatus(s => s.With(succeeded: false, CSharpFeaturesResources.Selection_can_not_contain_a_pattern_expression));
}
return selectionInfo;
}
private static SelectionInfo AssignInitialFinalTokens(SelectionInfo selectionInfo, SyntaxNode root, CancellationToken cancellationToken)
{
if (selectionInfo.Status.Failed)
return selectionInfo;
if (selectionInfo.SelectionInExpression)
{
// simple expression case
return selectionInfo.With(s => s.FirstTokenInFinalSpan = s.CommonRootFromOriginalSpan.GetFirstToken(includeZeroWidth: true))
.With(s => s.LastTokenInFinalSpan = s.CommonRootFromOriginalSpan.GetLastToken(includeZeroWidth: true));
}
var range = GetStatementRangeContainingSpan<StatementSyntax>(
CSharpSyntaxFacts.Instance,
root, TextSpan.FromBounds(selectionInfo.FirstTokenInOriginalSpan.SpanStart, selectionInfo.LastTokenInOriginalSpan.Span.End),
cancellationToken);
if (range == null)
{
return selectionInfo.WithStatus(s => s.With(succeeded: false, CSharpFeaturesResources.No_valid_statement_range_to_extract));
}
var statement1 = range.Value.Item1;
var statement2 = range.Value.Item2;
if (statement1 == statement2)
{
// check one more time to see whether it is an expression case
var expression = selectionInfo.CommonRootFromOriginalSpan.GetAncestor<ExpressionSyntax>();
if (expression != null && statement1.Span.Contains(expression.Span))
{
return selectionInfo.With(s => s.SelectionInExpression = true)
.With(s => s.FirstTokenInFinalSpan = expression.GetFirstToken(includeZeroWidth: true))
.With(s => s.LastTokenInFinalSpan = expression.GetLastToken(includeZeroWidth: true));
}
// single statement case
return selectionInfo.With(s => s.SelectionInSingleStatement = true)
.With(s => s.FirstTokenInFinalSpan = statement1.GetFirstToken(includeZeroWidth: true))
.With(s => s.LastTokenInFinalSpan = statement1.GetLastToken(includeZeroWidth: true));
}
// move only statements inside of the block
return selectionInfo.With(s => s.FirstTokenInFinalSpan = statement1.GetFirstToken(includeZeroWidth: true))
.With(s => s.LastTokenInFinalSpan = statement2.GetLastToken(includeZeroWidth: true));
}
private static SelectionInfo AssignFinalSpan(SelectionInfo selectionInfo, SourceText text)
{
if (selectionInfo.Status.Failed)
return selectionInfo;
// set final span
var start = (selectionInfo.FirstTokenInOriginalSpan == selectionInfo.FirstTokenInFinalSpan)
? Math.Min(selectionInfo.FirstTokenInOriginalSpan.SpanStart, selectionInfo.OriginalSpan.Start)
: selectionInfo.FirstTokenInFinalSpan.FullSpan.Start;
var end = (selectionInfo.LastTokenInOriginalSpan == selectionInfo.LastTokenInFinalSpan)
? Math.Max(selectionInfo.LastTokenInOriginalSpan.Span.End, selectionInfo.OriginalSpan.End)
: selectionInfo.LastTokenInFinalSpan.FullSpan.End;
return selectionInfo.With(s => s.FinalSpan = GetAdjustedSpan(text, TextSpan.FromBounds(start, end)));
}
public override bool ContainsNonReturnExitPointsStatements(IEnumerable<SyntaxNode> jumpsOutOfRegion)
=> jumpsOutOfRegion.Where(n => n is not ReturnStatementSyntax).Any();
public override IEnumerable<SyntaxNode> GetOuterReturnStatements(SyntaxNode commonRoot, IEnumerable<SyntaxNode> jumpsOutOfRegion)
{
var returnStatements = jumpsOutOfRegion.Where(s => s is ReturnStatementSyntax);
var container = commonRoot.GetAncestorsOrThis<SyntaxNode>().Where(a => a.IsReturnableConstruct()).FirstOrDefault();
if (container == null)
return [];
var returnableConstructPairs = returnStatements.Select(r => (r, r.GetAncestors<SyntaxNode>().Where(a => a.IsReturnableConstruct()).FirstOrDefault()))
.Where(p => p.Item2 != null);
// now filter return statements to only include the one under outmost container
return returnableConstructPairs.Where(p => p.Item2 == container).Select(p => p.Item1);
}
public override bool IsFinalSpanSemanticallyValidSpan(
SyntaxNode root, TextSpan textSpan,
IEnumerable<SyntaxNode> returnStatements, CancellationToken cancellationToken)
{
// return statement shouldn't contain any return value
if (returnStatements.Cast<ReturnStatementSyntax>().Any(r => r.Expression != null))
{
return false;
}
var lastToken = root.FindToken(textSpan.End);
if (lastToken.Kind() == SyntaxKind.None)
{
return false;
}
var container = lastToken.GetAncestors<SyntaxNode>().FirstOrDefault(n => n.IsReturnableConstruct());
if (container == null)
{
return false;
}
var body = container.GetBlockBody();
if (body == null)
{
return false;
}
// make sure that next token of the last token in the selection is the close braces of containing block
if (body.CloseBraceToken != lastToken.GetNextToken(includeZeroWidth: true))
{
return false;
}
// alright, for these constructs, it must be okay to be extracted
switch (container.Kind())
{
case SyntaxKind.AnonymousMethodExpression:
case SyntaxKind.SimpleLambdaExpression:
case SyntaxKind.ParenthesizedLambdaExpression:
return true;
}
// now, only method is okay to be extracted out
if (body.Parent is not MethodDeclarationSyntax method)
{
return false;
}
// make sure this method doesn't have return type.
return method.ReturnType is PredefinedTypeSyntax p &&
p.Keyword.Kind() == SyntaxKind.VoidKeyword;
}
private static TextSpan GetAdjustedSpan(SourceText text, TextSpan textSpan)
{
// beginning of a file
if (textSpan.IsEmpty || textSpan.End == 0)
{
return textSpan;
}
// if it is a start of new line, make it belong to previous line
var line = text.Lines.GetLineFromPosition(textSpan.End);
if (line.Start != textSpan.End)
{
return textSpan;
}
// get previous line
Contract.ThrowIfFalse(line.LineNumber > 0);
var previousLine = text.Lines[line.LineNumber - 1];
// if the span is past the end of the line (ie, in whitespace) then
// return to the end of the line including whitespace
if (textSpan.Start > previousLine.End)
{
return TextSpan.FromBounds(textSpan.Start, previousLine.EndIncludingLineBreak);
}
return TextSpan.FromBounds(textSpan.Start, previousLine.End);
}
}