Skip to content

Commit 7574267

Browse files
committed
Improved Get_Definition_At and Find_All_References
1 parent 4f8c2ff commit 7574267

File tree

4 files changed

+78
-64
lines changed

4 files changed

+78
-64
lines changed

source/ada/lsp-ada_cross_reference_services.adb

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,55 @@ package body LSP.Ada_Cross_Reference_Services is
2828
Include_Definition : Boolean := False) return Ref_Vector
2929
is
3030

31-
Context : constant Analysis_Context := Definition.Unit.Context;
32-
3331
References : Ref_Vector;
3432

3533
begin
3634

37-
for N in Sources'Range loop
35+
if not Definition.Is_Null then
3836

3937
declare
4038

41-
Unit : constant Analysis_Unit := Context.Get_From_File
42-
(Sources (N).Display_Full_Name);
39+
Context : constant Analysis_Context := Definition.Unit.Context;
4340

44-
Match_Iterator : Traverse_Iterator'Class := Find
45-
(Unit.Root,
46-
Kind_Is (Ada_Identifier) and Text_Is (Definition.Text));
41+
begin
4742

48-
Node : Ada_Node;
49-
Node_Definition : Defining_Name;
43+
for N in Sources'Range loop
5044

51-
begin
45+
declare
46+
47+
Unit : constant Analysis_Unit :=
48+
Context.Get_From_File
49+
(Sources (N).Display_Full_Name);
50+
51+
Match_Iterator : Traverse_Iterator'Class := Find
52+
(Unit.Root,
53+
Kind_Is (Ada_Identifier) and Text_Is (Definition.Text));
54+
55+
Node : Ada_Node;
56+
Node_Definition : Defining_Name;
57+
58+
begin
59+
60+
while (Match_Iterator.Next (Node)) loop
61+
62+
Node_Definition := Node.P_Xref;
5263

53-
while (Match_Iterator.Next (Node)) loop
64+
if Node_Definition = Definition and then
65+
(Include_Definition or else
66+
Node.As_Identifier /= Definition.F_Name)
67+
then
68+
References.Append (Node);
69+
end if;
5470

55-
Node_Definition := Node.P_Xref;
71+
end loop;
5672

57-
if Node_Definition = Definition and then
58-
(Include_Definition or else
59-
Node.As_Identifier /= Definition.F_Name)
60-
then
61-
References.Append (Node);
62-
end if;
73+
end;
6374

6475
end loop;
6576

6677
end;
6778

68-
end loop;
79+
end if;
6980

7081
return References;
7182

source/ada/lsp-ada_documents.adb

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -116,47 +116,39 @@ package body LSP.Ada_Documents is
116116
end loop;
117117
end Get_Symbols;
118118

119-
------------------------
120-
-- Get_Declaration_At --
121-
------------------------
119+
-----------------------
120+
-- Get_Definition_At --
121+
-----------------------
122122

123-
not overriding function Get_Declaration_At
123+
not overriding function Get_Definition_At
124124
(Self : Document;
125125
Position : LSP.Messages.Position)
126126
return Libadalang.Analysis.Defining_Name
127127
is
128128

129-
function Predicate (Node : Libadalang.Analysis.Ada_Node) return Boolean;
130-
131-
function Predicate
132-
(Node : Libadalang.Analysis.Ada_Node) return Boolean
133-
is
134-
135-
use Libadalang.Common;
136-
use LSP.Types;
129+
use Libadalang.Common;
130+
use Langkit_Support.Slocs;
137131

138-
Node_Start_Range :
139-
constant Langkit_Support.Slocs.Source_Location_Range :=
140-
Sloc_Range (Data (Node.Token_Start));
132+
Node : Libadalang.Analysis.Ada_Node := Self.Unit.Root.Lookup
133+
((Line => Line_Number (Position.line) + 1,
134+
Column => Column_Number (Position.character) + 1));
141135

142-
Node_End_Range :
143-
constant Langkit_Support.Slocs.Source_Location_Range :=
144-
Sloc_Range (Data (Node.Token_End));
136+
begin
145137

146-
Line : constant Line_Number := Position.line;
147-
Column : constant UTF_16_Index := Position.character;
138+
while not Node.Is_Null and
139+
Node.Kind /= Ada_Defining_Name and
140+
Node.Kind /= Ada_Compilation_Unit
141+
loop
142+
Node := Node.Parent;
143+
end loop;
148144

149-
begin
150-
return Node.Kind = Ada_Defining_Name and
151-
Line_Number (Node_Start_Range.Start_Line) - 1 = Line and
152-
UTF_16_Index (Node_Start_Range.Start_Column) - 1 <= Column and
153-
UTF_16_Index (Node_End_Range.End_Column) - 1 >= Column;
154-
end Predicate;
145+
if Node.Kind = Ada_Compilation_Unit then
146+
raise No_Defining_Name_At_Position;
147+
else
148+
return Node.As_Defining_Name;
149+
end if;
155150

156-
begin
157-
return Libadalang.Iterators.Find_First
158-
(Self.Unit.Root, Predicate'Access).As_Defining_Name;
159-
end Get_Declaration_At;
151+
end Get_Definition_At;
160152

161153
---------------------
162154
-- Get_Symbol_Kind --
@@ -302,7 +294,7 @@ package body LSP.Ada_Documents is
302294
(first =>
303295
(line => LSP.Types.Line_Number (Value.Start_Line) - 1,
304296
character => LSP.Types.UTF_16_Index -- FIXME (UTF16 index)!
305-
(Value.Start_Column) - 1),
297+
(Value.Start_Column) - 1),
306298
last =>
307299
(line => LSP.Types.Line_Number (Value.End_Line) - 1,
308300
character => LSP.Types.UTF_16_Index -- FIXME (UTF16 index)!

source/ada/lsp-ada_documents.ads

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ package LSP.Ada_Documents is
4141
(Self : Document;
4242
Result : out LSP.Messages.SymbolInformation_Vector);
4343

44-
not overriding function Get_Declaration_At
44+
No_Defining_Name_At_Position : exception;
45+
46+
not overriding function Get_Definition_At
4547
(Self : Document;
4648
Position : LSP.Messages.Position)
4749
return Libadalang.Analysis.Defining_Name;

source/ada/lsp-ada_handlers.adb

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,28 @@ package body LSP.Ada_Handlers is
113113
Response : in out LSP.Messages.Location_Response)
114114
is
115115

116-
Document : constant LSP.Ada_Documents.Document_Access :=
116+
Document : constant LSP.Ada_Documents.Document_Access :=
117117
Self.Context.Get_Document (Value.textDocument.uri);
118118

119-
References : constant LSP.Ada_Cross_Reference_Services.Ref_Vector :=
120-
LSP.Ada_Cross_Reference_Services.Find_All_References
121-
(Definition => Document.Get_Declaration_At (Value.position),
122-
Sources => Self.Context.Get_Source_Files,
123-
Include_Definition => Value.context.includeDeclaration);
124-
-- This call to `Find_All_References` will later be replaced by a call
125-
-- to a subprogram in Libadalang with the same functionality
119+
Definition : Libadalang.Analysis.Defining_Name;
120+
121+
References : LSP.Ada_Cross_Reference_Services.Ref_Vector;
126122

127123
begin
128124

125+
begin
126+
Definition := Document.Get_Definition_At (Value.position);
127+
exception
128+
when LSP.Ada_Documents.No_Defining_Name_At_Position => return;
129+
end;
130+
131+
References := LSP.Ada_Cross_Reference_Services.Find_All_References
132+
(Definition => Definition,
133+
Sources => Self.Context.Get_Source_Files,
134+
Include_Definition => Value.context.includeDeclaration);
135+
-- TODO: This call to `Find_All_References` should later be replaced by
136+
-- a call to a subprogram in Libadalang with the same functionality
137+
129138
for N in 1 .. Integer (References.Length) loop
130139

131140
declare
@@ -136,11 +145,11 @@ package body LSP.Ada_Handlers is
136145
use Libadalang.Common;
137146

138147
Start_Sloc_Range :
139-
constant Langkit_Support.Slocs.Source_Location_Range :=
140-
Sloc_Range (Data (Node.Token_Start));
148+
constant Langkit_Support.Slocs.Source_Location_Range :=
149+
Sloc_Range (Data (Node.Token_Start));
141150
End_Sloc_Range :
142-
constant Langkit_Support.Slocs.Source_Location_Range :=
143-
Sloc_Range (Data (Node.Token_End));
151+
constant Langkit_Support.Slocs.Source_Location_Range :=
152+
Sloc_Range (Data (Node.Token_End));
144153

145154
First_Position : constant LSP.Messages.Position :=
146155
(Line_Number (Start_Sloc_Range.Start_Line) - 1,

0 commit comments

Comments
 (0)