Skip to content

Commit 6699287

Browse files
committed
complete for builtin types, and enable completion for types when coercing
1 parent 59855be commit 6699287

File tree

6 files changed

+153
-15
lines changed

6 files changed

+153
-15
lines changed

.vscode/settings.json

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"editor.insertSpaces": false,
3-
"tslint.enable": true,
4-
"typescript.tsc.autoDetect": "off",
5-
"typescript.preferences.quoteStyle": "single",
6-
"editor.codeActionsOnSave": {
7-
"source.fixAll.eslint": true
8-
},
9-
"ocaml.sandbox": {
10-
"kind": "opam",
11-
"switch": "${workspaceFolder:rescript-vscode}/analysis"
12-
}
13-
}
2+
"editor.insertSpaces": false,
3+
"tslint.enable": true,
4+
"typescript.tsc.autoDetect": "off",
5+
"typescript.preferences.quoteStyle": "single",
6+
"editor.codeActionsOnSave": {
7+
"source.fixAll.eslint": true
8+
},
9+
"ocaml.sandbox": {
10+
"kind": "opam",
11+
"switch": "4.14.0"
12+
}
13+
}

analysis/src/CompletionBackEnd.ml

+23-1
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,26 @@ let getItemsFromOpens ~opens ~localTables ~prefix ~exact ~completionContext =
373373
completionsFromThisOpen @ results)
374374
[]
375375

376+
let builtinTypes =
377+
[
378+
("string", "string");
379+
("int", "int");
380+
("float", "float");
381+
("bool", "bool");
382+
("option", "option<${1:_}>");
383+
("array", "array<${1:_}>");
384+
("promise", "promise<${1:_}>");
385+
("result", "result<${1:_}, ${2:_}>");
386+
]
387+
388+
let getTypesFromBuiltins ~env ~prefix ~exact =
389+
builtinTypes
390+
|> List.filter (fun (typeName, _) ->
391+
if exact then typeName == prefix else Utils.startsWith typeName prefix)
392+
|> List.map (fun (typeName, template) ->
393+
Completion.createWithSnippet ~name:typeName ~insertText:template ~env
394+
~kind:(Completion.Label typeName) ())
395+
376396
let findLocalCompletionsForValuesAndConstructors ~(localTables : LocalTables.t)
377397
~env ~prefix ~exact ~opens ~scope =
378398
localTables |> LocalTables.populateValues ~env;
@@ -442,14 +462,15 @@ let findLocalCompletionsForTypes ~(localTables : LocalTables.t) ~env ~prefix
442462
let valuesFromOpens =
443463
getItemsFromOpens ~opens ~localTables ~prefix ~exact ~completionContext:Type
444464
in
465+
let typesFromBuiltins = getTypesFromBuiltins ~env ~prefix ~exact in
445466

446467
scope
447468
|> Scope.iterTypesAfterFirstOpen
448469
(processLocalType ~prefix ~exact ~env ~localTables);
449470
scope
450471
|> Scope.iterModulesAfterFirstOpen
451472
(processLocalModule ~prefix ~exact ~env ~localTables);
452-
List.rev_append localTables.resultRev valuesFromOpens
473+
List.rev_append localTables.resultRev (valuesFromOpens @ typesFromBuiltins)
453474

454475
let findLocalCompletionsForModules ~(localTables : LocalTables.t) ~env ~prefix
455476
~exact ~opens ~scope =
@@ -745,6 +766,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
745766
| Some (Tpromise (env, typ), _env) ->
746767
[Completion.create "dummy" ~env ~kind:(Completion.Value typ)]
747768
| _ -> [])
769+
| CPId ([], Type) -> getTypesFromBuiltins ~env ~prefix:"" ~exact
748770
| CPId (path, completionContext) ->
749771
path
750772
|> getCompletionsForPath ~debug ~package ~opens ~full ~pos ~exact

analysis/src/CompletionFrontEnd.ml

+4
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,10 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
819819
in
820820
typedCompletionExpr expr;
821821
match expr.pexp_desc with
822+
| Pexp_coerce (_e, _, typ)
823+
when expr.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor
824+
&& Utils.isTypeHole typ ->
825+
setResult (Cpath (CPId ([], Type)))
822826
| Pexp_match (expr, cases) when cases <> [] ->
823827
let ctxPath = exprToContextPath expr in
824828
let oldCtxPath = !currentCtxPath in

analysis/src/Utils.ml

+5
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,8 @@ let rec lastElements list =
216216
let lowercaseFirstChar s =
217217
if String.length s = 0 then s
218218
else String.mapi (fun i c -> if i = 0 then Char.lowercase_ascii c else c) s
219+
220+
let isTypeHole typ =
221+
match typ.Parsetree.ptyp_desc with
222+
| Ptyp_extension ({txt = "rescript.typehole"}, _) -> true
223+
| _ -> false

analysis/tests/src/Completion.res

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

463463
// let f: withUncurried = {fn: }
464464
// ^com
465+
466+
// let a = (fff :> )
467+
// ^com
468+
469+
// let a = (fff :> boo)
470+
// ^com

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

+103-2
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,14 @@ Path s
13231323
"tags": [],
13241324
"detail": "type someLocalVariant = SomeLocalVariantItem",
13251325
"documentation": null
1326+
}, {
1327+
"label": "string",
1328+
"kind": 4,
1329+
"tags": [],
1330+
"detail": "string",
1331+
"documentation": null,
1332+
"insertText": "string",
1333+
"insertTextFormat": 2
13261334
}]
13271335

13281336
Complete src/Completion.res 291:30
@@ -1950,8 +1958,8 @@ Path red
19501958
}]
19511959

19521960
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])
1961+
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:11->471:0]
1962+
Pexp_apply ...__ghost__[0:-1->0:-1] (...[405:11->423:17], ...[428:0->471:0])
19551963
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:11->423:17]
19561964
Pexp_apply ...__ghost__[0:-1->0:-1] (...[405:11->405:19], ...[405:21->423:17])
19571965
posCursor:[405:22] posNoWhite:[405:21] Found expr:[405:21->423:17]
@@ -2206,3 +2214,96 @@ Path withUncurried
22062214
"insertTextFormat": 2
22072215
}]
22082216

2217+
Complete src/Completion.res 465:19
2218+
XXX Not found!
2219+
Completable: Cpath Type[]
2220+
Raw opens: 2 Shadow.B.place holder ... Shadow.A.place holder
2221+
Package opens Pervasives.JsxModules.place holder
2222+
Resolved opens 3 Completion.res Completion.res pervasives
2223+
ContextPath Type[]
2224+
[{
2225+
"label": "string",
2226+
"kind": 4,
2227+
"tags": [],
2228+
"detail": "string",
2229+
"documentation": null,
2230+
"insertText": "string",
2231+
"insertTextFormat": 2
2232+
}, {
2233+
"label": "int",
2234+
"kind": 4,
2235+
"tags": [],
2236+
"detail": "int",
2237+
"documentation": null,
2238+
"insertText": "int",
2239+
"insertTextFormat": 2
2240+
}, {
2241+
"label": "float",
2242+
"kind": 4,
2243+
"tags": [],
2244+
"detail": "float",
2245+
"documentation": null,
2246+
"insertText": "float",
2247+
"insertTextFormat": 2
2248+
}, {
2249+
"label": "bool",
2250+
"kind": 4,
2251+
"tags": [],
2252+
"detail": "bool",
2253+
"documentation": null,
2254+
"insertText": "bool",
2255+
"insertTextFormat": 2
2256+
}, {
2257+
"label": "option",
2258+
"kind": 4,
2259+
"tags": [],
2260+
"detail": "option",
2261+
"documentation": null,
2262+
"insertText": "option<${1:_}>",
2263+
"insertTextFormat": 2
2264+
}, {
2265+
"label": "array",
2266+
"kind": 4,
2267+
"tags": [],
2268+
"detail": "array",
2269+
"documentation": null,
2270+
"insertText": "array<${1:_}>",
2271+
"insertTextFormat": 2
2272+
}, {
2273+
"label": "promise",
2274+
"kind": 4,
2275+
"tags": [],
2276+
"detail": "promise",
2277+
"documentation": null,
2278+
"insertText": "promise<${1:_}>",
2279+
"insertTextFormat": 2
2280+
}, {
2281+
"label": "result",
2282+
"kind": 4,
2283+
"tags": [],
2284+
"detail": "result",
2285+
"documentation": null,
2286+
"insertText": "result<${1:_}, ${2:_}>",
2287+
"insertTextFormat": 2
2288+
}]
2289+
2290+
Complete src/Completion.res 468:22
2291+
posCursor:[468:22] posNoWhite:[468:21] Found expr:[468:12->468:22]
2292+
posCursor:[468:22] posNoWhite:[468:21] Found type:[468:19->468:22]
2293+
Ptyp_constr boo:[468:19->468:22]
2294+
Completable: Cpath Type[boo]
2295+
Raw opens: 2 Shadow.B.place holder ... Shadow.A.place holder
2296+
Package opens Pervasives.JsxModules.place holder
2297+
Resolved opens 3 Completion.res Completion.res pervasives
2298+
ContextPath Type[boo]
2299+
Path boo
2300+
[{
2301+
"label": "bool",
2302+
"kind": 4,
2303+
"tags": [],
2304+
"detail": "bool",
2305+
"documentation": null,
2306+
"insertText": "bool",
2307+
"insertTextFormat": 2
2308+
}]
2309+

0 commit comments

Comments
 (0)