Skip to content

Commit 3a865b4

Browse files
committed
enhance variant constructor payload completion
1 parent 56a5f0c commit 3a865b4

18 files changed

+201
-31
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ analysis/tests/.bsb.lock
99
analysis/tests-generic-jsx-transform/lib
1010
analysis/tests-generic-jsx-transform/.bsb.lock
1111

12+
analysis/tests-incremental-typechecking/lib
13+
analysis/tests-incremental-typechecking/.bsb.lock
14+
1215
tools/node_modules
1316
tools/lib
1417
tools/**/*.res.js

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
- Extend signature help to work on constructor payloads as well. Can be turned off if wanted through settings. https://github.com/rescript-lang/rescript-vscode/pull/947
1818

19+
#### :nail_care: Polish
20+
21+
- Enhance variant constructor payload completion.
22+
1923
## 1.48.0
2024

2125
#### :bug: Bug Fix

analysis/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ build-tests:
66
build-tests-generic-jsx-transform:
77
make -C tests-generic-jsx-transform build
88

9+
build-tests-incremental-typechecking:
10+
make -C tests-generic-jsx-transform build
11+
912
build-reanalyze:
1013
make -C reanalyze build
1114

12-
build: build-reanalyze build-tests build-tests-generic-jsx-transform
15+
build: build-reanalyze build-tests build-tests-generic-jsx-transform build-tests-incremental-typechecking
1316

1417
dce: build-analysis-binary
1518
opam exec reanalyze.exe -- -dce-cmt _build -suppress vendor
1619

1720
test-analysis-binary:
1821
make -C tests test
19-
make -C tests-generic-jsx-transform test
22+
make -C tests-generic-jsx-transform test tests-incremental-typechecking
2023

2124
test-reanalyze:
2225
make -C reanalyze test

analysis/src/CompletionExpressions.ml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,37 @@ let prettyPrintFnTemplateArgName ?currentIndex ~env ~full
255255
(someName |> Utils.lowercaseFirstChar) ^ suffix
256256
| _ -> defaultVarName)
257257
| _ -> defaultVarName)
258+
259+
let completeConstructorPayload ~posBeforeCursor ~firstCharBeforeCursorNoWhite
260+
(constructorLid : Longident.t Location.loc) expr =
261+
match
262+
traverseExpr expr ~exprPath:[] ~pos:posBeforeCursor
263+
~firstCharBeforeCursorNoWhite
264+
with
265+
| None -> None
266+
| Some (prefix, nested) ->
267+
(* The nested path must start with the constructor name found, plus
268+
the target argument number for the constructor. We translate to
269+
that here, because we need to account for multi arg constructors
270+
being represented as tuples. *)
271+
let nested =
272+
match List.rev nested with
273+
| Completable.NTupleItem {itemNum} :: rest ->
274+
[
275+
Completable.NVariantPayload
276+
{constructorName = Longident.last constructorLid.txt; itemNum};
277+
]
278+
@ rest
279+
| nested ->
280+
[
281+
Completable.NVariantPayload
282+
{constructorName = Longident.last constructorLid.txt; itemNum = 0};
283+
]
284+
@ nested
285+
in
286+
let variantCtxPath =
287+
Completable.CTypeAtPos
288+
{constructorLid.loc with loc_start = constructorLid.loc.loc_end}
289+
in
290+
Some
291+
(Completable.Cexpression {contextPath = variantCtxPath; prefix; nested})

analysis/src/CompletionFrontEnd.ml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,9 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
10581058
&& expr |> Res_parsetree_viewer.isBracedExpr
10591059
then ValueOrField
10601060
else Value )))
1061-
| Pexp_construct (lid, eOpt) ->
1061+
| Pexp_construct ({txt = Lident ("::" | "()")}, _) ->
1062+
(* Ignore list expressions, used in JSX, unit, and more *) ()
1063+
| Pexp_construct (lid, eOpt) -> (
10621064
let lidPath = flattenLidCheckDot lid in
10631065
if debug && lid.txt <> Lident "Function$" then
10641066
Printf.printf "Pexp_construct %s:%s %s\n"
@@ -1071,6 +1073,19 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
10711073
eOpt = None && (not lid.loc.loc_ghost)
10721074
&& lid.loc |> Loc.hasPos ~pos:posBeforeCursor
10731075
then setResult (Cpath (CPId (lidPath, Value)))
1076+
else
1077+
match eOpt with
1078+
| Some e when locHasCursor e.pexp_loc -> (
1079+
match
1080+
CompletionExpressions.completeConstructorPayload
1081+
~posBeforeCursor ~firstCharBeforeCursorNoWhite lid e
1082+
with
1083+
| Some result ->
1084+
(* Check if anything else more important completes before setting this completion. *)
1085+
Ast_iterator.default_iterator.expr iterator e;
1086+
setResult result
1087+
| None -> ())
1088+
| _ -> ())
10741089
| Pexp_field (e, fieldName) -> (
10751090
if debug then
10761091
Printf.printf "Pexp_field %s %s:%s\n" (Loc.toString e.pexp_loc)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
SHELL = /bin/bash
2+
3+
node_modules/.bin/rescript:
4+
npm install
5+
6+
build:
7+
node_modules/.bin/rescript > /dev/null || true
8+
9+
test: build
10+
./test.sh
11+
12+
clean:
13+
rm -r node_modules lib
14+
15+
.DEFAULT_GOAL := test
16+
17+
.PHONY: clean test

analysis/tests-incremental-typechecking/package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"scripts": {
3+
"build": "rescript",
4+
"clean": "rescript clean -with-deps"
5+
},
6+
"private": true,
7+
"dependencies": {
8+
"rescript": "11.1.0-rc.2"
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "test-generic-jsx-transform",
3+
"sources": [
4+
{
5+
"dir": "src",
6+
"subdirs": true
7+
}
8+
],
9+
"bsc-flags": ["-w -33-44-8"],
10+
"jsx": { "module": "GenericJsx" }
11+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let x = Js.Json.Array()
2+
// ^com

0 commit comments

Comments
 (0)