Skip to content

Commit 58cf089

Browse files
committed
pipe dot completion for builtins
1 parent 2b3190c commit 58cf089

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,9 +1092,51 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
10921092
| None -> None
10931093
| Some completionPath -> Some (completionPath, tPath))
10941094
in
1095-
match tPath with
1096-
| None -> []
1097-
| Some (completionPath, tPath) ->
1095+
let env, typ =
1096+
typ
1097+
|> TypeUtils.resolveTypeForPipeCompletion ~env ~package ~full
1098+
~lhsLoc:Location.none
1099+
in
1100+
match (typ, tPath) with
1101+
| Builtin (builtin, _), _ ->
1102+
(* TODO: Unify with existing code *)
1103+
let {
1104+
arrayModulePath;
1105+
optionModulePath;
1106+
stringModulePath;
1107+
intModulePath;
1108+
floatModulePath;
1109+
promiseModulePath;
1110+
listModulePath;
1111+
resultModulePath;
1112+
regexpModulePath;
1113+
} =
1114+
package.builtInCompletionModules
1115+
in
1116+
let completionPath =
1117+
match builtin with
1118+
| Array -> arrayModulePath
1119+
| Option -> optionModulePath
1120+
| String -> stringModulePath
1121+
| Int -> intModulePath
1122+
| Float -> floatModulePath
1123+
| Promise -> promiseModulePath
1124+
| List -> listModulePath
1125+
| Result -> resultModulePath
1126+
| RegExp -> regexpModulePath
1127+
| Lazy -> ["Lazy"]
1128+
| Char -> ["Char"]
1129+
in
1130+
if Debug.verbose () then
1131+
Printf.printf "[dot_completion] Completing from builtin\n";
1132+
completionsForPipeFromCompletionPath ~envCompletionIsMadeFrom ~opens
1133+
~pos ~scope ~debug ~prefix:fieldName ~env ~rawOpens ~full
1134+
completionPath
1135+
|> List.filter_map (fun completion ->
1136+
TypeUtils.transformCompletionToPipeCompletion ~synthetic:true
1137+
~env ~replaceRange:fieldNameLoc completion)
1138+
| _, None -> []
1139+
| _, Some (completionPath, tPath) ->
10981140
if Debug.verbose () then
10991141
Printf.printf "[dot_completion]--> Got completion path %s\n"
11001142
(completionPath |> String.concat ".");

analysis/tests/src/DotCompletionEverywhere.res

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,8 @@ let obj = {
6161

6262
// obj.
6363
// ^com
64+
65+
let arr = [1, 2, 3]
66+
67+
// arr.m
68+
// ^com

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,38 @@ Path obj
217217
}
218218
}]
219219

220+
Complete src/DotCompletionEverywhere.res 66:8
221+
posCursor:[66:8] posNoWhite:[66:7] Found expr:[66:3->66:8]
222+
Pexp_field [66:3->66:6] m:[66:7->66:8]
223+
Completable: Cpath Value[arr].m
224+
Package opens Pervasives.JsxModules.place holder
225+
Resolved opens 1 pervasives
226+
ContextPath Value[arr].m
227+
ContextPath Value[arr]
228+
Path arr
229+
CPPipe pathFromEnv: found:true
230+
Path Js.Array2.m
231+
[{
232+
"label": "->Js.Array2.mapi",
233+
"kind": 12,
234+
"tags": [],
235+
"detail": "(t<'a>, ('a, int) => 'b) => t<'b>",
236+
"documentation": {"kind": "markdown", "value": "\nApplies the function (the second argument) to each item in the array, returning\na new array. The function acceps two arguments: an item from the array and its\nindex number. The result array does not have to have elements of the same type\nas the input array. See\n[`Array.map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)\non MDN.\n\n## Examples\n\n```rescript\n// multiply each item in array by its position\nlet product = (item, index) => item * index\nJs.Array2.mapi([10, 11, 12], product) == [0, 11, 24]\n```\n"},
237+
"sortText": "mapi",
238+
"textEdit": {
239+
"range": {"start": {"line": 66, "character": 7}, "end": {"line": 66, "character": 8}},
240+
"newText": "->Js.Array2.mapi"
241+
}
242+
}, {
243+
"label": "->Js.Array2.map",
244+
"kind": 12,
245+
"tags": [],
246+
"detail": "(t<'a>, 'a => 'b) => t<'b>",
247+
"documentation": {"kind": "markdown", "value": "\nApplies the function (the second argument) to each item in the array, returning\na new array. The result array does not have to have elements of the same type\nas the input array. See\n[`Array.map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)\non MDN.\n\n## Examples\n\n```rescript\nJs.Array2.map([12, 4, 8], x => x * x) == [144, 16, 64]\nJs.Array2.map([\"animal\", \"vegetable\", \"mineral\"], Js.String.length) == [6, 9, 7]\n```\n"},
248+
"sortText": "map",
249+
"textEdit": {
250+
"range": {"start": {"line": 66, "character": 7}, "end": {"line": 66, "character": 8}},
251+
"newText": "->Js.Array2.map"
252+
}
253+
}]
254+

0 commit comments

Comments
 (0)