editor: Improve selection highlights speed (#29097)

Before, we used to debounce selection highlight because it needed to
search the whole file to show gutter line highlights, etc. This
experience felt extremely laggy.

This PR introduces a new approach where:
1. We query only visible rows without debounce. The search function
itself is async and runs in a background thread, so it's not blocking
anything. With no debounce and such a small search space, highlights
feel realtime.
2. In parallel, we also query the whole file (still debounced, like
before). Once this query resolves, it updates highlights across the
file, making scrollbar markers visible.

This hybrid way gives the feeling of realtime, while keeping the same
functionality.


https://github.com/user-attachments/assets/432b65f1-89d2-4658-ad5e-048921b06a23

P.S. I have removed the user setting for custom debounce delay, because
(one) now it doesn't really make sense to configure that, and (two) the
whole logic is based on the assumption that the fast query will resolve
before the debounced query. A static debounce time makes sure of that.
Configuring it might lead to cases where the fast query resolves after
the debounced query, and we end up only seeing visible viewport
highlights.

Release Notes:

- Improved selection highlight speed.
This commit is contained in:
Smit Barmase 2025-04-20 01:20:36 +05:30 committed by GitHub
parent 8f308d835a
commit f737c4d01e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 146 additions and 100 deletions

View file

@ -181,8 +181,6 @@
"current_line_highlight": "all", "current_line_highlight": "all",
// Whether to highlight all occurrences of the selected text in an editor. // Whether to highlight all occurrences of the selected text in an editor.
"selection_highlight": true, "selection_highlight": true,
// The debounce delay before querying highlights based on the selected text.
"selection_highlight_debounce": 50,
// The debounce delay before querying highlights from the language // The debounce delay before querying highlights from the language
// server based on the current cursor location. // server based on the current cursor location.
"lsp_highlight_debounce": 75, "lsp_highlight_debounce": 75,

View file

@ -215,6 +215,7 @@ const MAX_SELECTION_HISTORY_LEN: usize = 1024;
pub(crate) const CURSORS_VISIBLE_FOR: Duration = Duration::from_millis(2000); pub(crate) const CURSORS_VISIBLE_FOR: Duration = Duration::from_millis(2000);
#[doc(hidden)] #[doc(hidden)]
pub const CODE_ACTIONS_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(250); pub const CODE_ACTIONS_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(250);
const SELECTION_HIGHLIGHT_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(100);
pub(crate) const CODE_ACTION_TIMEOUT: Duration = Duration::from_secs(5); pub(crate) const CODE_ACTION_TIMEOUT: Duration = Duration::from_secs(5);
pub(crate) const FORMAT_TIMEOUT: Duration = Duration::from_secs(5); pub(crate) const FORMAT_TIMEOUT: Duration = Duration::from_secs(5);
@ -811,7 +812,8 @@ pub struct Editor {
next_completion_id: CompletionId, next_completion_id: CompletionId,
available_code_actions: Option<(Location, Rc<[AvailableCodeAction]>)>, available_code_actions: Option<(Location, Rc<[AvailableCodeAction]>)>,
code_actions_task: Option<Task<Result<()>>>, code_actions_task: Option<Task<Result<()>>>,
selection_highlight_task: Option<Task<()>>, quick_selection_highlight_task: Option<(Range<Anchor>, Task<()>)>,
debounced_selection_highlight_task: Option<(Range<Anchor>, Task<()>)>,
document_highlights_task: Option<Task<()>>, document_highlights_task: Option<Task<()>>,
linked_editing_range_task: Option<Task<Option<()>>>, linked_editing_range_task: Option<Task<Option<()>>>,
linked_edit_ranges: linked_editing_ranges::LinkedEditingRanges, linked_edit_ranges: linked_editing_ranges::LinkedEditingRanges,
@ -1590,7 +1592,8 @@ impl Editor {
code_action_providers, code_action_providers,
available_code_actions: Default::default(), available_code_actions: Default::default(),
code_actions_task: Default::default(), code_actions_task: Default::default(),
selection_highlight_task: Default::default(), quick_selection_highlight_task: Default::default(),
debounced_selection_highlight_task: Default::default(),
document_highlights_task: Default::default(), document_highlights_task: Default::default(),
linked_editing_range_task: Default::default(), linked_editing_range_task: Default::default(),
pending_rename: Default::default(), pending_rename: Default::default(),
@ -5475,111 +5478,168 @@ impl Editor {
None None
} }
pub fn refresh_selected_text_highlights( fn prepare_highlight_query_from_selection(
&mut self, &mut self,
window: &mut Window,
cx: &mut Context<Editor>, cx: &mut Context<Editor>,
) { ) -> Option<(String, Range<Anchor>)> {
if matches!(self.mode, EditorMode::SingleLine { .. }) { if matches!(self.mode, EditorMode::SingleLine { .. }) {
return; return None;
} }
self.selection_highlight_task.take();
if !EditorSettings::get_global(cx).selection_highlight { if !EditorSettings::get_global(cx).selection_highlight {
self.clear_background_highlights::<SelectedTextHighlight>(cx); return None;
return;
} }
if self.selections.count() != 1 || self.selections.line_mode { if self.selections.count() != 1 || self.selections.line_mode {
self.clear_background_highlights::<SelectedTextHighlight>(cx); return None;
return;
} }
let selection = self.selections.newest::<Point>(cx); let selection = self.selections.newest::<Point>(cx);
if selection.is_empty() || selection.start.row != selection.end.row { if selection.is_empty() || selection.start.row != selection.end.row {
self.clear_background_highlights::<SelectedTextHighlight>(cx); return None;
return;
} }
let debounce = EditorSettings::get_global(cx).selection_highlight_debounce; let multi_buffer_snapshot = self.buffer().read(cx).snapshot(cx);
self.selection_highlight_task = Some(cx.spawn_in(window, async move |editor, cx| { let selection_anchor_range = selection.range().to_anchors(&multi_buffer_snapshot);
cx.background_executor() let query = multi_buffer_snapshot
.timer(Duration::from_millis(debounce)) .text_for_range(selection_anchor_range.clone())
.await; .collect::<String>();
let Some(Some(matches_task)) = editor if query.trim().is_empty() {
.update_in(cx, |editor, _, cx| { return None;
if editor.selections.count() != 1 || editor.selections.line_mode { }
editor.clear_background_highlights::<SelectedTextHighlight>(cx); Some((query, selection_anchor_range))
return None; }
}
let selection = editor.selections.newest::<Point>(cx); fn update_selection_occurrence_highlights(
if selection.is_empty() || selection.start.row != selection.end.row { &mut self,
editor.clear_background_highlights::<SelectedTextHighlight>(cx); query_text: String,
return None; query_range: Range<Anchor>,
} multi_buffer_range_to_query: Range<Point>,
let buffer = editor.buffer().read(cx).snapshot(cx); use_debounce: bool,
let query = buffer.text_for_range(selection.range()).collect::<String>(); window: &mut Window,
if query.trim().is_empty() { cx: &mut Context<Editor>,
editor.clear_background_highlights::<SelectedTextHighlight>(cx); ) -> Task<()> {
return None; let multi_buffer_snapshot = self.buffer().read(cx).snapshot(cx);
} cx.spawn_in(window, async move |editor, cx| {
Some(cx.background_spawn(async move { if use_debounce {
let mut ranges = Vec::new(); cx.background_executor()
let selection_anchors = selection.range().to_anchors(&buffer); .timer(SELECTION_HIGHLIGHT_DEBOUNCE_TIMEOUT)
for range in [buffer.anchor_before(0)..buffer.anchor_after(buffer.len())] { .await;
for (search_buffer, search_range, excerpt_id) in }
buffer.range_to_buffer_ranges(range) let match_task = cx.background_spawn(async move {
{ let buffer_ranges = multi_buffer_snapshot
ranges.extend( .range_to_buffer_ranges(multi_buffer_range_to_query)
project::search::SearchQuery::text( .into_iter()
query.clone(), .filter(|(_, excerpt_visible_range, _)| !excerpt_visible_range.is_empty());
false, let mut match_ranges = Vec::new();
false, for (buffer_snapshot, search_range, excerpt_id) in buffer_ranges {
false, match_ranges.extend(
Default::default(), project::search::SearchQuery::text(
Default::default(), query_text.clone(),
None, false,
) false,
.unwrap() false,
.search(search_buffer, Some(search_range.clone())) Default::default(),
.await Default::default(),
.into_iter() None,
.filter_map( )
|match_range| { .unwrap()
let start = search_buffer.anchor_after( .search(&buffer_snapshot, Some(search_range.clone()))
search_range.start + match_range.start, .await
); .into_iter()
let end = search_buffer.anchor_before( .filter_map(|match_range| {
search_range.start + match_range.end, let match_start = buffer_snapshot
); .anchor_after(search_range.start + match_range.start);
let range = Anchor::range_in_buffer( let match_end =
excerpt_id, buffer_snapshot.anchor_before(search_range.start + match_range.end);
search_buffer.remote_id(), let match_anchor_range = Anchor::range_in_buffer(
start..end, excerpt_id,
); buffer_snapshot.remote_id(),
(range != selection_anchors).then_some(range) match_start..match_end,
}, );
), (match_anchor_range != query_range).then_some(match_anchor_range)
); }),
} );
} }
ranges match_ranges
})) });
}) let match_ranges = match_task.await;
.log_err()
else {
return;
};
let matches = matches_task.await;
editor editor
.update_in(cx, |editor, _, cx| { .update_in(cx, |editor, _, cx| {
editor.clear_background_highlights::<SelectedTextHighlight>(cx); editor.clear_background_highlights::<SelectedTextHighlight>(cx);
if !matches.is_empty() { if !match_ranges.is_empty() {
editor.highlight_background::<SelectedTextHighlight>( editor.highlight_background::<SelectedTextHighlight>(
&matches, &match_ranges,
|theme| theme.editor_document_highlight_bracket_background, |theme| theme.editor_document_highlight_bracket_background,
cx, cx,
) )
} }
}) })
.log_err(); .log_err();
})); })
}
fn refresh_selected_text_highlights(&mut self, window: &mut Window, cx: &mut Context<Editor>) {
let Some((query_text, query_range)) = self.prepare_highlight_query_from_selection(cx)
else {
self.clear_background_highlights::<SelectedTextHighlight>(cx);
self.quick_selection_highlight_task.take();
self.debounced_selection_highlight_task.take();
return;
};
let multi_buffer_snapshot = self.buffer().read(cx).snapshot(cx);
if self
.quick_selection_highlight_task
.as_ref()
.map_or(true, |(prev_anchor_range, _)| {
prev_anchor_range != &query_range
})
{
let multi_buffer_visible_start = self
.scroll_manager
.anchor()
.anchor
.to_point(&multi_buffer_snapshot);
let multi_buffer_visible_end = multi_buffer_snapshot.clip_point(
multi_buffer_visible_start
+ Point::new(self.visible_line_count().unwrap_or(0.).ceil() as u32, 0),
Bias::Left,
);
let multi_buffer_visible_range = multi_buffer_visible_start..multi_buffer_visible_end;
self.quick_selection_highlight_task = Some((
query_range.clone(),
self.update_selection_occurrence_highlights(
query_text.clone(),
query_range.clone(),
multi_buffer_visible_range,
false,
window,
cx,
),
));
}
if self
.debounced_selection_highlight_task
.as_ref()
.map_or(true, |(prev_anchor_range, _)| {
prev_anchor_range != &query_range
})
{
let multi_buffer_start = multi_buffer_snapshot
.anchor_before(0)
.to_point(&multi_buffer_snapshot);
let multi_buffer_end = multi_buffer_snapshot
.anchor_after(multi_buffer_snapshot.len())
.to_point(&multi_buffer_snapshot);
let multi_buffer_full_range = multi_buffer_start..multi_buffer_end;
self.debounced_selection_highlight_task = Some((
query_range.clone(),
self.update_selection_occurrence_highlights(
query_text,
query_range,
multi_buffer_full_range,
true,
window,
cx,
),
));
}
} }
pub fn refresh_inline_completion( pub fn refresh_inline_completion(

View file

@ -10,7 +10,6 @@ pub struct EditorSettings {
pub cursor_shape: Option<CursorShape>, pub cursor_shape: Option<CursorShape>,
pub current_line_highlight: CurrentLineHighlight, pub current_line_highlight: CurrentLineHighlight,
pub selection_highlight: bool, pub selection_highlight: bool,
pub selection_highlight_debounce: u64,
pub lsp_highlight_debounce: u64, pub lsp_highlight_debounce: u64,
pub hover_popover_enabled: bool, pub hover_popover_enabled: bool,
pub hover_popover_delay: u64, pub hover_popover_delay: u64,
@ -263,10 +262,6 @@ pub struct EditorSettingsContent {
/// ///
/// Default: true /// Default: true
pub selection_highlight: Option<bool>, pub selection_highlight: Option<bool>,
/// The debounce delay before querying highlights based on the selected text.
///
/// Default: 75
pub selection_highlight_debounce: Option<u64>,
/// The debounce delay before querying highlights from the language /// The debounce delay before querying highlights from the language
/// server based on the current cursor location. /// server based on the current cursor location.
/// ///

View file

@ -536,13 +536,6 @@ List of `string` values
- Setting: `selection_highlight` - Setting: `selection_highlight`
- Default: `true` - Default: `true`
## Selection Highlight Debounce
- Description: The debounce delay before querying highlights based on the selected text.
- Setting: `selection_highlight_debounce`
- Default: `50`
## LSP Highlight Debounce ## LSP Highlight Debounce
- Description: The debounce delay before querying highlights from the language server based on the current cursor location. - Description: The debounce delay before querying highlights from the language server based on the current cursor location.