Skip to content

Commit 28600fd

Browse files
Merge branch 'topic/VB23-029' into 'master'
VB23-029: Use diagnostics to report renaming collisions See merge request eng/ide/ada_language_server!1155
2 parents bbc8ce6 + 5d65bfe commit 28600fd

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
@@ -1147,6 +1147,17 @@ package body LSP.Ada_Handlers is
11471147
Self.Line_Folding_Only := True;
11481148
end if;
11491149

1150+
if Value.capabilities.textDocument.publishDiagnostics.Is_Set
1151+
and then Value.capabilities.textDocument.publishDiagnostics.Value.
1152+
relatedInformation.Is_Set
1153+
then
1154+
-- Client capability to support relatedInformation field in
1155+
-- diagnostics.
1156+
Self.Supports_Related_Diagnostics :=
1157+
Value.capabilities.textDocument.publishDiagnostics.Value.
1158+
relatedInformation.Value;
1159+
end if;
1160+
11501161
if Value.capabilities.textDocument.completion.completionItem.Is_Set
11511162
and then Value.capabilities.textDocument.completion.
11521163
completionItem.Value.snippetSupport = True
@@ -3887,15 +3898,29 @@ package body LSP.Ada_Handlers is
38873898
-- possible to encounter the same Text_Edit more than once, so this
38883899
-- stores all the unique edits
38893900

3890-
procedure Process_Context (C : Context_Access);
3901+
Definition_Node : Defining_Name;
3902+
-- Used to retrieve the definition node found for a given context
3903+
3904+
procedure Process_Context
3905+
(C : Context_Access;
3906+
Definition_Node : out Defining_Name);
38913907
-- Process the rename request for the given context, and add the
38923908
-- edits to `All_Edits`.
38933909

3910+
function To_LSP_Diagnostic
3911+
(Problem : Laltools.Refactor.Refactoring_Diagnotic'Class;
3912+
Definition_Node : Defining_Name)
3913+
return LSP.Messages.Diagnostic;
3914+
-- Convert a laltool refactoring diagnostic into a LSP one.
3915+
38943916
---------------------
38953917
-- Process_Context --
38963918
---------------------
38973919

3898-
procedure Process_Context (C : Context_Access) is
3920+
procedure Process_Context
3921+
(C : Context_Access;
3922+
Definition_Node : out Defining_Name)
3923+
is
38993924
use GNATCOLL.Projects;
39003925
use Laltools.Refactor.Safe_Rename;
39013926

@@ -4145,6 +4170,8 @@ package body LSP.Ada_Handlers is
41454170
end Process_References;
41464171

41474172
begin
4173+
Definition_Node := Definition;
4174+
41484175
if Definition.Is_Null then
41494176
return;
41504177
end if;
@@ -4169,31 +4196,94 @@ package body LSP.Ada_Handlers is
41694196
Process_File_Renames;
41704197
end Process_Context;
41714198

4199+
-----------------------
4200+
-- To_LSP_Diagnostic --
4201+
-----------------------
4202+
4203+
function To_LSP_Diagnostic
4204+
(Problem : Laltools.Refactor.Refactoring_Diagnotic'Class;
4205+
Definition_Node : Defining_Name)
4206+
return LSP.Messages.Diagnostic
4207+
is
4208+
Diagnostic : LSP.Messages.Diagnostic;
4209+
begin
4210+
Diagnostic := LSP.Messages.Diagnostic'
4211+
(span =>
4212+
To_Span (Definition_Node.Sloc_Range),
4213+
severity => (True, LSP.Messages.Error),
4214+
code => <>,
4215+
codeDescription => <>,
4216+
source =>
4217+
(True, To_Virtual_String ("Ada")),
4218+
message =>
4219+
(if Self.Supports_Related_Diagnostics then
4220+
VSS.Strings.Conversions.To_Virtual_String
4221+
("Can't rename identifier '"
4222+
& Langkit_Support.Text.To_UTF8
4223+
(Definition_Node.Text)
4224+
& "'")
4225+
else VSS.Strings.Conversions.To_Virtual_String
4226+
(Problem.Info)),
4227+
tags => <>,
4228+
relatedInformation => <>);
4229+
4230+
if Self.Supports_Related_Diagnostics then
4231+
Diagnostic.relatedInformation.Append
4232+
(LSP.Messages.DiagnosticRelatedInformation'(
4233+
location => LSP.Messages.Location'
4234+
(uri => File_To_URI (Problem.Filename),
4235+
span => To_Span (Problem.Location),
4236+
alsKind => <>),
4237+
message => VSS.Strings.Conversions.To_Virtual_String
4238+
(Problem.Info)));
4239+
end if;
4240+
4241+
return Diagnostic;
4242+
end To_LSP_Diagnostic;
4243+
41724244
begin
41734245
for C of Self.Contexts_For_URI (Value.textDocument.uri) loop
4174-
Process_Context (C);
4246+
Process_Context (C, Definition_Node);
41754247

4176-
-- If problems were found, send an error message and do not proceed
4177-
-- with the renames.
4248+
-- If problems were found, send an error reponse and a diagnostic for
4249+
-- each issue. Do not proceed with the renames.
41784250

41794251
if not Context_Edits.Diagnostics.Is_Empty then
41804252
return Response : LSP.Messages.Server_Responses.Rename_Response
41814253
(Is_Error => True)
41824254
do
41834255
declare
4184-
Error_Message : VSS.String_Vectors.Virtual_String_Vector;
4185-
4256+
Diag_Params : LSP.Messages.PublishDiagnosticsParams;
4257+
Diagnostic : LSP.Messages.Diagnostic;
41864258
begin
4259+
-- For each problem detected in a given file by laltools,
4260+
-- convert it to a LSP diagnostic and publish them when
4261+
-- switching to another file.
4262+
41874263
for Problem of Context_Edits.Diagnostics loop
4188-
Error_Message.Append
4189-
(VSS.Strings.Conversions.To_Virtual_String
4190-
(Problem.Info));
4264+
Diagnostic := To_LSP_Diagnostic
4265+
(Problem, Definition_Node);
4266+
4267+
if To_UTF_8_String (Diag_Params.uri) = "" or else
4268+
To_UTF_8_String (Diag_Params.uri) = Problem.Filename
4269+
then
4270+
Diag_Params.diagnostics.Append (Diagnostic);
4271+
Diag_Params.uri := Value.textDocument.uri;
4272+
else
4273+
Self.Server.On_Publish_Diagnostics (Diag_Params);
4274+
Diag_Params.uri := File_To_URI ("");
4275+
Diag_Params.diagnostics.Clear;
4276+
end if;
41914277
end loop;
41924278

4279+
if not Diag_Params.diagnostics.Is_Empty then
4280+
Self.Server.On_Publish_Diagnostics (Diag_Params);
4281+
end if;
4282+
41934283
Response.error :=
41944284
(True,
4195-
(code => LSP.Errors.InvalidRequest,
4196-
message => Error_Message.Join_Lines (VSS.Strings.LF),
4285+
(code => LSP.Errors.RequestFailed,
4286+
message => <>,
41974287
data => Empty));
41984288
end;
41994289
end return;
@@ -6159,16 +6249,16 @@ package body LSP.Ada_Handlers is
61596249
(Self : access Message_Handler'Class;
61606250
Document : not null LSP.Ada_Documents.Document_Access)
61616251
is
6162-
Ok : Boolean;
6163-
Diag : LSP.Messages.PublishDiagnosticsParams;
6252+
Changed : Boolean;
6253+
Diag : LSP.Messages.PublishDiagnosticsParams;
61646254
begin
61656255
if Self.Diagnostics_Enabled then
61666256
Document.Get_Errors
6167-
(Self.Contexts.Get_Best_Context (Document.URI).all,
6168-
Ok,
6169-
Diag.diagnostics);
6257+
(Context => Self.Contexts.Get_Best_Context (Document.URI).all,
6258+
Changed => Changed,
6259+
Errors => Diag.diagnostics);
61706260

6171-
if Ok then
6261+
if Changed then
61726262
Diag.uri := Document.URI;
61736263
Self.Server.On_Publish_Diagnostics (Diag);
61746264
end if;

source/ada/lsp-ada_handlers.ads

+3
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ private
294294
Line_Folding_Only : Boolean := False;
295295
-- Client capabilities, folding only per lines
296296

297+
Supports_Related_Diagnostics : Boolean := False;
298+
-- Client capabilities to support diagnostics' relatedInformation.
299+
297300
Completion_Snippets_Enabled : Boolean := False;
298301
-- True if the client supports completion snippets
299302

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)