editor: Move runnables querying to background thread (#11487)

Originally reported by @mrnugget and @bennetbo 
Also, instead of requerying them every frame, we do so whenever buffer
changes.

As a bonus, I modified tree-sitter query for Rust tests.

Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2024-05-07 15:31:07 +02:00 committed by GitHub
parent 4eca7875ae
commit 0c11d841e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 83 additions and 46 deletions

View file

@ -505,6 +505,7 @@ pub struct Editor {
last_bounds: Option<Bounds<Pixels>>, last_bounds: Option<Bounds<Pixels>>,
expect_bounds_change: Option<Bounds<Pixels>>, expect_bounds_change: Option<Bounds<Pixels>>,
tasks: HashMap<u32, RunnableTasks>, tasks: HashMap<u32, RunnableTasks>,
tasks_update_task: Option<Task<()>>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -1688,8 +1689,9 @@ impl Editor {
}); });
}), }),
], ],
tasks_update_task: None,
}; };
this.tasks_update_task = Some(this.refresh_runnables(cx));
this._subscriptions.extend(project_subscriptions); this._subscriptions.extend(project_subscriptions);
this.end_selection(cx); this.end_selection(cx);
@ -7687,25 +7689,68 @@ impl Editor {
self.select_larger_syntax_node_stack = stack; self.select_larger_syntax_node_stack = stack;
} }
fn runnable_display_rows( fn refresh_runnables(&mut self, cx: &mut ViewContext<Self>) -> Task<()> {
&self, let project = self.project.clone();
range: Range<Anchor>, cx.spawn(|this, mut cx| async move {
let Ok(display_snapshot) = this.update(&mut cx, |this, cx| {
this.display_map.update(cx, |map, cx| map.snapshot(cx))
}) else {
return;
};
let Some(project) = project else {
return;
};
if project
.update(&mut cx, |this, _| this.is_remote())
.unwrap_or(true)
{
// Do not display any test indicators in remote projects.
return;
}
let new_rows =
cx.background_executor()
.spawn({
let snapshot = display_snapshot.clone();
async move {
Self::fetch_runnable_ranges(&snapshot, Anchor::min()..Anchor::max())
}
})
.await;
let rows = Self::refresh_runnable_display_rows(
project,
display_snapshot,
new_rows,
cx.clone(),
);
this.update(&mut cx, |this, _| {
this.clear_tasks();
for (row, tasks) in rows {
this.insert_tasks(row, tasks);
}
})
.ok();
})
}
fn fetch_runnable_ranges(
snapshot: &DisplaySnapshot, snapshot: &DisplaySnapshot,
cx: &WindowContext, range: Range<Anchor>,
) -> Vec<(Range<usize>, Runnable)> {
snapshot.buffer_snapshot.runnable_ranges(range).collect()
}
fn refresh_runnable_display_rows(
project: Model<Project>,
snapshot: DisplaySnapshot,
runnable_ranges: Vec<(Range<usize>, Runnable)>,
mut cx: AsyncWindowContext,
) -> Vec<(u32, RunnableTasks)> { ) -> Vec<(u32, RunnableTasks)> {
if self runnable_ranges
.project .into_iter()
.as_ref()
.map_or(false, |project| project.read(cx).is_remote())
{
// Do not display any test indicators in remote projects.
return vec![];
}
snapshot
.buffer_snapshot
.runnable_ranges(range)
.filter_map(|(multi_buffer_range, mut runnable)| { .filter_map(|(multi_buffer_range, mut runnable)| {
let (tasks, _) = self.resolve_runnable(&mut runnable, cx); let (tasks, _) = cx
.update(|cx| Self::resolve_runnable(project.clone(), &mut runnable, cx))
.ok()?;
if tasks.is_empty() { if tasks.is_empty() {
return None; return None;
} }
@ -7722,13 +7767,10 @@ impl Editor {
} }
fn resolve_runnable( fn resolve_runnable(
&self, project: Model<Project>,
runnable: &mut Runnable, runnable: &mut Runnable,
cx: &WindowContext<'_>, cx: &WindowContext<'_>,
) -> (Vec<(TaskSourceKind, TaskTemplate)>, Option<WorktreeId>) { ) -> (Vec<(TaskSourceKind, TaskTemplate)>, Option<WorktreeId>) {
let Some(project) = self.project.as_ref() else {
return Default::default();
};
let (inventory, worktree_id) = project.read_with(cx, |project, cx| { let (inventory, worktree_id) = project.read_with(cx, |project, cx| {
let worktree_id = project let worktree_id = project
.buffer_for_id(runnable.buffer) .buffer_for_id(runnable.buffer)
@ -10070,7 +10112,11 @@ impl Editor {
self.refresh_inlay_hints(InlayHintRefreshReason::ExcerptsRemoved(ids.clone()), cx); self.refresh_inlay_hints(InlayHintRefreshReason::ExcerptsRemoved(ids.clone()), cx);
cx.emit(EditorEvent::ExcerptsRemoved { ids: ids.clone() }) cx.emit(EditorEvent::ExcerptsRemoved { ids: ids.clone() })
} }
multi_buffer::Event::Reparsed => cx.emit(EditorEvent::Reparsed), multi_buffer::Event::Reparsed => {
self.tasks_update_task = Some(self.refresh_runnables(cx));
cx.emit(EditorEvent::Reparsed);
}
multi_buffer::Event::LanguageChanged => { multi_buffer::Event::LanguageChanged => {
cx.emit(EditorEvent::Reparsed); cx.emit(EditorEvent::Reparsed);
cx.notify(); cx.notify();

View file

@ -15,8 +15,8 @@ use crate::{
CodeActionsMenu, CursorShape, DisplayPoint, DocumentHighlightRead, DocumentHighlightWrite, CodeActionsMenu, CursorShape, DisplayPoint, DocumentHighlightRead, DocumentHighlightWrite,
Editor, EditorMode, EditorSettings, EditorSnapshot, EditorStyle, ExpandExcerpts, Editor, EditorMode, EditorSettings, EditorSnapshot, EditorStyle, ExpandExcerpts,
GutterDimensions, HalfPageDown, HalfPageUp, HoveredCursor, HunkToExpand, LineDown, LineUp, GutterDimensions, HalfPageDown, HalfPageUp, HoveredCursor, HunkToExpand, LineDown, LineUp,
OpenExcerpts, PageDown, PageUp, Point, RunnableTasks, SelectPhase, Selection, SoftWrap, OpenExcerpts, PageDown, PageUp, Point, SelectPhase, Selection, SoftWrap, ToPoint,
ToPoint, CURSORS_VISIBLE_FOR, MAX_LINE_LEN, CURSORS_VISIBLE_FOR, MAX_LINE_LEN,
}; };
use anyhow::Result; use anyhow::Result;
use client::ParticipantIndex; use client::ParticipantIndex;
@ -1377,7 +1377,6 @@ impl EditorElement {
fn layout_run_indicators( fn layout_run_indicators(
&self, &self,
task_lines: Vec<(u32, RunnableTasks)>,
line_height: Pixels, line_height: Pixels,
scroll_pixel_position: gpui::Point<Pixels>, scroll_pixel_position: gpui::Point<Pixels>,
gutter_dimensions: &GutterDimensions, gutter_dimensions: &GutterDimensions,
@ -1385,8 +1384,6 @@ impl EditorElement {
cx: &mut WindowContext, cx: &mut WindowContext,
) -> Vec<AnyElement> { ) -> Vec<AnyElement> {
self.editor.update(cx, |editor, cx| { self.editor.update(cx, |editor, cx| {
editor.clear_tasks();
let active_task_indicator_row = let active_task_indicator_row =
if let Some(crate::ContextMenu::CodeActions(CodeActionsMenu { if let Some(crate::ContextMenu::CodeActions(CodeActionsMenu {
deployed_from_indicator, deployed_from_indicator,
@ -1402,21 +1399,20 @@ impl EditorElement {
} else { } else {
None None
}; };
task_lines editor
.into_iter() .tasks
.map(|(row, tasks)| { .keys()
editor.insert_tasks(row, tasks); .map(|row| {
let button = editor.render_run_indicator( let button = editor.render_run_indicator(
&self.style, &self.style,
Some(row) == active_task_indicator_row, Some(*row) == active_task_indicator_row,
row, *row,
cx, cx,
); );
let button = prepaint_gutter_button( let button = prepaint_gutter_button(
button, button,
row, *row,
line_height, line_height,
gutter_dimensions, gutter_dimensions,
scroll_pixel_position, scroll_pixel_position,
@ -3835,12 +3831,6 @@ impl Element for EditorElement {
cx, cx,
); );
let test_lines = self.editor.read(cx).runnable_display_rows(
start_anchor..end_anchor,
&snapshot.display_snapshot,
cx,
);
let (selections, active_rows, newest_selection_head) = self.layout_selections( let (selections, active_rows, newest_selection_head) = self.layout_selections(
start_anchor, start_anchor,
end_anchor, end_anchor,
@ -4030,9 +4020,11 @@ impl Element for EditorElement {
cx, cx,
); );
if gutter_settings.code_actions { if gutter_settings.code_actions {
let has_test_indicator = test_lines let has_test_indicator = self
.iter() .editor
.any(|(line, _)| *line == newest_selection_head.row()); .read(cx)
.tasks
.contains_key(&newest_selection_head.row());
if !has_test_indicator { if !has_test_indicator {
code_actions_indicator = self.layout_code_actions_indicator( code_actions_indicator = self.layout_code_actions_indicator(
line_height, line_height,
@ -4048,7 +4040,6 @@ impl Element for EditorElement {
} }
let test_indicators = self.layout_run_indicators( let test_indicators = self.layout_run_indicators(
test_lines,
line_height, line_height,
scroll_pixel_position, scroll_pixel_position,
&gutter_dimensions, &gutter_dimensions,

View file

@ -1,6 +1,6 @@
( (
(attribute_item (attribute) @_attribute (attribute_item (attribute) @_attribute
(#match? @_attribute ".*test.*")) (#match? @_attribute ".*test"))
. .
(function_item (function_item
name: (_) @run) name: (_) @run)