From 373fe6fadf87bbeec3137d314f6d17ccf241cb3d Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 13 Jan 2022 09:49:46 -0800 Subject: [PATCH] Change Editor::set_highlighted_row to take a row range Co-Authored-By: Antonio Scandurra --- crates/editor/src/editor.rs | 12 ++++----- crates/editor/src/element.rs | 39 ++++++++++++++++++----------- crates/go_to_line/src/go_to_line.rs | 5 ++-- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 0c861a889f..574ff3822b 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -376,7 +376,7 @@ pub struct Editor { blinking_paused: bool, mode: EditorMode, placeholder_text: Option>, - highlighted_row: Option, + highlighted_rows: Option>, } pub struct EditorSnapshot { @@ -505,7 +505,7 @@ impl Editor { blinking_paused: false, mode: EditorMode::Full, placeholder_text: None, - highlighted_row: None, + highlighted_rows: None, }; let selection = Selection { id: post_inc(&mut this.next_selection_id), @@ -3546,12 +3546,12 @@ impl Editor { .update(cx, |map, cx| map.set_wrap_width(width, cx)) } - pub fn set_highlighted_row(&mut self, row: Option) { - self.highlighted_row = row; + pub fn set_highlighted_rows(&mut self, rows: Option>) { + self.highlighted_rows = rows; } - pub fn highlighted_row(&mut self) -> Option { - self.highlighted_row + pub fn highlighted_rows(&self) -> Option> { + self.highlighted_rows.clone() } fn next_blink_epoch(&mut self) -> usize { diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 5180314c2a..a7b082dbdf 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -263,12 +263,16 @@ impl EditorElement { } } - if let Some(highlighted_row) = layout.highlighted_row { + if let Some(highlighted_rows) = &layout.highlighted_rows { let origin = vec2f( bounds.origin_x(), - bounds.origin_y() + (layout.line_height * highlighted_row as f32) - scroll_top, + bounds.origin_y() + (layout.line_height * highlighted_rows.start as f32) + - scroll_top, + ); + let size = vec2f( + bounds.width(), + layout.line_height * highlighted_rows.len() as f32, ); - let size = vec2f(bounds.width(), layout.line_height); cx.scene.push_quad(Quad { bounds: RectF::new(origin, size), background: Some(style.highlighted_line_background), @@ -640,15 +644,20 @@ impl EditorElement { .to_display_point(snapshot) .row(); - let anchor_x = text_x + if rows.contains(&anchor_row) { - line_layouts[(anchor_row - rows.start) as usize] - .x_for_index(block.column() as usize) - } else { - layout_line(anchor_row, snapshot, style, cx.text_layout_cache) - .x_for_index(block.column() as usize) - }; + let anchor_x = text_x + + if rows.contains(&anchor_row) { + line_layouts[(anchor_row - rows.start) as usize] + .x_for_index(block.column() as usize) + } else { + layout_line(anchor_row, snapshot, style, cx.text_layout_cache) + .x_for_index(block.column() as usize) + }; - let mut element = block.render(&BlockContext { cx, anchor_x, line_number_x, }); + let mut element = block.render(&BlockContext { + cx, + anchor_x, + line_number_x, + }); element.layout( SizeConstraint { min: Vector2F::zero(), @@ -750,9 +759,9 @@ impl Element for EditorElement { let mut selections = HashMap::default(); let mut active_rows = BTreeMap::new(); - let mut highlighted_row = None; + let mut highlighted_rows = None; self.update_view(cx.app, |view, cx| { - highlighted_row = view.highlighted_row(); + highlighted_rows = view.highlighted_rows(); let display_map = view.display_map.update(cx, |map, cx| map.snapshot(cx)); let local_selections = view @@ -831,7 +840,7 @@ impl Element for EditorElement { snapshot, style: self.settings.style.clone(), active_rows, - highlighted_row, + highlighted_rows, line_layouts, line_number_layouts, blocks, @@ -962,7 +971,7 @@ pub struct LayoutState { style: EditorStyle, snapshot: EditorSnapshot, active_rows: BTreeMap, - highlighted_row: Option, + highlighted_rows: Option>, line_layouts: Vec, line_number_layouts: Vec>, blocks: Vec<(u32, ElementBox)>, diff --git a/crates/go_to_line/src/go_to_line.rs b/crates/go_to_line/src/go_to_line.rs index cf965ebf1b..6a90eb785b 100644 --- a/crates/go_to_line/src/go_to_line.rs +++ b/crates/go_to_line/src/go_to_line.rs @@ -143,8 +143,9 @@ impl GoToLine { let snapshot = active_editor.snapshot(cx).display_snapshot; let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left); let display_point = point.to_display_point(&snapshot); + let row = display_point.row(); active_editor.select_ranges([point..point], Some(Autoscroll::Center), cx); - active_editor.set_highlighted_row(Some(display_point.row())); + active_editor.set_highlighted_rows(Some(row..row + 1)); Some(active_editor.newest_selection(&snapshot.buffer_snapshot)) }); cx.notify(); @@ -162,7 +163,7 @@ impl Entity for GoToLine { let line_selection = self.line_selection.take(); let restore_state = self.restore_state.take(); self.active_editor.update(cx, |editor, cx| { - editor.set_highlighted_row(None); + editor.set_highlighted_rows(None); if let Some((line_selection, restore_state)) = line_selection.zip(restore_state) { let newest_selection = editor.newest_selection::(&editor.buffer().read(cx).read(cx));