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

Only get first document's highlights #2424

Merged
merged 1 commit into from
Jul 27, 2022
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
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
Expand Down Expand Up @@ -26,47 +27,49 @@ public SemanticHighlightService(OmniSharpWorkspace workspace, ILoggerFactory log

public async Task<SemanticHighlightResponse> Handle(SemanticHighlightRequest request)
{
var documents = _workspace.GetDocuments(request.FileName);
var document = _workspace.GetDocuments(request.FileName).FirstOrDefault();

if (document == null)
{
return new SemanticHighlightResponse() { Spans = Array.Empty<SemanticHighlightSpan>() };
}

var results = new List<ClassifiedResult>();

foreach (var document in documents)
{
var project = document.Project.Name;
var project = document.Project.Name;

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

var text = await highlightDocument.GetTextAsync();
var text = await highlightDocument.GetTextAsync();

TextSpan textSpan;
if (request.Range is object)
TextSpan textSpan;
if (request.Range is object)
{
if (request.Range.IsValid())
{
if (request.Range.IsValid())
{
var start = text.Lines.GetPosition(new LinePosition(request.Range.Start.Line, request.Range.Start.Column));
var end = text.Lines.GetPosition(new LinePosition(request.Range.End.Line, request.Range.End.Column));
textSpan = new TextSpan(start, end - start);
}
else
{
_logger.LogWarning($"Supplied highlight range {request.Range} in document {document.FilePath} is not valid.");
continue;
}
var start = text.Lines.GetPosition(new LinePosition(request.Range.Start.Line, request.Range.Start.Column));
var end = text.Lines.GetPosition(new LinePosition(request.Range.End.Line, request.Range.End.Column));
textSpan = new TextSpan(start, end - start);
}
else
{
textSpan = new TextSpan(0, text.Length);
_logger.LogWarning($"Supplied highlight range {request.Range} in document {document.FilePath} is not valid.");
return new SemanticHighlightResponse() { Spans = Array.Empty<SemanticHighlightSpan>() };
}

results.AddRange((await Classifier.GetClassifiedSpansAsync(highlightDocument, textSpan))
.Select(span => new ClassifiedResult()
{
Span = span,
Lines = text.Lines,
}));
}
else
{
textSpan = new TextSpan(0, text.Length);
}

results.AddRange((await Classifier.GetClassifiedSpansAsync(highlightDocument, textSpan))
.Select(span => new ClassifiedResult()
{
Span = span,
Lines = text.Lines,
}));

return new SemanticHighlightResponse()
{
Expand Down
36 changes: 36 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/SemanticHighlightFacts.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Models.SemanticHighlight;
Expand Down Expand Up @@ -351,6 +352,41 @@ record struct R1(string S, int I);
);
}

[Fact]
public async Task SemanticHighlightLinkedFiles()
{
var testFile = new TestFile("a.cs", @"
class C1 { }
");

TestHelpers.AddProjectToWorkspace(
SharedOmniSharpTestHost.Workspace,
Path.Combine(Directory.GetCurrentDirectory(), "a.csproj"),
new[] { "net472" },
new[] { testFile });

TestHelpers.AddProjectToWorkspace(
SharedOmniSharpTestHost.Workspace,
Path.Combine(Directory.GetCurrentDirectory(), "b.csproj"),
new[] { "net472" },
new[] { testFile });

var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
var request = new SemanticHighlightRequest
{
FileName = "a.cs",
};

var response = await requestHandler.Handle(request);

AssertSyntax(response.Spans, testFile.Content.Code, 0,
Keyword("class"),
ClassName("C1"),
Punctuation("{"),
Punctuation("}")
);
}

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