Skip to content

fall back to local completions when expr completion misses #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1410,17 +1410,27 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
fallbackOrEmpty ~items ())
| None -> fallbackOrEmpty ())
| Cexpression {contextPath; prefix; nested} -> (
(* Completions for local things like variables in scope, modules in the
project, etc. We only add completions when there's a prefix of some sort
we can filter on, since we know we're in some sort of context, and
therefore don't want to overwhelm the user with completion items. *)
let regularCompletions =
if prefix = "" then []
else
prefix
|> getComplementaryCompletionsForTypedValue ~opens ~allFiles ~env ~scope
in
match
contextPath
|> getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
~exact:true ~scope
|> completionsGetCompletionType ~full
with
| None -> []
| None -> regularCompletions
| Some (typ, env) -> (
match typ |> TypeUtils.resolveNested ~env ~full ~nested with
| None -> []
| Some (typ, env, completionContext) -> (
| None -> regularCompletions
| Some (typ, _env, completionContext) -> (
(* Wrap the insert text in braces when we're completing the root of a
JSX prop value. *)
let wrapInsertTextInBraces =
Expand Down Expand Up @@ -1448,12 +1458,6 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
match (prefix, completionContext) with
| "", _ -> items
| _, None ->
(* Completions for local things like variables in scope, modules in the project, etc. *)
let regularCompletions =
prefix
|> getComplementaryCompletionsForTypedValue ~opens ~allFiles ~env
~scope
in
let items =
if List.length regularCompletions > 0 then
(* The client will occasionally sort the list of completions alphabetically, disregarding the order
Expand Down Expand Up @@ -1538,8 +1542,8 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
if Utils.startsWith elementName prefix then
let name = "<" ^ elementName ^ ">" in
Some
(Completion.create name ~kind:(Label name) ~detail:description ~env
~docstring:[description] ~insertText:elementName
(Completion.create name ~kind:(Label name) ~detail:description
~env ~docstring:[description] ~insertText:elementName
?deprecated:
(match deprecated with
| true -> Some "true"
Expand Down
9 changes: 9 additions & 0 deletions analysis/tests/src/CompletionExpressions.res
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,12 @@ let fnTakingCallback = (

// fnTakingCallback(a, b, c, d, e, )
// ^com

let something = {
let second = true
let second2 = 1
ignore(second)
ignore(second2)
Js.log(s)
// ^com
}
34 changes: 34 additions & 0 deletions analysis/tests/src/expected/CompletionExpressions.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -713,3 +713,37 @@ Completable: Cexpression CArgument Value[fnTakingCallback]($5)
"insertTextFormat": 2
}]

Complete src/CompletionExpressions.res 185:10
posCursor:[185:10] posNoWhite:[185:9] Found expr:[181:2->185:11]
posCursor:[185:10] posNoWhite:[185:9] Found expr:[182:2->185:11]
posCursor:[185:10] posNoWhite:[185:9] Found expr:[183:2->185:11]
posCursor:[185:10] posNoWhite:[185:9] Found expr:[184:2->185:11]
posCursor:[185:10] posNoWhite:[185:9] Found expr:[185:2->185:11]
Pexp_apply ...[185:2->185:8] (...[185:9->185:10])
Completable: Cexpression CArgument Value[Js, log]($0)=s
[{
"label": "second2",
"kind": 12,
"tags": [],
"detail": "int",
"documentation": null
}, {
"label": "second",
"kind": 12,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "someBoolVar",
"kind": 12,
"tags": [],
"detail": "bool",
"documentation": null
}, {
"label": "s",
"kind": 12,
"tags": [],
"detail": "bool",
"documentation": null
}]