Skip to content

Commit ad94ba8

Browse files
committed
Semantic tokens request for GPR files
Refs #1230
1 parent d2423be commit ad94ba8

File tree

8 files changed

+413
-16
lines changed

8 files changed

+413
-16
lines changed

source/gpr/lsp-gpr_files-references.adb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- Language Server Protocol --
33
-- --
4-
-- Copyright (C) 2024, AdaCore --
4+
-- Copyright (C) 2024-2025, AdaCore --
55
-- --
66
-- This is free software; you can redistribute it and/or modify it under --
77
-- terms of the GNU General Public License as published by the Free Soft- --
@@ -46,10 +46,6 @@ package body LSP.GPR_Files.References is
4646
File : LSP.GPR_Files.File_Access) return GPC.Token_Reference;
4747
-- go to next token if any or stay at last
4848

49-
function Token_Reference
50-
(Ref : Reference) return Gpr_Parser.Common.Token_Reference;
51-
-- Token_Reference adapter.
52-
5349
--------------------
5450
-- Previous_Token --
5551
--------------------

source/gpr/lsp-gpr_files-references.ads

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- Language Server Protocol --
33
-- --
4-
-- Copyright (C) 2024, AdaCore --
4+
-- Copyright (C) 2024-2025, AdaCore --
55
-- --
66
-- This is free software; you can redistribute it and/or modify it under --
77
-- terms of the GNU General Public License as published by the Free Soft- --
@@ -111,6 +111,10 @@ package LSP.GPR_Files.References is
111111
-- if Reference is a valid attribute reference returns all GPR2
112112
-- informations needed to access attribute value.
113113

114+
function Token_Reference
115+
(Ref : Reference) return Gpr_Parser.Common.Token_Reference;
116+
-- Token_Reference adapter.
117+
114118
private
115119

116120
package GPC renames Gpr_Parser.Common;

source/gpr/lsp-gpr_files.adb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- Language Server Protocol --
33
-- --
4-
-- Copyright (C) 2023-2024, AdaCore --
4+
-- Copyright (C) 2023-2025, AdaCore --
55
-- --
66
-- This is free software; you can redistribute it and/or modify it under --
77
-- terms of the GNU General Public License as published by the Free Soft- --
@@ -1373,6 +1373,25 @@ package body LSP.GPR_Files is
13731373

13741374
end Reset;
13751375

1376+
--------------------
1377+
-- Package_Ranges --
1378+
--------------------
1379+
1380+
function Package_Ranges (Self : File) return Package_Range_Array is
1381+
Result : Package_Range_Array (1 .. Natural (Self.Packages.Length));
1382+
Last : Natural := 0;
1383+
begin
1384+
for J in Self.Packages.Iterate loop
1385+
Last := Last + 1;
1386+
Result (Last) :=
1387+
(Package_Id => Package_Maps.Key (J),
1388+
First => Self.Packages (J).First,
1389+
Last => Self.Packages (J).Last);
1390+
end loop;
1391+
1392+
return Result;
1393+
end Package_Ranges;
1394+
13761395
-----------
13771396
-- Parse --
13781397
-----------

source/gpr/lsp-gpr_files.ads

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- Language Server Protocol --
33
-- --
4-
-- Copyright (C) 2023-2024, AdaCore --
4+
-- Copyright (C) 2023-2025, AdaCore --
55
-- --
66
-- This is free software; you can redistribute it and/or modify it under --
77
-- terms of the GNU General Public License as published by the Free Soft- --
@@ -142,6 +142,22 @@ package LSP.GPR_Files is
142142
-- return the Position's Package_Id. Returns Project_Level_Scope if
143143
-- Position not inside a package.
144144

145+
type Package_Range is record
146+
Package_Id : GPR2.Package_Id;
147+
First : Gpr_Parser.Common.Token_Reference :=
148+
Gpr_Parser.Common.No_Token;
149+
-- 'First' is at 'package' token
150+
151+
Last : Gpr_Parser.Common.Token_Reference :=
152+
Gpr_Parser.Common.No_Token;
153+
-- 'Last' is at the package's end or at the end of the renaming.
154+
end record;
155+
156+
type Package_Range_Array is array (Positive range <>) of Package_Range;
157+
158+
function Package_Ranges (Self : File) return Package_Range_Array;
159+
-- Return array of ranges for File's packages.
160+
145161
function Kind (Self : File) return GPR2.Project_Kind;
146162
-- return project kind as defined in project qualifier
147163

source/gpr/lsp-gpr_handlers.adb

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- Language Server Protocol --
33
-- --
4-
-- Copyright (C) 2022-2024, AdaCore --
4+
-- Copyright (C) 2022-2025, AdaCore --
55
-- --
66
-- This is free software; you can redistribute it and/or modify it under --
77
-- terms of the GNU General Public License as published by the Free Soft- --
@@ -23,14 +23,15 @@ with GPR2.Path_Name.Set;
2323

2424
with Langkit_Support.Slocs;
2525

26-
with LSP.GPR_Completions;
2726
with LSP.Constants;
2827
with LSP.Enumerations;
2928
with LSP.Generic_Cancel_Check;
29+
with LSP.GPR_Completions;
3030
with LSP.GPR_Documentation;
3131
with LSP.GPR_File_Readers;
3232
with LSP.GPR_Files.References;
3333
with LSP.GPR_Files.Symbols;
34+
with LSP.Structures.Unwrap;
3435
with LSP.Text_Documents.Langkit_Documents;
3536
with LSP.Utils;
3637

@@ -398,7 +399,8 @@ package body LSP.GPR_Handlers is
398399
Value : LSP.Structures.InitializeParams)
399400
is
400401
Response : LSP.Structures.InitializeResult;
401-
Capabilities : LSP.Structures.ServerCapabilities;
402+
Capabilities : LSP.Structures.ServerCapabilities renames
403+
Response.capabilities;
402404

403405
begin
404406
Self.File_Reader := LSP.GPR_File_Readers.Create (Self'Unchecked_Access);
@@ -414,9 +416,7 @@ package body LSP.GPR_Handlers is
414416
resolveProvider => LSP.Constants.True,
415417
others => <>));
416418

417-
Response.capabilities := Capabilities;
418-
419-
Response.capabilities.textDocumentSync :=
419+
Capabilities.textDocumentSync :=
420420
(Is_Set => True,
421421
Value => (Is_TextDocumentSyncOptions => True,
422422
TextDocumentSyncOptions =>
@@ -427,7 +427,7 @@ package body LSP.GPR_Handlers is
427427
(Is_Set => True, Value => (True, True)),
428428
others => <>)));
429429

430-
Response.capabilities.documentSymbolProvider :=
430+
Capabilities.documentSymbolProvider :=
431431
(Is_Set => True,
432432
Value =>
433433
(Is_Boolean => False,
@@ -442,6 +442,32 @@ package body LSP.GPR_Handlers is
442442
and then Value.capabilities.textDocument.Value.documentSymbol.Value
443443
.hierarchicalDocumentSymbolSupport.Value;
444444

445+
declare
446+
use LSP.Structures.Unwrap;
447+
448+
Types : LSP.Structures.Virtual_String_Vector :=
449+
tokenTypes
450+
(semanticTokens (Value.capabilities.textDocument));
451+
452+
Modifiers : LSP.Structures.Virtual_String_Vector :=
453+
tokenModifiers
454+
(semanticTokens (Value.capabilities.textDocument));
455+
begin
456+
Self.Highlighter.Initialize (Types, Modifiers);
457+
458+
Capabilities.semanticTokensProvider :=
459+
(Is_Set => True,
460+
Value =>
461+
(Is_SemanticTokensOptions => True,
462+
SemanticTokensOptions =>
463+
(full => LSP.Constants.True,
464+
a_range => <>,
465+
legend =>
466+
(tokenTypes => Types,
467+
tokenModifiers => Modifiers),
468+
others => <>)));
469+
end;
470+
445471
Self.Sender.On_Initialize_Response (Id, Response);
446472
end On_Initialize_Request;
447473

@@ -713,6 +739,27 @@ package body LSP.GPR_Handlers is
713739
end loop;
714740
end On_DidChangeConfiguration_Notification;
715741

742+
----------------------------
743+
-- On_Tokens_Full_Request --
744+
----------------------------
745+
746+
overriding procedure On_Tokens_Full_Request
747+
(Self : in out Message_Handler;
748+
Id : LSP.Structures.Integer_Or_Virtual_String;
749+
Value : LSP.Structures.SemanticTokensParams)
750+
is
751+
File : constant LSP.GPR_Files.File_Access :=
752+
LSP.GPR_Files.Parse
753+
(File_Provider => Self'Unchecked_Access,
754+
Path => Self.To_File (Value.textDocument.uri));
755+
756+
Response : LSP.Structures.SemanticTokens_Or_Null (Is_Null => False);
757+
begin
758+
Self.Highlighter.Get_Tokens (File, Response.Value.data);
759+
760+
Self.Sender.On_Tokens_Full_Response (Id, Response);
761+
end On_Tokens_Full_Request;
762+
716763
-------------------------
717764
-- Publish_Diagnostics --
718765
-------------------------

source/gpr/lsp-gpr_handlers.ads

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- Language Server Protocol --
33
-- --
4-
-- Copyright (C) 2022-2024, AdaCore --
4+
-- Copyright (C) 2022-2025, AdaCore --
55
-- --
66
-- This is free software; you can redistribute it and/or modify it under --
77
-- terms of the GNU General Public License as published by the Free Soft- --
@@ -30,6 +30,7 @@ with LSP.Ada_Configurations;
3030
with LSP.Client_Message_Receivers;
3131
with LSP.GPR_Documents;
3232
with LSP.GPR_Files;
33+
with LSP.GPR_Highlighters;
3334
with LSP.GPR_Job_Contexts;
3435
with LSP.Server_Message_Visitors;
3536
private with LSP.Server_Requests;
@@ -121,6 +122,9 @@ private
121122

122123
Configuration : LSP.Ada_Configurations.Configuration;
123124
-- Ada/GPR configuration settings
125+
126+
Highlighter : LSP.GPR_Highlighters.GPR_Highlighter;
127+
-- Semantic token highlighter
124128
end record;
125129

126130
overriding procedure On_Server_Notification
@@ -186,6 +190,11 @@ private
186190
(Self : in out Message_Handler;
187191
Value : LSP.Structures.DidChangeConfigurationParams);
188192

193+
overriding procedure On_Tokens_Full_Request
194+
(Self : in out Message_Handler;
195+
Id : LSP.Structures.Integer_Or_Virtual_String;
196+
Value : LSP.Structures.SemanticTokensParams);
197+
189198
-----------------------------------------
190199
-- LSP.GPR_Documents.Document_Provider --
191200
-----------------------------------------

0 commit comments

Comments
 (0)