Repository navigation
Expand file tree
/
Copy pathcompletion_patterns.ml
More file actions
282 lines (279 loc) · 10.5 KB
/
Copy pathcompletion_patterns.ml
File metadata and controls
282 lines (279 loc) · 10.5 KB
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
open Shared_types
let is_pattern_hole pat =
match pat.Parsetree.ppat_desc with
| Ppat_extension ({txt = "rescript.patternhole"}, _) -> true
| _ -> false
let rec traverse_tuple_items tuple_items ~next_pattern_path
~result_from_found_item_num ~loc_has_cursor
~first_char_before_cursor_no_white ~pos_before_cursor =
let item_num = ref (-1) in
let item_with_cursor =
tuple_items
|> List.find_map (fun pat ->
item_num := !item_num + 1;
pat
|> traverse_pattern
~pattern_path:(next_pattern_path !item_num)
~loc_has_cursor ~first_char_before_cursor_no_white
~pos_before_cursor)
in
match (item_with_cursor, first_char_before_cursor_no_white) with
| None, Some ',' ->
(* No tuple item has the cursor, but there's a comma before the cursor.
Figure out what arg we're trying to complete. Example: (true, <com>, None) *)
let pos_num = ref (-1) in
tuple_items
|> List.iteri (fun index pat ->
if pos_before_cursor >= Loc.start pat.Parsetree.ppat_loc then
pos_num := index);
if !pos_num > -1 then Some ("", result_from_found_item_num !pos_num)
else None
| v, _ -> v
and traverse_pattern (pat : Parsetree.pattern) ~pattern_path ~loc_has_cursor
~first_char_before_cursor_no_white ~pos_before_cursor =
let some_if_has_cursor v debug_id =
if loc_has_cursor pat.Parsetree.ppat_loc then (
if Debug.verbose () then
Printf.printf
"[traverse_pattern:some_if_has_cursor] '%s' has cursor, returning \n"
debug_id;
Some v)
else None
in
let rest_cursor (rest : Parsetree.record_pat_rest option) =
match rest with
| Some {rest_name = {txt; loc}; _} when loc_has_cursor loc ->
Some (`Name txt)
| Some {rest_loc; _} when loc_has_cursor rest_loc -> Some `Rest
| _ -> None
in
match pat.ppat_desc with
| Ppat_constant _ | Ppat_interval _ -> None
| Ppat_constraint (p, _) | Ppat_alias (p, _) | Ppat_exception p ->
p
|> traverse_pattern ~pattern_path ~loc_has_cursor
~first_char_before_cursor_no_white ~pos_before_cursor
| Ppat_or (p1, p2) -> (
let or_pat_with_item =
[p1; p2]
|> List.find_map (fun p ->
p
|> traverse_pattern ~pattern_path ~loc_has_cursor
~first_char_before_cursor_no_white ~pos_before_cursor)
in
match or_pat_with_item with
| None when is_pattern_hole p1 || is_pattern_hole p2 ->
if Debug.verbose () then
Printf.printf
"[traverse_pattern] found or-pattern that was pattern hole\n";
Some ("", pattern_path)
| v -> v)
| Ppat_any ->
(* We treat any `_` as an empty completion. This is mainly because we're
inserting `_` in snippets and automatically put the cursor there. So
letting it trigger an empty completion improves the ergonomics by a
lot. *)
some_if_has_cursor ("", pattern_path) "Ppat_any"
| Ppat_var {txt} -> some_if_has_cursor (txt, pattern_path) "Ppat_var"
| Ppat_construct ({txt = Lident "()"}, {txt = []}) ->
(* switch s { | (<com>) }*)
some_if_has_cursor
("", pattern_path @ [Completable.NTupleItem {item_num = 0}])
"Ppat_construct()"
| Ppat_construct ({txt = Lident prefix}, {txt = []}) ->
some_if_has_cursor (prefix, pattern_path) "Ppat_construct(Lident)"
| Ppat_variant (prefix, {txt = []}) ->
some_if_has_cursor ("#" ^ prefix, pattern_path) "Ppat_variant"
| Ppat_array array_patterns ->
let next_pattern_path = [Completable.NArray] @ pattern_path in
if List.length array_patterns = 0 && loc_has_cursor pat.ppat_loc then
Some ("", next_pattern_path)
else
array_patterns
|> List.find_map (fun pat ->
pat
|> traverse_pattern ~pattern_path:next_pattern_path ~loc_has_cursor
~first_char_before_cursor_no_white ~pos_before_cursor)
| Ppat_tuple tuple_items when loc_has_cursor pat.ppat_loc ->
tuple_items
|> traverse_tuple_items ~first_char_before_cursor_no_white
~pos_before_cursor ~loc_has_cursor
~next_pattern_path:(fun item_num ->
[Completable.NTupleItem {item_num}] @ pattern_path)
~result_from_found_item_num:(fun item_num ->
[Completable.NTupleItem {item_num = item_num + 1}] @ pattern_path)
| Ppat_record ([], _, rest) -> (
(* Empty fields means we're in a record body `{}`. Complete for the fields. *)
match rest_cursor rest with
| Some (`Name txt) -> Some (txt, pattern_path)
| Some `Rest -> None
| None ->
some_if_has_cursor
("", [Completable.NRecordBody {seen_fields = []}] @ pattern_path)
"Ppat_record(empty)")
| Ppat_dict entries -> (
(* Complete inside the pattern of the entry with the cursor *)
let entry_with_cursor =
match
entries
|> List.find_opt (fun {Parsetree.pdp_pattern} ->
pdp_pattern.ppat_loc
|> Cursor_position.classify_loc ~pos:pos_before_cursor
= HasCursor)
with
| Some entry -> Some entry
| None ->
entries
|> List.find_opt (fun {Parsetree.pdp_pattern} ->
is_pattern_hole pdp_pattern)
in
match entry_with_cursor with
| None -> None
| Some {pdp_pattern; pdp_optional} -> (
let pattern_path =
[Completable.NDictValue {optional = pdp_optional}] @ pattern_path
in
match pdp_pattern.ppat_desc with
| Ppat_extension ({txt = "rescript.patternhole"}, _) ->
some_if_has_cursor ("", pattern_path) "patternhole"
| _ ->
pdp_pattern
|> traverse_pattern ~pattern_path ~loc_has_cursor
~first_char_before_cursor_no_white ~pos_before_cursor))
| Ppat_record (fields, _, rest) -> (
let field_with_cursor = ref None in
let field_with_pat_hole = ref None in
Ext_list.iter fields (fun {lid = fname; x = f} ->
match
( fname.Location.txt,
f.Parsetree.ppat_loc
|> Cursor_position.classify_loc ~pos:pos_before_cursor )
with
| Longident.Lident fname, HasCursor ->
field_with_cursor := Some (fname, f)
| Lident fname, _ when is_pattern_hole f ->
field_with_pat_hole := Some (fname, f)
| _ -> ());
let seen_fields =
Ext_list.filter_map fields (fun {lid = field_name} ->
match field_name with
| {Location.txt = Longident.Lident field_name} -> Some field_name
| _ -> None)
in
match (rest_cursor rest, !field_with_cursor, !field_with_pat_hole) with
| Some (`Name txt), _, _ -> Some (txt, pattern_path)
| Some `Rest, _, _ -> None
| None, Some (fname, f), _ | None, None, Some (fname, f) -> (
match f.ppat_desc with
| Ppat_extension ({txt = "rescript.patternhole"}, _) ->
(* A pattern hole means for example `{someField: <com>}`. We want to complete for the type of `someField`. *)
some_if_has_cursor
( "",
[Completable.NFollowRecordField {field_name = fname}] @ pattern_path
)
"patternhole"
| Ppat_var {txt} ->
(* A var means `{s}` or similar. Complete for fields. *)
some_if_has_cursor
(txt, [Completable.NRecordBody {seen_fields}] @ pattern_path)
"Ppat_var #2"
| _ ->
f
|> traverse_pattern
~pattern_path:
([Completable.NFollowRecordField {field_name = fname}]
@ pattern_path)
~loc_has_cursor ~first_char_before_cursor_no_white
~pos_before_cursor)
| None, None, None -> (
(* Figure out if we're completing for a new field.
If the cursor is inside of the record body, but no field has the cursor,
and there's no pattern hole. Check the first char to the left of the cursor,
ignoring white space. If that's a comma, we assume you're completing for a new field. *)
match first_char_before_cursor_no_white with
| Some ',' ->
some_if_has_cursor
("", [Completable.NRecordBody {seen_fields}] @ pattern_path)
"first_char_before_cursor_no_white:,"
| _ -> None))
| Ppat_construct
( {txt},
{
txt = [{ppat_loc; ppat_desc = Ppat_construct ({txt = Lident "()"}, _)}];
} )
when loc_has_cursor ppat_loc ->
(* Empty payload with cursor, like: Test(<com>) *)
Some
( "",
[
Completable.NVariantPayload
{
constructor_name = Longident.last txt;
item_num = 0;
source_arity = 1;
};
]
@ pattern_path )
| Ppat_construct ({txt}, {txt = patterns}) when loc_has_cursor pat.ppat_loc ->
patterns
|> traverse_tuple_items ~loc_has_cursor ~first_char_before_cursor_no_white
~pos_before_cursor
~next_pattern_path:(fun item_num ->
[
Completable.NVariantPayload
{
constructor_name = Longident.last txt;
item_num;
source_arity = List.length patterns;
};
]
@ pattern_path)
~result_from_found_item_num:(fun item_num ->
[
Completable.NVariantPayload
{
constructor_name = Longident.last txt;
item_num = item_num + 1;
source_arity = List.length patterns;
};
]
@ pattern_path)
| Ppat_variant
( txt,
{
txt = [{ppat_loc; ppat_desc = Ppat_construct ({txt = Lident "()"}, _)}];
} )
when loc_has_cursor ppat_loc ->
(* Empty payload with cursor, like: #test(<com>) *)
Some
( "",
[
Completable.NPolyvariantPayload
{constructor_name = txt; item_num = 0; source_arity = 1};
]
@ pattern_path )
| Ppat_variant (txt, {txt = patterns}) when loc_has_cursor pat.ppat_loc ->
patterns
|> traverse_tuple_items ~loc_has_cursor ~first_char_before_cursor_no_white
~pos_before_cursor
~next_pattern_path:(fun item_num ->
[
Completable.NPolyvariantPayload
{
constructor_name = txt;
item_num;
source_arity = List.length patterns;
};
]
@ pattern_path)
~result_from_found_item_num:(fun item_num ->
[
Completable.NPolyvariantPayload
{
constructor_name = txt;
item_num = item_num + 1;
source_arity = List.length patterns;
};
]
@ pattern_path)
| _ -> None