Skip to content

Global per-project configuration of autocomplete #7330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,25 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
| _ -> false
else true)
in

let globallyConfiguredCompletionsForType =
match package.autocomplete |> Misc.StringMap.find_opt mainTypeId with
| None -> []
| Some completionPaths ->
completionPaths |> List.map (fun p -> String.split_on_char '.' p)
in

let globallyConfiguredCompletions =
globallyConfiguredCompletionsForType
|> List.map (fun completionPath ->
completionsForPipeFromCompletionPath ~envCompletionIsMadeFrom
~opens ~pos ~scope ~debug ~prefix ~env ~rawOpens ~full
completionPath)
|> List.flatten
|> TypeUtils.filterPipeableFunctions ~synthetic:true ~env ~full
~targetTypeId:mainTypeId
in

(* Extra completions can be drawn from the @editor.completeFrom attribute. Here we
find and add those completions as well. *)
let extraCompletions =
Expand All @@ -1154,7 +1173,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
~full ~rawOpens typ
else []
in
jsxCompletions @ pipeCompletions @ extraCompletions))
jsxCompletions @ pipeCompletions @ extraCompletions
@ globallyConfiguredCompletions))
| CTuple ctxPaths ->
if Debug.verbose () then print_endline "[ctx_path]--> CTuple";
(* Turn a list of context paths into a list of type expressions. *)
Expand Down
23 changes: 23 additions & 0 deletions analysis/src/Packages.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ let newBsPackage ~rootPath =
| _ -> None)
| None -> None
in
let autocomplete =
match config |> Json.get "editor" with
| Some editorConfig -> (
match editorConfig |> Json.get "autocomplete" with
| Some (Object map) ->
map
|> List.fold_left
(fun acc (key, value) ->
match value with
| Json.Array items ->
let values =
items
|> List.filter_map (function
| Json.String s -> Some s
| _ -> None)
in
Misc.StringMap.add key values acc
| _ -> acc)
Misc.StringMap.empty
| _ -> Misc.StringMap.empty)
| None -> Misc.StringMap.empty
in
let uncurried = uncurried = Some true in
match libBs with
| None -> None
Expand Down Expand Up @@ -158,6 +180,7 @@ let newBsPackage ~rootPath =
opens;
namespace;
uncurried;
autocomplete;
}))
| None -> None
in
Expand Down
1 change: 1 addition & 0 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ type package = {
opens: path list;
uncurried: bool;
rescriptVersion: int * int;
autocomplete: file list Misc.StringMap.t;
}

let allFilesInPackage package =
Expand Down
22 changes: 22 additions & 0 deletions docs/docson/build-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,24 @@
"description": "(Not implemented yet)"
}
]
},
"editor": {
"type": "object",
"properties": {
"autocomplete": {
"type": "object",
"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.",
"patternProperties": {
"^[a-zA-Z0-9_.]+$": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
},
"additionalProperties": false
}
},
"title": "ReScript build configuration",
Expand Down Expand Up @@ -472,6 +490,10 @@
"reanalyze": {
"$ref": "#/definitions/reanalyze",
"description": "Configure reanalyze, a static code analysis tool for ReScript."
},
"editor": {
"$ref": "#/definitions/editor",
"description": "Configure editor functionality, like modules that should be included in autocompletions for given (built-in) types."
}
},
"additionalProperties": false,
Expand Down
8 changes: 7 additions & 1 deletion tests/analysis_tests/tests/rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@
"bsc-flags": ["-w -33-44-8"],
"bs-dependencies": ["@rescript/react"],
"jsx": { "version": 4 },
"suffix": ".res.js"
"suffix": ".res.js",
"editor": {
"autocomplete": {
"array": ["ArrayUtils"],
"Fastify.t": ["FastifyExt"]
}
}
}
1 change: 1 addition & 0 deletions tests/analysis_tests/tests/src/ArrayUtils.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let empty = arr => Array.length(arr) === 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let x = [1, 2, 3]

// x->em
// ^com

external fastify: Fastify.t = "fastify"

// fastify->doSt
// ^com
1 change: 1 addition & 0 deletions tests/analysis_tests/tests/src/Fastify.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type t
1 change: 1 addition & 0 deletions tests/analysis_tests/tests/src/FastifyExt.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let doStuff = (t: Fastify.t) => Console.log(t)
Empty file.
2 changes: 2 additions & 0 deletions tests/analysis_tests/tests/src/expected/Completion.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ Resolved opens 1 Stdlib
ContextPath array<int>->m
ContextPath array<int>
Path Stdlib.Array.m
Path ArrayUtils.m
[{
"label": "Array.map",
"kind": 12,
Expand Down Expand Up @@ -2215,6 +2216,7 @@ Resolved opens 3 Stdlib Completion Completion
ContextPath array->ma
ContextPath array
Path Stdlib.Array.ma
Path ArrayUtils.ma
[{
"label": "Array.map",
"kind": 12,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Complete src/CompletionConfiguredBuiltins.res 2:8
posCursor:[2:8] posNoWhite:[2:7] Found expr:[2:3->2:8]
Completable: Cpath Value[x]->em
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
Resolved opens 1 Stdlib
ContextPath Value[x]->em
ContextPath Value[x]
Path x
Path Stdlib.Array.em
Path ArrayUtils.em
[{
"label": "ArrayUtils.empty",
"kind": 12,
"tags": [],
"detail": "array<'a> => bool",
"documentation": null
}]

Complete src/CompletionConfiguredBuiltins.res 7:16
posCursor:[7:16] posNoWhite:[7:15] Found expr:[7:3->7:16]
Completable: Cpath Value[fastify]->doSt
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
Resolved opens 1 Stdlib
ContextPath Value[fastify]->doSt
ContextPath Value[fastify]
Path fastify
CPPipe pathFromEnv:Fastify found:false
Path Fastify.doSt
Path FastifyExt.doSt
[{
"label": "FastifyExt.doStuff",
"kind": 12,
"tags": [],
"detail": "Fastify.t => unit",
"documentation": null
}]

Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ ContextPath Value[String, split](Nolabel, Nolabel)
ContextPath Value[String, split]
Path String.split
Path Stdlib.Array.ma
Path ArrayUtils.ma
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change in output is fine.

[{
"label": "Array.map",
"kind": 12,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ ContextPath Value[someArr]->a <<jsx>>
ContextPath Value[someArr]
Path someArr
Path Stdlib.Array.a
Path ArrayUtils.a
[{
"label": "React.array",
"kind": 12,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ ContextPath Value[ffff]->u
ContextPath Value[ffff]
Path ffff
Path Stdlib.Array.u
Path ArrayUtils.u
[{
"label": "->Array.unshiftMany",
"kind": 12,
Expand Down Expand Up @@ -329,6 +330,7 @@ ContextPath Value[Array, filter](Nolabel, Nolabel)
ContextPath Value[Array, filter]
Path Array.filter
Path Stdlib.Array.filt
Path ArrayUtils.filt
[{
"label": "->Array.filterMap",
"kind": 12,
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Path t
CPPipe pathFromEnv: found:true
Path RecordCompletion.n
Path Stdlib.Array.m
Path ArrayUtils.m
[{
"label": "Array.map",
"kind": 12,
Expand Down Expand Up @@ -54,6 +55,7 @@ Path RecordCompletion.n2
CPPipe pathFromEnv: found:true
Path RecordCompletion.n
Path Stdlib.Array.m
Path ArrayUtils.m
[{
"label": "Array.map",
"kind": 12,
Expand Down