15
15
-- of the license. --
16
16
-- ----------------------------------------------------------------------------
17
17
18
+ with Ada.Characters.Latin_1 ;
18
19
with Ada.Strings.Unbounded ;
19
20
with Ada.Strings.UTF_Encoding ;
20
21
with Ada.Tags.Generic_Dispatching_Constructor ;
@@ -37,6 +38,7 @@ with Laltools.Common;
37
38
with Laltools.Partial_GNATPP ;
38
39
39
40
with Langkit_Support.Slocs ;
41
+ with Langkit_Support.Text ;
40
42
41
43
with LAL_Refactor.Extract_Subprogram ;
42
44
with LAL_Refactor.Introduce_Parameter ;
@@ -134,6 +136,17 @@ package body LSP.Ada_Handlers is
134
136
Name : String);
135
137
-- Save method in/out in a log file
136
138
139
+ function Resolve_Name
140
+ (Self : in out Message_Handler;
141
+ Id : LSP.Structures.Integer_Or_Virtual_String;
142
+ Context : LSP.Ada_Contexts.Context;
143
+ Name_Node : Libadalang.Analysis.Name;
144
+ Imprecise : out Boolean)
145
+ return Libadalang.Analysis.Defining_Name;
146
+ -- Toplayer Resolve_Name based on Laltools.Common.Resolve_Name.
147
+ -- This function is handling Imprecise and Error results during Nameres by
148
+ -- logging them and generating Diagnostics if needed.
149
+
137
150
function To_LSP_Location
138
151
(Self : in out Message_Handler'Class;
139
152
Node : Libadalang.Analysis.Ada_Node'Class;
@@ -345,11 +358,11 @@ package body LSP.Ada_Handlers is
345
358
Trace : constant GNATCOLL.Traces.Trace_Handle :=
346
359
LSP.GNATCOLL_Tracers.Handle (Self.Tracer.all );
347
360
348
- Name_Node : constant Libadalang.Analysis.Name :=
361
+ Name_Node : constant Libadalang.Analysis.Name :=
349
362
Laltools.Common.Get_Node_As_Name
350
363
(Self.Get_Node_At (Context, Position));
351
364
352
- Imprecise : Boolean ;
365
+ Ref_Kind : Libadalang.Common.Ref_Result_Kind ;
353
366
begin
354
367
if Name_Node.Is_Null then
355
368
return Libadalang.Analysis.No_Defining_Name;
@@ -358,7 +371,7 @@ package body LSP.Ada_Handlers is
358
371
return Laltools.Common.Resolve_Name
359
372
(Name_Node,
360
373
Trace,
361
- Imprecise => Imprecise );
374
+ Ref_Kind => Ref_Kind );
362
375
end Imprecise_Resolve_Name ;
363
376
364
377
-- -------------------------------
@@ -1731,7 +1744,7 @@ package body LSP.Ada_Handlers is
1731
1744
On_Defining_Name : Boolean := False;
1732
1745
-- Set to True if we are on a denfining name node
1733
1746
1734
- Is_Imprecise : Boolean;
1747
+ Imprecise : Boolean;
1735
1748
begin
1736
1749
if Name_Node.Is_Null then
1737
1750
return ;
@@ -1742,8 +1755,13 @@ package body LSP.Ada_Handlers is
1742
1755
1743
1756
if Definition.Is_Null then
1744
1757
-- If we aren't on a defining_name already then try to resolve
1745
- Definition := Laltools.Common.Resolve_Name
1746
- (Name_Node, Trace, Is_Imprecise);
1758
+ Definition :=
1759
+ Resolve_Name
1760
+ (Self => Self,
1761
+ Id => Id,
1762
+ Context => C.all ,
1763
+ Name_Node => Name_Node,
1764
+ Imprecise => Imprecise);
1747
1765
else
1748
1766
On_Defining_Name := True;
1749
1767
end if ;
@@ -1795,12 +1813,13 @@ package body LSP.Ada_Handlers is
1795
1813
1796
1814
if not Decl_For_Find_Overrides.Is_Null then
1797
1815
declare
1798
- Overridings : constant Libadalang.Analysis.Basic_Decl_Array :=
1816
+ Is_Imprecise : Boolean;
1817
+ Overridings : constant Libadalang.Analysis.Basic_Decl_Array :=
1799
1818
C.Find_All_Overrides
1800
1819
(Decl_For_Find_Overrides,
1801
1820
Imprecise_Results => Is_Imprecise);
1802
1821
1803
- Bases : constant Libadalang.Analysis.Basic_Decl_Array :=
1822
+ Bases : constant Libadalang.Analysis.Basic_Decl_Array :=
1804
1823
C.Find_All_Base_Declarations
1805
1824
(Decl_For_Find_Overrides,
1806
1825
Imprecise_Results => Is_Imprecise);
@@ -1856,7 +1875,7 @@ package body LSP.Ada_Handlers is
1856
1875
Vector : LSP.Structures.Location_Vector renames Response.Variant_1;
1857
1876
Filter : LSP.Locations.File_Span_Sets.Set;
1858
1877
1859
- Imprecise : Boolean := False ;
1878
+ Imprecise : Boolean;
1860
1879
1861
1880
Display_Method_Policy : constant
1862
1881
LSP.Enumerations.AlsDisplayMethodAncestryOnNavigationPolicy :=
@@ -1893,10 +1912,13 @@ package body LSP.Ada_Handlers is
1893
1912
Definition := Laltools.Common.Get_Name_As_Defining (Name_Node);
1894
1913
1895
1914
if Definition.Is_Null then
1896
- Definition := Laltools.Common.Resolve_Name
1897
- (Name_Node,
1898
- Trace,
1899
- Imprecise => Imprecise);
1915
+ Definition :=
1916
+ Resolve_Name
1917
+ (Self => Self,
1918
+ Id => Id,
1919
+ Context => C.all ,
1920
+ Name_Node => Name_Node,
1921
+ Imprecise => Imprecise);
1900
1922
1901
1923
if not Definition.Is_Null then
1902
1924
Self.Append_Location (Vector, Filter, Definition);
@@ -1970,6 +1992,7 @@ package body LSP.Ada_Handlers is
1970
1992
1971
1993
if not Decl_For_Find_Overrides.Is_Null then
1972
1994
declare
1995
+ Imprecise : Boolean;
1973
1996
Overridings : constant Basic_Decl_Array :=
1974
1997
C.Find_All_Overrides
1975
1998
(Decl_For_Find_Overrides,
@@ -2912,17 +2935,20 @@ package body LSP.Ada_Handlers is
2912
2935
end Update_Response ;
2913
2936
2914
2937
Definition : Libadalang.Analysis.Defining_Name;
2915
- Imprecise : Boolean;
2938
+ Imprecise : Boolean := False ;
2916
2939
Decl : Libadalang.Analysis.Basic_Decl;
2917
2940
2918
2941
begin
2919
2942
if Name_Node.Is_Null then
2920
2943
return ;
2921
2944
end if ;
2922
2945
2923
- -- Find the definition
2924
- Definition := Laltools.Common.Resolve_Name
2925
- (Name_Node, Trace, Imprecise);
2946
+ Definition := Resolve_Name
2947
+ (Self => Self,
2948
+ Id => Id,
2949
+ Context => C.all ,
2950
+ Name_Node => Name_Node,
2951
+ Imprecise => Imprecise);
2926
2952
2927
2953
-- If we didn't find a definition, give up for this context
2928
2954
if Definition.Is_Null then
@@ -3464,9 +3490,6 @@ package body LSP.Ada_Handlers is
3464
3490
Id : LSP.Structures.Integer_Or_Virtual_String;
3465
3491
Value : LSP.Structures.PrepareRenameParams)
3466
3492
is
3467
- Trace : constant GNATCOLL.Traces.Trace_Handle :=
3468
- LSP.GNATCOLL_Tracers.Handle (Self.Tracer.all );
3469
-
3470
3493
Response : LSP.Structures.PrepareRenameResult_Or_Null;
3471
3494
3472
3495
Context : constant LSP.Ada_Context_Sets.Context_Access :=
@@ -3480,12 +3503,14 @@ package body LSP.Ada_Handlers is
3480
3503
3481
3504
Defining_Name : Libadalang.Analysis.Defining_Name;
3482
3505
3483
- Imprecise : Boolean;
3506
+ Imprecise : Boolean := False ;
3484
3507
begin
3485
3508
if not Name_Node.Is_Null then
3486
- Defining_Name := Laltools.Common.Resolve_Name
3487
- (Name_Node,
3488
- Trace,
3509
+ Defining_Name := Resolve_Name
3510
+ (Self => Self,
3511
+ Id => Id,
3512
+ Context => Context.all ,
3513
+ Name_Node => Name_Node,
3489
3514
Imprecise => Imprecise);
3490
3515
end if ;
3491
3516
@@ -4101,9 +4126,6 @@ package body LSP.Ada_Handlers is
4101
4126
-- ----------------------
4102
4127
4103
4128
procedure Resolve_In_Context (C : LSP.Ada_Context_Sets.Context_Access) is
4104
- Trace : constant GNATCOLL.Traces.Trace_Handle :=
4105
- LSP.GNATCOLL_Tracers.Handle (Self.Tracer.all );
4106
-
4107
4129
Name_Node : constant Libadalang.Analysis.Name :=
4108
4130
Laltools.Common.Get_Node_As_Name (Self.Get_Node_At (C.all , Value));
4109
4131
@@ -4125,8 +4147,13 @@ package body LSP.Ada_Handlers is
4125
4147
Def_Name.P_Basic_Decl.P_Type_Expression;
4126
4148
begin
4127
4149
if not Type_Expr.Is_Null then
4128
- Definition := Laltools.Common.Resolve_Name
4129
- (Type_Expr.P_Type_Name, Trace, Imprecise);
4150
+ Definition :=
4151
+ Resolve_Name
4152
+ (Self => Self,
4153
+ Id => Id,
4154
+ Context => C.all ,
4155
+ Name_Node => Type_Expr.P_Type_Name,
4156
+ Imprecise => Imprecise);
4130
4157
end if ;
4131
4158
end ;
4132
4159
else
@@ -4221,6 +4248,104 @@ package body LSP.Ada_Handlers is
4221
4248
LSP.Ada_Handlers.Project_Loading.Reload_Project (Self);
4222
4249
end Reload_Project ;
4223
4250
4251
+ -- ----------------
4252
+ -- Resolve_Name --
4253
+ -- ----------------
4254
+
4255
+ function Resolve_Name
4256
+ (Self : in out Message_Handler;
4257
+ Id : LSP.Structures.Integer_Or_Virtual_String;
4258
+ Context : LSP.Ada_Contexts.Context;
4259
+ Name_Node : Libadalang.Analysis.Name;
4260
+ Imprecise : out Boolean)
4261
+ return Libadalang.Analysis.Defining_Name
4262
+ is
4263
+ Definition : Libadalang.Analysis.Defining_Name;
4264
+ Result_Kind : Libadalang.Common.Ref_Result_Kind;
4265
+ Trace : constant GNATCOLL.Traces.Trace_Handle :=
4266
+ LSP.GNATCOLL_Tracers.Handle (Self.Tracer.all );
4267
+ Id_Image : constant String :=
4268
+ (if Id.Is_Integer
4269
+ then Id.Integer'Image
4270
+ else VSS.Strings.Conversions.To_UTF_8_String (Id.Virtual_String));
4271
+ begin
4272
+ Imprecise := False;
4273
+
4274
+ if Name_Node.Is_Null then
4275
+ -- Internal tracing of resolve on null node
4276
+ Self.Tracer.Trace (" Can't resolve null node for request " & Id_Image);
4277
+ return Libadalang.Analysis.No_Defining_Name;
4278
+ end if ;
4279
+
4280
+ -- Find the definition
4281
+ Definition := Laltools.Common.Resolve_Name
4282
+ (Name_Node, Trace, Result_Kind);
4283
+
4284
+ if Result_Kind in Libadalang.Common.Error then
4285
+ declare
4286
+ Err_Msg : constant String :=
4287
+ " Failed to resolve " & Name_Node.Image;
4288
+ Diag_Params : LSP.Structures.PublishDiagnosticsParams;
4289
+ Diagnostic : LSP.Structures.Diagnostic;
4290
+ Loc : constant LSP.Structures.Location :=
4291
+ Self.To_LSP_Location (Name_Node);
4292
+ begin
4293
+ -- Internal tracing of failed resolution with context info
4294
+ Self.Tracer.Trace
4295
+ (Err_Msg
4296
+ & " in context "
4297
+ & VSS.Strings.Conversions.To_UTF_8_String (Context.Id)
4298
+ & " for request "
4299
+ & Id_Image);
4300
+
4301
+ -- Send a diagnostic for the user
4302
+ Diagnostic.a_range := Loc.a_range;
4303
+ Diagnostic.severity := LSP.Constants.Error;
4304
+ Diagnostic.source := " Ada" ;
4305
+ -- Diagnostics are shown to the user so show a simple
4306
+ -- representation of Namer_Node
4307
+ Diagnostic.message :=
4308
+ VSS.Strings.Conversions.To_Virtual_String
4309
+ (" Failed to resolve "
4310
+ & Langkit_Support.Text.To_UTF8 (Name_Node.Text)
4311
+ & Ada.Characters.Latin_1.LF
4312
+ & " Please check the output of the following command:"
4313
+ & Ada.Characters.Latin_1.LF
4314
+ & " lal_nameres -P "
4315
+ & String
4316
+ (Self.Project_Tree.Root_Project.Path_Name.Filesystem_String)
4317
+ & " --all --only-show-failures "
4318
+ & VSS.Strings.Conversions.To_UTF_8_String (Loc.uri));
4319
+
4320
+ Diag_Params.uri := Loc.uri;
4321
+ Diag_Params.diagnostics.Append (Diagnostic);
4322
+ Self.Sender.On_PublishDiagnostics_Notification (Diag_Params);
4323
+
4324
+ -- Inform the client that the request failed
4325
+ Self.Sender.On_Error_Response
4326
+ (Id,
4327
+ (code => LSP.Enumerations.InternalError,
4328
+ message => VSS.Strings.Conversions.To_Virtual_String
4329
+ (Err_Msg)));
4330
+
4331
+ return Libadalang.Analysis.No_Defining_Name;
4332
+ end ;
4333
+
4334
+ elsif Result_Kind in Libadalang.Common.Imprecise then
4335
+ -- Internal tracing of imprecise resolving
4336
+ Self.Tracer.Trace
4337
+ (" Imprecise result when resolving "
4338
+ & Name_Node.Image
4339
+ & " in context "
4340
+ & VSS.Strings.Conversions.To_UTF_8_String (Context.Id)
4341
+ & " for request "
4342
+ & Id_Image);
4343
+ Imprecise := True;
4344
+ end if ;
4345
+
4346
+ return Definition;
4347
+ end Resolve_Name ;
4348
+
4224
4349
-- ---------------------
4225
4350
-- Set_Configuration --
4226
4351
-- ---------------------
0 commit comments