Skip to content

Commit 072a943

Browse files
committed
Support autocomplete of records for variables defined in other files.
Fixes #321
1 parent 7ab2d23 commit 072a943

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Fix issue with JSX autocompletion not working after `foo=#variant`.
88
- Fix issue in JSX autocompletion where the `key` label would always appear.
99
- Fix issue in record field autocomplete not working with type aliases.
10+
- Support autocomplete of records for variables defined in other files.
1011

1112
## 1.1.3
1213

analysis/src/NewCompletions.ml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -557,23 +557,28 @@ let isCapitalized name =
557557

558558
type completion =
559559
| AbsAttribute of string list
560-
| Attribute of string list * string
560+
| Attribute of string list * string list * string
561561
| Normal of string list
562562

563563
let determineCompletion dotpath =
564564
let rec loop dotpath =
565565
match dotpath with
566566
| [] -> assert false
567567
| [one] -> Normal [one]
568-
| [one; two] when not (isCapitalized one) -> Attribute ([one], two)
568+
| [one; two] when not (isCapitalized one) -> Attribute ([one], [], two)
569569
| [one; two] -> Normal [one; two]
570570
| one :: rest -> (
571571
if isCapitalized one then
572-
match loop rest with Normal path -> Normal (one :: path) | x -> x
572+
match loop rest with
573+
| Normal path -> Normal (one :: path)
574+
| Attribute (firstPath, rest, suffix) ->
575+
Attribute (one :: firstPath, rest, suffix)
576+
| AbsAttribute _ as x -> x
573577
else
574578
match loop rest with
575579
| Normal path -> AbsAttribute path
576-
| Attribute (path, suffix) -> Attribute (one :: path, suffix)
580+
| Attribute ([first], path, suffix) ->
581+
Attribute ([one], first :: path, suffix)
577582
| x -> x)
578583
in
579584
loop dotpath
@@ -585,7 +590,7 @@ let determineCompletion dotpath =
585590
locally defined things...
586591
*)
587592
let getEnvWithOpens ~pos ~(env : QueryEnv.t) ~package ~(opens : QueryEnv.t list)
588-
path =
593+
(path : string list) =
589594
match ProcessCmt.resolveFromStamps ~env ~path ~package ~pos with
590595
| Some x -> Some x
591596
| None ->
@@ -817,13 +822,13 @@ let getItems ~full ~rawOpens ~allFiles ~pos ~dotpath =
817822
Log.log "Got the env";
818823
valueCompletions ~env suffix
819824
| None -> [])
820-
| Attribute (target, suffix) -> (
825+
| Attribute (firstPath, rest, suffix) -> (
821826
Log.log ("suffix :" ^ suffix);
822-
match target with
823-
| [] -> []
824-
| first :: rest -> (
825-
Log.log ("-------------- Looking for " ^ first);
826-
match ProcessCmt.findInScope pos first env.file.stamps.values with
827+
Log.log
828+
("-------------- Looking for " ^ (firstPath |> SharedTypes.pathToString));
829+
match getEnvWithOpens ~pos ~env ~package ~opens firstPath with
830+
| Some (env, name) -> (
831+
match ProcessCmt.findInScope pos name env.file.stamps.values with
827832
| None -> []
828833
| Some declared -> (
829834
Log.log ("Found it! " ^ declared.name.txt);
@@ -856,7 +861,8 @@ let getItems ~full ~rawOpens ~allFiles ~pos ~dotpath =
856861
(emptyDeclared f.fname.txt) with
857862
item = Field (f, typ);
858863
}
859-
else None)))))
864+
else None))))
865+
| None -> [])
860866
| AbsAttribute path -> (
861867
match getEnvWithOpens ~pos ~env ~package ~opens path with
862868
| None -> []

analysis/tests/src/Completion.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,5 @@ type r = {x:int, y:string}
8888
type rAlias = r
8989
let r:rAlias = assert false
9090
// ^com r.
91+
92+
// ^com Obj.Rec.recordVal.

analysis/tests/src/Obj.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
type objT = {"name": string, "age": int}
22

33
type nestedObjT = {"y": objT}
4+
5+
module Rec = {
6+
type recordt = {xx: int, ss: string}
7+
8+
let recordVal: recordt = assert false
9+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,3 +695,18 @@ Complete tests/src/Completion.res 88:3
695695
"documentation": null
696696
}]
697697

698+
Complete tests/src/Completion.res 90:3
699+
[{
700+
"label": "xx",
701+
"kind": 5,
702+
"tags": [],
703+
"detail": "xx: int\n\ntype recordt = {xx: int, ss: string}",
704+
"documentation": null
705+
}, {
706+
"label": "ss",
707+
"kind": 5,
708+
"tags": [],
709+
"detail": "ss: string\n\ntype recordt = {xx: int, ss: string}",
710+
"documentation": null
711+
}]
712+

0 commit comments

Comments
 (0)