Skip to content

Commit 4d2b45b

Browse files
committed
some snapshot refactoring and tweaking
1 parent 8f09841 commit 4d2b45b

9 files changed

+198
-66
lines changed

analysis/bin/main.ml

+7-4
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,13 @@ let main () =
130130
| [_; "completion"; path; line; col; currentFile] ->
131131
printHeaderInfo path line col;
132132
if !Cfg.useRevampedCompletion then
133-
let source = Files.readFile currentFile in
134-
Commands.completionRevamped ~source ~debug ~path
135-
~pos:(int_of_string line, int_of_string col)
136-
~currentFile
133+
match
134+
Commands.completionRevamped ~debug ~path
135+
~pos:(int_of_string line, int_of_string col)
136+
~currentFile
137+
with
138+
| None -> ()
139+
| Some (_, completablesText) -> print_endline completablesText
137140
else
138141
Commands.completion ~debug:true ~path
139142
~pos:(int_of_string line, int_of_string col)

analysis/src/Commands.ml

+41-19
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@ let completion ~(debug : bool) ~path ~pos ~currentFile =
1111
in
1212
completions |> Protocol.array |> print_endline
1313

14-
let completionRevamped ?(source = None) ~debug ~path ~pos ~currentFile =
15-
let completions =
16-
match
17-
Completions.getCompletionsRevamped ~source ~debug ~path ~pos ~currentFile
18-
with
19-
| None -> []
20-
| Some (completions, full, _) ->
21-
completions
22-
|> List.map (CompletionBackEnd.completionToItem ~full)
23-
|> List.map Protocol.stringifyCompletionItem
24-
in
25-
completions |> Protocol.array |> print_endline
14+
let completionRevamped ~debug ~path ~pos ~currentFile =
15+
match Completions.getCompletionsRevamped ~debug ~pos ~currentFile ~path with
16+
| None -> None
17+
| Some (completable, completions, full, _) ->
18+
Some
19+
( completable,
20+
completions
21+
|> List.map (CompletionBackEnd.completionToItem ~full)
22+
|> List.map Protocol.stringifyCompletionItem
23+
|> Protocol.array )
24+
2625
let completionResolve ~path ~modulePath =
2726
(* We ignore the internal module path as of now because there's currently
2827
no use case for it. But, if we wanted to move resolving documentation
@@ -380,15 +379,38 @@ let test ~path ~debug =
380379
^ string_of_int col);
381380
definition ~path ~pos:(line, col) ~debug:true
382381
| "com" ->
383-
print_endline
384-
("Complete " ^ path ^ " " ^ string_of_int line ^ ":"
385-
^ string_of_int col);
386382
let currentFile = createCurrentFile () in
387-
if !Cfg.useRevampedCompletion then
383+
if !Cfg.useRevampedCompletion then (
388384
let source = Files.readFile currentFile in
389-
completionRevamped ~source ~debug ~path ~pos:(line, col)
390-
~currentFile
391-
else completion ~debug:true ~path ~pos:(line, col) ~currentFile;
385+
let completions =
386+
completionRevamped ~debug ~path ~pos:(line, col) ~currentFile
387+
in
388+
(match (completions, source) with
389+
| None, _ ->
390+
print_endline "Completion Frontend did not return completable"
391+
| Some (completable, completionsText), Some text -> (
392+
match SharedTypes.CompletableRevamped.try_loc completable with
393+
| Some loc ->
394+
let range =
395+
CodeFence.
396+
{
397+
start = loc.Location.loc_start.pos_cnum;
398+
finish = loc.Warnings.loc_end.pos_cnum;
399+
}
400+
in
401+
Printf.printf "Found Completable: %s\n\n"
402+
(SharedTypes.CompletableRevamped.toString completable);
403+
CodeFence.format_code_snippet_cropped text (Some range) 3
404+
|> print_endline;
405+
print_endline completionsText
406+
| None -> ())
407+
| _ -> print_endline "ERR: Unexpected completion result");
408+
())
409+
else (
410+
print_endline
411+
("Complete " ^ path ^ " " ^ string_of_int line ^ ":"
412+
^ string_of_int col);
413+
completion ~debug:true ~path ~pos:(line, col) ~currentFile);
392414
Sys.remove currentFile
393415
| "cre" ->
394416
let modulePath = String.sub rest 3 (String.length rest - 3) in

analysis/src/Completions.ml

+3-27
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let getCompletions (debug : bool) ~path ~pos ~currentFile ~forHover =
2121
in
2222
Some (completables, full, scope)))
2323

24-
let getCompletionsRevamped ?(source = None) ~debug ~path ~pos ~currentFile =
24+
let getCompletionsRevamped ~debug ~pos ~currentFile ~path =
2525
let textOpt = Files.readFile currentFile in
2626
match textOpt with
2727
| None | Some "" -> None
@@ -30,32 +30,8 @@ let getCompletionsRevamped ?(source = None) ~debug ~path ~pos ~currentFile =
3030
CompletionFrontEndRevamped.completionWithParser ~debug ~path
3131
~posCursor:pos ~currentFile ~text
3232
with
33-
| None ->
34-
source
35-
|> Option.iter (fun _ ->
36-
print_endline "Completion Frontend did not return completable");
37-
None
33+
| None -> None
3834
| Some (completable, scope) -> (
39-
let _ =
40-
match source with
41-
| Some text -> (
42-
match SharedTypes.CompletableRevamped.try_loc completable with
43-
| Some loc ->
44-
let range =
45-
CodeFence.
46-
{
47-
start = loc.Location.loc_start.pos_cnum;
48-
finish = loc.Warnings.loc_end.pos_cnum;
49-
}
50-
in
51-
Printf.printf "Found Completable: %s\n\n"
52-
(SharedTypes.CompletableRevamped.toString completable);
53-
CodeFence.format_code_snippet_cropped text (Some range) 3
54-
|> print_endline
55-
| None -> ())
56-
| None -> ()
57-
in
58-
5935
(* Only perform expensive ast operations if there are completables *)
6036
match Cmt.loadFullCmtFromPath ~path with
6137
| None -> None
@@ -66,4 +42,4 @@ let getCompletionsRevamped ?(source = None) ~debug ~path ~pos ~currentFile =
6642
|> CompletionBackEndRevamped.processCompletable ~debug ~full ~pos
6743
~scope ~env
6844
in
69-
Some (completables, full, scope)))
45+
Some (completable, completables, full, scope)))

tests/analysis_new_tests/tests/snapshots.test.js

+32-16
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,30 @@ const incrementalDir = path.join(
1616
import.meta.dirname,
1717
"./lib/bs/___incremental"
1818
);
19+
const snapshotDir = path.join(testFilesDir, "__snapshots__");
1920

2021
// Recreate directories needed
21-
try {
22-
await fs.access(incrementalDir);
23-
await fs.rm(incrementalDir, { recursive: true });
24-
} catch (_) {}
25-
await fs.mkdir(incrementalDir, { recursive: true });
26-
27-
try {
28-
await fs.access(buildDir);
29-
await fs.rm(buildDir, { recursive: true });
30-
} catch (_) {}
31-
await fs.mkdir(buildDir, { recursive: true });
32-
33-
const resFiles = await glob("**/*.res", {
22+
async function ensureSnapshotDir() {
23+
await fs.mkdir(snapshotDir, { recursive: true }); // Ensure snapshot dir exists
24+
}
25+
26+
async function ensureIncrementalDir() {
27+
try {
28+
await fs.access(incrementalDir);
29+
await fs.rm(incrementalDir, { recursive: true });
30+
} catch (_) {}
31+
await fs.mkdir(incrementalDir, { recursive: true });
32+
}
33+
34+
async function ensureBuildDir() {
35+
try {
36+
await fs.access(buildDir);
37+
await fs.rm(buildDir, { recursive: true });
38+
} catch (_) {}
39+
await fs.mkdir(buildDir, { recursive: true });
40+
}
41+
42+
const resFilesPromise = glob("**/*.res", {
3443
cwd: testFilesDir,
3544
absolute: true,
3645
}).then((files) =>
@@ -40,6 +49,13 @@ const resFiles = await glob("**/*.res", {
4049
}))
4150
);
4251

52+
const [resFiles] = await Promise.all([
53+
resFilesPromise,
54+
ensureSnapshotDir(),
55+
ensureIncrementalDir(),
56+
ensureBuildDir(),
57+
]);
58+
4359
// Function to split test file contents into blocks
4460
const splitTestBlocks = (contents) => {
4561
const testBlocks = contents.split(/\/\/ == TEST:/);
@@ -154,9 +170,9 @@ resFiles.forEach((file) => {
154170
});
155171

156172
// Construct snapshot path
157-
const snapshotDir = path.join(testFilesDir, "__snapshots__");
158-
await fs.mkdir(snapshotDir, { recursive: true }); // Ensure snapshot dir exists
159-
const snapshotFileName = `${file.relativePath}_${block.description.replace(/\\s+/g, "_")}.snap`;
173+
const snapshotFileName = `${
174+
file.relativePath
175+
}_${block.description.replace(/[^a-zA-Z0-9]+/g, "_")}.snap`;
160176
const snapshotPath = path.join(snapshotDir, snapshotFileName);
161177

162178
// Use Vitest's expect().toMatchFileSnapshot()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Found Completable: Cexpression
2+
3+
1 + // Record field completion in nested record
4+
2 + let x = TestTypeDefs.nestedTestRecord.
5+
-----------------------------
6+
3 + // ^com
7+
4 +
8+
9+
[{
10+
"label": "test",
11+
"kind": 5,
12+
"tags": [],
13+
"detail": "bool",
14+
"documentation": {"kind": "markdown", "value": "```rescript\ntest: bool\n```\n\n```rescript\ntype nestedTestRecord = {\n test: bool,\n nested: {name: string, oneMoreLevel: {here: bool}},\n}\n```"}
15+
}, {
16+
"label": "nested",
17+
"kind": 5,
18+
"tags": [],
19+
"detail": "\\\"nestedTestRecord.nested\"",
20+
"documentation": {"kind": "markdown", "value": "```rescript\nnested: \\\"nestedTestRecord.nested\"\n```\n\n```rescript\ntype nestedTestRecord = {\n test: bool,\n nested: {name: string, oneMoreLevel: {here: bool}},\n}\n```"}
21+
}]
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Found Completable: Cexpression
2+
3+
1 + // Record field completion in nested record, another level
4+
2 + let x = TestTypeDefs.nestedTestRecord.nested.
5+
------------------------------------
6+
3 + // ^com
7+
4 +
8+
9+
[{
10+
"label": "name",
11+
"kind": 5,
12+
"tags": [],
13+
"detail": "string",
14+
"documentation": {"kind": "markdown", "value": "```rescript\nname: string\n```\n\n```rescript\ntype \\\"nestedTestRecord.nested\" = {\n name: string,\n oneMoreLevel: {here: bool},\n}\n```"}
15+
}, {
16+
"label": "oneMoreLevel",
17+
"kind": 5,
18+
"tags": [],
19+
"detail": "\\\"nestedTestRecord.nested.oneMoreLevel\"",
20+
"documentation": {"kind": "markdown", "value": "```rescript\noneMoreLevel: \\\"nestedTestRecord.nested.oneMoreLevel\"\n```\n\n```rescript\ntype \\\"nestedTestRecord.nested\" = {\n name: string,\n oneMoreLevel: {here: bool},\n}\n```"}
21+
}]
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Found Completable: Cpattern
2+
3+
1 + // Empty case, array
4+
2 + let someStringArr = ["hello"]
5+
3 +
6+
4 + let x = switch someStringArr {
7+
-------------
8+
5 + |
9+
6 + // ^com
10+
7 + }
11+
12+
[{
13+
"label": "[]",
14+
"kind": 12,
15+
"tags": [],
16+
"detail": "array<string>",
17+
"documentation": null,
18+
"sortText": "A",
19+
"insertText": "[$0]",
20+
"insertTextFormat": 2
21+
}]
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Found Completable: Cpattern
2+
3+
1 + // Empty case, bool
4+
2 + let x = switch true {
5+
----
6+
3 + |
7+
4 + // ^com
8+
5 + }
9+
10+
[{
11+
"label": "true",
12+
"kind": 12,
13+
"tags": [],
14+
"detail": "bool",
15+
"documentation": null,
16+
"sortText": "A",
17+
"insertText": "true",
18+
"insertTextFormat": 2
19+
}, {
20+
"label": "false",
21+
"kind": 12,
22+
"tags": [],
23+
"detail": "bool",
24+
"documentation": null,
25+
"sortText": "A",
26+
"insertText": "false",
27+
"insertTextFormat": 2
28+
}]
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Found Completable: Cpattern
2+
3+
1 + // Empty case, record
4+
2 + let x = switch TestTypeDefs.nestedTestRecord {
5+
-----------------------------
6+
3 + |
7+
4 + // ^com
8+
5 + }
9+
10+
[{
11+
"label": "{}",
12+
"kind": 12,
13+
"tags": [],
14+
"detail": "TestTypeDefs.nestedTestRecord",
15+
"documentation": {"kind": "markdown", "value": "```rescript\ntype nestedTestRecord = {\n test: bool,\n nested: {name: string, oneMoreLevel: {here: bool}},\n}\n```"},
16+
"sortText": "A",
17+
"insertText": "{$0}",
18+
"insertTextFormat": 2
19+
}]
20+

0 commit comments

Comments
 (0)