From de32247f30fb4aadf2bf665ac8273318b27e549a Mon Sep 17 00:00:00 2001 From: Rasmus Nygren Date: Sat, 22 Nov 2025 12:40:38 +0100 Subject: [PATCH] [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. --- crates/ty_ide/src/completion.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/ty_ide/src/completion.rs b/crates/ty_ide/src/completion.rs index 26175462a3..29f4992c05 100644 --- a/crates/ty_ide/src/completion.rs +++ b/crates/ty_ide/src/completion.rs @@ -1315,7 +1315,8 @@ fn find_typed_text( if last.end() < offset || last.range().is_empty() { 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. @@ -1633,6 +1634,21 @@ mod tests { ); } + #[test] + fn inside_token() { + let test = completion_test_builder( + "\ +foo_bar_baz = 1 +x = foobad +", + ); + + assert_snapshot!( + test.skip_builtins().build().snapshot(), + @"foo_bar_baz", + ); + } + #[test] fn type_keyword_dedup() { let test = completion_test_builder(