Skip to content

Commit 8ca1051

Browse files
committed
Fix semanticTokens request
to avoid returning token types/modifiers not supported by the client and avoid raising CONSTRAINT_ERROR. Fixes #1070
1 parent 6874829 commit 8ca1051

File tree

4 files changed

+288
-4
lines changed

4 files changed

+288
-4
lines changed

source/ada/lsp-ada_highlighters.adb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ package body LSP.Ada_Highlighters is
3131
Highlighter_Debug : constant GNATCOLL.Traces.Trace_Handle :=
3232
GNATCOLL.Traces.Create ("ALS.HIGHLIGHTERS.DEBUG", GNATCOLL.Traces.Off);
3333

34+
Skip : LSP.Messages.SemanticTokenTypes renames LSP.Messages.macro;
35+
-- A dedicated token type for unsupported tokens
36+
3437
package Highlights_Holders is
3538
type Highlights_Holder is tagged limited private;
3639
-- Highlights_Holder stores style for each token in the range given
@@ -344,16 +347,14 @@ package body LSP.Ada_Highlighters is
344347
Start : constant Langkit_Support.Slocs.Source_Location :=
345348
Langkit_Support.Slocs.Start_Sloc (Sloc_Range);
346349

347-
Skip : LSP.Messages.SemanticTokenTypes renames macro;
348-
349350
Map : constant array (Libadalang.Common.Token_Kind) of
350351
LSP.Messages.SemanticTokenTypes :=
351352
(Ada_All .. Ada_Xor | Ada_With => keyword,
352353
Ada_Par_Close .. Ada_Target => operator,
353354
Ada_String | Ada_Char => a_string,
354355
Ada_Decimal | Ada_Integer => number,
355356
Ada_Comment => comment,
356-
Ada_Identifier => modifier,
357+
Ada_Identifier => Skip,
357358
others => Skip);
358359

359360
Mapped_Token : constant LSP.Messages.SemanticTokenTypes :=
@@ -367,7 +368,8 @@ package body LSP.Ada_Highlighters is
367368
-- literal, or +/- before exponent in numeric literal, etc.
368369
if Value.Is_Set or
369370
(Mapped_Token /= Skip and then
370-
Value.Modifiers /= Highlights_Holders.Empty)
371+
Self.Token_Types.Contains (Mapped_Token) and then
372+
Value.Modifiers /= Highlights_Holders.Empty)
371373
then
372374
pragma Assert
373375
(Sloc_Range.End_Line = Sloc_Range.Start_Line);
@@ -704,6 +706,9 @@ package body LSP.Ada_Highlighters is
704706
if Token < From_Token or To_Token < Token then
705707
-- Skip uninteresting tokens
706708
return;
709+
elsif not Self.Token_Types.Contains (Kind) then
710+
-- Skip unsupported tokens
711+
return;
707712
end if;
708713

709714
Holder.Set_Token_Kind (Token, Kind);
@@ -720,6 +725,9 @@ package body LSP.Ada_Highlighters is
720725
if Token < From_Token or To_Token < Token then
721726
-- Skip uninteresting tokens
722727
return;
728+
elsif not Self.Token_Modifiers.Contains (Kind) then
729+
-- Skip unsupported tokens
730+
return;
723731
end if;
724732

725733
Holder.Set_Token_Modifier (Token, Kind);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
with Ada.Unchecked_Conversion;
2+
procedure Foo is
3+
4+
type My_Type is new Integer range 1 .. 10;
5+
6+
X, Y : My_Type := 1; -- Decl with multiple defining names
7+
begin
8+
X := Y + My_Type (9);
9+
end Foo;
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
[
2+
{
3+
"comment": [
4+
"Test semanticTokens to skip tokens not supported by a client.",
5+
"Initialize request skips `namespace` (package) token type and",
6+
"`declaration` token modifier. Server responses with 3 tokens:",
7+
"token `Ada` is skipped, because it is a package/namespace, so",
8+
"this token type isn't supported by the client.",
9+
"Next token `Unchecked_Conversion` is returned as `function`",
10+
"with `defaultLibrary` modifier. Then token `My_Type` as",
11+
"`type` with `static` modifier (and no `declaration` modifier).",
12+
"Then `Integer` as `type` and [`static` + `defaultLibrary`]",
13+
"modifier. Token types/modifiers encoding differs from",
14+
"'standard encoding' - the code decreased by one."
15+
]
16+
},
17+
{
18+
"start": {
19+
"cmd": [
20+
"${ALS}"
21+
]
22+
}
23+
},
24+
{
25+
"send": {
26+
"request": {
27+
"params": {
28+
"processId": 31570,
29+
"capabilities": {
30+
"textDocument": {
31+
"completion": {
32+
"completionItem": {
33+
"documentationFormat": [
34+
"plaintext",
35+
"markdown"
36+
],
37+
"snippetSupport": true
38+
},
39+
"dynamicRegistration": true
40+
},
41+
"semanticTokens": {
42+
"refreshSupport":true,
43+
"dynamicRegistration":true,
44+
"tokenTypes": ["type","class","enum","interface","struct","typeParameter","parameter","variable","property","enumMember","event","function","method","macro","keyword","modifier","comment","string","number","regexp","operator"],
45+
"tokenModifiers": ["definition","readonly","static","deprecated","abstract","async","modification","documentation","defaultLibrary"],
46+
"formats":["relative"],
47+
"requests": {
48+
"range": true,
49+
"full": true
50+
},
51+
"overlappingTokenSupport" : true,
52+
"multilineTokenSupport" : true
53+
},
54+
"definition": {},
55+
"hover": {},
56+
"formatting": {
57+
"dynamicRegistration": true
58+
},
59+
"implementation": {},
60+
"codeLens": {},
61+
"typeDefinition": {},
62+
"selectionRange": {},
63+
"documentHighlight": {},
64+
"documentSymbol": {
65+
"hierarchicalDocumentSymbolSupport": true
66+
},
67+
"synchronization": {},
68+
"references": {},
69+
"rangeFormatting": {},
70+
"onTypeFormatting": {},
71+
"declaration": {},
72+
"foldingRange": {
73+
"lineFoldingOnly": true
74+
},
75+
"colorProvider": {}
76+
},
77+
"workspace": {
78+
"applyEdit": true,
79+
"executeCommand": {},
80+
"didChangeWatchedFiles": {},
81+
"workspaceEdit": {},
82+
"didChangeConfiguration": {},
83+
"semanticTokens": {
84+
"refreshSupport": true
85+
}
86+
}
87+
},
88+
"rootUri": "$URI{.}"
89+
},
90+
"jsonrpc": "2.0",
91+
"id": 1,
92+
"method": "initialize"
93+
},
94+
"wait": [
95+
{
96+
"jsonrpc": "2.0",
97+
"id": 1,
98+
"result": {
99+
"capabilities": {
100+
"textDocumentSync": 2,
101+
"completionProvider": {
102+
"triggerCharacters": [
103+
".",
104+
",",
105+
"'",
106+
"("
107+
],
108+
"resolveProvider": true
109+
},
110+
"semanticTokensProvider": {
111+
"legend": {
112+
"tokenTypes": ["type","class","enum","interface","struct","typeParameter","parameter","variable","property","enumMember","function","keyword","modifier","comment","string","number","operator"],
113+
"tokenModifiers": ["definition","readonly","static","deprecated","abstract","modification","documentation","defaultLibrary"]
114+
},
115+
"range": true,
116+
"full": {}
117+
},
118+
"hoverProvider": true,
119+
"declarationProvider": true,
120+
"definitionProvider": true,
121+
"typeDefinitionProvider": true,
122+
"implementationProvider": true,
123+
"referencesProvider": true,
124+
"codeActionProvider": {
125+
},
126+
"renameProvider": {
127+
},
128+
"foldingRangeProvider": true,
129+
"executeCommandProvider": {},
130+
"alsReferenceKinds": [
131+
"reference",
132+
"access",
133+
"write",
134+
"call",
135+
"dispatching call",
136+
"parent",
137+
"child",
138+
"overriding"
139+
]
140+
}
141+
}
142+
}
143+
]
144+
}
145+
},
146+
{
147+
"send": {
148+
"request": {
149+
"jsonrpc": "2.0",
150+
"method": "initialized"
151+
},
152+
"wait": []
153+
}
154+
},
155+
{
156+
"send": {
157+
"request": {
158+
"params": {
159+
"settings": {
160+
"ada": {
161+
"projectFile": "$URI{default.gpr}",
162+
"scenarioVariables": {},
163+
"defaultCharset": "ISO-8859-1"
164+
}
165+
}
166+
},
167+
"jsonrpc": "2.0",
168+
"method": "workspace/didChangeConfiguration"
169+
},
170+
"wait": []
171+
}
172+
},
173+
{
174+
"send": {
175+
"request": {
176+
"jsonrpc": "2.0",
177+
"method": "textDocument/didOpen",
178+
"params": {
179+
"textDocument": {
180+
"uri": "$URI{foo.adb}",
181+
"languageId": "Ada",
182+
"version": 0,
183+
"text": "with Ada.Unchecked_Conversion;\nprocedure Foo is\n\n type My_Type is new Integer range 1 .. 10;\n\n X, Y : My_Type := 1; -- Decl with multiple defining names\nbegin\n X := Y + My_Type (9);\nend Foo;\n"
184+
}
185+
}
186+
},
187+
"wait": []
188+
}
189+
},
190+
{
191+
"send": {
192+
"request": {
193+
"jsonrpc": "2.0",
194+
"id": 2,
195+
"method": "textDocument/semanticTokens/range",
196+
"params": {
197+
"textDocument": {
198+
"uri": "$URI{foo.adb}"
199+
},
200+
"range": {
201+
"start": {
202+
"line": 0,
203+
"character": 0
204+
},
205+
"end": {
206+
"line": 4,
207+
"character": 0
208+
}
209+
}
210+
}
211+
},
212+
"wait": [
213+
{
214+
"jsonrpc": "2.0",
215+
"id": 2,
216+
"result": {
217+
"data": [
218+
0,
219+
9,
220+
20,
221+
10,
222+
128,
223+
224+
1,
225+
10,
226+
3,
227+
10,
228+
0,
229+
230+
2,
231+
8,
232+
7,
233+
0,
234+
4,
235+
236+
0,
237+
15,
238+
7,
239+
0,
240+
132
241+
]
242+
}
243+
}
244+
]
245+
}
246+
},
247+
{
248+
"send": {
249+
"request": {
250+
"jsonrpc": "2.0",
251+
"method": "textDocument/didClose",
252+
"params": {
253+
"textDocument": {
254+
"uri": "$URI{foo.adb}"
255+
}
256+
}
257+
},
258+
"wait": []
259+
}
260+
},
261+
{
262+
"stop": {
263+
"exit_code": 0
264+
}
265+
}
266+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
title: 'V628-012.highlighting.no-libs'

0 commit comments

Comments
 (0)