Skip to content

Commit efdcba1

Browse files
committed
Support global/localVariables in semanticTokenModifiers
For eng/ide/gnatstudio#389
1 parent c318ca6 commit efdcba1

File tree

7 files changed

+856
-5
lines changed

7 files changed

+856
-5
lines changed

source/ada/lsp-ada_highlighters.adb

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
with GNATCOLL.Traces;
1919

20+
with Laltools;
21+
with Laltools.Common;
2022
with Langkit_Support.Slocs;
2123
with Langkit_Support.Text;
2224
with Libadalang.Common; use Libadalang.Common;
@@ -477,6 +479,7 @@ package body LSP.Ada_Highlighters is
477479
use all type LSP.Enumerations.SemanticTokenTypes;
478480
use all type LSP.Enumerations.SemanticTokenModifiers;
479481
use type Libadalang.Analysis.Defining_Name;
482+
use type Libadalang.Analysis.Ada_Node;
480483

481484
procedure Highlight_Token
482485
(Token : Libadalang.Common.Token_Reference;
@@ -497,6 +500,9 @@ package body LSP.Ada_Highlighters is
497500
function Is_Predefined (Decl : Libadalang.Analysis.Basic_Decl)
498501
return Boolean;
499502

503+
procedure Get_Variable_Modifiers (Decl : Libadalang.Analysis.Basic_Decl);
504+
-- Add variable's modifiers if Decl is a variable
505+
500506
------------------
501507
-- Has_Abstract --
502508
------------------
@@ -741,10 +747,75 @@ package body LSP.Ada_Highlighters is
741747
end case;
742748
end To_Kind;
743749

750+
----------------------------
751+
-- Get_Variable_Modifiers --
752+
----------------------------
753+
754+
procedure Get_Variable_Modifiers
755+
(Decl : Libadalang.Analysis.Basic_Decl)
756+
is
757+
--------------------------
758+
-- Investigate_Variable --
759+
--------------------------
760+
761+
procedure Investigate_Variable;
762+
procedure Investigate_Variable is
763+
use Libadalang.Analysis;
764+
use Langkit_Support.Slocs;
765+
766+
Node_Enclosing_Declarative_Part : constant Declarative_Part :=
767+
Laltools.Common.Get_Enclosing_Declarative_Part (Node);
768+
769+
Decl_Declarative_Part : Declarative_Part;
770+
begin
771+
if Compare
772+
(Node_Enclosing_Declarative_Part.Sloc_Range,
773+
Decl.Sloc_Range.Start_Sloc) = Inside
774+
then
775+
Highlight_Token (Node.Token_Start, localVariable);
776+
777+
else
778+
Decl_Declarative_Part :=
779+
Laltools.Common.Get_Enclosing_Declarative_Part (Decl);
780+
781+
if Decl_Declarative_Part.Parent /= No_Ada_Node
782+
and then
783+
(Decl_Declarative_Part.Parent.Kind in
784+
Ada_Package_Body_Range
785+
or else Decl_Declarative_Part.Parent.Kind in
786+
Ada_Base_Package_Decl)
787+
then
788+
Highlight_Token (Node.Token_Start, globalVariable);
789+
end if;
790+
end if;
791+
end Investigate_Variable;
792+
793+
begin
794+
case Decl.Kind is
795+
when Ada_Base_Formal_Param_Decl =>
796+
if Ada_Base_Formal_Param_Decl'(Decl.Kind) =
797+
Ada_Generic_Formal_Obj_Decl
798+
then
799+
Investigate_Variable;
800+
end if;
801+
802+
when Ada_Entry_Index_Spec | Ada_Object_Decl |
803+
Ada_Single_Protected_Decl | Ada_Single_Task_Decl =>
804+
Investigate_Variable;
805+
806+
when Ada_For_Loop_Var_Decl |
807+
Ada_Extended_Return_Stmt_Object_Decl =>
808+
Highlight_Token (Node.Token_Start, localVariable);
809+
810+
when others =>
811+
null;
812+
end case;
813+
end Get_Variable_Modifiers;
814+
744815
Failsafe_Decl : Libadalang.Analysis.Refd_Decl;
745-
Def : Libadalang.Analysis.Defining_Name;
746-
Decl : Libadalang.Analysis.Basic_Decl;
747-
Kind : LSP.Enumerations.SemanticTokenTypes;
816+
Def : Libadalang.Analysis.Defining_Name;
817+
Decl : Libadalang.Analysis.Basic_Decl;
818+
Kind : LSP.Enumerations.SemanticTokenTypes;
748819
begin
749820
if Node.Kind not in Ada_Identifier | Ada_String_Literal then
750821
-- Highlight only identifiers and operator symbols
@@ -753,11 +824,10 @@ package body LSP.Ada_Highlighters is
753824

754825
if Node.P_Is_Defining then
755826
Def := Node.P_Enclosing_Defining_Name;
756-
757827
begin
758828
declare
759829
Is_Canonical : constant Boolean :=
760-
not Def.Is_Null and then Def.P_Canonical_Part = Def;
830+
not Def.Is_Null and then Def.P_Canonical_Part = Def;
761831
begin
762832
if Is_Canonical then
763833
Highlight_Token (Node.Token_Start, declaration);
@@ -795,6 +865,14 @@ package body LSP.Ada_Highlighters is
795865
Highlight_Token (Node.Token_Start, Kind);
796866
end if;
797867

868+
if Kind = variable
869+
-- not a declaration itself
870+
and then Laltools.Common.
871+
Find_First_Common_Parent (Decl, Node, True) /= Decl
872+
then
873+
Get_Variable_Modifiers (Decl);
874+
end if;
875+
798876
begin
799877
if Kind in variable | parameter | typeParameter | property
800878
and then Decl.P_Is_Constant_Object
@@ -961,6 +1039,8 @@ package body LSP.Ada_Highlighters is
9611039
Append_Modifier (modification, "modification");
9621040
Append_Modifier (documentation, "documentation");
9631041
Append_Modifier (defaultLibrary, "defaultLibrary");
1042+
Append_Modifier (globalVariable, "globalVariable");
1043+
Append_Modifier (localVariable, "localVariable");
9641044

9651045
Obsolescent := +"Obsolescent";
9661046
Ada_Package := +"Ada";
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
with Ada.Text_IO;
3+
with Ada.Strings.Unbounded;
4+
5+
package body A is
6+
7+
C : Boolean := True;
8+
9+
type SemanticTokenModifiers is
10+
(declaration,
11+
definition,
12+
readonly,
13+
static,
14+
deprecated,
15+
an_abstract,
16+
async,
17+
modification,
18+
documentation,
19+
defaultLibrary,
20+
globalVariable,
21+
localVariable);
22+
23+
type Modifiers_Array is array (SemanticTokenModifiers) of Boolean;
24+
25+
-----------
26+
-- Print --
27+
-----------
28+
29+
procedure Print is
30+
B : Boolean := True;
31+
32+
function Correct_Name (Value : String) return String;
33+
34+
function Correct_Name (Value : String) return String is
35+
begin
36+
return Value;
37+
end Correct_Name;
38+
39+
function Get_Modifiers (Modifiers : Modifiers_Array) return String
40+
is
41+
use Ada.Strings.Unbounded;
42+
43+
M : Unbounded_String;
44+
begin
45+
for Index in SemanticTokenModifiers'Range loop
46+
if Modifiers (Index) then
47+
if Index = globalVariable
48+
or else Index = localVariable
49+
then
50+
M := "-" & Correct_Name
51+
(SemanticTokenModifiers'Image (Index)) & M;
52+
else
53+
Append (M, "-" & Correct_Name
54+
(SemanticTokenModifiers'Image (Index)));
55+
end if;
56+
end if;
57+
end loop;
58+
59+
return To_String (M);
60+
end Get_Modifiers;
61+
62+
begin
63+
Ada.Text_IO.Put_Line ("A");
64+
if B
65+
and then C
66+
and then D
67+
then
68+
null;
69+
end if;
70+
71+
Ada.Text_IO.Put_Line
72+
(Get_Modifiers ((deprecated => True, others => False)));
73+
end Print;
74+
75+
-----------
76+
-- Print --
77+
-----------
78+
79+
procedure Print (Value : String) is
80+
begin
81+
Ada.Text_IO.Put_Line (Value);
82+
end Print;
83+
84+
end A;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package A is
2+
3+
procedure Print;
4+
5+
procedure Print (Value : String);
6+
7+
type Print_Access is access procedure (Value : String);
8+
9+
private
10+
11+
D : Boolean := True;
12+
13+
end A;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
project Hello is
2+
3+
for Source_Dirs use (".");
4+
for Object_Dir use "obj";
5+
for Main use ("main.adb");
6+
7+
end Hello;
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
with A;
3+
4+
procedure Main is
5+
use A;
6+
begin
7+
A.Print;
8+
end Main;

0 commit comments

Comments
 (0)