Skip to content

Commit 8ddf23f

Browse files
authored
Merge pull request #2304 from JoeRobich/versioned-document-highlight
Allow for alternate versions of documents to be semantically highlighted
2 parents 7c727c8 + 6401a60 commit 8ddf23f

3 files changed

Lines changed: 64 additions & 12 deletions

File tree

src/OmniSharp.Abstractions/Models/v2/SemanticHighlight/SemanticHighlightRequest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ namespace OmniSharp.Models.SemanticHighlight
77
public class SemanticHighlightRequest : Request
88
{
99
/// <summary>
10-
/// Specifies the range to highlight.
11-
/// If none is given, highlight the entire
12-
/// file.
10+
/// Specifies the range to highlight. If none is given, highlight the entire file.
1311
/// </summary>
1412
public Range Range { get; set; }
13+
14+
/// <summary>
15+
/// Optionally provide the text for a different version of the document to be highlighted.
16+
/// This property works differently than the Buffer property, since it is only used for
17+
/// highlighting and will not update the document in the CurrentSolution.
18+
/// </summary>
19+
public string VersionedText { get; set; }
1520
}
1621
}

src/OmniSharp.Roslyn.CSharp/Services/SemanticHighlight/SemanticHighlightService.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ public async Task<SemanticHighlightResponse> Handle(SemanticHighlightRequest req
3333
foreach (var document in documents)
3434
{
3535
var project = document.Project.Name;
36-
var text = await document.GetTextAsync();
36+
37+
var highlightDocument = request.VersionedText != null
38+
? document.WithText(SourceText.From(request.VersionedText))
39+
: document;
40+
41+
var text = await highlightDocument.GetTextAsync();
3742

3843
TextSpan textSpan;
3944
if (request.Range is object)
@@ -55,7 +60,7 @@ public async Task<SemanticHighlightResponse> Handle(SemanticHighlightRequest req
5560
textSpan = new TextSpan(0, text.Length);
5661
}
5762

58-
results.AddRange((await Classifier.GetClassifiedSpansAsync(document, textSpan))
63+
results.AddRange((await Classifier.GetClassifiedSpansAsync(highlightDocument, textSpan))
5964
.Select(span => new ClassifiedResult()
6065
{
6166
Span = span,

tests/OmniSharp.Roslyn.CSharp.Tests/SemanticHighlightFacts.cs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class C1 { int n = true; }
3131
");
3232

3333
var line = -1;
34-
var highlights = await GetSemanticHighlightsForLineAsync(testFile, line);
34+
var highlights = await GetSemanticHighlightsForLineAsync(testFile, line, versionedText: null);
3535

3636
Assert.Empty(highlights);
3737
}
@@ -47,7 +47,7 @@ class C1 { int n = true; }
4747
");
4848

4949
var line = 3;
50-
var highlights = await GetSemanticHighlightsForLineAsync(testFile, line);
50+
var highlights = await GetSemanticHighlightsForLineAsync(testFile, line, versionedText: null);
5151

5252
AssertSyntax(highlights, testFile.Content.Code, line,
5353
Keyword("class"),
@@ -90,6 +90,42 @@ class C1 { int n = true; }
9090
);
9191
}
9292

93+
[Fact]
94+
public async Task SemanticHighlightEntireFileWithVersionedText()
95+
{
96+
var testFile = new TestFile("a.cs", @"
97+
namespace N1
98+
{
99+
class C1 { int n = true; }
100+
}
101+
");
102+
var versionedText = @"
103+
namespace N1
104+
{
105+
class C { int n = false; }
106+
}
107+
";
108+
109+
var highlights = await GetSemanticHighlightsForFileAsync(testFile, versionedText);
110+
111+
AssertSyntax(highlights, versionedText, 0,
112+
Keyword("namespace"),
113+
NamespaceName("N1"),
114+
Punctuation("{"),
115+
Keyword("class"),
116+
ClassName("C"),
117+
Punctuation("{"),
118+
Keyword("int"),
119+
Field("n"),
120+
Operator("="),
121+
Keyword("false"),
122+
Punctuation(";"),
123+
Punctuation("}"),
124+
Punctuation("}")
125+
);
126+
}
127+
128+
93129
[Fact]
94130
public async Task SemanticHighlightStringInterpolation()
95131
{
@@ -317,28 +353,34 @@ record struct R1(string S, int I);
317353

318354
private Task<SemanticHighlightSpan[]> GetSemanticHighlightsForFileAsync(TestFile testFile)
319355
{
320-
return GetSemanticHighlightsAsync(testFile, range: null);
356+
return GetSemanticHighlightsAsync(testFile, range: null, versionedText: null);
357+
}
358+
359+
private Task<SemanticHighlightSpan[]> GetSemanticHighlightsForFileAsync(TestFile testFile, string versionedText)
360+
{
361+
return GetSemanticHighlightsAsync(testFile, range: null, versionedText);
321362
}
322363

323-
private Task<SemanticHighlightSpan[]> GetSemanticHighlightsForLineAsync(TestFile testFile, int line)
364+
private Task<SemanticHighlightSpan[]> GetSemanticHighlightsForLineAsync(TestFile testFile, int line, string versionedText)
324365
{
325366
var range = new Range()
326367
{
327368
Start = new Point() { Column = 0, Line = line },
328369
End = new Point() { Column = 0, Line = line + 1 }
329370
};
330371

331-
return GetSemanticHighlightsAsync(testFile, range);
372+
return GetSemanticHighlightsAsync(testFile, range, versionedText);
332373
}
333374

334-
private async Task<SemanticHighlightSpan[]> GetSemanticHighlightsAsync(TestFile testFile, Range range)
375+
private async Task<SemanticHighlightSpan[]> GetSemanticHighlightsAsync(TestFile testFile, Range range, string versionedText)
335376
{
336377
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
337378
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
338379
var request = new SemanticHighlightRequest
339380
{
340381
FileName = testFile.FileName,
341-
Range = range
382+
Range = range,
383+
VersionedText = versionedText,
342384
};
343385

344386
var response = await requestHandler.Handle(request);

0 commit comments

Comments
 (0)