Skip to content

Commit 077b33b

Browse files
committed
Minor code refactoring for definition request
* Always use `Find_Next_Part_For_Decl` because `Find_Next_Part` doesn't work for some constructs * Extract accept statement search and overridings search to subprograms to make code simpler
1 parent a830c15 commit 077b33b

File tree

1 file changed

+113
-103
lines changed

1 file changed

+113
-103
lines changed

source/ada/lsp-ada_definition.adb

Lines changed: 113 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ package body LSP.Ada_Definition is
9999
is
100100
use all type LSP.Enumerations.AlsDisplayMethodAncestryOnNavigationPolicy;
101101

102+
procedure Append_Accept_Statements (Decl : Libadalang.Analysis.Basic_Decl);
103+
-- Search for accept statements if we are on an entry
104+
105+
procedure Append_Overrides (Decl : Libadalang.Analysis.Basic_Decl);
106+
-- Append overloaded subprograms for given declaration
107+
102108
Message : LSP.Server_Requests.Definition.Request
103109
renames LSP.Server_Requests.Definition.Request (Self.Message.all);
104110

@@ -119,11 +125,80 @@ package body LSP.Ada_Definition is
119125
Name_Node : Libadalang.Analysis.Name;
120126
Definition : Libadalang.Analysis.Defining_Name;
121127
Other_Part : Libadalang.Analysis.Defining_Name;
122-
Manual_Fallback : Libadalang.Analysis.Defining_Name;
123-
Definition_Node : Libadalang.Analysis.Basic_Decl;
128+
Declaration : Libadalang.Analysis.Basic_Decl;
124129
Decl_For_Find_Overrides : Libadalang.Analysis.Basic_Decl;
125130

126131
Ignore : Boolean;
132+
133+
------------------------------
134+
-- Append_Accept_Statements --
135+
------------------------------
136+
137+
procedure Append_Accept_Statements
138+
(Decl : Libadalang.Analysis.Basic_Decl) is
139+
begin
140+
-- Search for accept statements only if we are on an entry
141+
if not Decl.Is_Null
142+
and then Decl.Kind in Libadalang.Common.Ada_Entry_Decl_Range
143+
then
144+
declare
145+
Entry_Decl_Node : constant Libadalang.Analysis.Entry_Decl :=
146+
Decl.As_Entry_Decl;
147+
Entry_Parent_Node : constant Libadalang.Analysis.Basic_Decl :=
148+
Entry_Decl_Node.P_Parent_Basic_Decl;
149+
begin
150+
-- P_Accept_Stmts is only valid for entries declared in tasks
151+
if Entry_Parent_Node.Kind in
152+
Libadalang.Common.Ada_Task_Type_Decl_Range
153+
then
154+
for Accept_Node of Entry_Decl_Node.P_Accept_Stmts loop
155+
Self.Parent.Context.Append_Location
156+
(Self.Response,
157+
Self.Filter,
158+
Accept_Node.F_Body_Decl.F_Name);
159+
end loop;
160+
end if;
161+
end;
162+
end if;
163+
end Append_Accept_Statements;
164+
165+
----------------------
166+
-- Append_Overrides --
167+
----------------------
168+
169+
procedure Append_Overrides (Decl : Libadalang.Analysis.Basic_Decl) is
170+
begin
171+
if not Decl.Is_Null then
172+
declare
173+
Overridings : constant Libadalang.Analysis.Basic_Decl_Array :=
174+
Context.Find_All_Overrides
175+
(Decl,
176+
Imprecise_Results => Ignore);
177+
178+
Bases : constant Libadalang.Analysis.Basic_Decl_Array :=
179+
Context.Find_All_Base_Declarations
180+
(Decl,
181+
Imprecise_Results => Ignore);
182+
begin
183+
for Subp of Bases loop
184+
Self.Parent.Context.Append_Location
185+
(Self.Response,
186+
Self.Filter,
187+
Subp.P_Defining_Name,
188+
Is_Parent);
189+
end loop;
190+
191+
for Subp of Overridings loop
192+
Self.Parent.Context.Append_Location
193+
(Self.Response,
194+
Self.Filter,
195+
Subp.P_Defining_Name,
196+
Is_Child);
197+
end loop;
198+
end;
199+
end if;
200+
end Append_Overrides;
201+
127202
begin
128203
if Self.Contexts.Is_Empty then
129204
-- No more contexts to process, sort and return collected results
@@ -151,10 +226,16 @@ package body LSP.Ada_Definition is
151226
return;
152227
end if;
153228

229+
-- We distinguish two cases here. When we navigate from the usage_name,
230+
-- we simply go to the defining_name. When we are already at the
231+
-- defining_name, we try to go to completion and add additional
232+
-- destinations, such as overrides, accept_statements, etc.
233+
154234
-- Check if we are on some defining name
155235
Definition := Laltools.Common.Get_Name_As_Defining (Name_Node);
156236

157237
if Definition.Is_Null then
238+
-- If we are on a usage_name, go to defining_name
158239
Definition := Self.Parent.Context.Imprecise_Resolve_Name (Name_Node);
159240

160241
if not Definition.Is_Null then
@@ -167,72 +248,22 @@ package body LSP.Ada_Definition is
167248
Decl_For_Find_Overrides := Definition.P_Basic_Decl;
168249
end if;
169250
end if;
170-
else -- If we are on a defining_name already
171-
Other_Part := Laltools.Common.Find_Next_Part (Definition, Trace);
172-
173-
Definition_Node := Definition.P_Basic_Decl;
174-
175-
-- Search for overriding subprograms only if we are on an
176-
-- abstract subprogram.
177-
if Display_Method_Policy /= Never
178-
and then
179-
(Display_Method_Policy /= Usage_And_Abstract_Only
180-
or else Definition_Node.Kind in
181-
Libadalang.Common.Ada_Abstract_Subp_Decl_Range)
182-
then
183-
Decl_For_Find_Overrides := Definition_Node;
184-
end if;
185-
186-
-- Search for accept statements only if we are on an entry
187-
if Definition_Node.Kind in Libadalang.Common.Ada_Entry_Decl_Range then
188-
declare
189-
Entry_Decl_Node : constant Libadalang.Analysis.Entry_Decl :=
190-
Definition_Node.As_Entry_Decl;
191-
Entry_Parent_Node : constant Libadalang.Analysis.Basic_Decl :=
192-
Entry_Decl_Node.P_Parent_Basic_Decl;
193-
begin
194-
-- P_Accept_Stmts is only valid for entries declared in tasks
195-
if Entry_Parent_Node.Kind in
196-
Libadalang.Common.Ada_Task_Type_Decl_Range
197-
then
198-
for Accept_Node of Entry_Decl_Node.P_Accept_Stmts loop
199-
Self.Parent.Context.Append_Location
200-
(Self.Response,
201-
Self.Filter,
202-
Accept_Node.F_Body_Decl.F_Name);
203-
end loop;
251+
else -- If we are on a defining_name already, find other_part
204252

205-
-- Others entries are are handled as simple subprograms
206-
else
207-
declare
208-
Other_Part_For_Decl : constant
209-
Libadalang.Analysis.Basic_Decl :=
210-
Laltools.Common.Find_Next_Part_For_Decl
211-
(Definition_Node, Trace);
212-
begin
213-
if not Other_Part_For_Decl.Is_Null then
214-
Other_Part := Other_Part_For_Decl.P_Defining_Name;
215-
end if;
216-
end;
217-
end if;
218-
end;
253+
Declaration := Definition.P_Basic_Decl;
219254

220-
elsif Definition_Node.Kind in
221-
Libadalang.Common.Ada_Single_Task_Type_Decl_Range |
222-
Libadalang.Common.Ada_Protected_Type_Decl_Range
223-
then
224-
-- These node types are not handled by Find_Next_Part
225-
-- (LAL design limitations)
226-
declare
227-
Other_Part_For_Decl : constant Libadalang.Analysis.Basic_Decl :=
228-
Laltools.Common.Find_Next_Part_For_Decl
229-
(Definition_Node, Trace);
230-
begin
231-
if not Other_Part_For_Decl.Is_Null then
232-
Other_Part := Other_Part_For_Decl.P_Defining_Name;
233-
end if;
234-
end;
235-
end if;
255+
-- Some node types are not handled by Find_Next_Part
256+
-- (LAL design limitations), so we use Find_Next_Part_For_Decl
257+
-- instead.
258+
declare
259+
Other_Part_For_Decl : constant Libadalang.Analysis.Basic_Decl :=
260+
Laltools.Common.Find_Next_Part_For_Decl
261+
(Declaration, Trace);
262+
begin
263+
if not Other_Part_For_Decl.Is_Null then
264+
Other_Part := Other_Part_For_Decl.P_Defining_Name;
265+
end if;
266+
end;
236267

237268
if Other_Part.Is_Null then
238269
-- No next part is found. Check first defining name
@@ -245,53 +276,32 @@ package body LSP.Ada_Definition is
245276
-- an answer using Find_Next_Part / Find_Canonical_Part.
246277
-- Use the manual fallback to attempt to find a good enough
247278
-- result.
248-
Manual_Fallback := Laltools.Common.Find_Other_Part_Fallback
279+
Other_Part := Laltools.Common.Find_Other_Part_Fallback
249280
(Definition, Trace);
281+
end if;
250282

251-
-- If we have found a result using the imprecise heuristics then
252-
-- append it.
253-
Self.Parent.Context.Append_Location
254-
(Self.Response,
255-
Self.Filter,
256-
Manual_Fallback);
257-
else
283+
if not Other_Part.Is_Null then
258284
Self.Parent.Context.Append_Location
259285
(Self.Response,
260286
Self.Filter,
261287
Other_Part);
288+
end if;
289+
290+
Append_Accept_Statements (Declaration);
262291

292+
-- Search for overriding subprograms only if we are on an
293+
-- abstract subprogram.
294+
if Display_Method_Policy /= Never
295+
and then
296+
(Display_Method_Policy /= Usage_And_Abstract_Only
297+
or else Declaration.Kind in
298+
Libadalang.Common.Ada_Abstract_Subp_Decl_Range)
299+
then
300+
Decl_For_Find_Overrides := Declaration;
263301
end if;
264302
end if;
265303

266-
if not Decl_For_Find_Overrides.Is_Null then
267-
declare
268-
Overridings : constant Libadalang.Analysis.Basic_Decl_Array :=
269-
Context.Find_All_Overrides
270-
(Decl_For_Find_Overrides,
271-
Imprecise_Results => Ignore);
272-
273-
Bases : constant Libadalang.Analysis.Basic_Decl_Array :=
274-
Context.Find_All_Base_Declarations
275-
(Decl_For_Find_Overrides,
276-
Imprecise_Results => Ignore);
277-
begin
278-
for Subp of Bases loop
279-
Self.Parent.Context.Append_Location
280-
(Self.Response,
281-
Self.Filter,
282-
Subp.P_Defining_Name,
283-
Is_Parent);
284-
end loop;
285-
286-
for Subp of Overridings loop
287-
Self.Parent.Context.Append_Location
288-
(Self.Response,
289-
Self.Filter,
290-
Subp.P_Defining_Name,
291-
Is_Child);
292-
end loop;
293-
end;
294-
end if;
304+
Append_Overrides (Decl_For_Find_Overrides);
295305
end Execute_Ada_Request;
296306

297307
end LSP.Ada_Definition;

0 commit comments

Comments
 (0)