-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathSemanticTokensBuilder.cs
179 lines (163 loc) · 7.39 KB
/
SemanticTokensBuilder.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using OmniSharp.Extensions.LanguageServer.Protocol.Models.Proposals;
using Range = OmniSharp.Extensions.LanguageServer.Protocol.Models.Range;
namespace OmniSharp.Extensions.LanguageServer.Protocol.Document.Proposals
{
[Obsolete(Constants.Proposal)]
public class SemanticTokensBuilder
{
/// <summary>
/// The window size of the data array
/// </summary>
private const int Parition = 5;
private int _prevLine;
private int _prevChar;
private readonly SemanticTokensDocument _document;
private readonly SemanticTokensLegend _legend;
private readonly ImmutableArray<int>.Builder _data;
private int _dataLen;
public SemanticTokensBuilder(
SemanticTokensDocument document,
SemanticTokensLegend legend
)
{
_document = document;
_legend = legend;
_data = ImmutableArray<int>.Empty.ToBuilder();
}
/// <summary>
/// Push a token onto the builder
/// </summary>
/// <param name="line"></param>
/// <param name="char"></param>
/// <param name="length"></param>
/// <param name="tokenType"></param>
/// <param name="tokenModifiers"></param>
public void Push(int line, int @char, int length, SemanticTokenType? tokenType, params SemanticTokenModifier[] tokenModifiers) => Push(
line, @char, length, _legend.GetTokenTypeIdentity(tokenType), _legend.GetTokenModifiersIdentity(tokenModifiers)
);
/// <summary>
/// Push a token onto the builder
/// </summary>
/// <remarks>Avoid creating the range just to call this method</remarks>
/// <param name="range">The range, cannot span multiple lines</param>
/// <param name="tokenType"></param>
/// <param name="tokenModifiers"></param>
public void Push(Range range, SemanticTokenType? tokenType, params SemanticTokenModifier[] tokenModifiers) => Push(
range, _legend.GetTokenTypeIdentity(tokenType), _legend.GetTokenModifiersIdentity(tokenModifiers)
);
/// <summary>
/// Push a token onto the builder
/// </summary>
/// <param name="line"></param>
/// <param name="char"></param>
/// <param name="length"></param>
/// <param name="tokenType"></param>
/// <param name="tokenModifiers"></param>
public void Push(int line, int @char, int length, SemanticTokenType? tokenType, IEnumerable<SemanticTokenModifier> tokenModifiers) => Push(
line, @char, length, _legend.GetTokenTypeIdentity(tokenType), _legend.GetTokenModifiersIdentity(tokenModifiers)
);
/// <summary>
/// Push a token onto the builder
/// </summary>
/// <remarks>Avoid creating the range just to call this method</remarks>
/// <param name="range">The range, cannot span multiple lines</param>
/// <param name="tokenType"></param>
/// <param name="tokenModifiers"></param>
public void Push(Range range, SemanticTokenType? tokenType, IEnumerable<SemanticTokenModifier> tokenModifiers) => Push(
range, _legend.GetTokenTypeIdentity(tokenType), _legend.GetTokenModifiersIdentity(tokenModifiers)
);
/// <summary>
/// Push a token onto the builder
/// </summary>
/// <param name="line"></param>
/// <param name="char"></param>
/// <param name="length"></param>
/// <param name="tokenType"></param>
/// <param name="tokenModifiers"></param>
public void Push(int line, int @char, int length, string tokenType, params string[] tokenModifiers) => Push(
line, @char, length, _legend.GetTokenTypeIdentity(tokenType), _legend.GetTokenModifiersIdentity(tokenModifiers)
);
/// <summary>
/// Push a token onto the builder
/// </summary>
/// <remarks>Avoid creating the range just to call this method</remarks>
/// <param name="range">The range, cannot span multiple lines</param>
/// <param name="tokenType"></param>
/// <param name="tokenModifiers"></param>
public void Push(Range range, string tokenType, params string[] tokenModifiers) => Push(
range, _legend.GetTokenTypeIdentity(tokenType), _legend.GetTokenModifiersIdentity(tokenModifiers)
);
/// <summary>
/// Push a token onto the builder
/// </summary>
/// <param name="line"></param>
/// <param name="char"></param>
/// <param name="length"></param>
/// <param name="tokenType"></param>
/// <param name="tokenModifiers"></param>
public void Push(int line, int @char, int length, string tokenType, IEnumerable<string> tokenModifiers) =>
Push(
line, @char, length, _legend.GetTokenTypeIdentity(tokenType),
_legend.GetTokenModifiersIdentity(tokenModifiers)
);
/// <summary>
/// Push a token onto the builder
/// </summary>
/// <remarks>Avoid creating the range just to call this method</remarks>
/// <param name="range">The range, cannot span multiple lines</param>
/// <param name="tokenType"></param>
/// <param name="tokenModifiers"></param>
public void Push(Range range, string tokenType, IEnumerable<string> tokenModifiers) => Push(
range, _legend.GetTokenTypeIdentity(tokenType), _legend.GetTokenModifiersIdentity(tokenModifiers)
);
/// <summary>
/// Push a token onto the builder
/// </summary>
/// <param name="line"></param>
/// <param name="char"></param>
/// <param name="length"></param>
/// <param name="tokenType"></param>
/// <param name="tokenModifiers"></param>
public void Push(int line, int @char, int length, int tokenType, int tokenModifiers)
{
var pushLine = line;
var pushChar = @char;
if (_dataLen > 0)
{
pushLine -= _prevLine;
if (pushLine == 0)
{
pushChar -= _prevChar;
}
}
_dataLen += Parition;
_data.AddRange(pushLine, pushChar, length, tokenType, tokenModifiers);
_prevLine = line;
_prevChar = @char;
}
/// <summary>
/// Push a token onto the builder
/// </summary>
/// <remarks>Avoid creating the range just to call this method</remarks>
/// <param name="range">The range, cannot span multiple lines</param>
/// <param name="tokenType"></param>
/// <param name="tokenModifiers"></param>
public void Push(Range range, int tokenType, int tokenModifiers)
{
if (range.Start.Line != range.End.Line) throw new ArgumentOutOfRangeException(nameof(range), "Range must not span multiple lines");
Push(range.Start.Line, range.Start.Character, range.End.Character - range.Start.Character, tokenType, tokenModifiers);
}
/// <summary>
/// Apply the changes to the source document
/// </summary>
public SemanticTokensDocument Commit()
{
_document.DataLen = _dataLen;
_document.Data = _data.ToImmutable();
return _document;
}
}
}