editor: Limit snippet query range instead of collecting from buffer start (#39617)

Smit Barmase created

Fixes hang when computing query for snippet completions when working
with really large buffers.

Release Notes:

- N/A

Change summary

crates/editor/src/editor.rs | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)

Detailed changes

crates/editor/src/editor.rs 🔗

@@ -22794,9 +22794,6 @@ fn snippet_completions(
     }
 
     let snapshot = buffer.read(cx).text_snapshot();
-    let chars: String = snapshot
-        .reversed_chars_for_range(text::Anchor::MIN..buffer_position)
-        .collect();
     let executor = cx.background_executor().clone();
 
     cx.background_spawn(async move {
@@ -22805,11 +22802,16 @@ fn snippet_completions(
         for (scope, snippets) in scopes.into_iter() {
             let classifier =
                 CharClassifier::new(Some(scope)).scope_context(Some(CharScopeContext::Completion));
-            let mut last_word = chars
-                .chars()
+
+            const MAX_WORD_PREFIX_LEN: usize = 128;
+            let last_word: String = snapshot
+                .reversed_chars_for_range(text::Anchor::MIN..buffer_position)
+                .take(MAX_WORD_PREFIX_LEN)
                 .take_while(|c| classifier.is_word(*c))
-                .collect::<String>();
-            last_word = last_word.chars().rev().collect();
+                .collect::<String>()
+                .chars()
+                .rev()
+                .collect();
 
             if last_word.is_empty() {
                 return Ok(CompletionResponse {