Skip to content

Commit d153a91

Browse files
authored
Complete tagged template application (#7278)
* WIP tagged template completion * Ensure debug logging is behind flag * Include completions of alias * Turn off evenMoreCompletions * Add support for tagged template + dot in FrontEnd. * Revert change to pipe * Yeah this is the revert pipe thing * Revert other changes and commit test result * Remove local execution commands * Remove debug log * Change sample * Add changelog entry * Split up cases in frontend. * Add example
1 parent 7b601f2 commit d153a91

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- Fix async context checking for module await. https://github.com/rescript-lang/rescript/pull/7271
2222
- Fix `%external` extension. https://github.com/rescript-lang/rescript/pull/7272
2323
- Fix issue with type environment for unified ops. https://github.com/rescript-lang/rescript/pull/7277
24+
- Fix completion for application with tagged template. https://github.com/rescript-lang/rescript/pull/7278
2425

2526
# 12.0.0-alpha.8
2627

analysis/src/CompletionFrontEnd.ml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,65 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
11271127
(* Case foo-> when the parser adds a ghost expression to the rhs
11281128
so the apply expression does not include the cursor *)
11291129
if setPipeResult ~lhs ~id:"" then setFound ()
1130+
(*
1131+
A dot completion for a tagged templated application with an expr hole.
1132+
Example:
1133+
sh`echo "meh"`.
1134+
*)
1135+
| Pexp_apply
1136+
{
1137+
funct = {pexp_desc = Pexp_ident {txt = Lident "."; loc = _}};
1138+
args =
1139+
[
1140+
(* sh`echo "meh"` *)
1141+
(_, ({pexp_desc = Pexp_apply _} as innerExpr));
1142+
(* recovery inserted node *)
1143+
(_, {pexp_desc = Pexp_extension ({txt = "rescript.exprhole"}, _)});
1144+
];
1145+
}
1146+
when Res_parsetree_viewer.is_tagged_template_literal innerExpr ->
1147+
exprToContextPath innerExpr
1148+
|> Option.iter (fun cpath ->
1149+
setResult
1150+
(Cpath
1151+
(CPField
1152+
{
1153+
contextPath = cpath;
1154+
fieldName = "";
1155+
posOfDot;
1156+
exprLoc = expr.pexp_loc;
1157+
}));
1158+
setFound ())
1159+
(*
1160+
A dot completion for a tagged templated application with an ident.
1161+
Example:
1162+
sh`echo "meh"`.foo
1163+
*)
1164+
| Pexp_apply
1165+
{
1166+
funct = {pexp_desc = Pexp_ident {txt = Lident "."; loc = _}};
1167+
args =
1168+
[
1169+
(* sh`echo "meh"` *)
1170+
(_, ({pexp_desc = Pexp_apply _} as innerExpr));
1171+
(* foo *)
1172+
(_, {pexp_desc = Pexp_ident {txt = Lident fieldName}});
1173+
];
1174+
}
1175+
when Res_parsetree_viewer.is_tagged_template_literal innerExpr
1176+
&& expr.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor ->
1177+
exprToContextPath innerExpr
1178+
|> Option.iter (fun cpath ->
1179+
setResult
1180+
(Cpath
1181+
(CPField
1182+
{
1183+
contextPath = cpath;
1184+
fieldName;
1185+
posOfDot;
1186+
exprLoc = expr.pexp_loc;
1187+
}));
1188+
setFound ())
11301189
| _ -> (
11311190
if expr.pexp_loc |> Loc.hasPos ~pos:posNoWhite && !result = None then (
11321191
setFound ();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module M = {
2+
type t
3+
4+
let a = (_t:t) => 4
5+
let b = (_:t) => "c"
6+
let xyz = (_:t, p:int) => p + 1
7+
}
8+
9+
@module("meh") @taggedTemplate
10+
external meh: (array<string>, array<string>) => M.t = "default"
11+
12+
// let x = meh`foo`.
13+
// ^com
14+
15+
// let y = meh`bar`.x
16+
// ^com
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Complete src/CompletionTaggedTemplate.res 11:20
2+
posCursor:[11:20] posNoWhite:[11:19] Found expr:[11:11->0:-1]
3+
Completable: Cpath Value[meh](Nolabel, Nolabel).""
4+
Package opens Pervasives.JsxModules.place holder
5+
ContextPath Value[meh](Nolabel, Nolabel).""
6+
ContextPath Value[meh](Nolabel, Nolabel)
7+
ContextPath Value[meh]
8+
Path meh
9+
ContextPath Value[meh](Nolabel, Nolabel, Nolabel)->
10+
ContextPath Value[meh](Nolabel, Nolabel, Nolabel)
11+
ContextPath Value[meh]
12+
Path meh
13+
CPPipe pathFromEnv:M found:true
14+
Path M.
15+
[{
16+
"label": "->M.xyz",
17+
"kind": 12,
18+
"tags": [],
19+
"detail": "(t, int) => int",
20+
"documentation": null,
21+
"sortText": "xyz",
22+
"insertText": "->M.xyz",
23+
"additionalTextEdits": [{
24+
"range": {"start": {"line": 11, "character": 19}, "end": {"line": 11, "character": 20}},
25+
"newText": ""
26+
}]
27+
}, {
28+
"label": "->M.b",
29+
"kind": 12,
30+
"tags": [],
31+
"detail": "t => string",
32+
"documentation": null,
33+
"sortText": "b",
34+
"insertText": "->M.b",
35+
"additionalTextEdits": [{
36+
"range": {"start": {"line": 11, "character": 19}, "end": {"line": 11, "character": 20}},
37+
"newText": ""
38+
}]
39+
}, {
40+
"label": "->M.a",
41+
"kind": 12,
42+
"tags": [],
43+
"detail": "t => int",
44+
"documentation": null,
45+
"sortText": "a",
46+
"insertText": "->M.a",
47+
"additionalTextEdits": [{
48+
"range": {"start": {"line": 11, "character": 19}, "end": {"line": 11, "character": 20}},
49+
"newText": ""
50+
}]
51+
}]
52+
53+
Complete src/CompletionTaggedTemplate.res 14:21
54+
posCursor:[14:21] posNoWhite:[14:20] Found expr:[14:11->14:21]
55+
Completable: Cpath Value[meh](Nolabel, Nolabel).x
56+
Package opens Pervasives.JsxModules.place holder
57+
ContextPath Value[meh](Nolabel, Nolabel).x
58+
ContextPath Value[meh](Nolabel, Nolabel)
59+
ContextPath Value[meh]
60+
Path meh
61+
ContextPath Value[meh](Nolabel, Nolabel, Nolabel)->x
62+
ContextPath Value[meh](Nolabel, Nolabel, Nolabel)
63+
ContextPath Value[meh]
64+
Path meh
65+
CPPipe pathFromEnv:M found:true
66+
Path M.x
67+
[{
68+
"label": "->M.xyz",
69+
"kind": 12,
70+
"tags": [],
71+
"detail": "(t, int) => int",
72+
"documentation": null,
73+
"sortText": "xyz",
74+
"insertText": "->M.xyz",
75+
"additionalTextEdits": [{
76+
"range": {"start": {"line": 14, "character": 19}, "end": {"line": 14, "character": 20}},
77+
"newText": ""
78+
}]
79+
}]
80+

0 commit comments

Comments
 (0)