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.

0 commit comments

Comments
 (0)