Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/view.rs')
| -rw-r--r-- | helix-view/src/view.rs | 146 |
1 files changed, 59 insertions, 87 deletions
diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index aecf09a6..af4fdfe4 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -1,7 +1,7 @@ use crate::{ align_view, annotations::diagnostics::InlineDiagnostics, - document::{DocumentColorSwatches, DocumentInlayHints}, + document::DocumentInlayHints, editor::{GutterConfig, GutterType}, graphics::Rect, handlers::diagnostics::DiagnosticsHandler, @@ -11,6 +11,7 @@ use crate::{ use helix_core::{ char_idx_at_visual_offset, doc_formatter::TextFormat, + syntax::Highlight, text_annotations::TextAnnotations, visual_offset_from_anchor, visual_offset_from_block, Position, RopeSlice, Selection, Transaction, @@ -127,6 +128,7 @@ pub struct ViewPosition { #[derive(Clone)] pub struct View { pub id: ViewId, + pub offset: ViewPosition, pub area: Rect, pub doc: DocumentId, pub jumps: JumpList, @@ -171,6 +173,11 @@ impl View { Self { id: ViewId::default(), doc, + offset: ViewPosition { + anchor: 0, + horizontal_offset: 0, + vertical_offset: 0, + }, area: Rect::default(), // will get calculated upon inserting into tree jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel docs_access_history: Vec::new(), @@ -233,34 +240,23 @@ impl View { doc: &Document, scrolloff: usize, ) -> Option<ViewPosition> { - let view_offset = doc.get_view_offset(self.id)?; let doc_text = doc.text().slice(..); let viewport = self.inner_area(doc); - let vertical_viewport_end = view_offset.vertical_offset + viewport.height as usize; + let vertical_viewport_end = self.offset.vertical_offset + viewport.height as usize; let text_fmt = doc.text_format(viewport.width, None); let annotations = self.text_annotations(doc, None); - let (scrolloff_top, scrolloff_bottom) = if CENTERING { - (0, 0) - } else { - ( - // - 1 from the top so we have at least one gap in the middle. - scrolloff.min(viewport.height.saturating_sub(1) as usize / 2), - scrolloff.min(viewport.height as usize / 2), - ) - }; - let (scrolloff_left, scrolloff_right) = if CENTERING { - (0, 0) + // - 1 so we have at least one gap in the middle. + // a height of 6 with padding of 3 on each side will keep shifting the view back and forth + // as we type + let scrolloff = if CENTERING { + 0 } else { - ( - // - 1 from the left so we have at least one gap in the middle. - scrolloff.min(viewport.width.saturating_sub(1) as usize / 2), - scrolloff.min(viewport.width as usize / 2), - ) + scrolloff.min(viewport.height.saturating_sub(1) as usize / 2) }; let cursor = doc.selection(self.id).primary().cursor(doc_text); - let mut offset = view_offset; + let mut offset = self.offset; let off = visual_offset_from_anchor( doc_text, offset.anchor, @@ -271,14 +267,14 @@ impl View { ); let (new_anchor, at_top) = match off { - Ok((visual_pos, _)) if visual_pos.row < scrolloff_top + offset.vertical_offset => { + Ok((visual_pos, _)) if visual_pos.row < scrolloff + offset.vertical_offset => { if CENTERING { // cursor out of view return None; } (true, true) } - Ok((visual_pos, _)) if visual_pos.row + scrolloff_bottom >= vertical_viewport_end => { + Ok((visual_pos, _)) if visual_pos.row + scrolloff >= vertical_viewport_end => { (true, false) } Ok((_, _)) => (false, false), @@ -289,9 +285,9 @@ impl View { if new_anchor { let v_off = if at_top { - scrolloff_top as isize + scrolloff as isize } else { - viewport.height as isize - scrolloff_bottom as isize - 1 + viewport.height as isize - scrolloff as isize - 1 }; (offset.anchor, offset.vertical_offset) = char_idx_at_visual_offset(doc_text, cursor, -v_off, 0, &text_fmt, &annotations); @@ -315,32 +311,32 @@ impl View { .col; let last_col = offset.horizontal_offset + viewport.width.saturating_sub(1) as usize; - if col > last_col.saturating_sub(scrolloff_right) { + if col > last_col.saturating_sub(scrolloff) { // scroll right - offset.horizontal_offset += col - (last_col.saturating_sub(scrolloff_right)) - } else if col < offset.horizontal_offset + scrolloff_left { + offset.horizontal_offset += col - (last_col.saturating_sub(scrolloff)) + } else if col < offset.horizontal_offset + scrolloff { // scroll left - offset.horizontal_offset = col.saturating_sub(scrolloff_left) + offset.horizontal_offset = col.saturating_sub(scrolloff) }; } // if we are not centering return None if view position is unchanged - if !CENTERING && offset == view_offset { + if !CENTERING && offset == self.offset { return None; } Some(offset) } - pub fn ensure_cursor_in_view(&self, doc: &mut Document, scrolloff: usize) { + pub fn ensure_cursor_in_view(&mut self, doc: &Document, scrolloff: usize) { if let Some(offset) = self.offset_coords_to_in_view_center::<false>(doc, scrolloff) { - doc.set_view_offset(self.id, offset); + self.offset = offset; } } - pub fn ensure_cursor_in_view_center(&self, doc: &mut Document, scrolloff: usize) { + pub fn ensure_cursor_in_view_center(&mut self, doc: &Document, scrolloff: usize) { if let Some(offset) = self.offset_coords_to_in_view_center::<true>(doc, scrolloff) { - doc.set_view_offset(self.id, offset); + self.offset = offset; } else { align_view(doc, self, Align::Center); } @@ -358,7 +354,7 @@ impl View { #[inline] pub fn estimate_last_doc_line(&self, doc: &Document) -> usize { let doc_text = doc.text().slice(..); - let line = doc_text.char_to_line(doc.view_offset(self.id).anchor.min(doc_text.len_chars())); + let line = doc_text.char_to_line(self.offset.anchor.min(doc_text.len_chars())); // Saturating subs to make it inclusive zero indexing. (line + self.inner_height()) .min(doc_text.len_lines()) @@ -372,10 +368,9 @@ impl View { let viewport = self.inner_area(doc); let text_fmt = doc.text_format(viewport.width, None); let annotations = self.text_annotations(doc, None); - let view_offset = doc.view_offset(self.id); // last visual line in view is trivial to compute - let visual_height = doc.view_offset(self.id).vertical_offset + viewport.height as usize; + let visual_height = self.offset.vertical_offset + viewport.height as usize; // fast path when the EOF is not visible on the screen, if self.estimate_last_doc_line(doc) < doc_text.len_lines() - 1 { @@ -385,7 +380,7 @@ impl View { // translate to document line let pos = visual_offset_from_anchor( doc_text, - view_offset.anchor, + self.offset.anchor, usize::MAX, &text_fmt, &annotations, @@ -393,7 +388,7 @@ impl View { ); match pos { - Ok((Position { row, .. }, _)) => row.saturating_sub(view_offset.vertical_offset), + Ok((Position { row, .. }, _)) => row.saturating_sub(self.offset.vertical_offset), Err(PosAfterMaxRow) => visual_height.saturating_sub(1), Err(PosBeforeAnchorRow) => 0, } @@ -408,15 +403,13 @@ impl View { text: RopeSlice, pos: usize, ) -> Option<Position> { - let view_offset = doc.view_offset(self.id); - let viewport = self.inner_area(doc); let text_fmt = doc.text_format(viewport.width, None); let annotations = self.text_annotations(doc, None); let mut pos = visual_offset_from_anchor( text, - view_offset.anchor, + self.offset.anchor, pos, &text_fmt, &annotations, @@ -424,14 +417,14 @@ impl View { ) .ok()? .0; - if pos.row < view_offset.vertical_offset { + if pos.row < self.offset.vertical_offset { return None; } - pos.row -= view_offset.vertical_offset; + pos.row -= self.offset.vertical_offset; if pos.row >= viewport.height as usize { return None; } - pos.col = pos.col.saturating_sub(view_offset.horizontal_offset); + pos.col = pos.col.saturating_sub(self.offset.horizontal_offset); Some(pos) } @@ -445,7 +438,9 @@ impl View { let mut text_annotations = TextAnnotations::default(); if let Some(labels) = doc.jump_labels.get(&self.id) { - let style = theme.and_then(|t| t.find_highlight("ui.virtual.jump-label")); + let style = theme + .and_then(|t| t.find_scope_index("ui.virtual.jump-label")) + .map(Highlight); text_annotations.add_overlay(labels, style); } @@ -458,10 +453,15 @@ impl View { padding_after_inlay_hints, }) = doc.inlay_hints.get(&self.id) { - let type_style = theme.and_then(|t| t.find_highlight("ui.virtual.inlay-hint.type")); - let parameter_style = - theme.and_then(|t| t.find_highlight("ui.virtual.inlay-hint.parameter")); - let other_style = theme.and_then(|t| t.find_highlight("ui.virtual.inlay-hint")); + let type_style = theme + .and_then(|t| t.find_scope_index("ui.virtual.inlay-hint.type")) + .map(Highlight); + let parameter_style = theme + .and_then(|t| t.find_scope_index("ui.virtual.inlay-hint.parameter")) + .map(Highlight); + let other_style = theme + .and_then(|t| t.find_scope_index("ui.virtual.inlay-hint")) + .map(Highlight); // Overlapping annotations are ignored apart from the first so the order here is not random: // types -> parameters -> others should hopefully be the "correct" order for most use cases, @@ -474,23 +474,6 @@ impl View { .add_inline_annotations(padding_after_inlay_hints, None); }; let config = doc.config.load(); - - if config.lsp.display_color_swatches { - if let Some(DocumentColorSwatches { - color_swatches, - colors, - color_swatches_padding, - }) = &doc.color_swatches - { - for (color_swatch, color) in color_swatches.iter().zip(colors) { - text_annotations - .add_inline_annotations(std::slice::from_ref(color_swatch), Some(*color)); - } - - text_annotations.add_inline_annotations(color_swatches_padding, None); - } - } - let width = self.inner_width(doc); let enable_cursor_line = self .diagnostics_handler @@ -505,7 +488,7 @@ impl View { doc, cursor, width, - doc.view_offset(self.id).horizontal_offset, + self.offset.horizontal_offset, config, )); } @@ -552,14 +535,13 @@ impl View { ignore_virtual_text: bool, ) -> Option<usize> { let text = doc.text().slice(..); - let view_offset = doc.view_offset(self.id); - let text_row = row as usize + view_offset.vertical_offset; - let text_col = column as usize + view_offset.horizontal_offset; + let text_row = row as usize + self.offset.vertical_offset; + let text_col = column as usize + self.offset.horizontal_offset; let (char_idx, virt_lines) = char_idx_at_visual_offset( text, - view_offset.anchor, + self.offset.anchor, text_row as isize, text_col, &text_fmt, @@ -691,7 +673,7 @@ mod tests { use super::*; use arc_swap::ArcSwap; - use helix_core::{syntax, Rope}; + use helix_core::Rope; // 1 diagnostic + 1 spacer + 3 linenr (< 1000 lines) + 1 spacer + 1 diff const DEFAULT_GUTTER_OFFSET: u16 = 7; @@ -707,13 +689,11 @@ mod tests { let mut view = View::new(DocumentId::default(), GutterConfig::default()); view.area = Rect::new(40, 40, 40, 40); let rope = Rope::from_str("abc\n\tdef"); - let mut doc = Document::from( + let doc = Document::from( rope, None, Arc::new(ArcSwap::new(Arc::new(Config::default()))), - Arc::new(ArcSwap::from_pointee(syntax::Loader::default())), ); - doc.ensure_view_init(view.id); assert_eq!( view.text_pos_at_screen_coords( @@ -883,13 +863,11 @@ mod tests { ); view.area = Rect::new(40, 40, 40, 40); let rope = Rope::from_str("abc\n\tdef"); - let mut doc = Document::from( + let doc = Document::from( rope, None, Arc::new(ArcSwap::new(Arc::new(Config::default()))), - Arc::new(ArcSwap::from_pointee(syntax::Loader::default())), ); - doc.ensure_view_init(view.id); assert_eq!( view.text_pos_at_screen_coords( &doc, @@ -914,13 +892,11 @@ mod tests { ); view.area = Rect::new(40, 40, 40, 40); let rope = Rope::from_str("abc\n\tdef"); - let mut doc = Document::from( + let doc = Document::from( rope, None, Arc::new(ArcSwap::new(Arc::new(Config::default()))), - Arc::new(ArcSwap::from_pointee(syntax::Loader::default())), ); - doc.ensure_view_init(view.id); assert_eq!( view.text_pos_at_screen_coords( &doc, @@ -939,13 +915,11 @@ mod tests { let mut view = View::new(DocumentId::default(), GutterConfig::default()); view.area = Rect::new(40, 40, 40, 40); let rope = Rope::from_str("Hi! こんにちは皆さん"); - let mut doc = Document::from( + let doc = Document::from( rope, None, Arc::new(ArcSwap::new(Arc::new(Config::default()))), - Arc::new(ArcSwap::from_pointee(syntax::Loader::default())), ); - doc.ensure_view_init(view.id); assert_eq!( view.text_pos_at_screen_coords( @@ -1024,13 +998,11 @@ mod tests { let mut view = View::new(DocumentId::default(), GutterConfig::default()); view.area = Rect::new(40, 40, 40, 40); let rope = Rope::from_str("Hèl̀l̀ò world!"); - let mut doc = Document::from( + let doc = Document::from( rope, None, Arc::new(ArcSwap::new(Arc::new(Config::default()))), - Arc::new(ArcSwap::from_pointee(syntax::Loader::default())), ); - doc.ensure_view_init(view.id); assert_eq!( view.text_pos_at_screen_coords( |