Skip to content

Commit 49956cf

Browse files
committed
Merge branch 'gs_389' into 'master'
Support global/localVariables in semanticTokenModifiers See merge request eng/ide/ada_language_server!2108
2 parents c318ca6 + d08bb13 commit 49956cf

File tree

11 files changed

+424
-5
lines changed

11 files changed

+424
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ section below it for the last release. -->
1616
* Provide an integration of the [e3-testsuite](https://github.com/AdaCore/e3-testsuite) framework with the VS Code testing UI
1717
* Provide an interactive options picker for GNATprove invocations
1818
* Automatically open SARIF reports generated by GNATprove
19+
* New SemanticTokenModifiers for global/local variables have been added.
1920

2021
## 26.0.202507021
2122

doc/Custom-colors-in-VS-Code.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Ada Language Server uses next modifiers of tokens:
5656
| modification | write access to a name
5757
| documentation | ghost code or aspect
5858
| defaultLibrary | predefined name
59+
| globalVariable | the variable exists during all time program execution
60+
| localVariable | the variable is local for the scope
5961

6062
## Example
6163

@@ -135,6 +137,8 @@ Open `settings.json` and append next:
135137
"*.modification": {"bold": true},
136138
"*.deprecated": {"strikethrough": true},
137139
"*.readonly": "#4FC1FF",
140+
"*.globalVariable": "#C92002",
141+
"*.localVariable": "#02A322",
138142
"property.readonly": "#3E52EB",
139143
"typeParameter.readonly": {"foreground": "#3E52EB", "italic": true},
140144
"*.documentation": "#6A9955",

doc/extensions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ related resources.
6969
* [Project file](project_file.md)
7070
* [Source dirs](source_dirs.md)
7171
* [Workspace symbol params](workspace_symbol_params.md)
72+
* [Global/local variables](global_local_variables.md)
7273

7374
```{toctree}
7475
:maxdepth: 1

doc/global_local_variables.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Global/Local Variables highlighting
2+
3+
## Short introduction
4+
5+
This feature uses custom modifiers for `SemanticTokenModifiers` to mark **global** and **local** variables in the `semanticTokens` request.
6+
7+
## Change description
8+
9+
We extend the `SemanticTokenModifiers` by adding extra modifiers:
10+
11+
* `globalVariable`: Marks a **global variable**. This refers to a variable whose **lifetime is the same as the program itself** (program-level visibility and duration). These variables are typically declared in the declarative part of a library-level package:
12+
* **Library-level packages**.
13+
* **Nested packages**, **Protected Objects** or **Tasks** that are themselves declared at the library level. .
14+
15+
The variable exists for the entire program execution and is not local to any locally declared subprogram or block.
16+
17+
* `localVariable`: Marks a **local variable**. This refers to a variable whose name is declared within the **nearest enclosing declarative part** (e.g., within a subprogram body, a block statement). Their scope and lifetime are typically restricted to that specific program element.

source/ada/lsp-ada_highlighters.adb

Lines changed: 80 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,70 @@ 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+
Parent : Ada_Node;
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+
Parent := Decl.Parent;
779+
while not Parent.Is_Null loop
780+
if Parent.Kind in Ada_Subp_Body_Range
781+
or else Parent.Kind in Ada_Entry_Body
782+
then
783+
return;
784+
end if;
785+
Parent := Parent.Parent;
786+
end loop;
787+
788+
Highlight_Token (Node.Token_Start, globalVariable);
789+
end if;
790+
end Investigate_Variable;
791+
792+
begin
793+
case Decl.Kind is
794+
when Ada_Generic_Formal_Obj_Decl =>
795+
Investigate_Variable;
796+
797+
when Ada_Entry_Index_Spec | Ada_Object_Decl |
798+
Ada_Single_Protected_Decl | Ada_Single_Task_Decl =>
799+
Investigate_Variable;
800+
801+
when Ada_For_Loop_Var_Decl |
802+
Ada_Extended_Return_Stmt_Object_Decl =>
803+
Highlight_Token (Node.Token_Start, localVariable);
804+
805+
when others =>
806+
null;
807+
end case;
808+
end Get_Variable_Modifiers;
809+
744810
Failsafe_Decl : Libadalang.Analysis.Refd_Decl;
745-
Def : Libadalang.Analysis.Defining_Name;
746-
Decl : Libadalang.Analysis.Basic_Decl;
747-
Kind : LSP.Enumerations.SemanticTokenTypes;
811+
Def : Libadalang.Analysis.Defining_Name;
812+
Decl : Libadalang.Analysis.Basic_Decl;
813+
Kind : LSP.Enumerations.SemanticTokenTypes;
748814
begin
749815
if Node.Kind not in Ada_Identifier | Ada_String_Literal then
750816
-- Highlight only identifiers and operator symbols
@@ -753,11 +819,10 @@ package body LSP.Ada_Highlighters is
753819

754820
if Node.P_Is_Defining then
755821
Def := Node.P_Enclosing_Defining_Name;
756-
757822
begin
758823
declare
759824
Is_Canonical : constant Boolean :=
760-
not Def.Is_Null and then Def.P_Canonical_Part = Def;
825+
not Def.Is_Null and then Def.P_Canonical_Part = Def;
761826
begin
762827
if Is_Canonical then
763828
Highlight_Token (Node.Token_Start, declaration);
@@ -795,6 +860,14 @@ package body LSP.Ada_Highlighters is
795860
Highlight_Token (Node.Token_Start, Kind);
796861
end if;
797862

863+
if Kind = variable
864+
-- not a declaration itself
865+
and then Laltools.Common.
866+
Find_First_Common_Parent (Decl, Node, True) /= Decl
867+
then
868+
Get_Variable_Modifiers (Decl);
869+
end if;
870+
798871
begin
799872
if Kind in variable | parameter | typeParameter | property
800873
and then Decl.P_Is_Constant_Object
@@ -961,6 +1034,8 @@ package body LSP.Ada_Highlighters is
9611034
Append_Modifier (modification, "modification");
9621035
Append_Modifier (documentation, "documentation");
9631036
Append_Modifier (defaultLibrary, "defaultLibrary");
1037+
Append_Modifier (globalVariable, "globalVariable");
1038+
Append_Modifier (localVariable, "localVariable");
9641039

9651040
Obsolescent := +"Obsolescent";
9661041
Ada_Package := +"Ada";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
with Ada.Text_IO;
3+
4+
package body A is
5+
6+
C : Boolean := True;
7+
8+
-----------
9+
-- Print --
10+
-----------
11+
12+
procedure Print is
13+
B : Boolean := True;
14+
begin
15+
Ada.Text_IO.Put_Line (B'Img & C'Img);
16+
end Print;
17+
18+
end A;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package A is
2+
3+
procedure Print;
4+
5+
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
with A;
3+
4+
procedure Main is
5+
begin
6+
A.Print;
7+
end Main;

0 commit comments

Comments
 (0)