Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for alternate versions of documents to be semantically highlighted #2304

Merged
merged 2 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ namespace OmniSharp.Models.SemanticHighlight
public class SemanticHighlightRequest : Request
{
/// <summary>
/// Specifies the range to highlight.
/// If none is given, highlight the entire
/// file.
/// Specifies the range to highlight. If none is given, highlight the entire file.
/// </summary>
public Range Range { get; set; }

/// <summary>
/// Optionally provide the text for a different version of the document to be highlighted.
/// This property works differently than the Buffer property, since it is only used for
/// highlighting and will not update the document in the CurrentSolution.
/// </summary>
public string VersionedText { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ public async Task<SemanticHighlightResponse> Handle(SemanticHighlightRequest req
foreach (var document in documents)
{
var project = document.Project.Name;
var text = await document.GetTextAsync();

var highlightDocument = request.VersionedText != null
? document.WithText(SourceText.From(request.VersionedText))
: document;

var text = await highlightDocument.GetTextAsync();

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

results.AddRange((await Classifier.GetClassifiedSpansAsync(document, textSpan))
results.AddRange((await Classifier.GetClassifiedSpansAsync(highlightDocument, textSpan))
.Select(span => new ClassifiedResult()
{
Span = span,
Expand Down
56 changes: 49 additions & 7 deletions tests/OmniSharp.Roslyn.CSharp.Tests/SemanticHighlightFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class C1 { int n = true; }
");

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

Assert.Empty(highlights);
}
Expand All @@ -47,7 +47,7 @@ class C1 { int n = true; }
");

var line = 3;
var highlights = await GetSemanticHighlightsForLineAsync(testFile, line);
var highlights = await GetSemanticHighlightsForLineAsync(testFile, line, versionedText: null);

AssertSyntax(highlights, testFile.Content.Code, line,
Keyword("class"),
Expand Down Expand Up @@ -90,6 +90,42 @@ class C1 { int n = true; }
);
}

[Fact]
public async Task SemanticHighlightEntireFileWithVersionedText()
{
var testFile = new TestFile("a.cs", @"
namespace N1
{
class C1 { int n = true; }
}
");
var versionedText = @"
namespace N1
{
class C { int n = false; }
}
";

var highlights = await GetSemanticHighlightsForFileAsync(testFile, versionedText);

AssertSyntax(highlights, versionedText, 0,
Keyword("namespace"),
NamespaceName("N1"),
Punctuation("{"),
Keyword("class"),
ClassName("C"),
Punctuation("{"),
Keyword("int"),
Field("n"),
Operator("="),
Keyword("false"),
Punctuation(";"),
Punctuation("}"),
Punctuation("}")
);
}


[Fact]
public async Task SemanticHighlightStringInterpolation()
{
Expand Down Expand Up @@ -317,28 +353,34 @@ record struct R1(string S, int I);

private Task<SemanticHighlightSpan[]> GetSemanticHighlightsForFileAsync(TestFile testFile)
{
return GetSemanticHighlightsAsync(testFile, range: null);
return GetSemanticHighlightsAsync(testFile, range: null, versionedText: null);
}

private Task<SemanticHighlightSpan[]> GetSemanticHighlightsForFileAsync(TestFile testFile, string versionedText)
{
return GetSemanticHighlightsAsync(testFile, range: null, versionedText);
}

private Task<SemanticHighlightSpan[]> GetSemanticHighlightsForLineAsync(TestFile testFile, int line)
private Task<SemanticHighlightSpan[]> GetSemanticHighlightsForLineAsync(TestFile testFile, int line, string versionedText)
{
var range = new Range()
{
Start = new Point() { Column = 0, Line = line },
End = new Point() { Column = 0, Line = line + 1 }
};

return GetSemanticHighlightsAsync(testFile, range);
return GetSemanticHighlightsAsync(testFile, range, versionedText);
}

private async Task<SemanticHighlightSpan[]> GetSemanticHighlightsAsync(TestFile testFile, Range range)
private async Task<SemanticHighlightSpan[]> GetSemanticHighlightsAsync(TestFile testFile, Range range, string versionedText)
{
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
var request = new SemanticHighlightRequest
{
FileName = testFile.FileName,
Range = range
Range = range,
VersionedText = versionedText,
};

var response = await requestHandler.Handle(request);
Expand Down