Skip to content

Commit cdd3983

Browse files
authored
Include fields when completing for identifiers in a braced expr that is likely a module path (#882)
* include fields when completing for identifiers in a braced expr that is likely a module path * changelog
1 parent 9868197 commit cdd3983

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
## master
1414

1515
- Better error recovery when analysis fails. https://github.com/rescript-lang/rescript-vscode/pull/880
16+
- Expand type aliases in hovers. https://github.com/rescript-lang/rescript-vscode/pull/881
17+
- 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
1618

1719
## 1.32.0
1820

19-
- Expand type aliases in hovers. https://github.com/rescript-lang/rescript-vscode/pull/881
20-
2121
#### :bug: Bug Fix
2222

2323
- Fix so that you don't need a leading `#` to complete for polyvariant constructors. https://github.com/rescript-lang/rescript-vscode/pull/874

analysis/src/CompletionBackEnd.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ let findAllCompletions ~(env : QueryEnv.t) ~prefix ~exact ~namesUsed
262262
| Field ->
263263
completionForExportedFields ~env ~prefix ~exact ~namesUsed
264264
@ completionForExportedModules ~env ~prefix ~exact ~namesUsed
265+
| ValueOrField ->
266+
completionForExportedValues ~env ~prefix ~exact ~namesUsed
267+
@ completionForExportedFields ~env ~prefix ~exact ~namesUsed
268+
@ completionForExportedModules ~env ~prefix ~exact ~namesUsed
265269

266270
let processLocalValue name loc contextPath scope ~prefix ~exact ~env
267271
~(localTables : LocalTables.t) =
@@ -476,7 +480,7 @@ let findLocalCompletionsWithOpens ~pos ~(env : QueryEnv.t) ~prefix ~exact ~opens
476480
^ Pos.toString pos);
477481
let localTables = LocalTables.create () in
478482
match completionContext with
479-
| Value ->
483+
| Value | ValueOrField ->
480484
findLocalCompletionsForValuesAndConstructors ~localTables ~env ~prefix
481485
~exact ~opens ~scope
482486
| Type ->

analysis/src/CompletionFrontEnd.ml

+17-1
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,23 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
866866
(lidPath |> String.concat ".")
867867
(Loc.toString lid.loc);
868868
if lid.loc |> Loc.hasPos ~pos:posBeforeCursor then
869-
setResult (Cpath (CPId (lidPath, Value)))
869+
let isLikelyModulePath =
870+
match lidPath with
871+
| head :: _
872+
when String.length head > 0
873+
&& head.[0] == Char.uppercase_ascii head.[0] ->
874+
true
875+
| _ -> false
876+
in
877+
setResult
878+
(Cpath
879+
(CPId
880+
( lidPath,
881+
if
882+
isLikelyModulePath
883+
&& expr |> Res_parsetree_viewer.isBracedExpr
884+
then ValueOrField
885+
else Value )))
870886
| Pexp_construct (lid, eOpt) ->
871887
let lidPath = flattenLidCheckDot lid in
872888
if debug && lid.txt <> Lident "Function$" then

analysis/src/SharedTypes.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ let _ = locItemToString
554554

555555
module Completable = struct
556556
(* Completion context *)
557-
type completionContext = Type | Value | Module | Field
557+
type completionContext = Type | Value | Module | Field | ValueOrField
558558

559559
type argumentLabel =
560560
| Unlabelled of {argumentPosition: int}
@@ -651,6 +651,7 @@ module Completable = struct
651651
| Type -> "Type"
652652
| Module -> "Module"
653653
| Field -> "Field"
654+
| ValueOrField -> "ValueOrField"
654655

655656
let rec contextPathToString = function
656657
| CPString -> "string"

analysis/tests/src/Completion.res

+3
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,6 @@ type withUncurried = {
462462

463463
// let f: withUncurried = {fn: }
464464
// ^com
465+
466+
// let someRecord = { FAR. }
467+
// ^com

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

+31-2
Original file line numberDiff line numberDiff line change
@@ -1950,8 +1950,8 @@ Path red
19501950
}]
19511951

19521952
Complete src/Completion.res 405:22
1953-
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:11->465:0]
1954-
Pexp_apply ...__ghost__[0:-1->0:-1] (...[405:11->423:17], ...[428:0->465:0])
1953+
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:11->467:0]
1954+
Pexp_apply ...__ghost__[0:-1->0:-1] (...[405:11->423:17], ...[428:0->467:0])
19551955
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:11->423:17]
19561956
Pexp_apply ...__ghost__[0:-1->0:-1] (...[405:11->405:19], ...[405:21->423:17])
19571957
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:21->423:17]
@@ -2206,3 +2206,32 @@ Path withUncurried
22062206
"insertTextFormat": 2
22072207
}]
22082208

2209+
Complete src/Completion.res 465:26
2210+
posCursor:[465:26] posNoWhite:[465:25] Found expr:[465:22->465:26]
2211+
Pexp_ident FAR.:[465:22->465:26]
2212+
Completable: Cpath ValueOrField[FAR, ""]
2213+
Raw opens: 2 Shadow.B.place holder ... Shadow.A.place holder
2214+
Package opens Pervasives.JsxModules.place holder
2215+
Resolved opens 3 pervasives Completion.res Completion.res
2216+
ContextPath ValueOrField[FAR, ""]
2217+
Path FAR.
2218+
[{
2219+
"label": "forAutoRecord",
2220+
"kind": 12,
2221+
"tags": [],
2222+
"detail": "forAutoRecord",
2223+
"documentation": null
2224+
}, {
2225+
"label": "forAuto",
2226+
"kind": 5,
2227+
"tags": [],
2228+
"detail": "forAuto: ForAuto.t\n\ntype forAutoRecord = {\n forAuto: ForAuto.t,\n something: option<int>,\n}",
2229+
"documentation": null
2230+
}, {
2231+
"label": "something",
2232+
"kind": 5,
2233+
"tags": [],
2234+
"detail": "something: option<int>\n\ntype forAutoRecord = {\n forAuto: ForAuto.t,\n something: option<int>,\n}",
2235+
"documentation": null
2236+
}]
2237+

0 commit comments

Comments
 (0)