Refactor TextLayoutDetails construction

This commit is contained in:
Conrad Irwin 2023-10-18 22:39:25 -06:00
parent 138fa45ecb
commit 3eb8aa8085
10 changed files with 52 additions and 64 deletions

View file

@ -1288,7 +1288,7 @@ pub mod tests {
cx.update_window(window, |cx| { cx.update_window(window, |cx| {
let text_layout_details = 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(); let font_cache = cx.font_cache().clone();

View file

@ -3065,6 +3065,14 @@ impl Editor {
.collect() .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( fn splice_inlay_hints(
&self, &self,
to_remove: Vec<InlayId>, to_remove: Vec<InlayId>,
@ -4988,7 +4996,7 @@ impl Editor {
} }
pub fn transpose(&mut self, _: &Transpose, cx: &mut ViewContext<Self>) { 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| { self.transact(cx, |this, cx| {
let edits = this.change_selections(Some(Autoscroll::fit()), cx, |s| { let edits = this.change_selections(Some(Autoscroll::fit()), cx, |s| {
let mut edits: Vec<(Range<usize>, String)> = Default::default(); let mut edits: Vec<(Range<usize>, String)> = Default::default();
@ -5279,7 +5287,7 @@ impl Editor {
return; 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| { self.change_selections(Some(Autoscroll::fit()), cx, |s| {
let line_mode = s.line_mode; let line_mode = s.line_mode;
@ -5321,7 +5329,7 @@ impl Editor {
Autoscroll::fit() 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| { self.change_selections(Some(autoscroll), cx, |s| {
let line_mode = s.line_mode; let line_mode = s.line_mode;
@ -5343,7 +5351,7 @@ impl Editor {
} }
pub fn select_up(&mut self, _: &SelectUp, cx: &mut ViewContext<Self>) { 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| { self.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.move_heads_with(|map, head, goal| { s.move_heads_with(|map, head, goal| {
movement::up(map, head, goal, false, &text_layout_details) movement::up(map, head, goal, false, &text_layout_details)
@ -5359,7 +5367,7 @@ impl Editor {
return; 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| { self.change_selections(Some(Autoscroll::fit()), cx, |s| {
let line_mode = s.line_mode; let line_mode = s.line_mode;
s.move_with(|map, selection| { s.move_with(|map, selection| {
@ -5409,7 +5417,7 @@ impl Editor {
Autoscroll::fit() 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| { self.change_selections(Some(autoscroll), cx, |s| {
let line_mode = s.line_mode; let line_mode = s.line_mode;
s.move_with(|map, selection| { s.move_with(|map, selection| {
@ -5430,7 +5438,7 @@ impl Editor {
} }
pub fn select_down(&mut self, _: &SelectDown, cx: &mut ViewContext<Self>) { 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| { self.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.move_heads_with(|map, head, goal| { s.move_heads_with(|map, head, goal| {
movement::down(map, head, goal, false, &text_layout_details) 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>) { 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 display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let mut selections = self.selections.all::<Point>(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 mut state = self.add_selections_state.take().unwrap_or_else(|| {
let oldest_selection = selections.iter().min_by_key(|s| s.id).unwrap().clone(); let oldest_selection = selections.iter().min_by_key(|s| s.id).unwrap().clone();
let range = oldest_selection.display_range(&display_map).sorted(); 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>) { 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| { self.transact(cx, |this, cx| {
let mut selections = this.selections.all::<Point>(cx); let mut selections = this.selections.all::<Point>(cx);
let mut edits = Vec::new(); let mut edits = Vec::new();

View file

@ -1,6 +1,6 @@
use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint}; use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint};
use crate::{char_kind, CharKind, Editor, EditorStyle, ToOffset, ToPoint}; use crate::{char_kind, CharKind, EditorStyle, ToOffset, ToPoint};
use gpui::{FontCache, TextLayoutCache, WindowContext}; use gpui::{FontCache, TextLayoutCache};
use language::Point; use language::Point;
use std::{ops::Range, sync::Arc}; use std::{ops::Range, sync::Arc};
@ -18,16 +18,6 @@ pub struct TextLayoutDetails {
pub editor_style: EditorStyle, 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 { pub fn left(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
if point.column() > 0 { if point.column() > 0 {
*point.column_mut() -= 1; *point.column_mut() -= 1;
@ -743,7 +733,7 @@ mod tests {
let window = cx.window.clone(); let window = cx.window.clone();
cx.update_window(window, |cx| { cx.update_window(window, |cx| {
let text_layout_details = 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 let family_id = cx
.font_cache() .font_cache()

View file

@ -18,7 +18,7 @@ use crate::{
Vim, Vim,
}; };
use collections::HashSet; use collections::HashSet;
use editor::{movement::TextLayoutDetails, scroll::autoscroll::Autoscroll}; use editor::scroll::autoscroll::Autoscroll;
use editor::{Bias, DisplayPoint}; use editor::{Bias, DisplayPoint};
use gpui::{actions, AppContext, ViewContext, WindowContext}; use gpui::{actions, AppContext, ViewContext, WindowContext};
use language::SelectionGoal; use language::SelectionGoal;
@ -177,7 +177,7 @@ pub(crate) fn move_cursor(
cx: &mut WindowContext, cx: &mut WindowContext,
) { ) {
vim.update_active_editor(cx, |editor, 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.change_selections(Some(Autoscroll::fit()), cx, |s| { editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.move_cursors_with(|map, cursor, goal| { s.move_cursors_with(|map, cursor, goal| {
motion motion
@ -280,7 +280,7 @@ fn insert_line_below(_: &mut Workspace, _: &InsertLineBelow, cx: &mut ViewContex
vim.start_recording(cx); vim.start_recording(cx);
vim.switch_mode(Mode::Insert, false, cx); vim.switch_mode(Mode::Insert, false, cx);
vim.update_active_editor(cx, |editor, 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.transact(cx, |editor, cx| {
let (map, old_selections) = editor.selections.all_display(cx); let (map, old_selections) = editor.selections.all_display(cx);

View file

@ -20,7 +20,7 @@ pub fn change_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &m
| Motion::StartOfLine { .. } | Motion::StartOfLine { .. }
); );
vim.update_active_editor(cx, |editor, 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.transact(cx, |editor, cx| {
// We are swapping to insert mode anyway. Just set the line end clipping behavior now // We are swapping to insert mode anyway. Just set the line end clipping behavior now
editor.set_clip_at_line_ends(false, cx); editor.set_clip_at_line_ends(false, cx);

View file

@ -1,15 +1,13 @@
use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim}; use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim};
use collections::{HashMap, HashSet}; use collections::{HashMap, HashSet};
use editor::{ use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Bias};
display_map::ToDisplayPoint, movement::TextLayoutDetails, scroll::autoscroll::Autoscroll, Bias,
};
use gpui::WindowContext; use gpui::WindowContext;
use language::Point; use language::Point;
pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) { pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
vim.stop_recording(); vim.stop_recording();
vim.update_active_editor(cx, |editor, 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.transact(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx); editor.set_clip_at_line_ends(false, cx);
let mut original_columns: HashMap<_, _> = Default::default(); let mut original_columns: HashMap<_, _> = Default::default();

View file

@ -1,10 +1,8 @@
use std::{borrow::Cow, cmp}; use std::{borrow::Cow, cmp};
use editor::{ use editor::{
display_map::ToDisplayPoint, display_map::ToDisplayPoint, movement, scroll::autoscroll::Autoscroll, ClipboardSelection,
movement::{self, TextLayoutDetails}, DisplayPoint,
scroll::autoscroll::Autoscroll,
ClipboardSelection, DisplayPoint,
}; };
use gpui::{impl_actions, AppContext, ViewContext}; use gpui::{impl_actions, AppContext, ViewContext};
use language::{Bias, SelectionGoal}; use language::{Bias, SelectionGoal};
@ -32,7 +30,7 @@ fn paste(_: &mut Workspace, action: &Paste, cx: &mut ViewContext<Workspace>) {
Vim::update(cx, |vim, cx| { Vim::update(cx, |vim, cx| {
vim.record_current_action(cx); vim.record_current_action(cx);
vim.update_active_editor(cx, |editor, 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.transact(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx); editor.set_clip_at_line_ends(false, cx);

View file

@ -1,4 +1,4 @@
use editor::movement::{self, TextLayoutDetails}; use editor::movement;
use gpui::{actions, AppContext, WindowContext}; use gpui::{actions, AppContext, WindowContext};
use language::Point; use language::Point;
use workspace::Workspace; use workspace::Workspace;
@ -32,7 +32,7 @@ pub fn substitute(vim: &mut Vim, count: Option<usize>, line_mode: bool, cx: &mut
vim.update_active_editor(cx, |editor, cx| { vim.update_active_editor(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx); editor.set_clip_at_line_ends(false, cx);
editor.transact(cx, |editor, 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| { editor.change_selections(None, cx, |s| {
s.move_with(|map, selection| { s.move_with(|map, selection| {
if selection.start == selection.end { if selection.start == selection.end {

View file

@ -1,11 +1,10 @@
use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim}; use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim};
use collections::HashMap; use collections::HashMap;
use editor::movement::TextLayoutDetails;
use gpui::WindowContext; use gpui::WindowContext;
pub fn yank_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) { pub fn yank_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
vim.update_active_editor(cx, |editor, 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.transact(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx); editor.set_clip_at_line_ends(false, cx);
let mut original_positions: HashMap<_, _> = Default::default(); let mut original_positions: HashMap<_, _> = Default::default();

View file

@ -4,7 +4,7 @@ use std::{cmp, sync::Arc};
use collections::HashMap; use collections::HashMap;
use editor::{ use editor::{
display_map::{DisplaySnapshot, ToDisplayPoint}, display_map::{DisplaySnapshot, ToDisplayPoint},
movement::{self, TextLayoutDetails}, movement,
scroll::autoscroll::Autoscroll, scroll::autoscroll::Autoscroll,
Bias, DisplayPoint, Editor, 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) { pub fn visual_motion(motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
Vim::update(cx, |vim, cx| { Vim::update(cx, |vim, cx| {
vim.update_active_editor(cx, |editor, 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 if vim.state().mode == Mode::VisualBlock
&& !matches!( && !matches!(
motion, motion,
@ -140,25 +140,23 @@ pub fn visual_block_motion(
SelectionGoal, SelectionGoal,
) -> Option<(DisplayPoint, 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| { editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
let map = &s.display_map(); let map = &s.display_map();
let mut head = s.newest_anchor().head().to_display_point(map); let mut head = s.newest_anchor().head().to_display_point(map);
let mut tail = s.oldest_anchor().tail().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 { let (start, end) = match s.newest_anchor().goal {
SelectionGoal::HorizontalRange { start, end } if preserve_goal => (start, end), SelectionGoal::HorizontalRange { start, end } if preserve_goal => (start, end),
SelectionGoal::HorizontalPosition(start) if preserve_goal => (start, start), SelectionGoal::HorizontalPosition(start) if preserve_goal => (start, start),
_ => ( _ => (tail_x, head_x),
map.x_for_point(tail, &text_layout_details),
map.x_for_point(head, &text_layout_details),
),
}; };
let mut goal = SelectionGoal::HorizontalRange { start, end }; 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 { if !was_reversed && !preserve_goal {
head = movement::saturating_left(map, head); head = movement::saturating_left(map, head);
} }
@ -167,24 +165,25 @@ pub fn visual_block_motion(
return; return;
}; };
head = new_head; 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 { 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 { } 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 { 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 { let positions = if is_reversed {
map.x_for_point(head, &text_layout_details)..map.x_for_point(tail, &text_layout_details) head_x..tail_x
} 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)
} else { } else {
map.x_for_point(tail, &text_layout_details)..map.x_for_point(head, &text_layout_details) tail_x..head_x
}; };
if !preserve_goal { if !preserve_goal {
@ -215,11 +214,7 @@ pub fn visual_block_motion(
} }
} }
if positions.start if positions.start <= layed_out_line.width() {
<=
//map.x_for_point(DisplayPoint::new(row, map.line_len(row)), &text_layout_details)
layed_out_line.width()
{
let selection = Selection { let selection = Selection {
id: s.new_selection_id(), id: s.new_selection_id(),
start: start.to_point(map), start: start.to_point(map),