Skip to content

Commit 5b11b89

Browse files
Merge branch 'topic/issue_65' into 'edge'
VB23-029: Use diagnostics to report renaming collisions See merge request eng/ide/ada_language_server!1163
2 parents 563a74f + 2dd162b commit 5b11b89

File tree

7 files changed

+191
-25
lines changed

7 files changed

+191
-25
lines changed

source/ada/lsp-ada_documents.adb

+17-1
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ package body LSP.Ada_Documents is
13231323
----------------
13241324

13251325
procedure Get_Errors
1326-
(Self : Document;
1326+
(Self : in out Document;
13271327
Context : LSP.Ada_Contexts.Context;
13281328
Changed : out Boolean;
13291329
Errors : out LSP.Messages.Diagnostic_Vector) is
@@ -2963,6 +2963,22 @@ package body LSP.Ada_Documents is
29632963
character => End_Iterator.Last_UTF16_Offset));
29642964
end To_LSP_Range;
29652965

2966+
---------------------
2967+
-- To_LSP_Location --
2968+
---------------------
2969+
2970+
function To_LSP_Location
2971+
(Self : Document;
2972+
Segment : Langkit_Support.Slocs.Source_Location_Range;
2973+
Kind : LSP.Messages.AlsReferenceKind_Set := LSP.Messages.Empty_Set)
2974+
return LSP.Messages.Location is
2975+
begin
2976+
return LSP.Messages.Location'
2977+
(uri => Self.URI,
2978+
span => To_LSP_Range (Self, Segment),
2979+
alsKind => <>);
2980+
end To_LSP_Location;
2981+
29662982
----------
29672983
-- Unit --
29682984
----------

source/ada/lsp-ada_documents.ads

+8-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ package LSP.Ada_Documents is
8383
return LSP.Messages.Span;
8484
-- Convert LAL's Source_Location_Range to LSP's Range
8585

86+
function To_LSP_Location
87+
(Self : Document;
88+
Segment : Langkit_Support.Slocs.Source_Location_Range;
89+
Kind : LSP.Messages.AlsReferenceKind_Set := LSP.Messages.Empty_Set)
90+
return LSP.Messages.Location;
91+
-- Convert LAL's Source_Location_Range and document's uri to a LSP location
92+
8693
procedure Apply_Changes
8794
(Self : aliased in out Document;
8895
Version : LSP.Types.LSP_Number;
@@ -99,7 +106,7 @@ package LSP.Ada_Documents is
99106
-- These requests are meaningful within a document/context pair
100107

101108
procedure Get_Errors
102-
(Self : Document;
109+
(Self : in out Document;
103110
Context : LSP.Ada_Contexts.Context;
104111
Changed : out Boolean;
105112
Errors : out LSP.Messages.Diagnostic_Vector);

source/ada/lsp-ada_handlers.adb

+108-18
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,17 @@ package body LSP.Ada_Handlers is
11841184
Self.Line_Folding_Only := True;
11851185
end if;
11861186

1187+
if Value.capabilities.textDocument.publishDiagnostics.Is_Set
1188+
and then Value.capabilities.textDocument.publishDiagnostics.Value.
1189+
relatedInformation.Is_Set
1190+
then
1191+
-- Client capability to support relatedInformation field in
1192+
-- diagnostics.
1193+
Self.Supports_Related_Diagnostics :=
1194+
Value.capabilities.textDocument.publishDiagnostics.Value.
1195+
relatedInformation.Value;
1196+
end if;
1197+
11871198
if Value.capabilities.textDocument.completion.completionItem.Is_Set
11881199
and then Value.capabilities.textDocument.completion.
11891200
completionItem.Value.snippetSupport = True
@@ -3924,15 +3935,29 @@ package body LSP.Ada_Handlers is
39243935
-- possible to encounter the same Text_Edit more than once, so this
39253936
-- stores all the unique edits
39263937

3927-
procedure Process_Context (C : Context_Access);
3938+
Definition_Node : Defining_Name;
3939+
-- Used to retrieve the definition node found for a given context
3940+
3941+
procedure Process_Context
3942+
(C : Context_Access;
3943+
Definition_Node : out Defining_Name);
39283944
-- Process the rename request for the given context, and add the
39293945
-- edits to `All_Edits`.
39303946

3947+
function To_LSP_Diagnostic
3948+
(Problem : Laltools.Refactor.Refactoring_Diagnotic'Class;
3949+
Definition_Node : Defining_Name)
3950+
return LSP.Messages.Diagnostic;
3951+
-- Convert a laltool refactoring diagnostic into a LSP one.
3952+
39313953
---------------------
39323954
-- Process_Context --
39333955
---------------------
39343956

3935-
procedure Process_Context (C : Context_Access) is
3957+
procedure Process_Context
3958+
(C : Context_Access;
3959+
Definition_Node : out Defining_Name)
3960+
is
39363961
use Laltools.Refactor.Safe_Rename;
39373962

39383963
Node : constant Ada_Node := C.Get_Node_At (Document, Position);
@@ -4182,6 +4207,8 @@ package body LSP.Ada_Handlers is
41824207
end Process_References;
41834208

41844209
begin
4210+
Definition_Node := Definition;
4211+
41854212
if Definition.Is_Null then
41864213
return;
41874214
end if;
@@ -4206,31 +4233,94 @@ package body LSP.Ada_Handlers is
42064233
Process_File_Renames;
42074234
end Process_Context;
42084235

4236+
-----------------------
4237+
-- To_LSP_Diagnostic --
4238+
-----------------------
4239+
4240+
function To_LSP_Diagnostic
4241+
(Problem : Laltools.Refactor.Refactoring_Diagnotic'Class;
4242+
Definition_Node : Defining_Name)
4243+
return LSP.Messages.Diagnostic
4244+
is
4245+
Diagnostic : LSP.Messages.Diagnostic;
4246+
begin
4247+
Diagnostic := LSP.Messages.Diagnostic'
4248+
(span =>
4249+
To_Span (Definition_Node.Sloc_Range),
4250+
severity => (True, LSP.Messages.Error),
4251+
code => <>,
4252+
codeDescription => <>,
4253+
source =>
4254+
(True, To_Virtual_String ("Ada")),
4255+
message =>
4256+
(if Self.Supports_Related_Diagnostics then
4257+
VSS.Strings.Conversions.To_Virtual_String
4258+
("Can't rename identifier '"
4259+
& Langkit_Support.Text.To_UTF8
4260+
(Definition_Node.Text)
4261+
& "'")
4262+
else VSS.Strings.Conversions.To_Virtual_String
4263+
(Problem.Info)),
4264+
tags => <>,
4265+
relatedInformation => <>);
4266+
4267+
if Self.Supports_Related_Diagnostics then
4268+
Diagnostic.relatedInformation.Append
4269+
(LSP.Messages.DiagnosticRelatedInformation'(
4270+
location => LSP.Messages.Location'
4271+
(uri => File_To_URI (Problem.Filename),
4272+
span => To_Span (Problem.Location),
4273+
alsKind => <>),
4274+
message => VSS.Strings.Conversions.To_Virtual_String
4275+
(Problem.Info)));
4276+
end if;
4277+
4278+
return Diagnostic;
4279+
end To_LSP_Diagnostic;
4280+
42094281
begin
42104282
for C of Self.Contexts_For_URI (Value.textDocument.uri) loop
4211-
Process_Context (C);
4283+
Process_Context (C, Definition_Node);
42124284

4213-
-- If problems were found, send an error message and do not proceed
4214-
-- with the renames.
4285+
-- If problems were found, send an error reponse and a diagnostic for
4286+
-- each issue. Do not proceed with the renames.
42154287

42164288
if not Context_Edits.Diagnostics.Is_Empty then
42174289
return Response : LSP.Messages.Server_Responses.Rename_Response
42184290
(Is_Error => True)
42194291
do
42204292
declare
4221-
Error_Message : VSS.String_Vectors.Virtual_String_Vector;
4222-
4293+
Diag_Params : LSP.Messages.PublishDiagnosticsParams;
4294+
Diagnostic : LSP.Messages.Diagnostic;
42234295
begin
4296+
-- For each problem detected in a given file by laltools,
4297+
-- convert it to a LSP diagnostic and publish them when
4298+
-- switching to another file.
4299+
42244300
for Problem of Context_Edits.Diagnostics loop
4225-
Error_Message.Append
4226-
(VSS.Strings.Conversions.To_Virtual_String
4227-
(Problem.Info));
4301+
Diagnostic := To_LSP_Diagnostic
4302+
(Problem, Definition_Node);
4303+
4304+
if To_UTF_8_String (Diag_Params.uri) = "" or else
4305+
To_UTF_8_String (Diag_Params.uri) = Problem.Filename
4306+
then
4307+
Diag_Params.diagnostics.Append (Diagnostic);
4308+
Diag_Params.uri := Value.textDocument.uri;
4309+
else
4310+
Self.Server.On_Publish_Diagnostics (Diag_Params);
4311+
Diag_Params.uri := File_To_URI ("");
4312+
Diag_Params.diagnostics.Clear;
4313+
end if;
42284314
end loop;
42294315

4316+
if not Diag_Params.diagnostics.Is_Empty then
4317+
Self.Server.On_Publish_Diagnostics (Diag_Params);
4318+
end if;
4319+
42304320
Response.error :=
42314321
(True,
4232-
(code => LSP.Errors.InvalidRequest,
4233-
message => Error_Message.Join_Lines (VSS.Strings.LF),
4322+
(code => LSP.Errors.RequestFailed,
4323+
message => <>,
42344324
data => Empty));
42354325
end;
42364326
end return;
@@ -6389,16 +6479,16 @@ package body LSP.Ada_Handlers is
63896479
(Self : access Message_Handler'Class;
63906480
Document : not null LSP.Ada_Documents.Document_Access)
63916481
is
6392-
Ok : Boolean;
6393-
Diag : LSP.Messages.PublishDiagnosticsParams;
6482+
Changed : Boolean;
6483+
Diag : LSP.Messages.PublishDiagnosticsParams;
63946484
begin
63956485
if Self.Diagnostics_Enabled then
63966486
Document.Get_Errors
6397-
(Self.Contexts.Get_Best_Context (Document.URI).all,
6398-
Ok,
6399-
Diag.diagnostics);
6487+
(Context => Self.Contexts.Get_Best_Context (Document.URI).all,
6488+
Changed => Changed,
6489+
Errors => Diag.diagnostics);
64006490

6401-
if Ok then
6491+
if Changed then
64026492
Diag.uri := Document.URI;
64036493
Self.Server.On_Publish_Diagnostics (Diag);
64046494
end if;

source/ada/lsp-ada_handlers.ads

+3
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ private
342342
Line_Folding_Only : Boolean := False;
343343
-- Client capabilities, folding only per lines
344344

345+
Supports_Related_Diagnostics : Boolean := False;
346+
-- Client capabilities to support diagnostics' relatedInformation.
347+
345348
Completion_Snippets_Enabled : Boolean := False;
346349
-- True if the client supports completion snippets
347350

source/protocol/lsp-errors.adb

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ package body LSP.Errors is
3434
ServerNotInitialized => -32002,
3535
UnknownErrorCode => -32001,
3636
RequestCancelled => -32800,
37-
ContentModified => -32801);
37+
ContentModified => -32801,
38+
ServerCancelled => -32802,
39+
RequestFailed => -32803);
3840

3941
------------------------
4042
-- Read_ResponseError --

source/protocol/lsp-errors.ads

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ package LSP.Errors is
3535
ServerNotInitialized,
3636
UnknownErrorCode,
3737
RequestCancelled,
38-
ContentModified);
38+
ContentModified,
39+
ServerCancelled,
40+
RequestFailed);
3941

4042
type ResponseError is record
4143
code : ErrorCodes;

testsuite/ada_lsp/SA11-038_Name_Collision/test.json

+49-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
"foldingRange": {
6666
"lineFoldingOnly": true
6767
},
68+
"publishDiagnostics": {
69+
"relatedInformation": true
70+
},
6871
"selectionRange": {},
6972
"callHierarchy": {}
7073
}
@@ -162,12 +165,55 @@
162165
},
163166
"wait": [
164167
{
168+
"jsonrpc": "2.0",
169+
"method": "textDocument/publishDiagnostics",
170+
"params": {
171+
"uri": "$URI{main.adb}",
172+
"diagnostics": [
173+
{
174+
"range": {
175+
"start": {
176+
"line": 1,
177+
"character": 3
178+
},
179+
"end": {
180+
"line": 1,
181+
"character": 4
182+
}
183+
},
184+
"severity": 1,
185+
"source": "Ada",
186+
"message": "Can't rename identifier 'A'",
187+
"relatedInformation": [
188+
{
189+
"location": {
190+
"uri": "$URI{main.adb}",
191+
"range": {
192+
"start": {
193+
"line": 2,
194+
"character": 3
195+
},
196+
"end": {
197+
"line": 2,
198+
"character": 4
199+
}
200+
}
201+
},
202+
"message": "Renaming A to B creates a name collision with <Id \"B\" main.adb:3:4-3:5>"
203+
}
204+
]
205+
}
206+
]
207+
}
208+
},
209+
{
210+
"jsonrpc": "2.0",
165211
"id": "ada-4",
166212
"error": {
167-
"code": -32600,
168-
"message": "Renaming A to B creates a name collision with <Id \"B\" main.adb:3:4-3:5>\n"
213+
"code": -32803,
214+
"message": ""
169215
}
170-
}
216+
}
171217
]
172218
}
173219
},

0 commit comments

Comments
 (0)