@@ -24,6 +24,7 @@ with Laltools.Common;
2424with Libadalang.Common ;
2525
2626with LSP.Ada_Completions.Filters ;
27+ with LSP.Ada_Documents ;
2728with LSP.Lal_Utils ;
2829with LSP.Types ; use LSP.Types;
2930
@@ -39,10 +40,10 @@ package body LSP.Ada_Completions.Parameters is
3940 Token : Libadalang.Common.Token_Reference;
4041 Node : Libadalang.Analysis.Ada_Node;
4142 Filter : in out LSP.Ada_Completions.Filters.Filter;
42- Names : out Ada_Completions.Completion_Maps.Map;
43- Result : out LSP.Messages.CompletionList)
43+ Names : in out Ada_Completions.Completion_Maps.Map;
44+ Result : in out LSP.Messages.CompletionList)
4445 is
45- pragma Unreferenced (Filter, Names );
46+ pragma Unreferenced (Filter);
4647 use Libadalang.Analysis;
4748 use Libadalang.Common;
4849
@@ -86,19 +87,39 @@ package body LSP.Ada_Completions.Parameters is
8687 end if ;
8788
8889 declare
89- Item : LSP.Messages.CompletionItem;
90- Prefix : constant Ada.Strings.UTF_Encoding.UTF_8_String :=
90+ Prefix : constant Ada.Strings.UTF_Encoding.UTF_8_String :=
9191 Langkit_Support.Text.To_UTF8 (Node.Text);
92- Name_Node : constant Libadalang.Analysis.Name :=
92+ Name_Node : constant Libadalang.Analysis.Name :=
9393 Call_Expr_Node.F_Name;
94+
95+ Nb_Items : constant Natural :=
96+ Natural (Names.Length) + Natural (Result.items.Length);
97+ -- Assuming that all the names are already present this will
98+ -- represent the total number of items inserted
99+
100+ Min_Width : constant Natural := Nb_Items'Img'Length - 1 ;
101+ -- The -1 remove the whitespace added by 'Img
102+
103+ Sort_Index : Natural := Natural (Result.items.Length);
104+ -- Index used to sort the completionItem
105+
106+ Params_Index : Natural := Sort_Index;
107+ -- Index of the "Param of" completionItem, this is related
108+ -- to Sort_Index
94109 begin
95110 for N of Self.Context.Find_All_Env_Elements (Name_Node) loop
96111 if N.Kind in Ada_Basic_Subp_Decl then
112+ -- Save an index for "Params of" node
113+ Params_Index := Sort_Index;
114+ Sort_Index := Sort_Index + 1 ;
115+
97116 declare
98117 Params_Snippet : LSP_String := Empty_LSP_String;
99- Snippet_Name : LSP_String := Empty_LSP_String;
100- -- $0 is used for the final tab stop
101- Index : Positive := 1 ;
118+
119+ Snippet_Index : Positive := 1 ;
120+ -- Index of the snippet: $0 is already used by the final
121+ -- tab stop
122+
102123 Spec : constant Libadalang.Analysis.Base_Subp_Spec
103124 := N.As_Basic_Decl.P_Subp_Spec_Or_Null;
104125 begin
@@ -109,6 +130,7 @@ package body LSP.Ada_Completions.Parameters is
109130 for Param of Spec.P_Params loop
110131 for Id of Param.F_Ids loop
111132 declare
133+ Item : LSP.Messages.CompletionItem;
112134 Name_Text : constant Text_Type := Id.Text;
113135 Name : constant LSP_String :=
114136 To_LSP_String (Name_Text);
@@ -127,8 +149,15 @@ package body LSP.Ada_Completions.Parameters is
127149 Item.insertText :=
128150 (True,
129151 Whitespace_Prefix & Name & " => " );
130- Item.kind := (True, LSP.Messages.Text);
152+ Item.kind := (True, LSP.Messages.Field);
153+ Item.sortText :=
154+ (True,
155+ To_LSP_String
156+ (GNATCOLL.Utils.Image
157+ (Sort_Index,
158+ Min_Width => Min_Width)));
131159 Result.items.Append (Item);
160+ Sort_Index := Sort_Index + 1 ;
132161 end if ;
133162
134163 Params_Snippet := Params_Snippet
@@ -137,9 +166,9 @@ package body LSP.Ada_Completions.Parameters is
137166 & " $"
138167 & To_LSP_String
139168 (GNATCOLL.Utils.Image
140- (Index , Min_Width => 1 ))
169+ (Snippet_Index , Min_Width => 1 ))
141170 & " , " ;
142- Index := Index + 1 ;
171+ Snippet_Index := Snippet_Index + 1 ;
143172 end if ;
144173 end ;
145174 end loop ;
@@ -157,18 +186,32 @@ package body LSP.Ada_Completions.Parameters is
157186 Length (Params_Snippet) - 2 );
158187 Params_Snippet := Params_Snippet & " )$0" ;
159188
160- Snippet_Name :=
161- Snippet_Name
162- & " Params of "
163- & To_LSP_String (Name_Node.Text);
164- Item.label := To_Virtual_String
165- (Snippet_Name);
166- Item.insertTextFormat :=
167- (True, LSP.Messages.Snippet);
168- Item.insertText :=
169- (True, Whitespace_Prefix & Params_Snippet);
170- Item.kind := (True, LSP.Messages.Snippet);
171- Result.items.Append (Item);
189+ declare
190+ Snippet_Name : constant LSP_String :=
191+ " Params of " & To_LSP_String (Name_Node.Text);
192+ Item : LSP.Messages.CompletionItem;
193+ begin
194+ Item.label := To_Virtual_String
195+ (Snippet_Name);
196+ Item.insertTextFormat :=
197+ (True, LSP.Messages.Snippet);
198+ Item.insertText :=
199+ (True, Whitespace_Prefix & Params_Snippet);
200+ Item.kind := (True, LSP.Messages.Snippet);
201+ LSP.Ada_Documents.Set_Completion_Item_Documentation
202+ (Context => Self.Context.all ,
203+ BD => N.As_Basic_Decl,
204+ Item => Item,
205+ Compute_Doc_And_Details =>
206+ Self.Compute_Doc_And_Details);
207+ Item.sortText :=
208+ (True,
209+ To_LSP_String
210+ (GNATCOLL.Utils.Image
211+ (Params_Index, Min_Width => Min_Width)));
212+
213+ Result.items.Append (Item);
214+ end ;
172215 end if ;
173216 end if ;
174217 end ;
0 commit comments