Use Horizontal ranges everywhere

This commit is contained in:
Conrad Irwin
2023-10-09 21:48:50 -06:00
parent 002e2cc42c
commit ab050d1890
13 changed files with 229 additions and 149 deletions
+1 -48
View File
@@ -15,7 +15,7 @@ use gpui::{
color::Color,
fonts::{FontId, HighlightStyle, Underline},
text_layout::{Line, RunStyle},
AppContext, Entity, FontCache, ModelContext, ModelHandle, TextLayoutCache,
Entity, ModelContext, ModelHandle,
};
use inlay_map::InlayMap;
use language::{
@@ -576,7 +576,6 @@ impl DisplaySnapshot {
let range = display_row..display_row + 1;
for chunk in self.highlighted_chunks(range, editor_style) {
dbg!(chunk.chunk);
line.push_str(chunk.chunk);
let text_style = if let Some(style) = chunk.style {
@@ -600,7 +599,6 @@ impl DisplaySnapshot {
));
}
dbg!(&line, &editor_style.text.font_size, &styles);
text_layout_cache.layout_str(&line, editor_style.text.font_size, &styles)
}
@@ -623,49 +621,6 @@ impl DisplaySnapshot {
layout_line.closest_index_for_x(x_coordinate) as u32
}
// column_for_x(row, x)
fn point(
&self,
display_point: DisplayPoint,
text_layout_cache: &TextLayoutCache,
editor_style: &EditorStyle,
cx: &AppContext,
) -> f32 {
let mut styles = Vec::new();
let mut line = String::new();
let range = display_point.row()..display_point.row() + 1;
for chunk in self.highlighted_chunks(range, editor_style) {
dbg!(chunk.chunk);
line.push_str(chunk.chunk);
let text_style = if let Some(style) = chunk.style {
editor_style
.text
.clone()
.highlight(style, cx.font_cache())
.map(Cow::Owned)
.unwrap_or_else(|_| Cow::Borrowed(&editor_style.text))
} else {
Cow::Borrowed(&editor_style.text)
};
styles.push((
chunk.chunk.len(),
RunStyle {
font_id: text_style.font_id,
color: text_style.color,
underline: text_style.underline,
},
));
}
dbg!(&line, &editor_style.text.font_size, &styles);
let layout_line = text_layout_cache.layout_str(&line, editor_style.text.font_size, &styles);
layout_line.x_for_index(display_point.column() as usize)
}
pub fn chars_at(
&self,
mut point: DisplayPoint,
@@ -1374,7 +1329,6 @@ pub mod tests {
);
let x = snapshot.x_for_point(DisplayPoint::new(1, 10), &text_layout_details);
dbg!(x);
assert_eq!(
movement::up(
&snapshot,
@@ -1401,7 +1355,6 @@ pub mod tests {
SelectionGoal::HorizontalPosition(x)
)
);
dbg!("starting down...");
assert_eq!(
movement::down(
&snapshot,
+18 -10
View File
@@ -48,9 +48,9 @@ use gpui::{
impl_actions,
keymap_matcher::KeymapContext,
platform::{CursorStyle, MouseButton},
serde_json, text_layout, AnyElement, AnyViewHandle, AppContext, AsyncAppContext, ClipboardItem,
Element, Entity, ModelHandle, Subscription, Task, View, ViewContext, ViewHandle,
WeakViewHandle, WindowContext,
serde_json, AnyElement, AnyViewHandle, AppContext, AsyncAppContext, ClipboardItem, Element,
Entity, ModelHandle, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle,
WindowContext,
};
use highlight_matching_bracket::refresh_matching_bracket_highlights;
use hover_popover::{hide_hover, HoverState};
@@ -5953,11 +5953,14 @@ 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 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();
let columns = cmp::min(range.start.column(), range.end.column())
..cmp::max(range.start.column(), range.end.column());
let start_x = display_map.x_for_point(range.start, &text_layout_details);
let end_x = display_map.x_for_point(range.end, &text_layout_details);
let positions = start_x.min(end_x)..start_x.max(end_x);
selections.clear();
let mut stack = Vec::new();
@@ -5965,8 +5968,9 @@ impl Editor {
if let Some(selection) = self.selections.build_columnar_selection(
&display_map,
row,
&columns,
&positions,
oldest_selection.reversed,
&text_layout_details,
) {
stack.push(selection.id);
selections.push(selection);
@@ -5994,12 +5998,15 @@ impl Editor {
let range = selection.display_range(&display_map).sorted();
debug_assert_eq!(range.start.row(), range.end.row());
let mut row = range.start.row();
let columns = if let SelectionGoal::ColumnRange { start, end } = selection.goal
let positions = if let SelectionGoal::HorizontalRange { start, end } =
selection.goal
{
start..end
} else {
cmp::min(range.start.column(), range.end.column())
..cmp::max(range.start.column(), range.end.column())
let start_x = display_map.x_for_point(range.start, &text_layout_details);
let end_x = display_map.x_for_point(range.end, &text_layout_details);
start_x.min(end_x)..start_x.max(end_x)
};
while row != end_row {
@@ -6012,8 +6019,9 @@ impl Editor {
if let Some(new_selection) = self.selections.build_columnar_selection(
&display_map,
row,
&columns,
&positions,
selection.reversed,
&text_layout_details,
) {
state.stack.push(new_selection.id);
if above {
+2 -3
View File
@@ -22,7 +22,7 @@ use git::diff::DiffHunkStatus;
use gpui::{
color::Color,
elements::*,
fonts::{HighlightStyle, TextStyle, Underline},
fonts::TextStyle,
geometry::{
rect::RectF,
vector::{vec2f, Vector2F},
@@ -37,8 +37,7 @@ use gpui::{
use itertools::Itertools;
use json::json;
use language::{
language_settings::ShowWhitespaceSetting, Bias, CursorShape, DiagnosticSeverity, OffsetUtf16,
Selection,
language_settings::ShowWhitespaceSetting, Bias, CursorShape, OffsetUtf16, Selection,
};
use project::{
project_settings::{GitGutterSetting, ProjectSettings},
+5 -1
View File
@@ -1,6 +1,6 @@
use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint};
use crate::{char_kind, CharKind, Editor, EditorStyle, ToOffset, ToPoint};
use gpui::{text_layout, FontCache, TextLayoutCache, WindowContext};
use gpui::{FontCache, TextLayoutCache, WindowContext};
use language::Point;
use std::{ops::Range, sync::Arc};
@@ -105,7 +105,9 @@ pub fn up_by_rows(
) -> (DisplayPoint, SelectionGoal) {
let mut goal_x = match goal {
SelectionGoal::HorizontalPosition(x) => x,
SelectionGoal::WrappedHorizontalPosition((_, x)) => x,
SelectionGoal::HorizontalRange { end, .. } => end,
SelectionGoal::WrappedHorizontalRange { end: (_, end), .. } => end,
_ => map.x_for_point(start, text_layout_details),
};
@@ -140,7 +142,9 @@ pub fn down_by_rows(
) -> (DisplayPoint, SelectionGoal) {
let mut goal_x = match goal {
SelectionGoal::HorizontalPosition(x) => x,
SelectionGoal::WrappedHorizontalPosition((_, x)) => x,
SelectionGoal::HorizontalRange { end, .. } => end,
SelectionGoal::WrappedHorizontalRange { end: (_, end), .. } => end,
_ => map.x_for_point(start, text_layout_details),
};
+14 -9
View File
@@ -1,6 +1,6 @@
use std::{
cell::Ref,
cmp, iter, mem,
iter, mem,
ops::{Deref, DerefMut, Range, Sub},
sync::Arc,
};
@@ -13,6 +13,7 @@ use util::post_inc;
use crate::{
display_map::{DisplayMap, DisplaySnapshot, ToDisplayPoint},
movement::TextLayoutDetails,
Anchor, DisplayPoint, ExcerptId, MultiBuffer, MultiBufferSnapshot, SelectMode, ToOffset,
};
@@ -305,23 +306,27 @@ impl SelectionsCollection {
&mut self,
display_map: &DisplaySnapshot,
row: u32,
columns: &Range<u32>,
positions: &Range<f32>,
reversed: bool,
text_layout_details: &TextLayoutDetails,
) -> Option<Selection<Point>> {
let is_empty = columns.start == columns.end;
let is_empty = positions.start == positions.end;
let line_len = display_map.line_len(row);
if columns.start < line_len || (is_empty && columns.start == line_len) {
let start = DisplayPoint::new(row, columns.start);
let end = DisplayPoint::new(row, cmp::min(columns.end, line_len));
let start_col = display_map.column_for_x(row, positions.start, text_layout_details);
if start_col < line_len || (is_empty && start_col == line_len) {
let start = DisplayPoint::new(row, start_col);
let end_col = display_map.column_for_x(row, positions.end, text_layout_details);
let end = DisplayPoint::new(row, end_col);
Some(Selection {
id: post_inc(&mut self.next_selection_id),
start: start.to_point(display_map),
end: end.to_point(display_map),
reversed,
goal: SelectionGoal::ColumnRange {
start: columns.start,
end: columns.end,
goal: SelectionGoal::HorizontalRange {
start: positions.start,
end: positions.end,
},
})
} else {