forked from OmniSharp/csharp-language-server-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemanticTokensHandler.cs
113 lines (102 loc) · 4.2 KB
/
SemanticTokensHandler.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
namespace SampleServer
{
#pragma warning disable 618
public class SemanticTokensHandler : SemanticTokensHandlerBase
{
private readonly ILogger _logger;
public SemanticTokensHandler(ILogger<SemanticTokensHandler> logger)
{
_logger = logger;
}
public override async Task<SemanticTokens?> Handle(
SemanticTokensParams request, CancellationToken cancellationToken
)
{
var result = await base.Handle(request, cancellationToken).ConfigureAwait(false);
return result;
}
public override async Task<SemanticTokens?> Handle(
SemanticTokensRangeParams request, CancellationToken cancellationToken
)
{
var result = await base.Handle(request, cancellationToken).ConfigureAwait(false);
return result;
}
public override async Task<SemanticTokensFullOrDelta?> Handle(
SemanticTokensDeltaParams request,
CancellationToken cancellationToken
)
{
var result = await base.Handle(request, cancellationToken).ConfigureAwait(false);
return result;
}
protected override async Task Tokenize(
SemanticTokensBuilder builder, ITextDocumentIdentifierParams identifier,
CancellationToken cancellationToken
)
{
using var typesEnumerator = RotateEnum(SemanticTokenType.Defaults).GetEnumerator();
using var modifiersEnumerator = RotateEnum(SemanticTokenModifier.Defaults).GetEnumerator();
// you would normally get this from a common source that is managed by current open editor, current active editor, etc.
var content = await File.ReadAllTextAsync(DocumentUri.GetFileSystemPath(identifier), cancellationToken).ConfigureAwait(false);
await Task.Yield();
foreach (var (line, text) in content.Split('\n').Select((text, line) => ( line, text )))
{
var parts = text.TrimEnd().Split(';', ' ', '.', '"', '(', ')');
var index = 0;
foreach (var part in parts)
{
typesEnumerator.MoveNext();
modifiersEnumerator.MoveNext();
if (string.IsNullOrWhiteSpace(part)) continue;
index = text.IndexOf(part, index, StringComparison.Ordinal);
builder.Push(line, index, part.Length, typesEnumerator.Current, modifiersEnumerator.Current);
}
}
}
protected override Task<SemanticTokensDocument>
GetSemanticTokensDocument(ITextDocumentIdentifierParams @params, CancellationToken cancellationToken)
{
return Task.FromResult(new SemanticTokensDocument(RegistrationOptions.Legend));
}
private IEnumerable<T> RotateEnum<T>(IEnumerable<T> values)
{
while (true)
{
foreach (var item in values)
yield return item;
}
}
protected override SemanticTokensRegistrationOptions CreateRegistrationOptions(
SemanticTokensCapability capability, ClientCapabilities clientCapabilities
)
{
return new SemanticTokensRegistrationOptions
{
DocumentSelector = TextDocumentSelector.ForLanguage("csharp"),
Legend = new SemanticTokensLegend
{
TokenModifiers = capability.TokenModifiers,
TokenTypes = capability.TokenTypes
},
Full = new SemanticTokensCapabilityRequestFull
{
Delta = true
},
Range = true
};
}
}
#pragma warning restore 618
}