From 80727a03bf2be92de046b2fb890f4eeb1224b253 Mon Sep 17 00:00:00 2001 From: Smit Barmase Date: Mon, 6 Oct 2025 20:54:42 +0530 Subject: [PATCH] editor: Limit snippet query range instead of collecting from buffer start (#39617) Fixes hang when computing query for snippet completions when working with really large buffers. Release Notes: - N/A --- crates/editor/src/editor.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 7b987a6962ad9b02b2beafa92a744112c8244afc..296f6ca11de7756dc4e958877ee3e67bafb6cb2e 100644 --- a/crates/editor/src/editor.rs +++ b/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::(); - last_word = last_word.chars().rev().collect(); + .collect::() + .chars() + .rev() + .collect(); if last_word.is_empty() { return Ok(CompletionResponse {