Skip to content

Commit f38c043

Browse files
committed
Generalize labelled argument completion, and remove dead code.
1 parent a21a1b4 commit f38c043

File tree

5 files changed

+55
-46
lines changed

5 files changed

+55
-46
lines changed

analysis/src/Commands.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ let completion ~debug ~path ~pos ~currentFile =
2020
| Some full ->
2121
let env = SharedTypes.QueryEnv.fromFile full.file in
2222
let package = full.package in
23-
NewCompletions.computeCompletions ~completable ~package ~pos ~scope
24-
~env)
23+
completable
24+
|> NewCompletions.processCompletable ~debug ~package ~pos ~scope
25+
~env)
2526
in
2627
completionItems
2728
in

analysis/src/Completion.ml

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,6 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor ~posAfterCompName
7575
in
7676
loop jsxProps.props
7777

78-
let rec skipLineComment ~pos ~i str =
79-
if i < String.length str then
80-
match str.[i] with
81-
| '\n' -> Some ((fst pos + 1, 0), i + 1)
82-
| _ -> skipLineComment ~pos:(fst pos, snd pos + 1) ~i:(i + 1) str
83-
else None
84-
85-
let rec skipComment ~pos ~i ~depth str =
86-
if i < String.length str then
87-
match str.[i] with
88-
| '\n' -> skipComment ~depth ~pos:(fst pos + 1, 0) ~i:(i + 1) str
89-
| '/' when i + 1 < String.length str && str.[i + 1] = '*' ->
90-
skipComment ~depth:(depth + 1) ~pos:(fst pos, snd pos + 2) ~i:(i + 2) str
91-
| '*' when i + 1 < String.length str && str.[i + 1] = '/' ->
92-
if depth > 1 then
93-
skipComment ~depth:(depth - 1)
94-
~pos:(fst pos, snd pos + 2)
95-
~i:(i + 2) str
96-
else Some ((fst pos, snd pos + 2), i + 2)
97-
| _ -> skipComment ~depth ~pos:(fst pos, snd pos + 1) ~i:(i + 1) str
98-
else None
99-
10078
let extractJsxProps ~(compName : Longident.t Location.loc) ~args =
10179
let thisCaseShouldNotHappen =
10280
{
@@ -147,9 +125,7 @@ type label = labelled option
147125
type arg = {label : label; exp : Parsetree.expression}
148126

149127
let findExpApplyCompletable ~(args : arg list) ~endPos ~posBeforeCursor
150-
~(funName : Longident.t Location.loc) =
151-
let funPath = Utils.flattenLongIdent funName.txt in
152-
let posAfterFunName = Loc.end_ funName.loc in
128+
~(contextPath : Completable.contextPath) ~posAfterFunExpr =
153129
let allNames =
154130
List.fold_right
155131
(fun arg allLabels ->
@@ -164,18 +140,15 @@ let findExpApplyCompletable ~(args : arg list) ~endPos ~posBeforeCursor
164140
if
165141
labelled.posStart <= posBeforeCursor
166142
&& posBeforeCursor < labelled.posEnd
167-
then
168-
Some
169-
(Completable.CnamedArg
170-
(Completable.CPId (funPath, Value), labelled.name, allNames))
143+
then Some (Completable.CnamedArg (contextPath, labelled.name, allNames))
171144
else if exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then None
172145
else loop rest
173146
| {label = None; exp} :: rest ->
174147
if exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then None
175148
else loop rest
176149
| [] ->
177-
if posAfterFunName <= posBeforeCursor && posBeforeCursor < endPos then
178-
Some (CnamedArg (Completable.CPId (funPath, Value), "", allNames))
150+
if posAfterFunExpr <= posBeforeCursor && posBeforeCursor < endPos then
151+
Some (CnamedArg (contextPath, "", allNames))
179152
else None
180153
in
181154
loop args
@@ -574,10 +547,11 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
574547
setPipeResult ~lhs ~id:"" |> ignore
575548
| Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident "|."}}, [_; _]) ->
576549
()
577-
| Pexp_apply ({pexp_desc = Pexp_ident funName}, args) ->
550+
| Pexp_apply (funExpr, args) ->
578551
let args = extractExpApplyArgs ~args in
579552
if debug then
580-
Printf.printf "Pexp_apply ...%s (%s)\n" (Loc.toString funName.loc)
553+
Printf.printf "Pexp_apply ...%s (%s)\n"
554+
(Loc.toString funExpr.pexp_loc)
581555
(args
582556
|> List.map (fun {label; exp} ->
583557
Printf.sprintf "%s...%s"
@@ -590,9 +564,14 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
590564
(Loc.toString exp.pexp_loc))
591565
|> String.concat ", ");
592566
let expApplyCompletable =
593-
findExpApplyCompletable ~funName ~args
594-
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
567+
match exprToContextPath funExpr with
568+
| Some contextPath ->
569+
findExpApplyCompletable ~contextPath ~args
570+
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
571+
~posAfterFunExpr:(Loc.end_ funExpr.pexp_loc)
572+
| None -> None
595573
in
574+
596575
setResultOpt expApplyCompletable
597576
| Pexp_send (lhs, {txt; loc}) -> (
598577
(* e["txt"]

analysis/src/NewCompletions.ml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
10761076
| Tfield (name, _, t1, t2) ->
10771077
let fields = t2 |> getFields in
10781078
(name, t1) :: fields
1079-
| Tlink te -> te |> getFields
1079+
| Tlink te | Tsubst te | Tpoly (te, []) -> te |> getFields
10801080
| Tvar None -> []
10811081
| _ -> []
10821082
in
@@ -1119,6 +1119,7 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
11191119
match typ.Types.desc with
11201120
| Tconstr (path, _, _)
11211121
| Tlink {desc = Tconstr (path, _, _)}
1122+
| Tsubst {desc = Tconstr (path, _, _)}
11221123
| Tpoly ({desc = Tconstr (path, _, _)}, []) ->
11231124
Some path
11241125
| _ -> None
@@ -1204,7 +1205,8 @@ let getOpens ~rawOpens ~package ~env =
12041205
(* Last open takes priority *)
12051206
List.rev resolvedOpens
12061207

1207-
let processCompletable ~package ~scope ~env ~pos (completable : Completable.t) =
1208+
let processCompletable ~debug ~package ~scope ~env ~pos
1209+
(completable : Completable.t) =
12081210
let rawOpens = Scope.getRawOpens scope in
12091211
let opens = getOpens ~rawOpens ~package ~env in
12101212
let allFiles = FileSet.union package.projectFiles package.dependenciesFiles in
@@ -1245,13 +1247,13 @@ let processCompletable ~package ~scope ~env ~pos (completable : Completable.t) =
12451247
| Tfield (name, _, t1, t2) ->
12461248
let fields = t2 |> getFields in
12471249
if name = "children" then fields else (name, t1) :: fields
1248-
| Tlink te -> te |> getFields
1250+
| Tlink te | Tsubst te | Tpoly (te, []) -> te |> getFields
12491251
| Tvar None -> []
12501252
| _ -> []
12511253
in
12521254
let rec getLabels (t : Types.type_expr) =
12531255
match t.desc with
1254-
| Tlink t1 | Tsubst t1 -> getLabels t1
1256+
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> getLabels t1
12551257
| Tarrow
12561258
( Nolabel,
12571259
{
@@ -1347,9 +1349,12 @@ let processCompletable ~package ~scope ~env ~pos (completable : Completable.t) =
13471349
|> completionsGetTypeEnv
13481350
with
13491351
| Some (typ, _env) ->
1352+
if debug then
1353+
Printf.printf "Found type for function %s\n"
1354+
(typ |> Shared.typeToString);
13501355
let rec getLabels (t : Types.type_expr) =
13511356
match t.desc with
1352-
| Tlink t1 | Tsubst t1 -> getLabels t1
1357+
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> getLabels t1
13531358
| Tarrow ((Labelled l | Optional l), tArg, tRet, _) ->
13541359
(l, tArg) :: getLabels tRet
13551360
| Tarrow (Nolabel, _, tRet, _) -> getLabels tRet
@@ -1367,7 +1372,3 @@ let processCompletable ~package ~scope ~env ~pos (completable : Completable.t) =
13671372
|> List.filter (fun (name, _t) ->
13681373
Utils.startsWith name prefix && not (List.mem name identsSeen))
13691374
|> List.map mkLabel
1370-
1371-
let computeCompletions ~(completable : Completable.t) ~package ~pos ~scope ~env
1372-
=
1373-
completable |> processCompletable ~package ~scope ~env ~pos

analysis/tests/src/Completion.res

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,13 @@ let _foo = _world => {
281281
type someType = {hello: string}
282282
// type t = SomeType(s)
283283
// ^com
284+
285+
type funRecord = {
286+
someFun: (~name: string) => unit,
287+
stuff: string,
288+
}
289+
290+
let funRecord: funRecord = assert false
291+
292+
// let _ = funRecord.someFun(~ )
293+
// ^com

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ Complete tests/src/Completion.res 23:20
362362
posCursor:[23:20] posNoWhite:[23:19] Found expr:[23:11->23:20]
363363
Pexp_apply ...[23:11->23:18] ()
364364
Completable: CnamedArg(Value[Lib, foo], "", [])
365+
Found type for function (~age: int, ~name: string) => string
365366
[{
366367
"label": "age",
367368
"kind": 4,
@@ -513,6 +514,7 @@ Complete tests/src/Completion.res 71:27
513514
posCursor:[71:27] posNoWhite:[71:26] Found expr:[71:11->71:27]
514515
Pexp_apply ...[71:11->71:18] (~name71:20->71:24=...[71:20->71:24])
515516
Completable: CnamedArg(Value[Lib, foo], "", [name])
517+
Found type for function (~age: int, ~name: string) => string
516518
[{
517519
"label": "age",
518520
"kind": 4,
@@ -525,6 +527,7 @@ Complete tests/src/Completion.res 74:26
525527
posCursor:[74:26] posNoWhite:[74:25] Found expr:[74:11->74:26]
526528
Pexp_apply ...[74:11->74:18] (~age74:20->74:23=...[74:20->74:23])
527529
Completable: CnamedArg(Value[Lib, foo], "", [age])
530+
Found type for function (~age: int, ~name: string) => string
528531
[{
529532
"label": "name",
530533
"kind": 4,
@@ -537,6 +540,7 @@ Complete tests/src/Completion.res 77:32
537540
posCursor:[77:32] posNoWhite:[77:31] Found expr:[77:11->77:32]
538541
Pexp_apply ...[77:11->77:18] (~age77:20->77:23=...[77:25->77:28])
539542
Completable: CnamedArg(Value[Lib, foo], "", [age])
543+
Found type for function (~age: int, ~name: string) => string
540544
[{
541545
"label": "name",
542546
"kind": 4,
@@ -549,6 +553,7 @@ Complete tests/src/Completion.res 82:5
549553
posCursor:[82:5] posNoWhite:[82:4] Found expr:[80:8->86:1]
550554
Pexp_apply ...[80:8->80:15] (~age84:3->84:6=...[84:7->84:8], ~name85:3->85:7=...[85:8->85:10])
551555
Completable: CnamedArg(Value[Lib, foo], "", [age, name])
556+
Found type for function (~age: int, ~name: string) => string
552557
[]
553558

554559
Complete tests/src/Completion.res 90:13
@@ -1068,3 +1073,16 @@ Completable: Cpath Type[s]
10681073
"documentation": null
10691074
}]
10701075

1076+
Complete tests/src/Completion.res 291:30
1077+
posCursor:[291:30] posNoWhite:[291:29] Found expr:[291:11->291:32]
1078+
Pexp_apply ...[291:11->291:28] ()
1079+
Completable: CnamedArg(Value[funRecord].someFun, "", [])
1080+
Found type for function (~name: string) => unit
1081+
[{
1082+
"label": "name",
1083+
"kind": 4,
1084+
"tags": [],
1085+
"detail": "string",
1086+
"documentation": null
1087+
}]
1088+

0 commit comments

Comments
 (0)