From 3eb8aa80852497f2e57107ff88715f5ef8bf3f49 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Wed, 18 Oct 2023 22:39:25 -0600 Subject: [PATCH] Refactor TextLayoutDetails construction --- crates/editor/src/display_map.rs | 2 +- crates/editor/src/editor.rs | 26 +++++++++++------ crates/editor/src/movement.rs | 16 ++--------- crates/vim/src/normal.rs | 6 ++-- crates/vim/src/normal/change.rs | 2 +- crates/vim/src/normal/delete.rs | 6 ++-- crates/vim/src/normal/paste.rs | 8 ++---- crates/vim/src/normal/substitute.rs | 4 +-- crates/vim/src/normal/yank.rs | 3 +- crates/vim/src/visual.rs | 43 +++++++++++++---------------- 10 files changed, 52 insertions(+), 64 deletions(-) diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index a92c46e072..6e179950f3 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -1288,7 +1288,7 @@ pub mod tests { cx.update_window(window, |cx| { let text_layout_details = - editor.read_with(cx, |editor, cx| TextLayoutDetails::new(editor, cx)); + editor.read_with(cx, |editor, cx| editor.text_layout_details(cx)); let font_cache = cx.font_cache().clone(); diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 88db2f1dfe..01b060fdb6 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -3065,6 +3065,14 @@ impl Editor { .collect() } + pub fn text_layout_details(&self, cx: &WindowContext) -> TextLayoutDetails { + TextLayoutDetails { + font_cache: cx.font_cache().clone(), + text_layout_cache: cx.text_layout_cache().clone(), + editor_style: self.style(cx), + } + } + fn splice_inlay_hints( &self, to_remove: Vec, @@ -4988,7 +4996,7 @@ impl Editor { } pub fn transpose(&mut self, _: &Transpose, cx: &mut ViewContext) { - let text_layout_details = TextLayoutDetails::new(&self, cx); + let text_layout_details = &self.text_layout_details(cx); self.transact(cx, |this, cx| { let edits = this.change_selections(Some(Autoscroll::fit()), cx, |s| { let mut edits: Vec<(Range, String)> = Default::default(); @@ -5279,7 +5287,7 @@ impl Editor { return; } - let text_layout_details = TextLayoutDetails::new(&self, cx); + let text_layout_details = &self.text_layout_details(cx); self.change_selections(Some(Autoscroll::fit()), cx, |s| { let line_mode = s.line_mode; @@ -5321,7 +5329,7 @@ impl Editor { Autoscroll::fit() }; - let text_layout_details = TextLayoutDetails::new(&self, cx); + let text_layout_details = &self.text_layout_details(cx); self.change_selections(Some(autoscroll), cx, |s| { let line_mode = s.line_mode; @@ -5343,7 +5351,7 @@ impl Editor { } pub fn select_up(&mut self, _: &SelectUp, cx: &mut ViewContext) { - let text_layout_details = TextLayoutDetails::new(&self, cx); + let text_layout_details = &self.text_layout_details(cx); self.change_selections(Some(Autoscroll::fit()), cx, |s| { s.move_heads_with(|map, head, goal| { movement::up(map, head, goal, false, &text_layout_details) @@ -5359,7 +5367,7 @@ impl Editor { return; } - let text_layout_details = TextLayoutDetails::new(&self, cx); + let text_layout_details = &self.text_layout_details(cx); self.change_selections(Some(Autoscroll::fit()), cx, |s| { let line_mode = s.line_mode; s.move_with(|map, selection| { @@ -5409,7 +5417,7 @@ impl Editor { Autoscroll::fit() }; - let text_layout_details = TextLayoutDetails::new(&self, cx); + let text_layout_details = &self.text_layout_details(cx); self.change_selections(Some(autoscroll), cx, |s| { let line_mode = s.line_mode; s.move_with(|map, selection| { @@ -5430,7 +5438,7 @@ impl Editor { } pub fn select_down(&mut self, _: &SelectDown, cx: &mut ViewContext) { - let text_layout_details = TextLayoutDetails::new(&self, cx); + let text_layout_details = &self.text_layout_details(cx); self.change_selections(Some(Autoscroll::fit()), cx, |s| { s.move_heads_with(|map, head, goal| { movement::down(map, head, goal, false, &text_layout_details) @@ -5953,7 +5961,7 @@ impl Editor { fn add_selection(&mut self, above: bool, cx: &mut ViewContext) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let mut selections = self.selections.all::(cx); - let text_layout_details = TextLayoutDetails::new(self, cx); + let text_layout_details = self.text_layout_details(cx); let mut state = self.add_selections_state.take().unwrap_or_else(|| { let oldest_selection = selections.iter().min_by_key(|s| s.id).unwrap().clone(); let range = oldest_selection.display_range(&display_map).sorted(); @@ -6315,7 +6323,7 @@ impl Editor { } pub fn toggle_comments(&mut self, action: &ToggleComments, cx: &mut ViewContext) { - let text_layout_details = TextLayoutDetails::new(&self, cx); + let text_layout_details = &self.text_layout_details(cx); self.transact(cx, |this, cx| { let mut selections = this.selections.all::(cx); let mut edits = Vec::new(); diff --git a/crates/editor/src/movement.rs b/crates/editor/src/movement.rs index 0305d853e6..62fd931ecc 100644 --- a/crates/editor/src/movement.rs +++ b/crates/editor/src/movement.rs @@ -1,6 +1,6 @@ use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint}; -use crate::{char_kind, CharKind, Editor, EditorStyle, ToOffset, ToPoint}; -use gpui::{FontCache, TextLayoutCache, WindowContext}; +use crate::{char_kind, CharKind, EditorStyle, ToOffset, ToPoint}; +use gpui::{FontCache, TextLayoutCache}; use language::Point; use std::{ops::Range, sync::Arc}; @@ -18,16 +18,6 @@ pub struct TextLayoutDetails { pub editor_style: EditorStyle, } -impl TextLayoutDetails { - pub fn new(editor: &Editor, cx: &WindowContext) -> TextLayoutDetails { - TextLayoutDetails { - font_cache: cx.font_cache().clone(), - text_layout_cache: cx.text_layout_cache().clone(), - editor_style: editor.style(cx), - } - } -} - pub fn left(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint { if point.column() > 0 { *point.column_mut() -= 1; @@ -743,7 +733,7 @@ mod tests { let window = cx.window.clone(); cx.update_window(window, |cx| { let text_layout_details = - editor.read_with(cx, |editor, cx| TextLayoutDetails::new(editor, cx)); + editor.read_with(cx, |editor, cx| editor.text_layout_details(cx)); let family_id = cx .font_cache() diff --git a/crates/vim/src/normal.rs b/crates/vim/src/normal.rs index 0e883cd758..1277d58b41 100644 --- a/crates/vim/src/normal.rs +++ b/crates/vim/src/normal.rs @@ -18,7 +18,7 @@ use crate::{ Vim, }; use collections::HashSet; -use editor::{movement::TextLayoutDetails, scroll::autoscroll::Autoscroll}; +use editor::scroll::autoscroll::Autoscroll; use editor::{Bias, DisplayPoint}; use gpui::{actions, AppContext, ViewContext, WindowContext}; use language::SelectionGoal; @@ -177,7 +177,7 @@ pub(crate) fn move_cursor( cx: &mut WindowContext, ) { vim.update_active_editor(cx, |editor, cx| { - let text_layout_details = TextLayoutDetails::new(editor, cx); + let text_layout_details = editor.text_layout_details(cx); editor.change_selections(Some(Autoscroll::fit()), cx, |s| { s.move_cursors_with(|map, cursor, goal| { motion @@ -280,7 +280,7 @@ fn insert_line_below(_: &mut Workspace, _: &InsertLineBelow, cx: &mut ViewContex vim.start_recording(cx); vim.switch_mode(Mode::Insert, false, cx); vim.update_active_editor(cx, |editor, cx| { - let text_layout_details = TextLayoutDetails::new(editor, cx); + let text_layout_details = editor.text_layout_details(cx); editor.transact(cx, |editor, cx| { let (map, old_selections) = editor.selections.all_display(cx); diff --git a/crates/vim/src/normal/change.rs b/crates/vim/src/normal/change.rs index 042897ec3c..bf2a25a98d 100644 --- a/crates/vim/src/normal/change.rs +++ b/crates/vim/src/normal/change.rs @@ -20,7 +20,7 @@ pub fn change_motion(vim: &mut Vim, motion: Motion, times: Option, cx: &m | Motion::StartOfLine { .. } ); vim.update_active_editor(cx, |editor, cx| { - let text_layout_details = TextLayoutDetails::new(editor, cx); + let text_layout_details = editor.text_layout_details(cx); editor.transact(cx, |editor, cx| { // We are swapping to insert mode anyway. Just set the line end clipping behavior now editor.set_clip_at_line_ends(false, cx); diff --git a/crates/vim/src/normal/delete.rs b/crates/vim/src/normal/delete.rs index 1ad91ff308..77e0e47be5 100644 --- a/crates/vim/src/normal/delete.rs +++ b/crates/vim/src/normal/delete.rs @@ -1,15 +1,13 @@ use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim}; use collections::{HashMap, HashSet}; -use editor::{ - display_map::ToDisplayPoint, movement::TextLayoutDetails, scroll::autoscroll::Autoscroll, Bias, -}; +use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Bias}; use gpui::WindowContext; use language::Point; pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option, cx: &mut WindowContext) { vim.stop_recording(); vim.update_active_editor(cx, |editor, cx| { - let text_layout_details = TextLayoutDetails::new(editor, cx); + let text_layout_details = editor.text_layout_details(cx); editor.transact(cx, |editor, cx| { editor.set_clip_at_line_ends(false, cx); let mut original_columns: HashMap<_, _> = Default::default(); diff --git a/crates/vim/src/normal/paste.rs b/crates/vim/src/normal/paste.rs index 7cb5261c49..6141e7c66f 100644 --- a/crates/vim/src/normal/paste.rs +++ b/crates/vim/src/normal/paste.rs @@ -1,10 +1,8 @@ use std::{borrow::Cow, cmp}; use editor::{ - display_map::ToDisplayPoint, - movement::{self, TextLayoutDetails}, - scroll::autoscroll::Autoscroll, - ClipboardSelection, DisplayPoint, + display_map::ToDisplayPoint, movement, scroll::autoscroll::Autoscroll, ClipboardSelection, + DisplayPoint, }; use gpui::{impl_actions, AppContext, ViewContext}; use language::{Bias, SelectionGoal}; @@ -32,7 +30,7 @@ fn paste(_: &mut Workspace, action: &Paste, cx: &mut ViewContext) { Vim::update(cx, |vim, cx| { vim.record_current_action(cx); vim.update_active_editor(cx, |editor, cx| { - let text_layout_details = TextLayoutDetails::new(editor, cx); + let text_layout_details = editor.text_layout_details(cx); editor.transact(cx, |editor, cx| { editor.set_clip_at_line_ends(false, cx); diff --git a/crates/vim/src/normal/substitute.rs b/crates/vim/src/normal/substitute.rs index e244e1d5d6..f0369a89bf 100644 --- a/crates/vim/src/normal/substitute.rs +++ b/crates/vim/src/normal/substitute.rs @@ -1,4 +1,4 @@ -use editor::movement::{self, TextLayoutDetails}; +use editor::movement; use gpui::{actions, AppContext, WindowContext}; use language::Point; use workspace::Workspace; @@ -32,7 +32,7 @@ pub fn substitute(vim: &mut Vim, count: Option, line_mode: bool, cx: &mut vim.update_active_editor(cx, |editor, cx| { editor.set_clip_at_line_ends(false, cx); editor.transact(cx, |editor, cx| { - let text_layout_details = TextLayoutDetails::new(editor, cx); + let text_layout_details = editor.text_layout_details(cx); editor.change_selections(None, cx, |s| { s.move_with(|map, selection| { if selection.start == selection.end { diff --git a/crates/vim/src/normal/yank.rs b/crates/vim/src/normal/yank.rs index b50fcdf7ec..33833500fa 100644 --- a/crates/vim/src/normal/yank.rs +++ b/crates/vim/src/normal/yank.rs @@ -1,11 +1,10 @@ use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim}; use collections::HashMap; -use editor::movement::TextLayoutDetails; use gpui::WindowContext; pub fn yank_motion(vim: &mut Vim, motion: Motion, times: Option, cx: &mut WindowContext) { vim.update_active_editor(cx, |editor, cx| { - let text_layout_details = TextLayoutDetails::new(editor, cx); + let text_layout_details = editor.text_layout_details(cx); editor.transact(cx, |editor, cx| { editor.set_clip_at_line_ends(false, cx); let mut original_positions: HashMap<_, _> = Default::default(); diff --git a/crates/vim/src/visual.rs b/crates/vim/src/visual.rs index f4a450c932..3157374b24 100644 --- a/crates/vim/src/visual.rs +++ b/crates/vim/src/visual.rs @@ -4,7 +4,7 @@ use std::{cmp, sync::Arc}; use collections::HashMap; use editor::{ display_map::{DisplaySnapshot, ToDisplayPoint}, - movement::{self, TextLayoutDetails}, + movement, scroll::autoscroll::Autoscroll, Bias, DisplayPoint, Editor, }; @@ -57,7 +57,7 @@ pub fn init(cx: &mut AppContext) { pub fn visual_motion(motion: Motion, times: Option, cx: &mut WindowContext) { Vim::update(cx, |vim, cx| { vim.update_active_editor(cx, |editor, cx| { - let text_layout_details = TextLayoutDetails::new(editor, cx); + let text_layout_details = editor.text_layout_details(cx); if vim.state().mode == Mode::VisualBlock && !matches!( motion, @@ -140,25 +140,23 @@ pub fn visual_block_motion( SelectionGoal, ) -> Option<(DisplayPoint, SelectionGoal)>, ) { - let text_layout_details = TextLayoutDetails::new(editor, cx); + let text_layout_details = editor.text_layout_details(cx); editor.change_selections(Some(Autoscroll::fit()), cx, |s| { let map = &s.display_map(); let mut head = s.newest_anchor().head().to_display_point(map); let mut tail = s.oldest_anchor().tail().to_display_point(map); - dbg!(head, tail); - dbg!(s.newest_anchor().goal); + + let mut head_x = map.x_for_point(head, &text_layout_details); + let mut tail_x = map.x_for_point(tail, &text_layout_details); let (start, end) = match s.newest_anchor().goal { SelectionGoal::HorizontalRange { start, end } if preserve_goal => (start, end), SelectionGoal::HorizontalPosition(start) if preserve_goal => (start, start), - _ => ( - map.x_for_point(tail, &text_layout_details), - map.x_for_point(head, &text_layout_details), - ), + _ => (tail_x, head_x), }; let mut goal = SelectionGoal::HorizontalRange { start, end }; - let was_reversed = tail.column() > head.column(); + let was_reversed = head_x > tail_x; if !was_reversed && !preserve_goal { head = movement::saturating_left(map, head); } @@ -167,24 +165,25 @@ pub fn visual_block_motion( return; }; head = new_head; + head_x = map.x_for_point(head, &text_layout_details); - let is_reversed = tail.column() > head.column(); + let is_reversed = tail_x > head_x; if was_reversed && !is_reversed { - tail = movement::left(map, tail) + tail = movement::left(map, tail); + tail_x = map.x_for_point(tail, &text_layout_details); } else if !was_reversed && is_reversed { - tail = movement::right(map, tail) + tail = movement::right(map, tail); + tail_x = map.x_for_point(tail, &text_layout_details); } if !is_reversed && !preserve_goal { - head = movement::saturating_right(map, head) + head = movement::saturating_right(map, head); + head_x = map.x_for_point(head, &text_layout_details); } let positions = if is_reversed { - map.x_for_point(head, &text_layout_details)..map.x_for_point(tail, &text_layout_details) - } else if head.column() == tail.column() { - let head_forward = movement::saturating_right(map, head); - map.x_for_point(head, &text_layout_details)..map.x_for_point(head, &text_layout_details) + head_x..tail_x } else { - map.x_for_point(tail, &text_layout_details)..map.x_for_point(head, &text_layout_details) + tail_x..head_x }; if !preserve_goal { @@ -215,11 +214,7 @@ pub fn visual_block_motion( } } - if positions.start - <= - //map.x_for_point(DisplayPoint::new(row, map.line_len(row)), &text_layout_details) - layed_out_line.width() - { + if positions.start <= layed_out_line.width() { let selection = Selection { id: s.new_selection_id(), start: start.to_point(map),