Skip to content

Commit ef2f7bf

Browse files
committed
Experiment with using the parser for autocomplete.
First step: find the parsetree expression for the item to be completed.
1 parent 0a4c6aa commit ef2f7bf

File tree

11 files changed

+144
-23
lines changed

11 files changed

+144
-23
lines changed

Diff for: analysis/src/Cli.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Options:
6868
let main () =
6969
match Array.to_list Sys.argv with
7070
| [_; "completion"; path; line; col; currentFile] ->
71-
Commands.completion ~path ~line:(int_of_string line)
71+
Commands.completion ~debug:false ~path ~line:(int_of_string line)
7272
~col:(int_of_string col) ~currentFile
7373
| [_; "definition"; path; line; col] ->
7474
Commands.definition ~path ~line:(int_of_string line)

Diff for: analysis/src/Commands.ml

+40-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
11
open SharedTypes
22

3-
let completion ~path ~line ~col ~currentFile =
3+
let posInLoc ~pos ~loc =
4+
Utils.tupleOfLexing loc.Location.loc_start <= pos
5+
&& pos < Utils.tupleOfLexing loc.loc_end
6+
7+
let completionWithParser ~debug ~path ~pos ~currentFile ~textOpt =
8+
let text = match textOpt with Some text -> text | None -> assert false in
9+
let offset =
10+
match PartialParser.positionToOffset text pos with
11+
| Some offset -> offset
12+
| None -> assert false
13+
in
14+
let line, col = pos in
15+
let offsetNoWhite = PartialParser.skipWhite text offset in
16+
let posNoWhite = (line, max 0 col - offset + offsetNoWhite) in
17+
18+
if Filename.check_suffix path ".res" then (
19+
let parser =
20+
Res_driver.parsingEngine.parseImplementation ~forPrinter:false
21+
in
22+
let found = ref false in
23+
let expr (iterator : Ast_iterator.iterator) (expr : Parsetree.expression) =
24+
if posInLoc ~pos:posNoWhite ~loc:expr.pexp_loc then (
25+
found := true;
26+
if debug then
27+
Printf.printf "Found expr:%s\n"
28+
(SemanticTokens.locToString expr.pexp_loc));
29+
Ast_iterator.default_iterator.expr iterator expr
30+
in
31+
let {Res_driver.parsetree = structure} = parser ~filename:currentFile in
32+
let iterator = {Ast_iterator.default_iterator with expr} in
33+
iterator.structure iterator structure |> ignore;
34+
if not !found then if debug then Printf.printf "XXX Not found!\n")
35+
36+
let completion ~debug ~path ~line ~col ~currentFile =
437
let pos = (line, col) in
38+
539
let result =
640
let textOpt = Files.readFile currentFile in
41+
completionWithParser ~debug ~path ~pos ~currentFile ~textOpt;
742
let completionItems =
843
match NewCompletions.getCompletable ~textOpt ~pos with
944
| None -> []
@@ -216,10 +251,9 @@ let format ~path =
216251
Res_driver.parsingEngine.parseImplementation ~forPrinter:true
217252
~filename:path
218253
in
219-
if List.length diagnostics > 0 then ""
220-
else
221-
Res_printer.printImplementation !Res_cli.ResClflags.width structure
222-
comments
254+
(* if List.length diagnostics > 0 then ""
255+
else *)
256+
Res_printer.printImplementation !Res_cli.ResClflags.width structure comments
223257
else if Filename.check_suffix path ".resi" then
224258
let {Res_driver.parsetree = signature; comments; diagnostics} =
225259
Res_driver.parsingEngine.parseInterface ~forPrinter:true ~filename:path
@@ -291,7 +325,7 @@ let test ~path =
291325
let line = line + 1 in
292326
let col = len - mlen - 3 in
293327
close_out cout;
294-
completion ~path ~line ~col ~currentFile;
328+
completion ~debug:true ~path ~line ~col ~currentFile;
295329
Sys.remove currentFile
296330
| "hig" ->
297331
print_endline ("Highlight " ^ path);

Diff for: analysis/src/vendor/res_outcome_printer/res_core.ml

+4-1
Original file line numberDiff line numberDiff line change
@@ -3527,8 +3527,11 @@ and parseValueOrConstructor p =
35273527
Ast_helper.Exp.ident ~loc (Location.mkloc lident loc)
35283528
| token ->
35293529
Parser.next p;
3530+
let loc = mkLoc startPos p.prevEndPos in
35303531
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
3531-
Recover.defaultExpr()
3532+
let lident = buildLongident ("$"::acc) in
3533+
Ast_helper.Exp.ident ~loc (Location.mkloc lident loc)
3534+
(* Recover.defaultExpr() *)
35323535
in
35333536
aux p []
35343537

Diff for: analysis/tests/src/expected/CompletePrioritize1.res.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Complete tests/src/CompletePrioritize1.res 4:2
2+
Found expr:(5,2)->(5,4)
23
[{
34
"label": "Test.add",
45
"kind": 12,

Diff for: analysis/tests/src/expected/CompletePrioritize2.res.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Complete tests/src/CompletePrioritize2.res 8:2
2+
Found expr:(9,2)->(9,4)
23
[{
34
"label": "Test.add",
45
"kind": 12,

Diff for: analysis/tests/src/expected/Completion.res.txt

+36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Complete tests/src/Completion.res 0:2
2+
Found expr:(1,1)->(1,9)
23
[{
34
"label": "mapReverse",
45
"kind": 12,
@@ -68,6 +69,7 @@ Complete tests/src/Completion.res 0:2
6869
}]
6970

7071
Complete tests/src/Completion.res 1:2
72+
Found expr:(2,1)->(6,6)
7173
[{
7274
"label": "Floatarray",
7375
"kind": 9,
@@ -287,6 +289,7 @@ Complete tests/src/Completion.res 1:2
287289
}]
288290

289291
Complete tests/src/Completion.res 2:2
292+
Found expr:(3,1)->(3,8)
290293
[{
291294
"label": "mapi",
292295
"kind": 12,
@@ -338,6 +341,7 @@ Complete tests/src/Completion.res 2:2
338341
}]
339342

340343
Complete tests/src/Completion.res 12:2
344+
Found expr:(13,10)->(13,15)
341345
[{
342346
"label": "customDouble",
343347
"kind": 12,
@@ -347,6 +351,7 @@ Complete tests/src/Completion.res 12:2
347351
}]
348352

349353
Complete tests/src/Completion.res 19:2
354+
Found expr:(20,9)->(20,18)
350355
[{
351356
"label": "age",
352357
"kind": 4,
@@ -362,6 +367,8 @@ Complete tests/src/Completion.res 19:2
362367
}]
363368

364369
Complete tests/src/Completion.res 21:2
370+
Found expr:(22,1)->(22,11)
371+
Found expr:(22,10)->(22,11)
365372
[{
366373
"label": "Js.Array2.mapi",
367374
"kind": 12,
@@ -377,6 +384,8 @@ Complete tests/src/Completion.res 21:2
377384
}]
378385

379386
Complete tests/src/Completion.res 23:2
387+
Found expr:(24,1)->(24,11)
388+
Found expr:(24,8)->(24,11)
380389
[{
381390
"label": "Js.String2.toUpperCase",
382391
"kind": 12,
@@ -386,6 +395,8 @@ Complete tests/src/Completion.res 23:2
386395
}]
387396

388397
Complete tests/src/Completion.res 27:2
398+
Found expr:(28,1)->(28,6)
399+
Found expr:(28,5)->(28,6)
389400
[{
390401
"label": "Belt.Option.eqU",
391402
"kind": 12,
@@ -401,6 +412,9 @@ Complete tests/src/Completion.res 27:2
401412
}]
402413

403414
Complete tests/src/Completion.res 36:2
415+
Found expr:(37,1)->(46,3)
416+
Found expr:(37,1)->(41,8)
417+
Found expr:(37,3)->(37,5)
404418
[{
405419
"label": "ForAuto.abc",
406420
"kind": 12,
@@ -416,6 +430,8 @@ Complete tests/src/Completion.res 36:2
416430
}]
417431

418432
Complete tests/src/Completion.res 38:2
433+
Found expr:(39,1)->(39,19)
434+
Found expr:(39,10)->(39,19)
419435
[{
420436
"label": "unsafeGet",
421437
"kind": 12,
@@ -431,6 +447,8 @@ Complete tests/src/Completion.res 38:2
431447
}]
432448

433449
Complete tests/src/Completion.res 50:2
450+
Found expr:(51,13)->(51,28)
451+
Found expr:(51,27)->(51,28)
434452
[{
435453
"label": "zzz",
436454
"kind": 12,
@@ -440,6 +458,8 @@ Complete tests/src/Completion.res 50:2
440458
}]
441459

442460
Complete tests/src/Completion.res 52:2
461+
Found expr:(53,13)->(53,21)
462+
Found expr:(53,20)->(53,21)
443463
[{
444464
"label": "zoo",
445465
"kind": 4,
@@ -643,6 +663,7 @@ DocumentSymbol tests/src/Completion.res
643663
]
644664

645665
Complete tests/src/Completion.res 56:2
666+
XXX Not found!
646667
[{
647668
"label": "react.component",
648669
"kind": 4,
@@ -652,6 +673,7 @@ Complete tests/src/Completion.res 56:2
652673
}]
653674

654675
Complete tests/src/Completion.res 58:2
676+
Found expr:(0,-1)->(69,17)
655677
[{
656678
"label": "component",
657679
"kind": 4,
@@ -661,6 +683,7 @@ Complete tests/src/Completion.res 58:2
661683
}]
662684

663685
Complete tests/src/Completion.res 60:2
686+
Found expr:(61,9)->(61,25)
664687
[{
665688
"label": "age",
666689
"kind": 4,
@@ -670,6 +693,7 @@ Complete tests/src/Completion.res 60:2
670693
}]
671694

672695
Complete tests/src/Completion.res 62:2
696+
Found expr:(63,9)->(63,24)
673697
[{
674698
"label": "name",
675699
"kind": 4,
@@ -679,6 +703,7 @@ Complete tests/src/Completion.res 62:2
679703
}]
680704

681705
Complete tests/src/Completion.res 64:2
706+
Found expr:(65,9)->(65,30)
682707
[{
683708
"label": "name",
684709
"kind": 4,
@@ -688,6 +713,7 @@ Complete tests/src/Completion.res 64:2
688713
}]
689714

690715
Complete tests/src/Completion.res 67:2
716+
Found expr:(67,8)->(69,17)
691717
[{
692718
"label": "age",
693719
"kind": 4,
@@ -703,6 +729,7 @@ Complete tests/src/Completion.res 67:2
703729
}]
704730

705731
Complete tests/src/Completion.res 72:2
732+
Found expr:(73,8)->(75,18)
706733
[{
707734
"label": "age",
708735
"kind": 4,
@@ -712,6 +739,7 @@ Complete tests/src/Completion.res 72:2
712739
}]
713740

714741
Complete tests/src/Completion.res 76:2
742+
Found expr:(77,20)->(80,10)
715743
[{
716744
"label": "age",
717745
"kind": 4,
@@ -727,6 +755,7 @@ Complete tests/src/Completion.res 76:2
727755
}]
728756

729757
Complete tests/src/Completion.res 79:2
758+
Found expr:(80,2)->(82,20)
730759
[{
731760
"label": "age",
732761
"kind": 4,
@@ -736,6 +765,7 @@ Complete tests/src/Completion.res 79:2
736765
}]
737766

738767
Complete tests/src/Completion.res 83:2
768+
Found expr:(84,13)->(101,20)
739769
[{
740770
"label": "name",
741771
"kind": 4,
@@ -751,6 +781,7 @@ Complete tests/src/Completion.res 83:2
751781
}]
752782

753783
Complete tests/src/Completion.res 88:3
784+
Found expr:(89,1)->(93,3)
754785
[{
755786
"label": "x",
756787
"kind": 5,
@@ -766,6 +797,7 @@ Complete tests/src/Completion.res 88:3
766797
}]
767798

768799
Complete tests/src/Completion.res 90:3
800+
Found expr:(91,1)->(93,3)
769801
[{
770802
"label": "xx",
771803
"kind": 5,
@@ -781,6 +813,9 @@ Complete tests/src/Completion.res 90:3
781813
}]
782814

783815
Complete tests/src/Completion.res 96:3
816+
Found expr:(96,11)->(99,1)
817+
Found expr:(97,1)->(98,5)
818+
Found expr:(97,1)->(97,3)
784819
[{
785820
"label": "myAmazingFunction",
786821
"kind": 12,
@@ -790,6 +825,7 @@ Complete tests/src/Completion.res 96:3
790825
}]
791826

792827
Complete tests/src/Completion.res 100:3
828+
Found expr:(101,11)->(120,32)
793829
[{
794830
"label": "name",
795831
"kind": 4,

Diff for: analysis/tests/src/expected/Cross.res.txt

+1
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,6 @@ TypeDefinition tests/src/Cross.res 37:37
9494
{"uri": "DefinitionWithInterface.resi", "range": {"start": {"line": 3, "character": 0}, "end": {"line": 3, "character": 6}}}
9595

9696
Complete tests/src/Cross.res 39:2
97+
Found expr:(40,1)->(40,26)
9798
[]
9899

Diff for: analysis/tests/src/expected/Div.res.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Hover tests/src/Div.res 1:10
22
{"contents": "```rescript\n(\n string,\n ~props: ReactDOMRe.domProps=?,\n array<React.element>,\n) => React.element\n```"}
33

44
Complete tests/src/Div.res 3:3
5+
Found expr:(4,2)->(4,15)
6+
Found expr:(4,6)->(4,15)
57
[{
68
"label": "dangerouslySetInnerHTML",
79
"kind": 4,

0 commit comments

Comments
 (0)