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

Add SemanticHighlight Endpoint #1734

Merged
merged 3 commits into from
Mar 16, 2020
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
@@ -0,0 +1,72 @@
namespace OmniSharp.Models.SemanticHighlight
{
// Enum of the names defined Roslyn's ClassificationTypeNames.
public enum SemanticHighlightClassification
{
Comment,
ExcludedCode,
Identifier,
Keyword,
ControlKeyword,
NumericLiteral,
Operator,
OperatorOverloaded,
PreprocessorKeyword,
StringLiteral,
WhiteSpace,
Text,
StaticSymbol,
PreprocessorText,
Punctuation,
VerbatimStringLiteral,
StringEscapeCharacter,
ClassName,
DelegateName,
EnumName,
InterfaceName,
ModuleName,
StructName,
TypeParameterName,
FieldName,
EnumMemberName,
ConstantName,
LocalName,
ParameterName,
MethodName,
ExtensionMethodName,
PropertyName,
EventName,
NamespaceName,
LabelName,
XmlDocCommentAttributeName,
XmlDocCommentAttributeQuotes,
XmlDocCommentAttributeValue,
XmlDocCommentCDataSection,
XmlDocCommentComment,
XmlDocCommentDelimiter,
XmlDocCommentEntityReference,
XmlDocCommentName,
XmlDocCommentProcessingInstruction,
XmlDocCommentText,
XmlLiteralAttributeName,
XmlLiteralAttributeQuotes,
XmlLiteralAttributeValue,
XmlLiteralCDataSection,
XmlLiteralComment,
XmlLiteralDelimiter,
XmlLiteralEmbeddedExpression,
XmlLiteralEntityReference,
XmlLiteralName,
XmlLiteralProcessingInstruction,
XmlLiteralText,
RegexComment,
RegexCharacterClass,
RegexAnchor,
RegexQuantifier,
RegexGrouping,
RegexAlternation,
RegexText,
RegexSelfEscapedCharacter,
RegexOtherEscape,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OmniSharp.Models.SemanticHighlight
{
public enum SemanticHighlightModifier
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't seem to be used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the work isn't planned, we can expect that Rolsyn will introduce a classification API which provides this additional information about classified spans. I wanted the API to reflect it so that consumers could expect it being returned. I could probably process the classifications and return a static modifier...

{
Static
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using OmniSharp.Mef;
using OmniSharp.Models.V2;

namespace OmniSharp.Models.SemanticHighlight
{
[OmniSharpEndpoint(OmniSharpEndpoints.V2.Highlight, typeof(SemanticHighlightRequest), typeof(SemanticHighlightResponse))]
public class SemanticHighlightRequest : Request
{
/// <summary>
/// Specifies the range to highlight.
/// If none is given, highlight the entire
/// file.
/// </summary>
public Range Range { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OmniSharp.Models.SemanticHighlight
{
public class SemanticHighlightResponse
{
public SemanticHighlightSpan[] Spans { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace OmniSharp.Models.SemanticHighlight
{
public class SemanticHighlightSpan : IComparable<SemanticHighlightSpan>
{
[JsonConverter(typeof(ZeroBasedIndexConverter))]
public int StartLine { get; set; }
[JsonConverter(typeof(ZeroBasedIndexConverter))]
public int StartColumn { get; set; }
[JsonConverter(typeof(ZeroBasedIndexConverter))]
public int EndLine { get; set; }
[JsonConverter(typeof(ZeroBasedIndexConverter))]
public int EndColumn { get; set; }
public SemanticHighlightClassification Type { get; set; }
public IEnumerable<SemanticHighlightModifier> Modifiers { get; set; }

public int CompareTo(SemanticHighlightSpan other)
{
if (other.StartLine < StartLine)
{
return 1;
}
else if (other.StartLine > StartLine)
{
return -1;
}
// same start line
else if (other.StartColumn < StartColumn)
{
return 1;
}
else if (other.StartColumn > StartColumn)
{
return -1;
}
// same start line and start column
else if (other.EndLine < EndLine)
{
return 1;
}
else if (other.EndLine > EndLine)
{
return -1;
}
// same start line, start column, and end line
else if (other.EndColumn < EndColumn)
{
return 1;
}
else if (other.EndColumn > EndColumn)
{
return -1;
}
// same, same
else
{
return 0;
}
}

public override bool Equals(object other)
{
var node = other as SemanticHighlightSpan;
return node != null
&& node.StartLine == StartLine
&& node.StartColumn == StartColumn
&& node.EndLine == EndLine
&& node.EndColumn == EndColumn;
}

public override int GetHashCode()
{
return 13 * StartLine +
17 * StartColumn +
23 * EndLine +
31 * EndColumn;
}
}
}
2 changes: 2 additions & 0 deletions src/OmniSharp.Abstractions/OmniSharpEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public static class V2

public const string BlockStructure = "/v2/blockstructure";
public const string CodeStructure = "/v2/codestructure";

public const string Highlight = "/v2/highlight";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Mef;
using OmniSharp.Models.SemanticHighlight;

namespace OmniSharp.Roslyn.CSharp.Services.SemanticHighlight
{
[OmniSharpHandler(OmniSharpEndpoints.V2.Highlight, LanguageNames.CSharp)]
public class SemanticHighlightService : IRequestHandler<SemanticHighlightRequest, SemanticHighlightResponse>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, this current impl doesn't take advantage of the partial semantic classification tech that's available right?

Oh and "partial" in the sense of only updating a single lines classification if only that line changed.

I ask because for C# files this might be a huge performance hit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The highlight request has an optional range property so that only that TextSpan is classified.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought VSCode re-requested the entire document pretty frequently though to account for external file changes, project changes etc. Maybe I'm wrong though, my information is coming from design discussions during the features creation; not out of personal trial and error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, perhaps you are referring to the provideDocumentSemanticTokensEdits api

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup exactly!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking about that API for a bit, I'm not sure the Edits api is super useful. It seems mostly to benefit requesting changes from a remote LSP where bandwidth is a concern. It also requires the LSP to hold on to previous state in order to generate the Edits diff as well as fully classify the new state of the document.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure how the client interprets all of the data in a response but I imagine it'd also optimize the clients processing of the data

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I feel like that api would basically be faked by the editor assuming they have a good method of generating the diffs from two sets of SyntaxTokens. Since they already have the before state which the LSP really shouldn't be expected to hold on to.

{
[ImportingConstructor]
public SemanticHighlightService(OmniSharpWorkspace workspace)
{
_workspace = workspace;
}

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

var results = new List<ClassifiedResult>();

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

TextSpan textSpan;
if (request.Range is object)
{
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);
}

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

return new SemanticHighlightResponse()
{
Spans = results
.GroupBy(result => result.Span.TextSpan.ToString())
.Select(grouping => CreateSemanticSpan(grouping, grouping.First().Lines))
.ToArray()
};
}

private static SemanticHighlightSpan CreateSemanticSpan(IEnumerable<ClassifiedResult> results, TextLineCollection lines)
{
var additiveResults = results.Where(result => ClassificationTypeNames.AdditiveTypeNames.Contains(result.Span.ClassificationType));
var modifiers = additiveResults.Select(result => _modifierMap[result.Span.ClassificationType]).ToArray();

var result = results.Except(additiveResults).Single();
var span = result.Span;

var linePos = lines.GetLinePositionSpan(span.TextSpan);

return new SemanticHighlightSpan
{
StartLine = linePos.Start.Line,
EndLine = linePos.End.Line,
StartColumn = linePos.Start.Character,
EndColumn = linePos.End.Character,
Type = _classificationMap[span.ClassificationType],
Modifiers = modifiers
};
}

class ClassifiedResult
{
public ClassifiedSpan Span { get; set; }
public TextLineCollection Lines { get; set; }
}

private static readonly Dictionary<string, SemanticHighlightClassification> _classificationMap =
new Dictionary<string, SemanticHighlightClassification>
{
[ClassificationTypeNames.Comment] = SemanticHighlightClassification.Comment,
[ClassificationTypeNames.ExcludedCode] = SemanticHighlightClassification.ExcludedCode,
[ClassificationTypeNames.Identifier] = SemanticHighlightClassification.Identifier,
[ClassificationTypeNames.Keyword] = SemanticHighlightClassification.Keyword,
[ClassificationTypeNames.ControlKeyword] = SemanticHighlightClassification.ControlKeyword,
[ClassificationTypeNames.NumericLiteral] = SemanticHighlightClassification.NumericLiteral,
[ClassificationTypeNames.Operator] = SemanticHighlightClassification.Operator,
[ClassificationTypeNames.OperatorOverloaded] = SemanticHighlightClassification.OperatorOverloaded,
[ClassificationTypeNames.PreprocessorKeyword] = SemanticHighlightClassification.PreprocessorKeyword,
[ClassificationTypeNames.StringLiteral] = SemanticHighlightClassification.StringLiteral,
[ClassificationTypeNames.WhiteSpace] = SemanticHighlightClassification.WhiteSpace,
[ClassificationTypeNames.Text] = SemanticHighlightClassification.Text,
[ClassificationTypeNames.StaticSymbol] = SemanticHighlightClassification.StaticSymbol,
[ClassificationTypeNames.PreprocessorText] = SemanticHighlightClassification.PreprocessorText,
[ClassificationTypeNames.Punctuation] = SemanticHighlightClassification.Punctuation,
[ClassificationTypeNames.VerbatimStringLiteral] = SemanticHighlightClassification.VerbatimStringLiteral,
[ClassificationTypeNames.StringEscapeCharacter] = SemanticHighlightClassification.StringEscapeCharacter,
[ClassificationTypeNames.ClassName] = SemanticHighlightClassification.ClassName,
[ClassificationTypeNames.DelegateName] = SemanticHighlightClassification.DelegateName,
[ClassificationTypeNames.EnumName] = SemanticHighlightClassification.EnumName,
[ClassificationTypeNames.InterfaceName] = SemanticHighlightClassification.InterfaceName,
[ClassificationTypeNames.ModuleName] = SemanticHighlightClassification.ModuleName,
[ClassificationTypeNames.StructName] = SemanticHighlightClassification.StructName,
[ClassificationTypeNames.TypeParameterName] = SemanticHighlightClassification.TypeParameterName,
[ClassificationTypeNames.FieldName] = SemanticHighlightClassification.FieldName,
[ClassificationTypeNames.EnumMemberName] = SemanticHighlightClassification.EnumMemberName,
[ClassificationTypeNames.ConstantName] = SemanticHighlightClassification.ConstantName,
[ClassificationTypeNames.LocalName] = SemanticHighlightClassification.LocalName,
[ClassificationTypeNames.ParameterName] = SemanticHighlightClassification.ParameterName,
[ClassificationTypeNames.MethodName] = SemanticHighlightClassification.MethodName,
[ClassificationTypeNames.ExtensionMethodName] = SemanticHighlightClassification.ExtensionMethodName,
[ClassificationTypeNames.PropertyName] = SemanticHighlightClassification.PropertyName,
[ClassificationTypeNames.EventName] = SemanticHighlightClassification.EventName,
[ClassificationTypeNames.NamespaceName] = SemanticHighlightClassification.NamespaceName,
[ClassificationTypeNames.LabelName] = SemanticHighlightClassification.LabelName,
[ClassificationTypeNames.XmlDocCommentAttributeName] = SemanticHighlightClassification.XmlDocCommentAttributeName,
[ClassificationTypeNames.XmlDocCommentAttributeQuotes] = SemanticHighlightClassification.XmlDocCommentAttributeQuotes,
[ClassificationTypeNames.XmlDocCommentAttributeValue] = SemanticHighlightClassification.XmlDocCommentAttributeValue,
[ClassificationTypeNames.XmlDocCommentCDataSection] = SemanticHighlightClassification.XmlDocCommentCDataSection,
[ClassificationTypeNames.XmlDocCommentComment] = SemanticHighlightClassification.XmlDocCommentComment,
[ClassificationTypeNames.XmlDocCommentDelimiter] = SemanticHighlightClassification.XmlDocCommentDelimiter,
[ClassificationTypeNames.XmlDocCommentEntityReference] = SemanticHighlightClassification.XmlDocCommentEntityReference,
[ClassificationTypeNames.XmlDocCommentName] = SemanticHighlightClassification.XmlDocCommentName,
[ClassificationTypeNames.XmlDocCommentProcessingInstruction] = SemanticHighlightClassification.XmlDocCommentProcessingInstruction,
[ClassificationTypeNames.XmlDocCommentText] = SemanticHighlightClassification.XmlDocCommentText,
[ClassificationTypeNames.XmlLiteralAttributeName] = SemanticHighlightClassification.XmlLiteralAttributeName,
[ClassificationTypeNames.XmlLiteralAttributeQuotes] = SemanticHighlightClassification.XmlLiteralAttributeQuotes,
[ClassificationTypeNames.XmlLiteralAttributeValue] = SemanticHighlightClassification.XmlLiteralAttributeValue,
[ClassificationTypeNames.XmlLiteralCDataSection] = SemanticHighlightClassification.XmlLiteralCDataSection,
[ClassificationTypeNames.XmlLiteralComment] = SemanticHighlightClassification.XmlLiteralComment,
[ClassificationTypeNames.XmlLiteralDelimiter] = SemanticHighlightClassification.XmlLiteralDelimiter,
[ClassificationTypeNames.XmlLiteralEmbeddedExpression] = SemanticHighlightClassification.XmlLiteralEmbeddedExpression,
[ClassificationTypeNames.XmlLiteralEntityReference] = SemanticHighlightClassification.XmlLiteralEntityReference,
[ClassificationTypeNames.XmlLiteralName] = SemanticHighlightClassification.XmlLiteralName,
[ClassificationTypeNames.XmlLiteralProcessingInstruction] = SemanticHighlightClassification.XmlLiteralProcessingInstruction,
[ClassificationTypeNames.XmlLiteralText] = SemanticHighlightClassification.XmlLiteralText,
[ClassificationTypeNames.RegexComment] = SemanticHighlightClassification.RegexComment,
[ClassificationTypeNames.RegexCharacterClass] = SemanticHighlightClassification.RegexCharacterClass,
[ClassificationTypeNames.RegexAnchor] = SemanticHighlightClassification.RegexAnchor,
[ClassificationTypeNames.RegexQuantifier] = SemanticHighlightClassification.RegexQuantifier,
[ClassificationTypeNames.RegexGrouping] = SemanticHighlightClassification.RegexGrouping,
[ClassificationTypeNames.RegexAlternation] = SemanticHighlightClassification.RegexAlternation,
[ClassificationTypeNames.RegexText] = SemanticHighlightClassification.RegexText,
[ClassificationTypeNames.RegexSelfEscapedCharacter] = SemanticHighlightClassification.RegexSelfEscapedCharacter,
[ClassificationTypeNames.RegexOtherEscape] = SemanticHighlightClassification.RegexOtherEscape,
};

private static readonly Dictionary<string, SemanticHighlightModifier> _modifierMap =
new Dictionary<string, SemanticHighlightModifier>
{
[ClassificationTypeNames.StaticSymbol] = SemanticHighlightModifier.Static,
};

private readonly OmniSharpWorkspace _workspace;
}
}
Loading