diff --git a/analysis/src/CompletionBackEnd.ml b/analysis/src/CompletionBackEnd.ml index d9f9c04fa..dada1028a 100644 --- a/analysis/src/CompletionBackEnd.ml +++ b/analysis/src/CompletionBackEnd.ml @@ -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 = @@ -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 @@ -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" diff --git a/analysis/tests/src/CompletionExpressions.res b/analysis/tests/src/CompletionExpressions.res index 605813e1d..2bd5390cd 100644 --- a/analysis/tests/src/CompletionExpressions.res +++ b/analysis/tests/src/CompletionExpressions.res @@ -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 +} diff --git a/analysis/tests/src/expected/CompletionExpressions.res.txt b/analysis/tests/src/expected/CompletionExpressions.res.txt index ae108ccd3..d6217d792 100644 --- a/analysis/tests/src/expected/CompletionExpressions.res.txt +++ b/analysis/tests/src/expected/CompletionExpressions.res.txt @@ -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 + }] +