@@ -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 ;
0 commit comments