Skip to content

Commit 2692d3e

Browse files
Display diagnostics on the project file when possible
Instead of emitting them on the workspace's root directory. We now emit project diagnostics on the workspace's root directory only when we don't have any project file (e.g: when multiple project files are found in the same directory, without a suitable ada.projectFile setting). Adapt the tests when needed. For eng/ide/ada_language_server#1564
1 parent dfaa7e2 commit 2692d3e

File tree

22 files changed

+653
-648
lines changed

22 files changed

+653
-648
lines changed

source/ada/lsp-ada_handlers-project_diagnostics.adb

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,37 @@ package body LSP.Ada_Handlers.Project_Diagnostics is
3030

3131
overriding
3232
procedure Get_Diagnostics
33-
(Self : in out Diagnostic_Source;
34-
Diagnostics : out LSP.Structures.Diagnostic_Vector) is
33+
(Self : in out Diagnostic_Source;
34+
Diagnostics : out LSP.Structures.Diagnostic_Vector;
35+
Target_File : out GNATCOLL.VFS.Virtual_File)
36+
is
37+
use GNATCOLL.VFS;
3538
begin
3639
if Self.Handler.Configuration.Project_Diagnostics_Enabled then
37-
Self.Last_Status := Self.Handler.Project_Status;
40+
declare
41+
Project_File : constant Virtual_File :=
42+
Self.Handler.Project_Status.Get_Project_File;
43+
Root_Dir : constant Virtual_File :=
44+
Self.Handler.Client.Root_Directory;
45+
begin
46+
-- Set the target file according to the source's initial target
47+
-- (root directory or project file itself)
48+
Target_File :=
49+
(if Project_File.Is_Regular_File then Project_File
50+
else Root_Dir);
3851

39-
Tracer.Trace ("Project loading status: " & Self.Last_Status'Image);
52+
Self.Last_Status := Self.Handler.Project_Status;
4053

41-
-- If we have a valid project return immediately: we want to display
42-
-- diagnostics only if there is an issue to solve or a potential
43-
-- enhancement.
54+
Tracer.Trace ("Project loading status: " & Self.Last_Status'Image);
55+
56+
-- If we have a valid project return immediately: we want to display
57+
-- diagnostics only if there is an issue to solve or a potential
58+
-- enhancement.
59+
60+
Diagnostics.Append_Vector
61+
(LSP.Ada_Project_Loading.Get_Diagnostics (Self.Last_Status));
62+
end;
4463

45-
Diagnostics.Append_Vector
46-
(LSP.Ada_Project_Loading.Get_Diagnostics (Self.Last_Status));
4764
end if;
4865
end Get_Diagnostics;
4966

source/ada/lsp-ada_handlers-project_diagnostics.ads

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,25 @@
1515
-- of the license. --
1616
------------------------------------------------------------------------------
1717

18+
with GNATCOLL.VFS;
1819
with LSP.Ada_Project_Loading;
1920
with LSP.Diagnostic_Sources;
2021

2122
package LSP.Ada_Handlers.Project_Diagnostics is
2223

2324
Project_Diagnostics_Source_ID : constant VSS.Strings.Virtual_String :=
2425
"ada.project";
25-
2626
type Diagnostic_Source
2727
(Handler : not null access LSP.Ada_Handlers.Message_Handler'Class)
2828
is limited new LSP.Diagnostic_Sources.Workspace_Diagnostic_Source with private;
2929

30-
overriding
31-
procedure Get_Diagnostics
32-
(Self : in out Diagnostic_Source;
33-
Diagnostics : out LSP.Structures.Diagnostic_Vector);
30+
overriding procedure Get_Diagnostics
31+
(Self : in out Diagnostic_Source;
32+
Diagnostics : out LSP.Structures.Diagnostic_Vector;
33+
Target_File : out GNATCOLL.VFS.Virtual_File);
3434
-- Fill diagnostics for given document.
35+
-- Target_File is the file where diagnostics should be published
36+
-- (e.g: project file, workspace's root directory).
3537

3638
overriding function Has_New_Diagnostic
3739
(Self : in out Diagnostic_Source) return Boolean;

source/ada/lsp-ada_handlers.adb

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,6 @@ package body LSP.Ada_Handlers is
178178
(Self : in out Message_Handler; Context : LSP.Ada_Contexts.Context)
179179
return Libadalang.Analysis.Analysis_Unit_Array;
180180

181-
procedure Get_Workspace_Diagnostics
182-
(Self : in out Message_Handler'Class;
183-
Changed : out Boolean;
184-
Errors : out LSP.Structures.Diagnostic_Vector;
185-
Force : Boolean := False);
186-
-- Get all the worskpace-specific diagnostics, using the handler's workspace
187-
-- diagnostics' sources.
188-
-- When Force is True, any existing diagnostic will be retrieved, no matter
189-
-- if they have changed or not since the last query.
190-
191181
-----------------------------
192182
-- Allocate_Progress_Token --
193183
-----------------------------
@@ -391,27 +381,6 @@ package body LSP.Ada_Handlers is
391381
end if;
392382
end Get_Open_Document_Version;
393383

394-
-------------------------------
395-
-- Get_Workspace_Diagnostics --
396-
-------------------------------
397-
398-
procedure Get_Workspace_Diagnostics
399-
(Self : in out Message_Handler'Class;
400-
Changed : out Boolean;
401-
Errors : out LSP.Structures.Diagnostic_Vector;
402-
Force : Boolean := False) is
403-
begin
404-
Errors.Clear;
405-
Changed := (for some Source of Self.Diagnostic_Sources =>
406-
Source.Has_New_Diagnostic);
407-
408-
if Changed or else Force then
409-
for Source of Self.Diagnostic_Sources loop
410-
Source.Get_Diagnostics (Errors);
411-
end loop;
412-
end if;
413-
end Get_Workspace_Diagnostics;
414-
415384
----------------------------
416385
-- Imprecise_Resolve_Name --
417386
----------------------------
@@ -454,13 +423,12 @@ package body LSP.Ada_Handlers is
454423
(Self : in out Message_Handler;
455424
Incremental_Text_Changes : Boolean;
456425
CLI_Config_File : GNATCOLL.VFS.Virtual_File :=
457-
GNATCOLL.VFS.No_File)
458-
is
426+
GNATCOLL.VFS.No_File) is
459427
begin
460428
Self.Incremental_Text_Changes := Incremental_Text_Changes;
461429
Self.File_Monitor :=
462430
new LSP.Servers.FS_Watch.FS_Watch_Monitor (Self.Server);
463-
Self.Diagnostic_Sources :=
431+
Self.Workspace_Diagnostic_Sources :=
464432
[new LSP.Ada_Handlers.Project_Diagnostics.Diagnostic_Source
465433
(Self'Unchecked_Access)];
466434

@@ -3739,32 +3707,55 @@ package body LSP.Ada_Handlers is
37393707
-------------------------
37403708

37413709
procedure Publish_Diagnostics
3742-
(Self : in out Message_Handler;
3743-
Other_Diagnostics : LSP.Structures.Diagnostic_Vector :=
3744-
LSP.Structures.Empty;
3745-
Force : Boolean := False)
3710+
(Self : in out Message_Handler; Force : Boolean := False)
37463711
is
3747-
Diag : LSP.Structures.PublishDiagnosticsParams;
3748-
Changed : Boolean;
3712+
use GNATCOLL.VFS;
37493713

3714+
Diag : LSP.Structures.PublishDiagnosticsParams;
3715+
Changed : Boolean;
3716+
Target_File : Virtual_File;
37503717
begin
37513718
-- Retrieve all the workspace diagnostics and publish them.
37523719

37533720
if Self.Configuration.Diagnostics_Enabled then
3754-
Diag.diagnostics.Append_Vector (Other_Diagnostics);
3721+
Changed :=
3722+
(for some Source of Self.Workspace_Diagnostic_Sources
3723+
=> Source.Has_New_Diagnostic);
3724+
3725+
if Changed or else Force then
3726+
-- First clear any currently published workspace diagnostics
3727+
for File of Self.Workspace_Diagnostic_Files loop
3728+
Self.Sender.On_PublishDiagnostics_Notification
3729+
(LSP.Structures.PublishDiagnosticsParams'
3730+
(uri =>
3731+
(VSS.Strings.Conversions.To_Virtual_String
3732+
(URIs.Conversions.From_File (File.Display_Full_Name))
3733+
with null record),
3734+
others => <>));
3735+
end loop;
37553736

3756-
Self.Get_Workspace_Diagnostics
3757-
(Changed => Changed,
3758-
Errors => Diag.diagnostics,
3759-
Force => Force);
3737+
Self.Workspace_Diagnostic_Files.Clear;
37603738

3761-
if Changed or else not Other_Diagnostics.Is_Empty then
3762-
Diag.uri :=
3763-
(VSS.Strings.Conversions.To_Virtual_String
3764-
(URIs.Conversions.From_File
3765-
(Self.Client.Root_Directory.Display_Full_Name))
3766-
with null record);
3767-
Self.Sender.On_PublishDiagnostics_Notification (Diag);
3739+
-- Query all the workspace diagnostic sources, and publish
3740+
-- diagnostics on their target file, if any
3741+
for Source of Self.Workspace_Diagnostic_Sources loop
3742+
Source.Get_Diagnostics
3743+
(Diagnostics => Diag.diagnostics, Target_File => Target_File);
3744+
3745+
-- We have some diagnostics: publish them
3746+
if not Diag.diagnostics.Is_Empty then
3747+
Self.Workspace_Diagnostic_Files.Include (Target_File);
3748+
3749+
Diag.uri :=
3750+
(VSS.Strings.Conversions.To_Virtual_String
3751+
(URIs.Conversions.From_File
3752+
(Target_File.Display_Full_Name))
3753+
with null record);
3754+
Self.Sender.On_PublishDiagnostics_Notification (Diag);
3755+
3756+
Diag.diagnostics.Clear;
3757+
end if;
3758+
end loop;
37683759
end if;
37693760
end if;
37703761
end Publish_Diagnostics;

source/ada/lsp-ada_handlers.ads

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,15 @@ private
200200
-- is known to the server, this context should map to the implicit
201201
-- project.
202202

203-
Diagnostic_Sources : Workspace_Diagnostic_Source_Vectors.Vector;
203+
Workspace_Diagnostic_Sources :
204+
Workspace_Diagnostic_Source_Vectors.Vector;
204205
-- Workspace diagnostic sources.
205206

207+
Workspace_Diagnostic_Files : LSP.Ada_File_Sets.File_Sets.Set;
208+
-- Files for which workspace diagnostics have been published.
209+
-- Used to clear any existing workspace diagnostic before querying
210+
-- new ones.
211+
206212
Highlighter : aliased LSP.Ada_Highlighters.Ada_Highlighter;
207213
-- Semantic token highlighter for Ada
208214

@@ -400,15 +406,12 @@ private
400406
-- they have changed or not.
401407

402408
procedure Publish_Diagnostics
403-
(Self : in out Message_Handler;
404-
Other_Diagnostics : LSP.Structures.Diagnostic_Vector :=
405-
LSP.Structures.Empty;
406-
Force : Boolean := False);
409+
(Self : in out Message_Handler; Force : Boolean := False);
407410
-- Publish workspace diagnostic messages.
408-
-- Other_Diagnostics can be used to specify punctual diagnostics not coming
409-
-- from sources that analyze files when being opened or modified.
410411
-- When Force is True, the diagnostics will always be sent, regardless if
411412
-- they have changed or not.
413+
-- Currently published workspace diagnostics are always cleared
414+
-- before querying new workspace diagnostics.
412415

413416
overriding function To_File
414417
(Self : Message_Handler;

source/ada/lsp-diagnostic_sources.ads

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
with Ada.Containers.Vectors;
1919
with Ada.Unchecked_Deallocation;
20+
with GNATCOLL.VFS;
2021
with LSP.Structures;
2122
limited with LSP.Ada_Contexts;
2223

@@ -49,8 +50,10 @@ package LSP.Diagnostic_Sources is
4950
(Diagnostic_Source'Class, Diagnostic_Source_Access);
5051

5152
type Workspace_Diagnostic_Source is limited interface;
52-
-- Interface for worspace diagnostics (i.e: diagnostics that
53+
-- Interface for workspace diagnostics (i.e: diagnostics that
5354
-- are not specific to a given context/document).
55+
-- Currently published workspace diagnostics are always cleared
56+
-- before querying new workspace diagnostics.
5457

5558
type Workspace_Diagnostic_Source_Access is
5659
access LSP.Diagnostic_Sources.Workspace_Diagnostic_Source'Class;
@@ -61,8 +64,9 @@ package LSP.Diagnostic_Sources is
6164
LSP.Diagnostic_Sources."=");
6265

6366
procedure Get_Diagnostics
64-
(Self : in out Workspace_Diagnostic_Source;
65-
Diagnostics : out LSP.Structures.Diagnostic_Vector)
67+
(Self : in out Workspace_Diagnostic_Source;
68+
Diagnostics : out LSP.Structures.Diagnostic_Vector;
69+
Target_File : out GNATCOLL.VFS.Virtual_File)
6670
is abstract;
6771

6872
function Has_New_Diagnostic

testsuite/ada_lsp/project_config.missing_file/test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"jsonrpc": "2.0",
7171
"method": "textDocument/publishDiagnostics",
7272
"params": {
73-
"uri": "$URI{}",
73+
"uri": "$URI{prj1.gpr}",
7474
"diagnostics": [
7575
{
7676
"range": {

testsuite/ada_lsp/project_diags.errors_to_errors/test.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
{
5353
"method": "textDocument/publishDiagnostics",
5454
"params": {
55-
"uri": "$URI{}",
55+
"uri": "$URI{test.gpr}",
5656
"diagnostics": [
5757
{
5858
"range": {
@@ -125,7 +125,7 @@
125125
"jsonrpc": "2.0",
126126
"method": "textDocument/publishDiagnostics",
127127
"params": {
128-
"uri": "$URI{}",
128+
"uri": "$URI{not_visible_project/project_with_errors.gpr}",
129129
"diagnostics": [
130130
{
131131
"range": {

testsuite/ada_lsp/project_diags.errors_to_not_found/test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
{
5353
"method": "textDocument/publishDiagnostics",
5454
"params": {
55-
"uri": "$URI{}",
55+
"uri": "$URI{test.gpr}",
5656
"diagnostics": [
5757
{
5858
"range": {

0 commit comments

Comments
 (0)