| @@ -46,9 +46,9 @@ use gpui::{ · 45 unmodified lines |
| 46 | 46 | KeybindingKeystroke, Length, Modifiers, ModifiersChangedEvent, MouseButton, MouseClickEvent, |
| 47 | 47 | MouseDownEvent, MouseMoveEvent, MousePressureEvent, MouseUpEvent, PaintQuad, ParentElement, |
| 48 | 48 | Pixels, PressureStage, ScrollDelta, ScrollHandle, ScrollWheelEvent, ShapedLine, SharedString, |
| 49 | | − Size, StatefulInteractiveElement, Style, Styled, TextRun, TextStyleRefinement, WeakEntity, |
| 50 | | − Window, anchored, deferred, div, fill, linear_color_stop, linear_gradient, outline, point, px, |
| 51 | | − quad, relative, size, solid_background, transparent_black, |
| 49 | + Size, StatefulInteractiveElement, Style, Styled, TextAlign, TextRun, TextStyleRefinement, |
| 50 | + WeakEntity, Window, anchored, deferred, div, fill, linear_color_stop, linear_gradient, outline, |
| 51 | + point, px, quad, relative, size, solid_background, transparent_black, |
| 52 | 52 | }; |
| 53 | 53 | use itertools::Itertools; |
| 54 | 54 | use language::{IndentGuideSettings, language_settings::ShowWhitespaceSetting}; |
| @@ -1695,9 +1695,13 @@ impl EditorElement { · 1640 unmodified lines |
| 1695 | 1695 | [cursor_position.row().minus(visible_display_row_range.start) as usize]; |
| 1696 | 1696 | let cursor_column = cursor_position.column() as usize; |
| 1697 | 1697 | |
| 1698 | | − let cursor_character_x = cursor_row_layout.x_for_index(cursor_column); |
| 1699 | | − let mut block_width = |
| 1700 | | − cursor_row_layout.x_for_index(cursor_column + 1) - cursor_character_x; |
| 1698 | + let cursor_character_x = cursor_row_layout.x_for_index(cursor_column) |
| 1699 | + + cursor_row_layout |
| 1700 | + .alignment_offset(self.style.text.text_align, text_hitbox.size.width); |
| 1701 | + let cursor_next_x = cursor_row_layout.x_for_index(cursor_column + 1) |
| 1702 | + + cursor_row_layout |
| 1703 | + .alignment_offset(self.style.text.text_align, text_hitbox.size.width); |
| 1704 | + let mut block_width = cursor_next_x - cursor_character_x; |
| 1701 | 1705 | if block_width == Pixels::ZERO { |
| 1702 | 1706 | block_width = em_advance; |
| 1703 | 1707 | } |
| @@ -6160,10 +6164,25 @@ impl EditorElement { · 4456 unmodified lines |
| 6160 | 6164 | let color = cx.theme().colors().editor_hover_line_number; |
| 6161 | 6165 | |
| 6162 | 6166 | let line = self.shape_line_number(shaped_line.text.clone(), color, window); |
| 6163 | | − line.paint(hitbox.origin, line_height, window, cx).log_err() |
| 6167 | + line.paint( |
| 6168 | + hitbox.origin, |
| 6169 | + line_height, |
| 6170 | + TextAlign::Left, |
| 6171 | + None, |
| 6172 | + window, |
| 6173 | + cx, |
| 6174 | + ) |
| 6175 | + .log_err() |
| 6164 | 6176 | } else { |
| 6165 | 6177 | shaped_line |
| 6166 | | − .paint(hitbox.origin, line_height, window, cx) |
| 6178 | + .paint( |
| 6179 | + hitbox.origin, |
| 6180 | + line_height, |
| 6181 | + TextAlign::Left, |
| 6182 | + None, |
| 6183 | + window, |
| 6184 | + cx, |
| 6185 | + ) |
| 6167 | 6186 | .log_err() |
| 6168 | 6187 | }) else { |
| 6169 | 6188 | continue; |
| @@ -7252,23 +7271,27 @@ impl EditorElement { · 1082 unmodified lines |
| 7252 | 7271 | .map(|row| { |
| 7253 | 7272 | let line_layout = |
| 7254 | 7273 | &layout.position_map.line_layouts[row.minus(start_row) as usize]; |
| 7274 | + let alignment_offset = |
| 7275 | + line_layout.alignment_offset(layout.text_align, layout.content_width); |
| 7255 | 7276 | HighlightedRangeLine { |
| 7256 | 7277 | start_x: if row == range.start.row() { |
| 7257 | 7278 | layout.content_origin.x |
| 7258 | 7279 | + Pixels::from( |
| 7259 | 7280 | ScrollPixelOffset::from( |
| 7260 | | − line_layout.x_for_index(range.start.column() as usize), |
| 7281 | + line_layout.x_for_index(range.start.column() as usize) |
| 7282 | + + alignment_offset, |
| 7261 | 7283 | ) - layout.position_map.scroll_pixel_position.x, |
| 7262 | 7284 | ) |
| 7263 | 7285 | } else { |
| 7264 | | − layout.content_origin.x |
| 7286 | + layout.content_origin.x + alignment_offset |
| 7265 | 7287 | - Pixels::from(layout.position_map.scroll_pixel_position.x) |
| 7266 | 7288 | }, |
| 7267 | 7289 | end_x: if row == range.end.row() { |
| 7268 | 7290 | layout.content_origin.x |
| 7269 | 7291 | + Pixels::from( |
| 7270 | 7292 | ScrollPixelOffset::from( |
| 7271 | | − line_layout.x_for_index(range.end.column() as usize), |
| 7293 | + line_layout.x_for_index(range.end.column() as usize) |
| 7294 | + + alignment_offset, |
| 7272 | 7295 | ) - layout.position_map.scroll_pixel_position.x, |
| 7273 | 7296 | ) |
| 7274 | 7297 | } else { |
| @@ -7276,6 +7299,7 @@ impl EditorElement { · 1 unmodified line |
| 7276 | 7299 | ScrollPixelOffset::from( |
| 7277 | 7300 | layout.content_origin.x |
| 7278 | 7301 | + line_layout.width |
| 7302 | + + alignment_offset |
| 7279 | 7303 | + line_end_overshoot, |
| 7280 | 7304 | ) - layout.position_map.scroll_pixel_position.x, |
| 7281 | 7305 | ) |
| @@ -8516,8 +8540,15 @@ impl LineWithInvisibles { · 1234 unmodified lines |
| 8516 | 8540 | for fragment in &self.fragments { |
| 8517 | 8541 | match fragment { |
| 8518 | 8542 | LineFragment::Text(line) => { |
| 8519 | | − line.paint(fragment_origin, line_height, window, cx) |
| 8520 | | − .log_err(); |
| 8543 | + line.paint( |
| 8544 | + fragment_origin, |
| 8545 | + line_height, |
| 8546 | + layout.text_align, |
| 8547 | + Some(layout.content_width), |
| 8548 | + window, |
| 8549 | + cx, |
| 8550 | + ) |
| 8551 | + .log_err(); |
| 8521 | 8552 | fragment_origin.x += line.width; |
| 8522 | 8553 | } |
| 8523 | 8554 | LineFragment::Element { size, .. } => { |
| @@ -8559,8 +8590,15 @@ impl LineWithInvisibles { · 35 unmodified lines |
| 8559 | 8590 | for fragment in &self.fragments { |
| 8560 | 8591 | match fragment { |
| 8561 | 8592 | LineFragment::Text(line) => { |
| 8562 | | − line.paint_background(fragment_origin, line_height, window, cx) |
| 8563 | | − .log_err(); |
| 8593 | + line.paint_background( |
| 8594 | + fragment_origin, |
| 8595 | + line_height, |
| 8596 | + layout.text_align, |
| 8597 | + Some(layout.content_width), |
| 8598 | + window, |
| 8599 | + cx, |
| 8600 | + ) |
| 8601 | + .log_err(); |
| 8564 | 8602 | fragment_origin.x += line.width; |
| 8565 | 8603 | } |
| 8566 | 8604 | LineFragment::Element { size, .. } => { |
| @@ -8609,7 +8647,7 @@ impl LineWithInvisibles { · 42 unmodified lines |
| 8609 | 8647 | [token_offset, token_end_offset], |
| 8610 | 8648 | Box::new(move |window: &mut Window, cx: &mut App| { |
| 8611 | 8649 | invisible_symbol |
| 8612 | | − .paint(origin, line_height, window, cx) |
| 8650 | + .paint(origin, line_height, TextAlign::Left, None, window, cx) |
| 8613 | 8651 | .log_err(); |
| 8614 | 8652 | }), |
| 8615 | 8653 | ) |
| @@ -8770,6 +8808,15 @@ impl LineWithInvisibles { · 154 unmodified lines |
| 8770 | 8808 | |
| 8771 | 8809 | None |
| 8772 | 8810 | } |
| 8811 | + |
| 8812 | + pub fn alignment_offset(&self, text_align: TextAlign, content_width: Pixels) -> Pixels { |
| 8813 | + let line_width = self.width; |
| 8814 | + match text_align { |
| 8815 | + TextAlign::Left => px(0.0), |
| 8816 | + TextAlign::Center => (content_width - line_width) / 2.0, |
| 8817 | + TextAlign::Right => content_width - line_width, |
| 8818 | + } |
| 8819 | + } |
| 8773 | 8820 | } |
| 8774 | 8821 | |
| 8775 | 8822 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| @@ -10172,6 +10219,8 @@ impl Element for EditorElement { · 1396 unmodified lines |
| 10172 | 10219 | em_width, |
| 10173 | 10220 | em_advance, |
| 10174 | 10221 | snapshot, |
| 10222 | + text_align: self.style.text.text_align, |
| 10223 | + content_width: text_hitbox.size.width, |
| 10175 | 10224 | gutter_hitbox: gutter_hitbox.clone(), |
| 10176 | 10225 | text_hitbox: text_hitbox.clone(), |
| 10177 | 10226 | inline_blame_bounds: inline_blame_layout |
| @@ -10225,6 +10274,8 @@ impl Element for EditorElement { · 47 unmodified lines |
| 10225 | 10274 | sticky_buffer_header, |
| 10226 | 10275 | sticky_headers, |
| 10227 | 10276 | expand_toggles, |
| 10277 | + text_align: self.style.text.text_align, |
| 10278 | + content_width: text_hitbox.size.width, |
| 10228 | 10279 | } |
| 10229 | 10280 | }) |
| 10230 | 10281 | }) |
| @@ -10405,6 +10456,8 @@ pub struct EditorLayout { · 174 unmodified lines |
| 10405 | 10456 | sticky_buffer_header: Option<AnyElement>, |
| 10406 | 10457 | sticky_headers: Option<StickyHeaders>, |
| 10407 | 10458 | document_colors: Option<(DocumentColorsRenderMode, Vec<(Range<DisplayPoint>, Hsla)>)>, |
| 10459 | + text_align: TextAlign, |
| 10460 | + content_width: Pixels, |
| 10408 | 10461 | } |
| 10409 | 10462 | |
| 10410 | 10463 | struct StickyHeaders { |
| @@ -10572,7 +10625,9 @@ impl StickyHeaderLine { · 161 unmodified lines |
| 10572 | 10625 | gutter_origin.x + gutter_width - gutter_right_padding - line_number.width, |
| 10573 | 10626 | gutter_origin.y, |
| 10574 | 10627 | ); |
| 10575 | | − line_number.paint(origin, line_height, window, cx).log_err(); |
| 10628 | + line_number |
| 10629 | + .paint(origin, line_height, TextAlign::Left, None, window, cx) |
| 10630 | + .log_err(); |
| 10576 | 10631 | } |
| 10577 | 10632 | } |
| 10578 | 10633 | } |
| @@ -11011,6 +11066,8 @@ pub(crate) struct PositionMap { · 432 unmodified lines |
| 11011 | 11066 | pub visible_row_range: Range<DisplayRow>, |
| 11012 | 11067 | pub line_layouts: Vec<LineWithInvisibles>, |
| 11013 | 11068 | pub snapshot: EditorSnapshot, |
| 11069 | + pub text_align: TextAlign, |
| 11070 | + pub content_width: Pixels, |
| 11014 | 11071 | pub text_hitbox: Hitbox, |
| 11015 | 11072 | pub gutter_hitbox: Hitbox, |
| 11016 | 11073 | pub inline_blame_bounds: Option<(Bounds<Pixels>, BufferId, BlameEntry)>, |
| @@ -11076,10 +11133,12 @@ impl PositionMap { · 59 unmodified lines |
| 11076 | 11133 | .line_layouts |
| 11077 | 11134 | .get(row as usize - scroll_position.y as usize) |
| 11078 | 11135 | { |
| 11079 | | − if let Some(ix) = line.index_for_x(x) { |
| 11136 | + let alignment_offset = line.alignment_offset(self.text_align, self.content_width); |
| 11137 | + let x_relative_to_text = x - alignment_offset; |
| 11138 | + if let Some(ix) = line.index_for_x(x_relative_to_text) { |
| 11080 | 11139 | (ix as u32, px(0.)) |
| 11081 | 11140 | } else { |
| 11082 | | − (line.len as u32, px(0.).max(x - line.width)) |
| 11141 | + (line.len as u32, px(0.).max(x_relative_to_text - line.width)) |
| 11083 | 11142 | } |
| 11084 | 11143 | } else { |
| 11085 | 11144 | (0, x) |
| @@ -11268,7 +11327,14 @@ impl CursorLayout { · 182 unmodified lines |
| 11268 | 11327 | |
| 11269 | 11328 | if let Some(block_text) = &self.block_text { |
| 11270 | 11329 | block_text |
| 11271 | | − .paint(self.origin + origin, self.line_height, window, cx) |
| 11330 | + .paint( |
| 11331 | + self.origin + origin, |
| 11332 | + self.line_height, |
| 11333 | + TextAlign::Left, |
| 11334 | + None, |
| 11335 | + window, |
| 11336 | + cx, |
| 11337 | + ) |
| 11272 | 11338 | .log_err(); |
| 11273 | 11339 | } |
| 11274 | 11340 | } |