[ty] Only suggest completions based on text before the cursor

Previously we extracted the entire token as the query
independently of the cursor position. By not doing that
you avoid having to do special range handling
to figure out the start position of the current token.

It's likely also more intuitive from a user perspective
to only consider characters left of the cursor when
suggesting autocompletions.
This commit is contained in:
Rasmus Nygren 2025-11-22 12:40:38 +01:00 committed by Andrew Gallant
parent 209ea06592
commit de32247f30
1 changed files with 17 additions and 1 deletions

View File

@ -1315,7 +1315,8 @@ fn find_typed_text(
if last.end() < offset || last.range().is_empty() { if last.end() < offset || last.range().is_empty() {
return None; return None;
} }
Some(source[last.range()].to_string()) let range = TextRange::new(last.start(), offset);
Some(source[range].to_string())
} }
/// Whether the last token is in a place where we should not provide completions. /// Whether the last token is in a place where we should not provide completions.
@ -1633,6 +1634,21 @@ mod tests {
); );
} }
#[test]
fn inside_token() {
let test = completion_test_builder(
"\
foo_bar_baz = 1
x = foo<CURSOR>bad
",
);
assert_snapshot!(
test.skip_builtins().build().snapshot(),
@"foo_bar_baz",
);
}
#[test] #[test]
fn type_keyword_dedup() { fn type_keyword_dedup() {
let test = completion_test_builder( let test = completion_test_builder(