Skip to content

Commit 9bc738e

Browse files
Anthony Leonardo GracioAnthonyLeonardoGracio
authored andcommitted
V614-021: Use completion weight in sortText
In order to place items with an higher weight at the top of the completion window.
1 parent 3bbb022 commit 9bc738e

File tree

16 files changed

+55
-30
lines changed

16 files changed

+55
-30
lines changed

source/ada/lsp-ada_completions-names.adb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ package body LSP.Ada_Completions.Names is
128128
(Is_Dot_Call => False,
129129
Is_Visible => True,
130130
Use_Snippets => Use_Snippets,
131-
Pos => 1));
131+
Pos => 1,
132+
Weight => 0));
132133
return;
133134
end if;
134135
end if;
@@ -174,7 +175,8 @@ package body LSP.Ada_Completions.Names is
174175
(Is_Dot_Call (Item),
175176
Is_Visible (Item),
176177
Use_Snippets,
177-
Completion_Count));
178+
Completion_Count,
179+
Weight (Item)));
178180
end if;
179181
end loop;
180182
end if;

source/ada/lsp-ada_completions.adb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ package body LSP.Ada_Completions is
9999
Is_Dot_Call => Info.Is_Dot_Call,
100100
Is_Visible => Info.Is_Visible,
101101
Pos => Info.Pos,
102+
Weight => Info.Weight,
102103
Completions_Count => Length));
103104
end if;
104105
end;

source/ada/lsp-ada_completions.ads

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ package LSP.Ada_Completions is
4343
-- for completion, since LAL can return several times the same declaration
4444
-- and specially subprograms from generic instantiations.
4545

46+
subtype Completion_Item_Weight_Type is Integer range 0 .. 100;
47+
-- Type representing the weight returned by LAL for each completion item.
48+
-- Used to sort them accordingly on the client-side.
49+
4650
type Name_Information is record
4751
Is_Dot_Call : Boolean;
4852
-- True if we are dealing with a dotted call.
@@ -57,6 +61,9 @@ package LSP.Ada_Completions is
5761
Pos : Integer := -1;
5862
-- The position of the item in the fully computed completion list. Used
5963
-- for sorting properly the items on client-side.
64+
65+
Weight : Completion_Item_Weight_Type := 0;
66+
-- The completion item's weight. Used for sorting on the client-side.
6067
end record;
6168

6269
package Completion_Maps is new Ada.Containers.Hashed_Maps

source/ada/lsp-ada_documents.adb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,7 @@ package body LSP.Ada_Documents is
18381838
Is_Dot_Call : Boolean;
18391839
Is_Visible : Boolean;
18401840
Pos : Integer;
1841+
Weight : Completion_Item_Weight_Type;
18411842
Completions_Count : Natural)
18421843
return LSP.Messages.CompletionItem
18431844
is
@@ -1860,14 +1861,22 @@ package body LSP.Ada_Documents is
18601861

18611862
function Get_Sort_Text
18621863
(Base_Label : VSS.Strings.Virtual_String)
1863-
return VSS.Strings.Virtual_String is
1864+
return VSS.Strings.Virtual_String
1865+
is
1866+
use VSS.Strings;
18641867
begin
18651868
return Sort_Text : VSS.Strings.Virtual_String do
1866-
if Pos /= -1 then
1867-
Sort_Text :=
1868-
VSS.Strings.Conversions.To_Virtual_String
1869-
(GNATCOLL.Utils.Image (Pos, Min_Width => Min_Width));
1870-
end if;
1869+
1870+
Sort_Text :=
1871+
VSS.Strings.Conversions.To_Virtual_String
1872+
(GNATCOLL.Utils.Image
1873+
(Value => Completion_Item_Weight_Type'Last - Weight,
1874+
Min_Width =>
1875+
Completion_Item_Weight_Type'Last'Img'Length - 1)) & "&";
1876+
1877+
Sort_Text := Sort_Text &
1878+
VSS.Strings.Conversions.To_Virtual_String
1879+
(GNATCOLL.Utils.Image (Pos, Min_Width => Min_Width));
18711880

18721881
Sort_Text.Append (Base_Label);
18731882

@@ -2146,7 +2155,8 @@ package body LSP.Ada_Documents is
21462155
(Is_Dot_Call => False,
21472156
Is_Visible => False,
21482157
Use_Snippets => False,
2149-
Pos => <>));
2158+
Pos => <>,
2159+
Weight => <>));
21502160
end if;
21512161
end Insert;
21522162

source/ada/lsp-ada_documents.ads

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ with GNATCOLL.Traces;
3333
with Pp.Command_Lines;
3434

3535
limited with LSP.Ada_Contexts;
36-
with LSP.Ada_Completions;
36+
with LSP.Ada_Completions; use LSP.Ada_Completions;
3737
with LSP.Ada_Highlighters;
3838
with LSP.Diagnostic_Sources;
3939
with LSP.Messages;
@@ -252,6 +252,7 @@ package LSP.Ada_Documents is
252252
Is_Dot_Call : Boolean;
253253
Is_Visible : Boolean;
254254
Pos : Integer;
255+
Weight : Completion_Item_Weight_Type;
255256
Completions_Count : Natural)
256257
return LSP.Messages.CompletionItem;
257258
-- Compute a completion item.
@@ -264,6 +265,8 @@ package LSP.Ada_Documents is
264265
-- named notation is used for subprogram completion snippets.
265266
-- Is_Dot_Call is used to know if we should omit the first parameter
266267
-- when computing subprogram snippets.
268+
-- Weight is used for sorting: items with an higher weight will be placed
269+
-- at the top.
267270
-- Completions_Count is the total number of completion items.
268271

269272
procedure Set_Completion_Item_Documentation

source/ada/lsp-ada_handlers-invisibles.adb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ package body LSP.Ada_Handlers.Invisibles is
6868
(Is_Dot_Call => False,
6969
Is_Visible => False,
7070
Use_Snippets => False,
71-
Pos => Pos));
71+
Pos => Pos,
72+
Weight => <>));
7273

7374
Pos := Pos + 1;
7475

source/ada/lsp-ada_handlers.adb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5121,7 +5121,8 @@ package body LSP.Ada_Handlers is
51215121
(Is_Dot_Call => False,
51225122
Is_Visible => False,
51235123
Use_Snippets => False,
5124-
Pos => <>));
5124+
Pos => <>,
5125+
Weight => <>));
51255126
end if;
51265127

51275128
Stop := Canceled.Has_Been_Canceled;

testsuite/ada_lsp/U915-024.completion.invisible_snippets/test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
{
205205
"label": "Do_Something (invisible)",
206206
"kind": 3,
207-
"sortText": "~0Do_Something",
207+
"sortText": "~100&0Do_Something",
208208
"filterText": "Do_Something",
209209
"insertText": "Do_Something",
210210
"additionalTextEdits": [],

testsuite/ada_lsp/U921-010.completion.no_generic_duplicates/test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@
244244
{
245245
"label": "Primitive",
246246
"kind": 3,
247-
"sortText": "2Primitive",
247+
"sortText": "100&2Primitive",
248248
"insertText": "Primitive (${1:Obj : Parent})$0",
249249
"insertTextFormat": 2,
250250
"additionalTextEdits": [],

testsuite/ada_lsp/completion.duplicates/test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
{
200200
"label": "Do_Something",
201201
"kind": 3,
202-
"sortText": "2Do_Something",
202+
"sortText": "100&2Do_Something",
203203
"additionalTextEdits": [],
204204
"data": {
205205
"uri": "$URI{main.adb}",

0 commit comments

Comments
 (0)