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