Skip to content

Commit fbcd530

Browse files
committed
ensure typed completions end up on top by sorting them to come before regular completions
1 parent aa9aec1 commit fbcd530

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,10 +2281,25 @@ Note: The `@react.component` decorator requires the react-jsx config to be set i
22812281
match (prefix, completionContext) with
22822282
| "", _ -> items
22832283
| _, None ->
2284+
(* Completions for local things like variables in scope, modules in the project, etc. *)
22842285
let regularCompletions =
22852286
prefix
22862287
|> getComplementaryCompletionsForTypedValue ~opens ~allFiles ~env
22872288
~scope
22882289
in
2290+
let items =
2291+
if List.length regularCompletions > 0 then
2292+
(* The client will occasionally sort the list of completions alphabetically, disregarding the order
2293+
in which we send it. This fixes that by providing a sort text making the typed completions
2294+
guaranteed to end up on top. *)
2295+
items
2296+
|> List.mapi (fun index (c : Completion.t) ->
2297+
{
2298+
c with
2299+
sortText =
2300+
Some ("AAA" ^ string_of_int (index + 1) ^ " " ^ c.name);
2301+
})
2302+
else items
2303+
in
22892304
items @ regularCompletions
22902305
| _ -> items)))

analysis/tests/src/expected/CompletionExpressions.res.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ Completable: Cexpression CArgument Value[fnTakingRecord]($0)=O->recordField(vari
233233
"tags": [],
234234
"detail": "One\n\ntype someVariant = One | Two | Three(int, string)",
235235
"documentation": null,
236+
"sortText": "AAA1 One",
236237
"insertText": "One",
237238
"insertTextFormat": 2
238239
}, {

analysis/tests/src/expected/CompletionFunctionArguments.res.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Completable: Cexpression CArgument Value[someFn](~isOn)=t
2525
"kind": 4,
2626
"tags": [],
2727
"detail": "bool",
28-
"documentation": null
28+
"documentation": null,
29+
"sortText": "AAA1 true"
2930
}, {
3031
"label": "tLocalVar",
3132
"kind": 12,
@@ -126,6 +127,7 @@ Completable: Cexpression CArgument Value[someFnTakingVariant](~config)=O
126127
"tags": [],
127128
"detail": "One\n\ntype someVariant = One | Two | Three(int, string)",
128129
"documentation": null,
130+
"sortText": "AAA1 One",
129131
"insertText": "One",
130132
"insertTextFormat": 2
131133
}, {
@@ -158,6 +160,7 @@ Completable: Cexpression CArgument Value[someFnTakingVariant]($0)=So
158160
"tags": [],
159161
"detail": "someVariant",
160162
"documentation": null,
163+
"sortText": "AAA1 Some(_)",
161164
"insertText": "Some(${1:_})",
162165
"insertTextFormat": 2
163166
}, {
@@ -178,6 +181,7 @@ Completable: Cexpression CArgument Value[someFnTakingVariant](~configOpt2)=O
178181
"tags": [],
179182
"detail": "One\n\ntype someVariant = One | Two | Three(int, string)",
180183
"documentation": null,
184+
"sortText": "AAA1 One",
181185
"insertText": "One",
182186
"insertTextFormat": 2
183187
}, {
@@ -244,7 +248,8 @@ Completable: Cexpression CArgument Value[someOtherFn]($2)=t
244248
"kind": 4,
245249
"tags": [],
246250
"detail": "bool",
247-
"documentation": null
251+
"documentation": null,
252+
"sortText": "AAA1 true"
248253
}, {
249254
"label": "tLocalVar",
250255
"kind": 12,

analysis/tests/src/expected/CompletionJsxProps.res.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Completable: Cexpression CJsxPropValue [CompletionSupport, TestComponent] test=T
3838
"tags": [],
3939
"detail": "Two\n\ntype testVariant = One | Two | Three(int)",
4040
"documentation": null,
41+
"sortText": "AAA1 Two",
4142
"insertText": "Two",
4243
"insertTextFormat": 2
4344
}, {
@@ -46,6 +47,7 @@ Completable: Cexpression CJsxPropValue [CompletionSupport, TestComponent] test=T
4647
"tags": [],
4748
"detail": "Three(int)\n\ntype testVariant = One | Two | Three(int)",
4849
"documentation": null,
50+
"sortText": "AAA2 Three(_)",
4951
"insertText": "Three(${1:_})",
5052
"insertTextFormat": 2
5153
}, {

0 commit comments

Comments
 (0)