@@ -178,11 +178,6 @@ public bool TryMapToRazorDocumentRange(RazorCSharpDocument csharpDocument, LineP
178
178
179
179
public bool TryMapToCSharpDocumentRange ( RazorCSharpDocument csharpDocument , LinePositionSpan razorRange , out LinePositionSpan csharpRange )
180
180
{
181
- if ( csharpDocument . CodeDocument is not { } codeDocument )
182
- {
183
- throw new InvalidOperationException ( "Cannot use document mapping service on a generated document that has a null CodeDocument." ) ;
184
- }
185
-
186
181
csharpRange = default ;
187
182
188
183
if ( razorRange . End . Line < razorRange . Start . Line ||
@@ -194,7 +189,7 @@ public bool TryMapToCSharpDocumentRange(RazorCSharpDocument csharpDocument, Line
194
189
return false ;
195
190
}
196
191
197
- var sourceText = codeDocument . Source . Text ;
192
+ var sourceText = csharpDocument . CodeDocument . Source . Text ;
198
193
var range = razorRange ;
199
194
if ( ! IsRangeWithinDocument ( range , sourceText ) )
200
195
{
@@ -231,11 +226,6 @@ public bool TryMapToCSharpDocumentRange(RazorCSharpDocument csharpDocument, Line
231
226
232
227
public bool TryMapToRazorDocumentPosition ( RazorCSharpDocument csharpDocument , int csharpIndex , out LinePosition razorPosition , out int razorIndex )
233
228
{
234
- if ( csharpDocument . CodeDocument is not { } codeDocument )
235
- {
236
- throw new InvalidOperationException ( "Cannot use document mapping service on a generated document that has a null CodeDocument." ) ;
237
- }
238
-
239
229
var sourceMappings = csharpDocument . SourceMappings ;
240
230
241
231
// We expect source mappings to be ordered by their generated document absolute index, because that is how the compiler creates them: As it
@@ -268,7 +258,7 @@ public bool TryMapToRazorDocumentPosition(RazorCSharpDocument csharpDocument, in
268
258
var distanceIntoGeneratedSpan = csharpIndex - generatedAbsoluteIndex ;
269
259
270
260
razorIndex = mapping . OriginalSpan . AbsoluteIndex + distanceIntoGeneratedSpan ;
271
- razorPosition = codeDocument . Source . Text . GetLinePosition ( razorIndex ) ;
261
+ razorPosition = csharpDocument . CodeDocument . Source . Text . GetLinePosition ( razorIndex ) ;
272
262
return true ;
273
263
}
274
264
@@ -285,11 +275,6 @@ public bool TryMapToCSharpDocumentPosition(RazorCSharpDocument csharpDocument, i
285
275
286
276
private static bool TryMapToCSharpDocumentPositionInternal ( RazorCSharpDocument csharpDocument , int razorIndex , bool nextCSharpPositionOnFailure , out LinePosition csharpPosition , out int csharpIndex )
287
277
{
288
- if ( csharpDocument . CodeDocument is not { } codeDocument )
289
- {
290
- throw new InvalidOperationException ( "Cannot use document mapping service on a generated document that has a null CodeDocument." ) ;
291
- }
292
-
293
278
foreach ( var mapping in csharpDocument . SourceMappings )
294
279
{
295
280
var originalSpan = mapping . OriginalSpan ;
@@ -310,7 +295,7 @@ private static bool TryMapToCSharpDocumentPositionInternal(RazorCSharpDocument c
310
295
{
311
296
// The "next" C# location is only valid if it is on the same line in the source document
312
297
// as the requested position.
313
- var hostDocumentLinePosition = codeDocument . Source . Text . GetLinePosition ( razorIndex ) ;
298
+ var hostDocumentLinePosition = csharpDocument . CodeDocument . Source . Text . GetLinePosition ( razorIndex ) ;
314
299
315
300
if ( mapping . OriginalSpan . LineIndex == hostDocumentLinePosition . Line )
316
301
{
@@ -364,11 +349,6 @@ private bool TryMapToRazorDocumentRangeStrict(RazorCSharpDocument csharpDocument
364
349
365
350
private bool TryMapToRazorDocumentRangeInclusive ( RazorCSharpDocument csharpDocument , LinePositionSpan csharpRange , out LinePositionSpan rangeRange )
366
351
{
367
- if ( csharpDocument . CodeDocument is not { } codeDocument )
368
- {
369
- throw new InvalidOperationException ( "Cannot use document mapping service on a generated document that has a null CodeDocument." ) ;
370
- }
371
-
372
352
rangeRange = default ;
373
353
374
354
var csharpSourceText = csharpDocument . Text ;
@@ -417,7 +397,7 @@ private bool TryMapToRazorDocumentRangeInclusive(RazorCSharpDocument csharpDocum
417
397
// We're intersecting or overlapping a single mapping, lets choose that.
418
398
419
399
var mapping = candidateMappings [ 0 ] ;
420
- rangeRange = codeDocument . Source . Text . GetLinePositionSpan ( mapping . OriginalSpan ) ;
400
+ rangeRange = csharpDocument . CodeDocument . Source . Text . GetLinePositionSpan ( mapping . OriginalSpan ) ;
421
401
return true ;
422
402
}
423
403
else
@@ -442,11 +422,6 @@ bool IntersectsWith(int position, SourceSpan span)
442
422
443
423
private bool TryMapToRazorDocumentRangeInferred ( RazorCSharpDocument csharpDocument , LinePositionSpan csharpRange , out LinePositionSpan razorRange )
444
424
{
445
- if ( csharpDocument . CodeDocument is not { } codeDocument )
446
- {
447
- throw new InvalidOperationException ( "Cannot use document mapping service on a generated document that has a null CodeDocument." ) ;
448
- }
449
-
450
425
// Inferred mapping behavior is a superset of inclusive mapping behavior so if the range is "inclusive" lets use that mapping.
451
426
if ( TryMapToRazorDocumentRangeInclusive ( csharpDocument , csharpRange , out razorRange ) )
452
427
{
@@ -492,7 +467,7 @@ private bool TryMapToRazorDocumentRangeInferred(RazorCSharpDocument csharpDocume
492
467
return false ;
493
468
}
494
469
495
- var sourceDocument = codeDocument . Source ;
470
+ var sourceDocument = csharpDocument . CodeDocument . Source ;
496
471
var originalSpanBeforeGeneratedRange = mappingBeforeGeneratedRange . OriginalSpan ;
497
472
var originalEndBeforeGeneratedRange = originalSpanBeforeGeneratedRange . AbsoluteIndex + originalSpanBeforeGeneratedRange . Length ;
498
473
var inferredStartPosition = sourceDocument . Text . GetLinePosition ( originalEndBeforeGeneratedRange ) ;
0 commit comments