Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Refactor TextLayoutDetails construction
Conrad Irwin3eb8aa808524
diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rsindex a92c46e072e..6e179950f35 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.rsindex 88db2f1dfe1..01b060fdb64 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<InlayId>,@@ -4988,7 +4996,7 @@ impl Editor {}pub fn transpose(&mut self, _: &Transpose, cx: &mut ViewContext<Self>) {- 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<usize>, 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<Self>) {- 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<Self>) {- 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<Self>) {let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));let mut selections = self.selections.all::<Point>(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<Self>) {- 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::<Point>(cx);let mut edits = Vec::new();diff --git a/crates/editor/src/movement.rs b/crates/editor/src/movement.rsindex 0305d853e6f..62fd931eccc 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.rsindex 0e883cd7584..1277d58b419 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 ViewContexvim.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.rsindex 042897ec3c7..bf2a25a98d5 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<usize>, 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 noweditor.set_clip_at_line_ends(false, cx);diff --git a/crates/vim/src/normal/delete.rs b/crates/vim/src/normal/delete.rsindex 1ad91ff3083..77e0e47be59 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<usize>, 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.rsindex 7cb5261c493..6141e7c66f2 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<Workspace>) {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.rsindex e244e1d5d64..f0369a89bff 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<usize>, line_mode: bool, cx: &mutvim.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.rsindex b50fcdf7ecb..33833500fab 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<usize>, 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.rsindex f4a450c9324..3157374b246 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<usize>, 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),