Skip to content
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

Include fields when completing for identifiers in a braced expr that is likely a module path #882

Merged
merged 2 commits into from
Jan 7, 2024
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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
## master

- Better error recovery when analysis fails. https://github.com/rescript-lang/rescript-vscode/pull/880
- Expand type aliases in hovers. https://github.com/rescript-lang/rescript-vscode/pull/881
- Include fields when completing a braced expr that's an ID, where it the path likely starts with a module. https://github.com/rescript-lang/rescript-vscode/pull/882

## 1.32.0

- Expand type aliases in hovers. https://github.com/rescript-lang/rescript-vscode/pull/881

#### :bug: Bug Fix

- Fix so that you don't need a leading `#` to complete for polyvariant constructors. https://github.com/rescript-lang/rescript-vscode/pull/874
Expand Down
6 changes: 5 additions & 1 deletion analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ let findAllCompletions ~(env : QueryEnv.t) ~prefix ~exact ~namesUsed
| Field ->
completionForExportedFields ~env ~prefix ~exact ~namesUsed
@ completionForExportedModules ~env ~prefix ~exact ~namesUsed
| ValueOrField ->
completionForExportedValues ~env ~prefix ~exact ~namesUsed
@ completionForExportedFields ~env ~prefix ~exact ~namesUsed
@ completionForExportedModules ~env ~prefix ~exact ~namesUsed

let processLocalValue name loc contextPath scope ~prefix ~exact ~env
~(localTables : LocalTables.t) =
Expand Down Expand Up @@ -476,7 +480,7 @@ let findLocalCompletionsWithOpens ~pos ~(env : QueryEnv.t) ~prefix ~exact ~opens
^ Pos.toString pos);
let localTables = LocalTables.create () in
match completionContext with
| Value ->
| Value | ValueOrField ->
findLocalCompletionsForValuesAndConstructors ~localTables ~env ~prefix
~exact ~opens ~scope
| Type ->
Expand Down
18 changes: 17 additions & 1 deletion analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,23 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
(lidPath |> String.concat ".")
(Loc.toString lid.loc);
if lid.loc |> Loc.hasPos ~pos:posBeforeCursor then
setResult (Cpath (CPId (lidPath, Value)))
let isLikelyModulePath =
match lidPath with
| head :: _
when String.length head > 0
&& head.[0] == Char.uppercase_ascii head.[0] ->
true
| _ -> false
in
setResult
(Cpath
(CPId
( lidPath,
if
isLikelyModulePath
&& expr |> Res_parsetree_viewer.isBracedExpr
then ValueOrField
else Value )))
| Pexp_construct (lid, eOpt) ->
let lidPath = flattenLidCheckDot lid in
if debug && lid.txt <> Lident "Function$" then
Expand Down
3 changes: 2 additions & 1 deletion analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ let _ = locItemToString

module Completable = struct
(* Completion context *)
type completionContext = Type | Value | Module | Field
type completionContext = Type | Value | Module | Field | ValueOrField

type argumentLabel =
| Unlabelled of {argumentPosition: int}
Expand Down Expand Up @@ -651,6 +651,7 @@ module Completable = struct
| Type -> "Type"
| Module -> "Module"
| Field -> "Field"
| ValueOrField -> "ValueOrField"

let rec contextPathToString = function
| CPString -> "string"
Expand Down
3 changes: 3 additions & 0 deletions analysis/tests/src/Completion.res
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,6 @@ type withUncurried = {

// let f: withUncurried = {fn: }
// ^com

// let someRecord = { FAR. }
// ^com
33 changes: 31 additions & 2 deletions analysis/tests/src/expected/Completion.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1950,8 +1950,8 @@ Path red
}]

Complete src/Completion.res 405:22
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:11->465:0]
Pexp_apply ...__ghost__[0:-1->0:-1] (...[405:11->423:17], ...[428:0->465:0])
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:11->467:0]
Pexp_apply ...__ghost__[0:-1->0:-1] (...[405:11->423:17], ...[428:0->467:0])
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:11->423:17]
Pexp_apply ...__ghost__[0:-1->0:-1] (...[405:11->405:19], ...[405:21->423:17])
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:21->423:17]
Expand Down Expand Up @@ -2206,3 +2206,32 @@ Path withUncurried
"insertTextFormat": 2
}]

Complete src/Completion.res 465:26
posCursor:[465:26] posNoWhite:[465:25] Found expr:[465:22->465:26]
Pexp_ident FAR.:[465:22->465:26]
Completable: Cpath ValueOrField[FAR, ""]
Raw opens: 2 Shadow.B.place holder ... Shadow.A.place holder
Package opens Pervasives.JsxModules.place holder
Resolved opens 3 pervasives Completion.res Completion.res
ContextPath ValueOrField[FAR, ""]
Path FAR.
[{
"label": "forAutoRecord",
"kind": 12,
"tags": [],
"detail": "forAutoRecord",
"documentation": null
}, {
"label": "forAuto",
"kind": 5,
"tags": [],
"detail": "forAuto: ForAuto.t\n\ntype forAutoRecord = {\n forAuto: ForAuto.t,\n something: option<int>,\n}",
"documentation": null
}, {
"label": "something",
"kind": 5,
"tags": [],
"detail": "something: option<int>\n\ntype forAutoRecord = {\n forAuto: ForAuto.t,\n something: option<int>,\n}",
"documentation": null
}]