Skip to content

Commit a7264cb

Browse files
committed
fix(rowan): better completion detection
1 parent 6a7a78a commit a7264cb

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

crates/rowan/src/query/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ impl Query {
200200
pub fn can_complete_op(&self) -> bool {
201201
// Deal with empty expressions first.
202202
if let Some(before) = &self.before {
203+
if before.syntax.parent().map(|e| e.kind()) == Some(EXPR_BLOCK) {
204+
return false;
205+
}
206+
203207
if let Some(exp_w) = before.expr_wrapper() {
204208
if let Some(binary_exp) = exp_w.parent() {
205209
if binary_exp.kind() == EXPR_BINARY {
@@ -219,6 +223,18 @@ impl Query {
219223
}
220224

221225
if let Some(after) = &self.after {
226+
if after.syntax.parent().map(|e| e.kind()) == Some(EXPR_BLOCK) {
227+
return false;
228+
}
229+
230+
if after.syntax.kind() == PUNCT_BRACE_END
231+
&& after.syntax.next_sibling_or_token().map(|t| t.kind()) == Some(LIT_STR)
232+
&& after.syntax.prev_sibling_or_token().map(|t| t.kind())
233+
== Some(LIT_STR_TEMPLATE_INTERPOLATION)
234+
{
235+
return true;
236+
}
237+
222238
if let Some(exp_w) = after.expr_wrapper() {
223239
if let Some(binary_exp) = exp_w.parent() {
224240
if binary_exp.kind() == EXPR_BINARY {

crates/rowan/src/query/tests.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ fn test_query_path_segment_index() {
177177
fn test_complete_ref() {
178178
let (offsets, src) = src_cursor_offsets(
179179
r#"$$
180+
fn asd() {
181+
if hello() is b {
182+
$$
183+
} else {
184+
$$
185+
}
186+
}
187+
180188
let a =$$ $$;
181189
$$
182190
@@ -190,6 +198,10 @@ fn test_complete_ref() {
190198
3*$$
191199
}
192200
201+
fn b() {
202+
a$$
203+
}
204+
193205
const bar = $$;
194206
195207
{$$
@@ -211,6 +223,8 @@ fn test_complete_ref() {
211223
fn test_complete_op() {
212224
let (offsets, src) = src_cursor_offsets(
213225
r#"
226+
a = `${a $$}`;
227+
214228
let a = 3 $$;
215229
216230
let b = a $$;
@@ -221,6 +235,7 @@ fn test_complete_op() {
221235
222236
let c = "foo" $$
223237
238+
let in_template = `a ${a $$} b`;
224239
"#,
225240
);
226241

@@ -234,9 +249,7 @@ fn test_complete_op() {
234249
assert!(q.can_complete_op(), "test failed for index {idx}",);
235250
}
236251

237-
let (offsets, src) = src_cursor_offsets(
238-
r#"a op 2 $$"#,
239-
);
252+
let (offsets, src) = src_cursor_offsets(r#"a op 2 $$"#);
240253

241254
let syntax = Parser::new(&src)
242255
.with_operator("op", Operator::default())
@@ -250,16 +263,24 @@ fn test_complete_op() {
250263
}
251264

252265
#[test]
253-
fn test_complete_op_fails() {
266+
fn test_op_completion_should_fail() {
254267
let (offsets, src) = src_cursor_offsets(
255268
r#"
256269
let a = $$ op foo;
257270
258271
let a = 3* $$;
259-
272+
260273
let a = 3*$$;
261-
274+
262275
let b = $$ + $$;
276+
277+
fn asd() {
278+
if hello() + 2 {
279+
$$
280+
} else {
281+
$$
282+
}
283+
}
263284
"#,
264285
);
265286

0 commit comments

Comments
 (0)