-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathRazorSpanMappingServiceWrapper.cs
71 lines (62 loc) · 3.12 KB
/
RazorSpanMappingServiceWrapper.cs
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
// 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.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal sealed class RazorSpanMappingServiceWrapper : AbstractSpanMappingService
{
private readonly IRazorSpanMappingService _razorSpanMappingService;
public RazorSpanMappingServiceWrapper(IRazorSpanMappingService razorSpanMappingService)
{
_razorSpanMappingService = razorSpanMappingService ?? throw new ArgumentNullException(nameof(razorSpanMappingService));
}
/// <summary>
/// Modern razor span mapping service can handle if we add imports. Razor will then rewrite that
/// to their own form.
/// </summary>
public override bool SupportsMappingImportDirectives => true;
public override async Task<ImmutableArray<(string mappedFilePath, TextChange mappedTextChange)>> GetMappedTextChangesAsync(
Document oldDocument,
Document newDocument,
CancellationToken cancellationToken)
{
var diffService = newDocument.Project.Solution.Services.GetRequiredService<IDocumentTextDifferencingService>();
// This is a hack that finds a minimal diff. It's not the ideal algorithm but should cover most scenarios. In the future,
// we should improve this algorithm - see https://github.com/dotnet/roslyn/issues/53346 for additional details.
var textChanges = await diffService.GetTextChangesAsync(oldDocument, newDocument, cancellationToken).ConfigureAwait(false);
var mappedSpanResults = await MapSpansAsync(oldDocument, textChanges.Select(tc => tc.Span), cancellationToken).ConfigureAwait(false);
var mappedTextChanges = MatchMappedSpansToTextChanges(textChanges, mappedSpanResults);
return mappedTextChanges;
}
public override async Task<ImmutableArray<MappedSpanResult>> MapSpansAsync(
Document document,
IEnumerable<TextSpan> spans,
CancellationToken cancellationToken)
{
var razorSpans = await _razorSpanMappingService.MapSpansAsync(document, spans, cancellationToken).ConfigureAwait(false);
var roslynSpans = new MappedSpanResult[razorSpans.Length];
for (var i = 0; i < razorSpans.Length; i++)
{
var razorSpan = razorSpans[i];
if (razorSpan.IsDefault)
{
// Unmapped location
roslynSpans[i] = default;
}
else
{
roslynSpans[i] = new MappedSpanResult(razorSpan.FilePath, razorSpan.LinePositionSpan, razorSpan.Span);
}
}
return roslynSpans.ToImmutableArray();
}
}
}