Skip to content

Commit 155ab6e

Browse files
zthfhammerschmidt
andauthored
Global per-project configuration of autocomplete (#7330)
* sketch out global completion config setup * Update json schema * Read autocompleteConfig from rescript.json * update emitted output * test output * Remove hardcoded 'array' from config schema and document autocomplete * autocompleteConfig --> autocomplete --------- Co-authored-by: Florian Hammerschmidt <fh@cca.io>
1 parent c610a12 commit 155ab6e

18 files changed

+131
-2
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,25 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
11351135
| _ -> false
11361136
else true)
11371137
in
1138+
1139+
let globallyConfiguredCompletionsForType =
1140+
match package.autocomplete |> Misc.StringMap.find_opt mainTypeId with
1141+
| None -> []
1142+
| Some completionPaths ->
1143+
completionPaths |> List.map (fun p -> String.split_on_char '.' p)
1144+
in
1145+
1146+
let globallyConfiguredCompletions =
1147+
globallyConfiguredCompletionsForType
1148+
|> List.map (fun completionPath ->
1149+
completionsForPipeFromCompletionPath ~envCompletionIsMadeFrom
1150+
~opens ~pos ~scope ~debug ~prefix ~env ~rawOpens ~full
1151+
completionPath)
1152+
|> List.flatten
1153+
|> TypeUtils.filterPipeableFunctions ~synthetic:true ~env ~full
1154+
~targetTypeId:mainTypeId
1155+
in
1156+
11381157
(* Extra completions can be drawn from the @editor.completeFrom attribute. Here we
11391158
find and add those completions as well. *)
11401159
let extraCompletions =
@@ -1154,7 +1173,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
11541173
~full ~rawOpens typ
11551174
else []
11561175
in
1157-
jsxCompletions @ pipeCompletions @ extraCompletions))
1176+
jsxCompletions @ pipeCompletions @ extraCompletions
1177+
@ globallyConfiguredCompletions))
11581178
| CTuple ctxPaths ->
11591179
if Debug.verbose () then print_endline "[ctx_path]--> CTuple";
11601180
(* Turn a list of context paths into a list of type expressions. *)

analysis/src/Packages.ml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ let newBsPackage ~rootPath =
6666
| _ -> None)
6767
| None -> None
6868
in
69+
let autocomplete =
70+
match config |> Json.get "editor" with
71+
| Some editorConfig -> (
72+
match editorConfig |> Json.get "autocomplete" with
73+
| Some (Object map) ->
74+
map
75+
|> List.fold_left
76+
(fun acc (key, value) ->
77+
match value with
78+
| Json.Array items ->
79+
let values =
80+
items
81+
|> List.filter_map (function
82+
| Json.String s -> Some s
83+
| _ -> None)
84+
in
85+
Misc.StringMap.add key values acc
86+
| _ -> acc)
87+
Misc.StringMap.empty
88+
| _ -> Misc.StringMap.empty)
89+
| None -> Misc.StringMap.empty
90+
in
6991
let uncurried = uncurried = Some true in
7092
match libBs with
7193
| None -> None
@@ -158,6 +180,7 @@ let newBsPackage ~rootPath =
158180
opens;
159181
namespace;
160182
uncurried;
183+
autocomplete;
161184
}))
162185
| None -> None
163186
in

analysis/src/SharedTypes.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ type package = {
494494
opens: path list;
495495
uncurried: bool;
496496
rescriptVersion: int * int;
497+
autocomplete: file list Misc.StringMap.t;
497498
}
498499

499500
let allFilesInPackage package =

docs/docson/build-schema.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,24 @@
349349
"description": "(Not implemented yet)"
350350
}
351351
]
352+
},
353+
"editor": {
354+
"type": "object",
355+
"properties": {
356+
"autocomplete": {
357+
"type": "object",
358+
"description": "A mapping to extend the autocompletion for a given type with an array of modules. E.g. `{ \"int\": [\"IntUtils\", \"IntExt\"] }` will extend the autocompletion of the `int` type with the values from `IntUtils` and `IntExt` modules.",
359+
"patternProperties": {
360+
"^[a-zA-Z0-9_.]+$": {
361+
"type": "array",
362+
"items": {
363+
"type": "string"
364+
}
365+
}
366+
}
367+
}
368+
},
369+
"additionalProperties": false
352370
}
353371
},
354372
"title": "ReScript build configuration",
@@ -472,6 +490,10 @@
472490
"reanalyze": {
473491
"$ref": "#/definitions/reanalyze",
474492
"description": "Configure reanalyze, a static code analysis tool for ReScript."
493+
},
494+
"editor": {
495+
"$ref": "#/definitions/editor",
496+
"description": "Configure editor functionality, like modules that should be included in autocompletions for given (built-in) types."
475497
}
476498
},
477499
"additionalProperties": false,

tests/analysis_tests/tests/rescript.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,11 @@
1212
"bsc-flags": ["-w -33-44-8"],
1313
"bs-dependencies": ["@rescript/react"],
1414
"jsx": { "version": 4 },
15-
"suffix": ".res.js"
15+
"suffix": ".res.js",
16+
"editor": {
17+
"autocomplete": {
18+
"array": ["ArrayUtils"],
19+
"Fastify.t": ["FastifyExt"]
20+
}
21+
}
1622
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let empty = arr => Array.length(arr) === 0
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let x = [1, 2, 3]
2+
3+
// x->em
4+
// ^com
5+
6+
external fastify: Fastify.t = "fastify"
7+
8+
// fastify->doSt
9+
// ^com
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type t
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let doStuff = (t: Fastify.t) => Console.log(t)

tests/analysis_tests/tests/src/expected/ArrayUtils.res.txt

Whitespace-only changes.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ Resolved opens 1 Stdlib
633633
ContextPath array<int>->m
634634
ContextPath array<int>
635635
Path Stdlib.Array.m
636+
Path ArrayUtils.m
636637
[{
637638
"label": "Array.map",
638639
"kind": 12,
@@ -2215,6 +2216,7 @@ Resolved opens 3 Stdlib Completion Completion
22152216
ContextPath array->ma
22162217
ContextPath array
22172218
Path Stdlib.Array.ma
2219+
Path ArrayUtils.ma
22182220
[{
22192221
"label": "Array.map",
22202222
"kind": 12,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Complete src/CompletionConfiguredBuiltins.res 2:8
2+
posCursor:[2:8] posNoWhite:[2:7] Found expr:[2:3->2:8]
3+
Completable: Cpath Value[x]->em
4+
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
5+
Resolved opens 1 Stdlib
6+
ContextPath Value[x]->em
7+
ContextPath Value[x]
8+
Path x
9+
Path Stdlib.Array.em
10+
Path ArrayUtils.em
11+
[{
12+
"label": "ArrayUtils.empty",
13+
"kind": 12,
14+
"tags": [],
15+
"detail": "array<'a> => bool",
16+
"documentation": null
17+
}]
18+
19+
Complete src/CompletionConfiguredBuiltins.res 7:16
20+
posCursor:[7:16] posNoWhite:[7:15] Found expr:[7:3->7:16]
21+
Completable: Cpath Value[fastify]->doSt
22+
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
23+
Resolved opens 1 Stdlib
24+
ContextPath Value[fastify]->doSt
25+
ContextPath Value[fastify]
26+
Path fastify
27+
CPPipe pathFromEnv:Fastify found:false
28+
Path Fastify.doSt
29+
Path FastifyExt.doSt
30+
[{
31+
"label": "FastifyExt.doStuff",
32+
"kind": 12,
33+
"tags": [],
34+
"detail": "Fastify.t => unit",
35+
"documentation": null
36+
}]
37+

tests/analysis_tests/tests/src/expected/CompletionInferValues.res.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ ContextPath Value[String, split](Nolabel, Nolabel)
415415
ContextPath Value[String, split]
416416
Path String.split
417417
Path Stdlib.Array.ma
418+
Path ArrayUtils.ma
418419
[{
419420
"label": "Array.map",
420421
"kind": 12,

tests/analysis_tests/tests/src/expected/CompletionJsx.res.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ ContextPath Value[someArr]->a <<jsx>>
527527
ContextPath Value[someArr]
528528
Path someArr
529529
Path Stdlib.Array.a
530+
Path ArrayUtils.a
530531
[{
531532
"label": "React.array",
532533
"kind": 12,

tests/analysis_tests/tests/src/expected/DotPipeCompletionSpec.res.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ ContextPath Value[ffff]->u
216216
ContextPath Value[ffff]
217217
Path ffff
218218
Path Stdlib.Array.u
219+
Path ArrayUtils.u
219220
[{
220221
"label": "->Array.unshiftMany",
221222
"kind": 12,
@@ -329,6 +330,7 @@ ContextPath Value[Array, filter](Nolabel, Nolabel)
329330
ContextPath Value[Array, filter]
330331
Path Array.filter
331332
Path Stdlib.Array.filt
333+
Path ArrayUtils.filt
332334
[{
333335
"label": "->Array.filterMap",
334336
"kind": 12,

tests/analysis_tests/tests/src/expected/Fastify.res.txt

Whitespace-only changes.

tests/analysis_tests/tests/src/expected/FastifyExt.res.txt

Whitespace-only changes.

tests/analysis_tests/tests/src/expected/RecordCompletion.res.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Path t
1313
CPPipe pathFromEnv: found:true
1414
Path RecordCompletion.n
1515
Path Stdlib.Array.m
16+
Path ArrayUtils.m
1617
[{
1718
"label": "Array.map",
1819
"kind": 12,
@@ -54,6 +55,7 @@ Path RecordCompletion.n2
5455
CPPipe pathFromEnv: found:true
5556
Path RecordCompletion.n
5657
Path Stdlib.Array.m
58+
Path ArrayUtils.m
5759
[{
5860
"label": "Array.map",
5961
"kind": 12,

0 commit comments

Comments
 (0)