Skip to content

Commit 7408d2d

Browse files
Merge branch 'topic/indexing' into 'master'
Emit indexing progress when enqueuing the job See merge request eng/ide/ada_language_server!2152
2 parents 6e2a4b8 + 31b247b commit 7408d2d

File tree

1 file changed

+68
-47
lines changed

1 file changed

+68
-47
lines changed

source/ada/lsp-ada_indexing.adb

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,56 +27,66 @@ with VSS.Strings.Templates;
2727

2828
package body LSP.Ada_Indexing is
2929

30+
procedure Emit_Progress_Report
31+
(Self : in out Indexing_Job'Class;
32+
Files_Indexed : Natural;
33+
Total_Files : Natural;
34+
Client :
35+
in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class;
36+
Force : Boolean := False);
37+
-- Emit a progress report to the client as a percentage of files indexed.
38+
-- If Force is True, the progress report is sent regardless of the time
39+
-- elapsed since the last report.
40+
41+
--------------------------
42+
-- Emit_Progress_Report --
43+
--------------------------
44+
45+
procedure Emit_Progress_Report
46+
(Self : in out Indexing_Job'Class;
47+
Files_Indexed : Natural;
48+
Total_Files : Natural;
49+
Client :
50+
in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class;
51+
Force : Boolean := False)
52+
is
53+
use type Ada.Calendar.Time;
54+
55+
Current : constant Ada.Calendar.Time := Ada.Calendar.Clock;
56+
Message_Template : VSS.Strings.Templates.Virtual_String_Template :=
57+
"{}/{} files";
58+
59+
begin
60+
if not Force and then Current - Self.Progress_Report_Sent < 0.5 then
61+
-- Send only 2 notifications per second
62+
return;
63+
end if;
64+
65+
Self.Progress_Report_Sent := Current;
66+
67+
Client.On_ProgressReport_Work_Done
68+
(Self.Indexing_Token,
69+
(percentage => (True, (Files_Indexed * 100) / Total_Files),
70+
message =>
71+
Message_Template.Format
72+
(VSS.Strings.Formatters.Integers.Image (Files_Indexed),
73+
VSS.Strings.Formatters.Integers.Image (Total_Files)),
74+
others => <>));
75+
end Emit_Progress_Report;
76+
3077
-------------
3178
-- Execute --
3279
-------------
3380

34-
overriding procedure Execute
81+
overriding
82+
procedure Execute
3583
(Self : in out Indexing_Job;
3684
Client :
3785
in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class;
3886
Status : out LSP.Server_Jobs.Execution_Status)
3987
is
4088
use type LSP.Ada_Handlers.Project_Stamp;
41-
42-
procedure Emit_Progress_Report (Files_Indexed, Total_Files : Natural);
43-
44-
--------------------------
45-
-- Emit_Progress_Report --
46-
--------------------------
47-
48-
procedure Emit_Progress_Report (Files_Indexed, Total_Files : Natural) is
49-
use type Ada.Calendar.Time;
50-
51-
Current : constant Ada.Calendar.Time := Ada.Calendar.Clock;
52-
Message_Template : VSS.Strings.Templates.Virtual_String_Template :=
53-
"{}/{} files";
54-
55-
begin
56-
if Current - Self.Progress_Report_Sent < 0.5 then
57-
-- Send only 2 notifications per second
58-
return;
59-
end if;
60-
61-
Self.Progress_Report_Sent := Current;
62-
63-
Client.On_ProgressReport_Work_Done
64-
(Self.Indexing_Token,
65-
(percentage => (True, (Files_Indexed * 100) / Total_Files),
66-
message =>
67-
Message_Template.Format
68-
(VSS.Strings.Formatters.Integers.Image (Files_Indexed),
69-
VSS.Strings.Formatters.Integers.Image (Total_Files)),
70-
others => <>));
71-
end Emit_Progress_Report;
72-
7389
begin
74-
if Self.Report_Progress and then Self.Total_Files_Indexed = 0 then
75-
Client.On_ProgressBegin_Work_Done
76-
(Self.Indexing_Token,
77-
(title => "Indexing", percentage => (True, 0), others => <>));
78-
end if;
79-
8090
if Self.Handler.Get_Project_Stamp /= Self.Project_Stamp
8191
or Self.Handler.Is_Shutdown
8292
then
@@ -104,7 +114,10 @@ package body LSP.Ada_Indexing is
104114

105115
if Self.Report_Progress then
106116
Emit_Progress_Report
107-
(Self.Total_Files_Indexed, Self.Total_Files_To_Index);
117+
(Files_Indexed => Self.Total_Files_Indexed,
118+
Total_Files => Self.Total_Files_To_Index,
119+
Self => Self,
120+
Client => Client);
108121
end if;
109122

110123
exit;
@@ -156,7 +169,8 @@ package body LSP.Ada_Indexing is
156169
end if;
157170

158171
Status :=
159-
(if Self.Files_To_Index.Is_Empty then LSP.Server_Jobs.Done
172+
(if Self.Files_To_Index.Is_Empty
173+
then LSP.Server_Jobs.Done
160174
else LSP.Server_Jobs.Continue);
161175
end Execute;
162176

@@ -185,7 +199,7 @@ package body LSP.Ada_Indexing is
185199
Server.Allocate_Request_Id;
186200
Token : constant LSP.Structures.ProgressToken :=
187201
Handler.Allocate_Progress_Token ("indexing");
188-
Job : Indexing_Job_Access :=
202+
Job : Indexing_Job_Access :=
189203
new Indexing_Job
190204
(Handler => Handler, Report_Progress => Report_Progress);
191205
begin
@@ -195,16 +209,23 @@ package body LSP.Ada_Indexing is
195209
Job.Project_Stamp := Project_Stamp;
196210
Job.Index_Runtime := Index_Runtime;
197211

198-
-- FIXME: wait response before sending progress notifications.
199-
-- Currenctly, we just send a `window/workDoneProgress/create`
200-
-- request and immediately after this start sending notifications.
201-
-- We could do better, send request, wait for client response and
202-
-- start progress-report sending only after response.
212+
-- If progress reporting is enabled, send the initial progress
213+
-- notification to the client, and immediately emit the first
214+
-- progress report (0% done).
203215
if Report_Progress then
204216
Job.Indexing_Token := Token;
205217
Job.Progress_Report_Sent := Ada.Calendar.Clock;
206218

207219
Server.On_Progress_Create_Request (Id, (token => Token));
220+
Server.On_ProgressBegin_Work_Done
221+
(Job.Indexing_Token,
222+
(title => "Indexing", percentage => (True, 0), others => <>));
223+
Emit_Progress_Report
224+
(Self => Job.all,
225+
Files_Indexed => 0,
226+
Total_Files => Job.Total_Files_To_Index,
227+
Client => Server.all,
228+
Force => True);
208229
end if;
209230

210231
Server.Enqueue (LSP.Server_Jobs.Server_Job_Access (Job));

0 commit comments

Comments
 (0)