Skip to content

Commit 9a3f1b5

Browse files
committed
Restore client_side_file_monitors and fs_watch
Implement `DidChangeWatchedFiles` notification. Refs #1170
1 parent b4dbb4c commit 9a3f1b5

File tree

22 files changed

+370
-256
lines changed

22 files changed

+370
-256
lines changed

gnat/ignore_in_317.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
lsp-ada_handlers-alire.adb
22
lsp-ada_handlers-alire.ads
3-
lsp-client_side_file_monitors.adb
4-
lsp-client_side_file_monitors.ads
53
lsp-common.adb
64
lsp-common.ads
75
lsp-error_decorators.adb
@@ -10,14 +8,11 @@ lsp-fuzz_decorators.adb
108
lsp-fuzz_decorators.ads
119
lsp-lal_utils.adb
1210
lsp-lal_utils.ads
13-
lsp-file_monitors.ads
1411
lsp-message_loggers.adb
1512
lsp-message_loggers.ads
1613
lsp-server_backends.ads
1714
lsp-servers-decode_notification.adb
1815
lsp-servers-decode_notification.ads
1916
lsp-servers-decode_request.adb
2017
lsp-servers-decode_request.ads
21-
lsp-servers-fs_watch.adb
22-
lsp-servers-fs_watch.ads
2318
lsp-servers-handle_request.ads

source/ada/lsp-ada_client_capabilities.adb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ package body LSP.Ada_Client_Capabilities is
6363
else False);
6464
end Code_ActionLiteralSupport;
6565

66+
-----------------------------------------------
67+
-- didChangeWatchedFiles_dynamicRegistration --
68+
-----------------------------------------------
69+
70+
function didChangeWatchedFiles_dynamicRegistration
71+
(Self : Client_Capability'Class) return Boolean
72+
is
73+
use LSP.Structures.Unwrap;
74+
75+
Result : constant LSP.Structures.Boolean_Optional :=
76+
dynamicRegistration
77+
(didChangeWatchedFiles
78+
(Self.Value.capabilities.workspace));
79+
begin
80+
return (if Result.Is_Set then Result.Value else False);
81+
end didChangeWatchedFiles_dynamicRegistration;
82+
6683
-------------------------
6784
-- Hierarchical_Symbol --
6885
-------------------------

source/ada/lsp-ada_client_capabilities.ads

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ package LSP.Ada_Client_Capabilities is
7575
(Self : Client_Capability'Class) return Boolean;
7676
-- Returns textDocument.documentSymbol.hierarchicalDocumentSymbolSupport
7777

78+
function didChangeWatchedFiles_dynamicRegistration
79+
(Self : Client_Capability'Class) return Boolean;
7880
-- Resource 0perations --
7981

8082
function Resource_Create_Supported

source/ada/lsp-ada_handlers-project_loading.adb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -501,23 +501,19 @@ package body LSP.Ada_Handlers.Project_Loading is
501501

502502
-- Reindex all open documents immediately after project reload, so
503503
-- that navigation from editors is accurate.
504-
-- for Document of Self.Open_Documents loop
505-
-- for Context of Self.Contexts_For_URI (Document.URI) loop
506-
-- Context.Index_Document (Document.all);
507-
-- end loop;
508-
--
509-
-- Self.Publish_Diagnostics (Document_Access (Document));
510-
-- end loop;
504+
for Document of Self.Open_Documents loop
505+
for Context of Self.Contexts_For_URI (Document.URI) loop
506+
Context.Index_Document (Document.all);
507+
end loop;
511508

512-
-- if not Self.File_Monitor.Assigned then
513-
-- Self.File_Monitor :=
514-
-- new LSP.Servers.FS_Watch.FS_Watch_Monitor (Self.Server);
515-
-- end if;
509+
Self.Publish_Diagnostics
510+
(LSP.Ada_Documents.Document_Access (Document));
511+
end loop;
516512

517513
-- We have successfully loaded a real project: monitor the filesystem
518514
-- for any changes on the sources of the project
519-
-- Self.File_Monitor.Monitor_Directories
520-
-- (Self.Contexts.All_Source_Directories);
515+
Self.File_Monitor.Monitor_Directories
516+
(Self.Contexts.All_Source_Directories);
521517

522518
-- Reindex the files from disk in the background after a project reload
523519
Mark_Source_Files_For_Indexing (Self);

source/ada/lsp-ada_handlers.adb

Lines changed: 142 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ with LSP.Ada_Handlers.Refactor.Suppress_Seperate;
8585
with LSP.Ada_Handlers.Renaming;
8686
with LSP.Ada_Handlers.Symbols;
8787
with LSP.Ada_Commands;
88+
with LSP.Client_Side_File_Monitors;
8889
with LSP.Constants;
8990
with LSP.Diagnostic_Sources;
9091
with LSP.Enumerations;
@@ -95,6 +96,7 @@ with LSP.GNATCOLL_Tracers.Handle;
9596
with LSP.Search;
9697
with LSP.Server_Notifications.DidChange;
9798
with LSP.Servers;
99+
with LSP.Servers.FS_Watch;
98100
with LSP.Structures.LSPAny_Vectors;
99101
with LSP.Utils;
100102

@@ -110,15 +112,6 @@ package body LSP.Ada_Handlers is
110112
function Is_Child return AlsReferenceKind_Array is
111113
([LSP.Enumerations.child => True, others => False]);
112114

113-
function Contexts_For_URI
114-
(Self : access Message_Handler;
115-
URI : LSP.Structures.DocumentUri)
116-
return LSP.Ada_Context_Sets.Context_Lists.List;
117-
-- Return a list of contexts that are suitable for the given File/URI:
118-
-- a list of all contexts where the file is known to be part of the
119-
-- project tree, or is a runtime file for this project. If the file
120-
-- is not known to any project, return an empty list.
121-
122115
procedure Clean_Diagnostics
123116
(Self : in out Message_Handler'Class;
124117
Document : not null LSP.Ada_Documents.Document_Access);
@@ -392,7 +385,7 @@ package body LSP.Ada_Handlers is
392385
----------------
393386

394387
procedure Initialize
395-
(Self : in out Message_Handler'Class;
388+
(Self : access Message_Handler'Class;
396389
Incremental_Text_Changes : Boolean;
397390
Config_File : VSS.Strings.Virtual_String)
398391
is
@@ -416,11 +409,13 @@ package body LSP.Ada_Handlers is
416409

417410
begin
418411
Self.Incremental_Text_Changes := Incremental_Text_Changes;
412+
Self.File_Monitor :=
413+
new LSP.Servers.FS_Watch.FS_Watch_Monitor (Self.Server);
419414

420415
if not Config_File.Is_Empty then
421416
Self.Configuration.Read_File (Config_File);
422417
Self.Client.Set_Root_If_Empty (Directory (Config_File));
423-
LSP.Ada_Handlers.Project_Loading.Reload_Project (Self);
418+
LSP.Ada_Handlers.Project_Loading.Reload_Project (Self.all);
424419
end if;
425420
end Initialize;
426421

@@ -2238,6 +2233,130 @@ package body LSP.Ada_Handlers is
22382233
end if;
22392234
end On_DidChangeConfiguration_Notification;
22402235

2236+
-------------------------------------------
2237+
-- On_DidChangeWatchedFiles_Notification --
2238+
-------------------------------------------
2239+
2240+
overriding procedure On_DidChangeWatchedFiles_Notification
2241+
(Self : in out Message_Handler;
2242+
Value : LSP.Structures.DidChangeWatchedFilesParams)
2243+
is
2244+
use type LSP.Ada_Documents.Document_Access;
2245+
2246+
URI : LSP.Structures.DocumentUri;
2247+
File : GNATCOLL.VFS.Virtual_File;
2248+
2249+
procedure Process_Created_File;
2250+
-- Processes a created file
2251+
2252+
procedure Process_Deleted_File;
2253+
-- Processes a deleted file
2254+
2255+
procedure Process_Changed_File;
2256+
-- Processes a changed file
2257+
2258+
--------------------------
2259+
-- Process_Changed_File --
2260+
--------------------------
2261+
2262+
procedure Process_Changed_File is
2263+
begin
2264+
if Self.Get_Open_Document (URI) = null then
2265+
-- If there is no document, reindex the file for each
2266+
-- context where it is relevant.
2267+
File := Self.To_File (URI);
2268+
2269+
for C of Self.Contexts_For_File (File) loop
2270+
C.Index_File (File);
2271+
end loop;
2272+
end if;
2273+
end Process_Changed_File;
2274+
2275+
--------------------------
2276+
-- Process_Created_File --
2277+
--------------------------
2278+
2279+
procedure Process_Created_File
2280+
is
2281+
use VSS.Strings.Conversions;
2282+
2283+
Contexts : constant LSP.Ada_Context_Sets.Context_Lists.List :=
2284+
Self.Contexts_For_File (File);
2285+
2286+
function Has_Dir
2287+
(Context : LSP.Ada_Contexts.Context)
2288+
return Boolean
2289+
is (Context.List_Source_Directories.Contains (File.Dir));
2290+
-- Return True if File is in a source directory of the project held
2291+
-- by Context.
2292+
2293+
begin
2294+
-- If the file was created by the client, then the DidCreateFiles
2295+
-- notification might have been received from it. In that case,
2296+
-- Contexts wont be empty, and all we need to do is check if
2297+
-- there's an open document. If there is, it takes precedence over
2298+
-- the filesystem.
2299+
-- If Contexts is empty, then we need to check if is a new source
2300+
-- that needs to be added. For instance, a source that was moved
2301+
-- to the the project source directories.
2302+
2303+
if Contexts.Is_Empty then
2304+
for Context of Self.Contexts.Each_Context
2305+
(Has_Dir'Unrestricted_Access)
2306+
loop
2307+
Context.Include_File (File);
2308+
Context.Index_File (File);
2309+
2310+
Self.Tracer.Trace
2311+
("Included " & File.Display_Base_Name
2312+
& " in context " & To_UTF_8_String (Context.Id));
2313+
end loop;
2314+
2315+
else
2316+
if Self.Get_Open_Document (URI) = null then
2317+
for Context of Contexts loop
2318+
Context.Index_File (File);
2319+
end loop;
2320+
end if;
2321+
end if;
2322+
end Process_Created_File;
2323+
2324+
---------------------------
2325+
-- Process_Deleted_Files --
2326+
---------------------------
2327+
2328+
procedure Process_Deleted_File is
2329+
begin
2330+
if Self.Get_Open_Document (URI) = null then
2331+
-- If there is no document, remove from the sources list
2332+
-- and reindex the file for each context where it is
2333+
-- relevant.
2334+
File := Self.To_File (URI);
2335+
2336+
for C of Self.Contexts_For_File (File) loop
2337+
C.Exclude_File (File);
2338+
C.Index_File (File);
2339+
end loop;
2340+
end if;
2341+
end Process_Deleted_File;
2342+
2343+
begin
2344+
-- Look through each change, filtering non Ada source files
2345+
for Change of Value.changes loop
2346+
URI := Change.uri;
2347+
File := Self.To_File (URI);
2348+
2349+
case Change.a_type is
2350+
when LSP.Enumerations.Created =>
2351+
Process_Created_File;
2352+
when LSP.Enumerations.Deleted =>
2353+
Process_Deleted_File;
2354+
when LSP.Enumerations.Changed =>
2355+
Process_Changed_File;
2356+
end case;
2357+
end loop;
2358+
end On_DidChangeWatchedFiles_Notification;
2359+
22412360
-----------------------------------------------
22422361
-- On_DidChangeWorkspaceFolders_Notification --
22432362
-----------------------------------------------
@@ -3253,6 +3372,10 @@ package body LSP.Ada_Handlers is
32533372
Id : LSP.Structures.Integer_Or_Virtual_String;
32543373
Value : LSP.Structures.InitializeParams)
32553374
is
3375+
procedure Free is new Ada.Unchecked_Deallocation
3376+
(LSP.File_Monitors.File_Monitor'Class,
3377+
LSP.File_Monitors.File_Monitor_Access);
3378+
32563379
Response : LSP.Structures.InitializeResult;
32573380
Token_Types : LSP.Structures.Virtual_String_Vector;
32583381
Token_Motifiers : LSP.Structures.Virtual_String_Vector;
@@ -3268,6 +3391,14 @@ package body LSP.Ada_Handlers is
32683391
Token_Types,
32693392
Token_Motifiers);
32703393

3394+
if Self.Client.didChangeWatchedFiles_dynamicRegistration then
3395+
Free (Self.File_Monitor);
3396+
3397+
Self.File_Monitor :=
3398+
new LSP.Client_Side_File_Monitors.File_Monitor
3399+
(Self'Unchecked_Access);
3400+
end if;
3401+
32713402
Self.Sender.On_Initialize_Response (Id, Response);
32723403
end On_Initialize_Request;
32733404

source/ada/lsp-ada_handlers.ads

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ with LSP.Ada_Documents;
3434
with LSP.Ada_File_Sets;
3535
with LSP.Ada_Highlighters;
3636
with LSP.Client_Message_Receivers;
37+
with LSP.File_Monitors;
3738
with LSP.Server_Message_Visitors;
3839
with LSP.Server_Notification_Receivers;
3940
with LSP.Server_Notifications;
@@ -62,9 +63,9 @@ package LSP.Ada_Handlers is
6263
with private;
6364

6465
procedure Initialize
65-
(Self : in out Message_Handler'Class;
66+
(Self : access Message_Handler'Class;
6667
Incremental_Text_Changes : Boolean;
67-
Config_File : VSS.Strings.Virtual_String);
68+
Config_File : VSS.Strings.Virtual_String);
6869
-- Initialize the message handler and configure it.
6970
--
7071
-- Incremental_Text_Changes - activate the support for incremental text
@@ -218,6 +219,9 @@ private
218219
Token_Id : Integer := 0;
219220
-- An ever-increasing number used to generate unique progress tokens
220221

222+
File_Monitor : LSP.File_Monitors.File_Monitor_Access;
223+
-- Filesystem monitoring
224+
221225
----------------------
222226
-- Project handling --
223227
----------------------
@@ -353,6 +357,10 @@ private
353357
(Self : in out Message_Handler;
354358
Value : LSP.Structures.DidChangeConfigurationParams);
355359

360+
overriding procedure On_DidChangeWatchedFiles_Notification
361+
(Self : in out Message_Handler;
362+
Value : LSP.Structures.DidChangeWatchedFilesParams);
363+
356364
overriding procedure On_DidOpen_Notification
357365
(Self : in out Message_Handler;
358366
Value : LSP.Structures.DidOpenTextDocumentParams);
@@ -462,4 +470,13 @@ private
462470
-- Converts a Refactoring_Edits into a WorkspaceEdit. The Rename flag
463471
-- controls if files that are supposed to be deleted, are renamed instead.
464472

473+
function Contexts_For_URI
474+
(Self : access Message_Handler;
475+
URI : LSP.Structures.DocumentUri)
476+
return LSP.Ada_Context_Sets.Context_Lists.List;
477+
-- Return a list of contexts that are suitable for the given File/URI:
478+
-- a list of all contexts where the file is known to be part of the
479+
-- project tree, or is a runtime file for this project. If the file
480+
-- is not known to any project, return an empty list.
481+
465482
end LSP.Ada_Handlers;

0 commit comments

Comments
 (0)