-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathCompletionFrontEnd.ml
889 lines (867 loc) · 32.9 KB
/
CompletionFrontEnd.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
open SharedTypes
let rec skipWhite text i =
if i < 0 then 0
else
match text.[i] with
| ' ' | '\n' | '\r' | '\t' -> skipWhite text (i - 1)
| _ -> i
type prop = {
name: string;
posStart: int * int;
posEnd: int * int;
exp: Parsetree.expression;
}
type jsxProps = {
compName: Longident.t Location.loc;
props: prop list;
childrenStart: (int * int) option;
}
let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor ~posAfterCompName
=
let allLabels =
List.fold_right
(fun prop allLabels -> prop.name :: allLabels)
jsxProps.props []
in
let rec loop props =
match props with
| prop :: rest ->
if prop.posStart <= posBeforeCursor && posBeforeCursor < prop.posEnd then
(* Cursor on the prop name *)
Some
(Completable.Cjsx
( Utils.flattenLongIdent ~jsx:true jsxProps.compName.txt,
prop.name,
allLabels ))
else if
prop.posEnd <= posBeforeCursor
&& posBeforeCursor < Loc.start prop.exp.pexp_loc
then (* Cursor between the prop name and expr assigned *)
None
else if prop.exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then
(* Cursor on expr assigned *)
None
else if prop.exp.pexp_loc |> Loc.end_ = (Location.none |> Loc.end_) then
(* Expr assigned presumably is "rescript.exprhole" after parser recovery.
To be on the safe side, don't do label completion. *)
None
else loop rest
| [] ->
let beforeChildrenStart =
match jsxProps.childrenStart with
| Some childrenPos -> posBeforeCursor < childrenPos
| None -> posBeforeCursor <= endPos
in
let afterCompName = posBeforeCursor >= posAfterCompName in
if afterCompName && beforeChildrenStart then
Some
(Cjsx
( Utils.flattenLongIdent ~jsx:true jsxProps.compName.txt,
"",
allLabels ))
else None
in
loop jsxProps.props
let extractJsxProps ~(compName : Longident.t Location.loc) ~args =
let thisCaseShouldNotHappen =
{
compName = Location.mknoloc (Longident.Lident "");
props = [];
childrenStart = None;
}
in
let rec processProps ~acc args =
match args with
| (Asttypes.Labelled "children", {Parsetree.pexp_loc}) :: _ ->
{
compName;
props = List.rev acc;
childrenStart =
(if pexp_loc.loc_ghost then None else Some (Loc.start pexp_loc));
}
| ((Labelled s | Optional s), (eProp : Parsetree.expression)) :: rest -> (
let namedArgLoc =
eProp.pexp_attributes
|> List.find_opt (fun ({Asttypes.txt}, _) -> txt = "ns.namedArgLoc")
in
match namedArgLoc with
| Some ({loc}, _) ->
processProps
~acc:
({
name = s;
posStart = Loc.start loc;
posEnd = Loc.end_ loc;
exp = eProp;
}
:: acc)
rest
| None -> processProps ~acc rest)
| _ -> thisCaseShouldNotHappen
in
args |> processProps ~acc:[]
let extractCompletableArgValueInfo exp =
match exp.Parsetree.pexp_desc with
| Pexp_ident {txt = Lident txt} -> Some txt
| Pexp_construct ({txt = Lident "()"}, _) -> Some ""
| Pexp_construct ({txt = Lident txt}, _) -> Some txt
| _ -> None
let isExprHole exp =
match exp.Parsetree.pexp_desc with
| Pexp_extension ({txt = "rescript.exprhole"}, _) -> true
| _ -> false
let findArgCompletables ~(args : arg list) ~endPos ~posBeforeCursor
~(contextPath : Completable.contextPath) ~posAfterFunExpr ~charBeforeCursor
~isPipedExpr =
let fnHasCursor =
posAfterFunExpr <= posBeforeCursor && posBeforeCursor < endPos
in
let allNames =
List.fold_right
(fun arg allLabels ->
match arg with
| {label = Some labelled} -> labelled.name :: allLabels
| {label = None} -> allLabels)
args []
in
let unlabelledCount = ref (if isPipedExpr then 1 else 0) in
let rec loop args =
match args with
| {label = Some labelled; exp} :: rest ->
if
labelled.posStart <= posBeforeCursor
&& posBeforeCursor < labelled.posEnd
then Some (Completable.CnamedArg (contextPath, labelled.name, allNames))
else if exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then
(* Completing in the assignment of labelled argument *)
match extractCompletableArgValueInfo exp with
| None -> None
| Some prefix ->
Some
(Cargument
{
functionContextPath = contextPath;
argumentLabel = Labelled labelled.name;
prefix;
})
else if isExprHole exp then
Some
(Cargument
{
functionContextPath = contextPath;
argumentLabel = Labelled labelled.name;
prefix = "";
})
else loop rest
| {label = None; exp} :: rest ->
if Res_parsetree_viewer.isTemplateLiteral exp then None
else if exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then
(* Completing in an unlabelled argument *)
match extractCompletableArgValueInfo exp with
| None -> None
| Some prefix ->
Some
(Cargument
{
functionContextPath = contextPath;
argumentLabel =
Unlabelled {argumentPosition = !unlabelledCount};
prefix;
})
else if isExprHole exp then
Some
(Cargument
{
functionContextPath = contextPath;
argumentLabel = Unlabelled {argumentPosition = !unlabelledCount};
prefix = "";
})
else (
unlabelledCount := !unlabelledCount + 1;
loop rest)
| [] ->
if fnHasCursor then
match charBeforeCursor with
| Some '~' -> Some (Completable.CnamedArg (contextPath, "", allNames))
| _ ->
Some
(Cargument
{
functionContextPath = contextPath;
argumentLabel =
Unlabelled {argumentPosition = !unlabelledCount};
prefix = "";
})
else None
in
match args with
(* Special handling for empty fn calls, e.g. `let _ = someFn(<com>)` *)
| [
{label = None; exp = {pexp_desc = Pexp_construct ({txt = Lident "()"}, _)}};
]
when fnHasCursor ->
Some
(Completable.Cargument
{
functionContextPath = contextPath;
argumentLabel = Unlabelled {argumentPosition = 0};
prefix = "";
})
| _ -> loop args
let rec exprToContextPath (e : Parsetree.expression) =
match e.pexp_desc with
| Pexp_constant (Pconst_string _) -> Some Completable.CPString
| Pexp_constant (Pconst_integer _) -> Some CPInt
| Pexp_constant (Pconst_float _) -> Some CPFloat
| Pexp_array _ -> Some CPArray
| Pexp_ident {txt} -> Some (CPId (Utils.flattenLongIdent txt, Value))
| Pexp_field (e1, {txt = Lident name}) -> (
match exprToContextPath e1 with
| Some contextPath -> Some (CPField (contextPath, name))
| _ -> None)
| Pexp_field (_, {txt = Ldot (lid, name)}) ->
(* Case x.M.field ignore the x part *)
Some (CPField (CPId (Utils.flattenLongIdent lid, Module), name))
| Pexp_send (e1, {txt}) -> (
match exprToContextPath e1 with
| None -> None
| Some contexPath -> Some (CPObj (contexPath, txt)))
| Pexp_apply (e1, args) -> (
match exprToContextPath e1 with
| None -> None
| Some contexPath -> Some (CPApply (contexPath, args |> List.map fst)))
| _ -> None
let completePipeChain ~(lhs : Parsetree.expression) =
(* Complete the end of pipe chains by reconstructing the pipe chain as a single pipe,
so it can be completed.
Example:
someArray->Js.Array2.filter(v => v > 10)->Js.Array2.map(v => v + 2)->
will complete as:
Js.Array2.map(someArray->Js.Array2.filter(v => v > 10), v => v + 2)->
*)
match lhs.pexp_desc with
(* When the left side of the pipe we're completing is a function application.
Example: someArray->Js.Array2.map(v => v + 2)-> *)
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
[
(_, lhs);
(_, {pexp_desc = Pexp_apply (d, args); pexp_loc; pexp_attributes});
] ) ->
exprToContextPath
{
pexp_desc = Pexp_apply (d, (Nolabel, lhs) :: args);
pexp_loc;
pexp_attributes;
}
|> Option.map (fun ctxPath -> (ctxPath, d.pexp_loc))
(* When the left side of the pipe we're completing is an identifier application.
Example: someArray->filterAllTheGoodStuff-> *)
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
[(_, lhs); (_, {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes})]
) ->
exprToContextPath
{
pexp_desc =
Pexp_apply
( {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes},
[(Nolabel, lhs)] );
pexp_loc;
pexp_attributes;
}
|> Option.map (fun ctxPath -> (ctxPath, pexp_loc))
| _ -> None
let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
let offsetNoWhite = skipWhite text (offset - 1) in
let posNoWhite =
let line, col = posCursor in
(line, max 0 col - offset + offsetNoWhite)
in
let posBeforeCursor = Pos.posBeforeCursor posCursor in
let charBeforeCursor, blankAfterCursor =
match Pos.positionToOffset text posCursor with
| Some offset when offset > 0 -> (
let charBeforeCursor = text.[offset - 1] in
let charAtCursor =
if offset < String.length text then text.[offset] else '\n'
in
match charAtCursor with
| ' ' | '\t' | '\r' | '\n' ->
(Some charBeforeCursor, Some charBeforeCursor)
| _ -> (Some charBeforeCursor, None))
| _ -> (None, None)
in
let flattenLidCheckDot ?(jsx = true) (lid : Longident.t Location.loc) =
(* Flatten an identifier keeping track of whether the current cursor
is after a "." in the id followed by a blank character.
In that case, cut the path after ".". *)
let cutAtOffset =
let idStart = Loc.start lid.loc in
match blankAfterCursor with
| Some '.' ->
if fst posBeforeCursor = fst idStart then
Some (snd posBeforeCursor - snd idStart)
else None
| _ -> None
in
Utils.flattenLongIdent ~cutAtOffset ~jsx lid.txt
in
let found = ref false in
let result = ref None in
let scope = ref (Scope.create ()) in
let setResultOpt x =
if !result = None then
match x with
| None -> ()
| Some x -> result := Some (x, !scope)
in
let setResult x = setResultOpt (Some x) in
let scopeValueDescription (vd : Parsetree.value_description) =
scope :=
!scope |> Scope.addValue ~name:vd.pval_name.txt ~loc:vd.pval_name.loc
in
let rec scopePattern (pat : Parsetree.pattern) =
match pat.ppat_desc with
| Ppat_any -> ()
| Ppat_var {txt; loc} -> scope := !scope |> Scope.addValue ~name:txt ~loc
| Ppat_alias (p, asA) ->
scopePattern p;
scope := !scope |> Scope.addValue ~name:asA.txt ~loc:asA.loc
| Ppat_constant _ | Ppat_interval _ -> ()
| Ppat_tuple pl -> pl |> List.iter scopePattern
| Ppat_construct (_, None) -> ()
| Ppat_construct (_, Some p) -> scopePattern p
| Ppat_variant (_, None) -> ()
| Ppat_variant (_, Some p) -> scopePattern p
| Ppat_record (fields, _) ->
fields |> List.iter (fun (_, p) -> scopePattern p)
| Ppat_array pl -> pl |> List.iter scopePattern
| Ppat_or (p1, _) -> scopePattern p1
| Ppat_constraint (p, _) -> scopePattern p
| Ppat_type _ -> ()
| Ppat_lazy p -> scopePattern p
| Ppat_unpack {txt; loc} -> scope := !scope |> Scope.addValue ~name:txt ~loc
| Ppat_exception p -> scopePattern p
| Ppat_extension _ -> ()
| Ppat_open (_, p) -> scopePattern p
in
let scopeValueBinding (vb : Parsetree.value_binding) =
scopePattern vb.pvb_pat
in
let scopeTypeKind (tk : Parsetree.type_kind) =
match tk with
| Ptype_variant constrDecls ->
constrDecls
|> List.iter (fun (cd : Parsetree.constructor_declaration) ->
scope :=
!scope
|> Scope.addConstructor ~name:cd.pcd_name.txt ~loc:cd.pcd_loc)
| Ptype_record labelDecls ->
labelDecls
|> List.iter (fun (ld : Parsetree.label_declaration) ->
scope :=
!scope |> Scope.addField ~name:ld.pld_name.txt ~loc:ld.pld_loc)
| _ -> ()
in
let scopeTypeDeclaration (td : Parsetree.type_declaration) =
scope :=
!scope |> Scope.addType ~name:td.ptype_name.txt ~loc:td.ptype_name.loc;
scopeTypeKind td.ptype_kind
in
let scopeModuleBinding (mb : Parsetree.module_binding) =
scope :=
!scope |> Scope.addModule ~name:mb.pmb_name.txt ~loc:mb.pmb_name.loc
in
let scopeModuleDeclaration (md : Parsetree.module_declaration) =
scope :=
!scope |> Scope.addModule ~name:md.pmd_name.txt ~loc:md.pmd_name.loc
in
let case (iterator : Ast_iterator.iterator) (case : Parsetree.case) =
let oldScope = !scope in
scopePattern case.pc_lhs;
Ast_iterator.default_iterator.case iterator case;
scope := oldScope
in
let structure (iterator : Ast_iterator.iterator)
(structure : Parsetree.structure) =
let oldScope = !scope in
Ast_iterator.default_iterator.structure iterator structure;
scope := oldScope
in
let structure_item (iterator : Ast_iterator.iterator)
(item : Parsetree.structure_item) =
let processed = ref false in
(match item.pstr_desc with
| Pstr_open {popen_lid} ->
scope := !scope |> Scope.addOpen ~lid:popen_lid.txt
| Pstr_primitive vd -> scopeValueDescription vd
| Pstr_value (recFlag, bindings) ->
if recFlag = Recursive then bindings |> List.iter scopeValueBinding;
bindings |> List.iter (fun vb -> iterator.value_binding iterator vb);
if recFlag = Nonrecursive then bindings |> List.iter scopeValueBinding;
processed := true
| Pstr_type (recFlag, decls) ->
if recFlag = Recursive then decls |> List.iter scopeTypeDeclaration;
decls |> List.iter (fun td -> iterator.type_declaration iterator td);
if recFlag = Nonrecursive then decls |> List.iter scopeTypeDeclaration;
processed := true
| Pstr_module mb ->
iterator.module_binding iterator mb;
scopeModuleBinding mb;
processed := true
| Pstr_recmodule mbs ->
mbs |> List.iter scopeModuleBinding;
mbs |> List.iter (fun b -> iterator.module_binding iterator b);
processed := true
| _ -> ());
if not !processed then
Ast_iterator.default_iterator.structure_item iterator item
in
let signature (iterator : Ast_iterator.iterator)
(signature : Parsetree.signature) =
let oldScope = !scope in
Ast_iterator.default_iterator.signature iterator signature;
scope := oldScope
in
let signature_item (iterator : Ast_iterator.iterator)
(item : Parsetree.signature_item) =
let processed = ref false in
(match item.psig_desc with
| Psig_open {popen_lid} ->
scope := !scope |> Scope.addOpen ~lid:popen_lid.txt
| Psig_value vd -> scopeValueDescription vd
| Psig_type (recFlag, decls) ->
if recFlag = Recursive then decls |> List.iter scopeTypeDeclaration;
decls |> List.iter (fun td -> iterator.type_declaration iterator td);
if recFlag = Nonrecursive then decls |> List.iter scopeTypeDeclaration;
processed := true
| Psig_module md ->
iterator.module_declaration iterator md;
scopeModuleDeclaration md;
processed := true
| Psig_recmodule mds ->
mds |> List.iter scopeModuleDeclaration;
mds |> List.iter (fun d -> iterator.module_declaration iterator d);
processed := true
| _ -> ());
if not !processed then
Ast_iterator.default_iterator.signature_item iterator item
in
let attribute (iterator : Ast_iterator.iterator)
((id, payload) : Parsetree.attribute) =
(if String.length id.txt >= 3 && String.sub id.txt 0 3 = "ns." then
(* skip: internal parser attribute *) ()
else if id.loc.loc_ghost then ()
else if id.loc |> Loc.hasPos ~pos:posBeforeCursor then
let posStart, posEnd = Loc.range id.loc in
match
(Pos.positionToOffset text posStart, Pos.positionToOffset text posEnd)
with
| Some offsetStart, Some offsetEnd ->
(* Can't trust the parser's location
E.g. @foo. let x... gives as label @foo.let *)
let label =
let rawLabel =
String.sub text offsetStart (offsetEnd - offsetStart)
in
let ( ++ ) x y =
match (x, y) with
| Some i1, Some i2 -> Some (min i1 i2)
| Some _, None -> x
| None, _ -> y
in
let label =
match
String.index_opt rawLabel ' '
++ String.index_opt rawLabel '\t'
++ String.index_opt rawLabel '\r'
++ String.index_opt rawLabel '\n'
with
| None -> rawLabel
| Some i -> String.sub rawLabel 0 i
in
if label <> "" && label.[0] = '@' then
String.sub label 1 (String.length label - 1)
else label
in
found := true;
if debug then
Printf.printf "Attribute id:%s:%s label:%s\n" id.txt
(Loc.toString id.loc) label;
setResult (Completable.Cdecorator label)
| _ -> ());
Ast_iterator.default_iterator.attribute iterator (id, payload)
in
let expr (iterator : Ast_iterator.iterator) (expr : Parsetree.expression) =
let processed = ref false in
let setFound () =
found := true;
if debug then
Printf.printf "posCursor:[%s] posNoWhite:[%s] Found expr:%s\n"
(Pos.toString posCursor) (Pos.toString posNoWhite)
(Loc.toString expr.pexp_loc)
in
let setPipeResult ~(lhs : Parsetree.expression) ~id =
match completePipeChain ~lhs with
| None -> (
match exprToContextPath lhs with
| Some pipe ->
setResult
(Cpath (CPPipe {contextPath = pipe; id; lhsLoc = lhs.pexp_loc}));
true
| None -> false)
| Some (pipe, lhsLoc) ->
setResult (Cpath (CPPipe {contextPath = pipe; id; lhsLoc}));
true
in
match expr.pexp_desc with
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident "|."; loc = opLoc}},
[
(_, lhs);
(_, {pexp_desc = Pexp_extension _; pexp_loc = {loc_ghost = true}});
] )
when opLoc |> Loc.hasPos ~pos:posBeforeCursor ->
(* Case foo-> when the parser adds a ghost expression to the rhs
so the apply expression does not include the cursor *)
if setPipeResult ~lhs ~id:"" then setFound ()
| _ ->
if expr.pexp_loc |> Loc.hasPos ~pos:posNoWhite && !result = None then (
setFound ();
match expr.pexp_desc with
| Pexp_constant _ -> setResult Cnone
| Pexp_ident lid ->
let lidPath = flattenLidCheckDot lid in
if debug then
Printf.printf "Pexp_ident %s:%s\n"
(lidPath |> String.concat ".")
(Loc.toString lid.loc);
if lid.loc |> Loc.hasPos ~pos:posBeforeCursor then
setResult (Cpath (CPId (lidPath, Value)))
| Pexp_construct (lid, eOpt) ->
let lidPath = flattenLidCheckDot lid in
if debug then
Printf.printf "Pexp_construct %s:%s %s\n"
(lidPath |> String.concat "\n")
(Loc.toString lid.loc)
(match eOpt with
| None -> "None"
| Some e -> Loc.toString e.pexp_loc);
if
eOpt = None && (not lid.loc.loc_ghost)
&& lid.loc |> Loc.hasPos ~pos:posBeforeCursor
then setResult (Cpath (CPId (lidPath, Value)))
| Pexp_field (e, fieldName) -> (
if debug then
Printf.printf "Pexp_field %s %s:%s\n" (Loc.toString e.pexp_loc)
(Utils.flattenLongIdent fieldName.txt |> String.concat ".")
(Loc.toString fieldName.loc);
if fieldName.loc |> Loc.hasPos ~pos:posBeforeCursor then
match fieldName.txt with
| Lident name -> (
match exprToContextPath e with
| Some contextPath ->
let contextPath = Completable.CPField (contextPath, name) in
setResult (Cpath contextPath)
| None -> ())
| Ldot (id, name) ->
(* Case x.M.field ignore the x part *)
let contextPath =
Completable.CPField
( CPId (Utils.flattenLongIdent id, Module),
if blankAfterCursor = Some '.' then
(* x.M. field ---> M. *) ""
else if name = "_" then ""
else name )
in
setResult (Cpath contextPath)
| Lapply _ -> ()
else if Loc.end_ e.pexp_loc = posBeforeCursor then
match exprToContextPath e with
| Some contextPath -> setResult (Cpath (CPField (contextPath, "")))
| None -> ())
| Pexp_apply ({pexp_desc = Pexp_ident compName}, args)
when Res_parsetree_viewer.isJsxExpression expr ->
let jsxProps = extractJsxProps ~compName ~args in
let compNamePath = flattenLidCheckDot ~jsx:true compName in
if debug then
Printf.printf "JSX <%s:%s %s> _children:%s\n"
(compNamePath |> String.concat ".")
(Loc.toString compName.loc)
(jsxProps.props
|> List.map (fun {name; posStart; posEnd; exp} ->
Printf.sprintf "%s[%s->%s]=...%s" name
(Pos.toString posStart) (Pos.toString posEnd)
(Loc.toString exp.pexp_loc))
|> String.concat " ")
(match jsxProps.childrenStart with
| None -> "None"
| Some childrenPosStart -> Pos.toString childrenPosStart);
let jsxCompletable =
findJsxPropsCompletable ~jsxProps ~endPos:(Loc.end_ expr.pexp_loc)
~posBeforeCursor ~posAfterCompName:(Loc.end_ compName.loc)
in
if jsxCompletable <> None then setResultOpt jsxCompletable
else if compName.loc |> Loc.hasPos ~pos:posBeforeCursor then
setResult (Cpath (CPId (compNamePath, Module)))
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
[
(_, lhs);
(_, {pexp_desc = Pexp_ident {txt = Longident.Lident id; loc}});
] )
when loc |> Loc.hasPos ~pos:posBeforeCursor ->
(* Case foo->id *)
setPipeResult ~lhs ~id |> ignore
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident "|."; loc = opLoc}},
[(_, lhs); _] )
when Loc.end_ opLoc = posCursor ->
(* Case foo-> *)
setPipeResult ~lhs ~id:"" |> ignore
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
[_; (_, {pexp_desc = Pexp_apply (funExpr, args)})] )
when (* Normally named arg completion fires when the cursor is right after the expression.
E.g in foo(~<---there
But it should not fire in foo(~a)<---there *)
not
(Loc.end_ expr.pexp_loc = posCursor
&& charBeforeCursor = Some ')') ->
(* Complete fn argument values and named args when the fn call is piped. E.g. someVar->someFn(<com>). *)
let args = extractExpApplyArgs ~args in
let argCompletable =
match exprToContextPath funExpr with
| Some contextPath ->
findArgCompletables ~contextPath ~args
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
~posAfterFunExpr:(Loc.end_ funExpr.pexp_loc)
~charBeforeCursor ~isPipedExpr:true
| None -> None
in
setResultOpt argCompletable
| Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident "|."}}, [_; _]) ->
(* Ignore any other pipe. *)
()
| Pexp_apply (funExpr, args)
when not
(Loc.end_ expr.pexp_loc = posCursor
&& charBeforeCursor = Some ')') ->
(* Complete fn argument values and named args when the fn call is _not_ piped. E.g. someFn(<com>). *)
let args = extractExpApplyArgs ~args in
if debug then
Printf.printf "Pexp_apply ...%s (%s)\n"
(Loc.toString funExpr.pexp_loc)
(args
|> List.map (fun {label; exp} ->
Printf.sprintf "%s...%s"
(match label with
| None -> ""
| Some {name; opt; posStart; posEnd} ->
"~" ^ name ^ Pos.toString posStart ^ "->"
^ Pos.toString posEnd ^ "="
^ if opt then "?" else "")
(Loc.toString exp.pexp_loc))
|> String.concat ", ");
let argCompletable =
match exprToContextPath funExpr with
| Some contextPath ->
findArgCompletables ~contextPath ~args
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
~posAfterFunExpr:(Loc.end_ funExpr.pexp_loc)
~charBeforeCursor ~isPipedExpr:false
| None -> None
in
setResultOpt argCompletable
| Pexp_send (lhs, {txt; loc}) -> (
(* e["txt"]
If the string for txt is not closed, it could go over several lines.
Only take the first like to represent the label *)
let txtLines = txt |> String.split_on_char '\n' in
let label = List.hd txtLines in
let label =
if label <> "" && label.[String.length label - 1] = '\r' then
String.sub label 0 (String.length label - 1)
else label
in
let labelRange =
let l, c = Loc.start loc in
((l, c + 1), (l, c + 1 + String.length label))
in
if debug then
Printf.printf "Pexp_send %s%s e:%s\n" label
(Range.toString labelRange)
(Loc.toString lhs.pexp_loc);
if
labelRange |> Range.hasPos ~pos:posBeforeCursor
|| (label = "" && posCursor = fst labelRange)
then
match exprToContextPath lhs with
| Some contextPath -> setResult (Cpath (CPObj (contextPath, label)))
| None -> ())
| Pexp_fun (_lbl, defaultExpOpt, pat, e) ->
let oldScope = !scope in
(match defaultExpOpt with
| None -> ()
| Some defaultExp -> iterator.expr iterator defaultExp);
scopePattern pat;
iterator.pat iterator pat;
iterator.expr iterator e;
scope := oldScope;
processed := true
| Pexp_let (recFlag, bindings, e) ->
let oldScope = !scope in
if recFlag = Recursive then bindings |> List.iter scopeValueBinding;
bindings |> List.iter (fun vb -> iterator.value_binding iterator vb);
if recFlag = Nonrecursive then bindings |> List.iter scopeValueBinding;
iterator.expr iterator e;
scope := oldScope;
processed := true
| Pexp_letmodule (name, modExpr, modBody) ->
let oldScope = !scope in
iterator.location iterator name.loc;
iterator.module_expr iterator modExpr;
scope := !scope |> Scope.addModule ~name:name.txt ~loc:name.loc;
iterator.expr iterator modBody;
scope := oldScope;
processed := true
| Pexp_open (_, lid, e) ->
let oldScope = !scope in
iterator.location iterator lid.loc;
scope := !scope |> Scope.addOpen ~lid:lid.txt;
iterator.expr iterator e;
scope := oldScope;
processed := true
| _ -> ());
if not !processed then Ast_iterator.default_iterator.expr iterator expr
in
let typ (iterator : Ast_iterator.iterator) (core_type : Parsetree.core_type) =
if core_type.ptyp_loc |> Loc.hasPos ~pos:posNoWhite then (
found := true;
if debug then
Printf.printf "posCursor:[%s] posNoWhite:[%s] Found type:%s\n"
(Pos.toString posCursor) (Pos.toString posNoWhite)
(Loc.toString core_type.ptyp_loc);
match core_type.ptyp_desc with
| Ptyp_constr (lid, _args) ->
let lidPath = flattenLidCheckDot lid in
if debug then
Printf.printf "Ptyp_constr %s:%s\n"
(lidPath |> String.concat ".")
(Loc.toString lid.loc);
if lid.loc |> Loc.hasPos ~pos:posBeforeCursor then
setResult (Cpath (CPId (lidPath, Type)))
| _ -> ());
Ast_iterator.default_iterator.typ iterator core_type
in
let pat (iterator : Ast_iterator.iterator) (pat : Parsetree.pattern) =
if pat.ppat_loc |> Loc.hasPos ~pos:posNoWhite then (
found := true;
if debug then
Printf.printf "posCursor:[%s] posNoWhite:[%s] Found pattern:%s\n"
(Pos.toString posCursor) (Pos.toString posNoWhite)
(Loc.toString pat.ppat_loc);
(match pat.ppat_desc with
| Ppat_construct (lid, _) ->
let lidPath = flattenLidCheckDot lid in
if debug then
Printf.printf "Ppat_construct %s:%s\n"
(lidPath |> String.concat ".")
(Loc.toString lid.loc);
setResult (Cpath (CPId (lidPath, Value)))
| _ -> ());
Ast_iterator.default_iterator.pat iterator pat)
in
let module_expr (iterator : Ast_iterator.iterator)
(me : Parsetree.module_expr) =
(match me.pmod_desc with
| Pmod_ident lid when lid.loc |> Loc.hasPos ~pos:posBeforeCursor ->
let lidPath = flattenLidCheckDot lid in
if debug then
Printf.printf "Pmod_ident %s:%s\n"
(lidPath |> String.concat ".")
(Loc.toString lid.loc);
found := true;
setResult (Cpath (CPId (lidPath, Module)))
| _ -> ());
Ast_iterator.default_iterator.module_expr iterator me
in
let module_type (iterator : Ast_iterator.iterator)
(mt : Parsetree.module_type) =
(match mt.pmty_desc with
| Pmty_ident lid when lid.loc |> Loc.hasPos ~pos:posBeforeCursor ->
let lidPath = flattenLidCheckDot lid in
if debug then
Printf.printf "Pmty_ident %s:%s\n"
(lidPath |> String.concat ".")
(Loc.toString lid.loc);
found := true;
setResult (Cpath (CPId (lidPath, Module)))
| _ -> ());
Ast_iterator.default_iterator.module_type iterator mt
in
let type_kind (iterator : Ast_iterator.iterator)
(type_kind : Parsetree.type_kind) =
(match type_kind with
| Ptype_variant [decl]
when decl.pcd_name.loc |> Loc.hasPos ~pos:posNoWhite
&& decl.pcd_args = Pcstr_tuple [] ->
(* "type t = Pre" could signal the intent to complete variant "Prelude",
or the beginning of "Prefix.t" *)
if debug then
Printf.printf "Ptype_variant unary %s:%s\n" decl.pcd_name.txt
(Loc.toString decl.pcd_name.loc);
found := true;
setResult (Cpath (CPId ([decl.pcd_name.txt], Value)))
| _ -> ());
Ast_iterator.default_iterator.type_kind iterator type_kind
in
let lastScopeBeforeCursor = ref (Scope.create ()) in
let location (_iterator : Ast_iterator.iterator) (loc : Location.t) =
if Loc.end_ loc <= posCursor then lastScopeBeforeCursor := !scope
in
let iterator =
{
Ast_iterator.default_iterator with
attribute;
case;
expr;
location;
module_expr;
module_type;
pat;
signature;
signature_item;
structure;
structure_item;
typ;
type_kind;
}
in
if Filename.check_suffix path ".res" then (
let parser =
Res_driver.parsingEngine.parseImplementation ~forPrinter:false
in
let {Res_driver.parsetree = str} = parser ~filename:currentFile in
iterator.structure iterator str |> ignore;
if blankAfterCursor = Some ' ' || blankAfterCursor = Some '\n' then (
scope := !lastScopeBeforeCursor;
setResult (Cpath (CPId ([""], Value))));
if !found = false then if debug then Printf.printf "XXX Not found!\n";
!result)
else if Filename.check_suffix path ".resi" then (
let parser = Res_driver.parsingEngine.parseInterface ~forPrinter:false in
let {Res_driver.parsetree = signature} = parser ~filename:currentFile in
iterator.signature iterator signature |> ignore;
if blankAfterCursor = Some ' ' || blankAfterCursor = Some '\n' then (
scope := !lastScopeBeforeCursor;
setResult (Cpath (CPId ([""], Type))));
if !found = false then if debug then Printf.printf "XXX Not found!\n";
!result)
else None
let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
match Pos.positionToOffset text posCursor with
| Some offset ->
completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text
| None -> None