1515-- of the license. --
1616-- ----------------------------------------------------------------------------
1717
18+ with Ada.Characters.Latin_1 ;
1819with Ada.Strings.Unbounded ;
1920with Ada.Strings.UTF_Encoding ;
2021with Ada.Tags.Generic_Dispatching_Constructor ;
@@ -37,6 +38,7 @@ with Laltools.Common;
3738with Laltools.Partial_GNATPP ;
3839
3940with Langkit_Support.Slocs ;
41+ with Langkit_Support.Text ;
4042
4143with LAL_Refactor.Extract_Subprogram ;
4244with LAL_Refactor.Introduce_Parameter ;
@@ -134,6 +136,17 @@ package body LSP.Ada_Handlers is
134136 Name : String);
135137 -- Save method in/out in a log file
136138
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+
137150 function To_LSP_Location
138151 (Self : in out Message_Handler'Class;
139152 Node : Libadalang.Analysis.Ada_Node'Class;
@@ -345,11 +358,11 @@ package body LSP.Ada_Handlers is
345358 Trace : constant GNATCOLL.Traces.Trace_Handle :=
346359 LSP.GNATCOLL_Tracers.Handle (Self.Tracer.all );
347360
348- Name_Node : constant Libadalang.Analysis.Name :=
361+ Name_Node : constant Libadalang.Analysis.Name :=
349362 Laltools.Common.Get_Node_As_Name
350363 (Self.Get_Node_At (Context, Position));
351364
352- Imprecise : Boolean ;
365+ Ref_Kind : Libadalang.Common.Ref_Result_Kind ;
353366 begin
354367 if Name_Node.Is_Null then
355368 return Libadalang.Analysis.No_Defining_Name;
@@ -358,7 +371,7 @@ package body LSP.Ada_Handlers is
358371 return Laltools.Common.Resolve_Name
359372 (Name_Node,
360373 Trace,
361- Imprecise => Imprecise );
374+ Ref_Kind => Ref_Kind );
362375 end Imprecise_Resolve_Name ;
363376
364377 -- -------------------------------
@@ -1731,7 +1744,7 @@ package body LSP.Ada_Handlers is
17311744 On_Defining_Name : Boolean := False;
17321745 -- Set to True if we are on a denfining name node
17331746
1734- Is_Imprecise : Boolean;
1747+ Imprecise : Boolean;
17351748 begin
17361749 if Name_Node.Is_Null then
17371750 return ;
@@ -1742,8 +1755,13 @@ package body LSP.Ada_Handlers is
17421755
17431756 if Definition.Is_Null then
17441757 -- 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);
17471765 else
17481766 On_Defining_Name := True;
17491767 end if ;
@@ -1795,12 +1813,13 @@ package body LSP.Ada_Handlers is
17951813
17961814 if not Decl_For_Find_Overrides.Is_Null then
17971815 declare
1798- Overridings : constant Libadalang.Analysis.Basic_Decl_Array :=
1816+ Is_Imprecise : Boolean;
1817+ Overridings : constant Libadalang.Analysis.Basic_Decl_Array :=
17991818 C.Find_All_Overrides
18001819 (Decl_For_Find_Overrides,
18011820 Imprecise_Results => Is_Imprecise);
18021821
1803- Bases : constant Libadalang.Analysis.Basic_Decl_Array :=
1822+ Bases : constant Libadalang.Analysis.Basic_Decl_Array :=
18041823 C.Find_All_Base_Declarations
18051824 (Decl_For_Find_Overrides,
18061825 Imprecise_Results => Is_Imprecise);
@@ -1856,7 +1875,7 @@ package body LSP.Ada_Handlers is
18561875 Vector : LSP.Structures.Location_Vector renames Response.Variant_1;
18571876 Filter : LSP.Locations.File_Span_Sets.Set;
18581877
1859- Imprecise : Boolean := False ;
1878+ Imprecise : Boolean;
18601879
18611880 Display_Method_Policy : constant
18621881 LSP.Enumerations.AlsDisplayMethodAncestryOnNavigationPolicy :=
@@ -1893,10 +1912,13 @@ package body LSP.Ada_Handlers is
18931912 Definition := Laltools.Common.Get_Name_As_Defining (Name_Node);
18941913
18951914 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);
19001922
19011923 if not Definition.Is_Null then
19021924 Self.Append_Location (Vector, Filter, Definition);
@@ -1970,6 +1992,7 @@ package body LSP.Ada_Handlers is
19701992
19711993 if not Decl_For_Find_Overrides.Is_Null then
19721994 declare
1995+ Imprecise : Boolean;
19731996 Overridings : constant Basic_Decl_Array :=
19741997 C.Find_All_Overrides
19751998 (Decl_For_Find_Overrides,
@@ -2912,17 +2935,20 @@ package body LSP.Ada_Handlers is
29122935 end Update_Response ;
29132936
29142937 Definition : Libadalang.Analysis.Defining_Name;
2915- Imprecise : Boolean;
2938+ Imprecise : Boolean := False ;
29162939 Decl : Libadalang.Analysis.Basic_Decl;
29172940
29182941 begin
29192942 if Name_Node.Is_Null then
29202943 return ;
29212944 end if ;
29222945
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);
29262952
29272953 -- If we didn't find a definition, give up for this context
29282954 if Definition.Is_Null then
@@ -3464,9 +3490,6 @@ package body LSP.Ada_Handlers is
34643490 Id : LSP.Structures.Integer_Or_Virtual_String;
34653491 Value : LSP.Structures.PrepareRenameParams)
34663492 is
3467- Trace : constant GNATCOLL.Traces.Trace_Handle :=
3468- LSP.GNATCOLL_Tracers.Handle (Self.Tracer.all );
3469-
34703493 Response : LSP.Structures.PrepareRenameResult_Or_Null;
34713494
34723495 Context : constant LSP.Ada_Context_Sets.Context_Access :=
@@ -3480,12 +3503,14 @@ package body LSP.Ada_Handlers is
34803503
34813504 Defining_Name : Libadalang.Analysis.Defining_Name;
34823505
3483- Imprecise : Boolean;
3506+ Imprecise : Boolean := False ;
34843507 begin
34853508 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,
34893514 Imprecise => Imprecise);
34903515 end if ;
34913516
@@ -4101,9 +4126,6 @@ package body LSP.Ada_Handlers is
41014126 -- ----------------------
41024127
41034128 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-
41074129 Name_Node : constant Libadalang.Analysis.Name :=
41084130 Laltools.Common.Get_Node_As_Name (Self.Get_Node_At (C.all , Value));
41094131
@@ -4125,8 +4147,13 @@ package body LSP.Ada_Handlers is
41254147 Def_Name.P_Basic_Decl.P_Type_Expression;
41264148 begin
41274149 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);
41304157 end if ;
41314158 end ;
41324159 else
@@ -4221,6 +4248,104 @@ package body LSP.Ada_Handlers is
42214248 LSP.Ada_Handlers.Project_Loading.Reload_Project (Self);
42224249 end Reload_Project ;
42234250
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+
42244349 -- ---------------------
42254350 -- Set_Configuration --
42264351 -- ---------------------
0 commit comments