on master help sign in
Refactor view.first_line/first_col into view.offset (Position)
commit9776553ad057c12ccee8c0d54d419b159977f34e authorBlaž Hrastnik <blaz@mxxn.io> dateThu 19 Aug 2021, 12:52 (5 years ago) parent115754c
5 files changed+37−40
helix-term/src/commands.rs+11−11 helix-term/src/ui/completion.rs+1−1 helix-term/src/ui/editor.rs+7−8 helix-view/src/editor.rs+2−2 helix-view/src/view.rs+16−18
helix-term/src/commands.rs+11−11
@@ -118,7 +118,7 @@ fn align_view(doc: &Document, view: &mut View, align: Align) {
118118 Align::Bottom => height,
119119 };
120120
121− view.first_line = line.saturating_sub(relative);
121+ view.offset.row = line.saturating_sub(relative);
122122}
123123
124124/// A command is composed of a static name, and a function that takes the current state plus a count,
@@ -465,8 +465,8 @@ fn goto_window(cx: &mut Context, align: Align) {
465465 let last_line = view.last_line(doc);
466466
467467 let line = match align {
468− Align::Top => (view.first_line + scrolloff),
469− Align::Center => (view.first_line + (height / 2)),
468+ Align::Top => (view.offset.row + scrolloff),
469+ Align::Center => (view.offset.row + (height / 2)),
470470 Align::Bottom => last_line.saturating_sub(scrolloff),
471471 }
472472 .min(last_line.saturating_sub(scrolloff));
@@ -892,7 +892,7 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
892892
893893 let last_line = view.last_line(doc);
894894
895− if direction == Backward && view.first_line == 0
895+ if direction == Backward && view.offset.row == 0
896896 || direction == Forward && last_line == doc_last_line
897897 {
898898 return;
@@ -904,9 +904,9 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
904904 .scrolloff
905905 .min(view.area.height as usize / 2);
906906
907− view.first_line = match direction {
908− Forward => view.first_line + offset,
909− Backward => view.first_line.saturating_sub(offset),
907+ view.offset.row = match direction {
908+ Forward => view.offset.row + offset,
909+ Backward => view.offset.row.saturating_sub(offset),
910910 }
911911 .min(doc_last_line);
912912
@@ -916,7 +916,7 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
916916 // clamp into viewport
917917 let line = cursor
918918 .row
919− .max(view.first_line + scrolloff)
919+ .max(view.offset.row + scrolloff)
920920 .min(last_line.saturating_sub(scrolloff));
921921
922922 let text = doc.text().slice(..);
@@ -4058,13 +4058,13 @@ fn split(cx: &mut Context, action: Action) {
40584058 let (view, doc) = current!(cx.editor);
40594059 let id = doc.id();
40604060 let selection = doc.selection(view.id).clone();
4061− let first_line = view.first_line;
4061+ let offset = view.offset;
40624062
40634063 cx.editor.switch(id, action);
40644064
40654065 // match the selection in the previous view
40664066 let (view, doc) = current!(cx.editor);
4067− view.first_line = first_line;
4067+ view.offset = offset;
40684068 doc.set_selection(view.id, selection);
40694069}
40704070
@@ -4113,7 +4113,7 @@ fn align_view_middle(cx: &mut Context) {
41134113 .cursor(doc.text().slice(..));
41144114 let pos = coords_at_pos(doc.text().slice(..), pos);
41154115
4116− view.first_col = pos.col.saturating_sub(
4116+ view.offset.col = pos.col.saturating_sub(
41174117 ((view.area.width as usize).saturating_sub(crate::ui::editor::GUTTER_OFFSET as usize)) / 2,
41184118 );
41194119}
helix-term/src/ui/completion.rs+1−1
@@ -261,7 +261,7 @@ impl Component for Completion {
261261 .primary()
262262 .cursor(doc.text().slice(..));
263263 let cursor_pos = (helix_core::coords_at_pos(doc.text().slice(..), cursor_pos).row
264− - view.first_line) as u16;
264+ - view.offset.row) as u16;
265265
266266 let mut doc = match &option.documentation {
267267 Some(lsp::Documentation::String(contents))
helix-term/src/ui/editor.rs+7−8
@@ -80,10 +80,9 @@ impl EditorView {
8080 view.area.width - GUTTER_OFFSET,
8181 view.area.height.saturating_sub(1),
8282 ); // - 1 for statusline
83− let offset = Position::new(view.first_line, view.first_col);
8483 let height = view.area.height.saturating_sub(1); // - 1 for statusline
8584
86− let highlights = Self::doc_syntax_highlights(doc, offset, height, theme, loader);
85+ let highlights = Self::doc_syntax_highlights(doc, view.offset, height, theme, loader);
8786 let highlights = syntax::merge(highlights, Self::doc_diagnostics_highlights(doc, theme));
8887 let highlights: Box<dyn Iterator<Item = HighlightEvent>> = if is_focused {
8988 Box::new(syntax::merge(
@@ -94,7 +93,7 @@ impl EditorView {
9493 Box::new(highlights)
9594 };
9695
97− Self::render_text_highlights(doc, offset, area, surface, theme, highlights);
96+ Self::render_text_highlights(doc, view.offset, area, surface, theme, highlights);
9897 Self::render_gutter(doc, view, area, surface, theme, config);
9998
10099 if is_focused {
@@ -382,7 +381,7 @@ impl EditorView {
382381 let selection = doc.selection(view.id);
383382 let last_line = view.last_line(doc);
384383 let screen = {
385− let start = text.line_to_char(view.first_line);
384+ let start = text.line_to_char(view.offset.row);
386385 let end = text.line_to_char(last_line + 1) + 1; // +1 for cursor at end of text.
387386 Range::new(start, end)
388387 };
@@ -408,7 +407,7 @@ impl EditorView {
408407 );
409408 if let Some(head) = head {
410409 // Highlight line number for selected lines.
411− let line_number = view.first_line + head.row;
410+ let line_number = view.offset.row + head.row;
412411 let line_number_text = if line_number == last_line && !draw_last {
413412 " ~".into()
414413 } else {
@@ -435,8 +434,8 @@ impl EditorView {
435434
436435 if let Some(pos) = pos {
437436 // ensure col is on screen
438− if (pos.col as u16) < viewport.width + view.first_col as u16
439− && pos.col >= view.first_col
437+ if (pos.col as u16) < viewport.width + view.offset.col as u16
438+ && pos.col >= view.offset.col
440439 {
441440 let style = theme.try_get("ui.cursor.match").unwrap_or_else(|| {
442441 Style::default()
@@ -479,7 +478,7 @@ impl EditorView {
479478 let current_line = doc
480479 .text()
481480 .char_to_line(doc.selection(view.id).primary().anchor);
482− for (i, line) in (view.first_line..(last_line + 1)).enumerate() {
481+ for (i, line) in (view.offset.row..(last_line + 1)).enumerate() {
483482 use helix_core::diagnostic::Severity;
484483 if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) {
485484 surface.set_stringn(
helix-view/src/editor.rs+2−2
@@ -180,7 +180,7 @@ impl Editor {
180180 view.jumps.push(jump);
181181 view.last_accessed_doc = Some(view.doc);
182182 view.doc = id;
183− view.first_line = 0;
183+ view.offset = Position::default();
184184
185185 let (view, doc) = current!(self);
186186
@@ -194,7 +194,7 @@ impl Editor {
194194 .primary()
195195 .cursor(doc.text().slice(..));
196196 let line = doc.text().char_to_line(pos);
197− view.first_line = line.saturating_sub(view.area.height as usize / 2);
197+ view.offset.row = line.saturating_sub(view.area.height as usize / 2);
198198
199199 return;
200200 }
helix-view/src/view.rs+16−18
@@ -61,8 +61,7 @@ impl JumpList {
6161pub struct View {
6262 pub id: ViewId,
6363 pub doc: DocumentId,
64− pub first_line: usize,
65− pub first_col: usize,
64+ pub offset: Position,
6665 pub area: Rect,
6766 pub jumps: JumpList,
6867 /// the last accessed file before the current one
@@ -74,8 +73,7 @@ impl View {
7473 Self {
7574 id: ViewId::default(),
7675 doc,
77− first_line: 0,
78− first_col: 0,
76+ offset: Position::new(0, 0),
7977 area: Rect::default(), // will get calculated upon inserting into tree
8078 jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
8179 last_accessed_doc: None,
@@ -91,7 +89,7 @@ impl View {
9189 let line = pos.row;
9290 let col = pos.col;
9391 let height = self.area.height.saturating_sub(1); // - 1 for statusline
94− let last_line = (self.first_line + height as usize).saturating_sub(1);
92+ let last_line = (self.offset.row + height as usize).saturating_sub(1);
9593
9694 // - 1 so we have at least one gap in the middle.
9795 // a height of 6 with padding of 3 on each side will keep shifting the view back and forth
@@ -100,22 +98,22 @@ impl View {
10098
10199 // TODO: not ideal
102100 const OFFSET: usize = 7; // 1 diagnostic + 5 linenr + 1 gutter
103− let last_col = (self.first_col + self.area.width as usize).saturating_sub(OFFSET + 1);
101+ let last_col = (self.offset.col + self.area.width as usize).saturating_sub(OFFSET + 1);
104102
105103 if line > last_line.saturating_sub(scrolloff) {
106104 // scroll down
107− self.first_line += line - (last_line.saturating_sub(scrolloff));
108− } else if line < self.first_line + scrolloff {
105+ self.offset.row += line - (last_line.saturating_sub(scrolloff));
106+ } else if line < self.offset.row + scrolloff {
109107 // scroll up
110− self.first_line = line.saturating_sub(scrolloff);
108+ self.offset.row = line.saturating_sub(scrolloff);
111109 }
112110
113111 if col > last_col.saturating_sub(scrolloff) {
114112 // scroll right
115− self.first_col += col - (last_col.saturating_sub(scrolloff));
116− } else if col < self.first_col + scrolloff {
113+ self.offset.col += col - (last_col.saturating_sub(scrolloff));
114+ } else if col < self.offset.col + scrolloff {
117115 // scroll left
118− self.first_col = col.saturating_sub(scrolloff);
116+ self.offset.col = col.saturating_sub(scrolloff);
119117 }
120118 }
121119
@@ -125,7 +123,7 @@ impl View {
125123 let height = self.area.height.saturating_sub(1); // - 1 for statusline
126124 std::cmp::min(
127125 // Saturating subs to make it inclusive zero indexing.
128− (self.first_line + height as usize).saturating_sub(1),
126+ (self.offset.row + height as usize).saturating_sub(1),
129127 doc.text().len_lines().saturating_sub(1),
130128 )
131129 }
@@ -141,7 +139,7 @@ impl View {
141139 ) -> Option<Position> {
142140 let line = text.char_to_line(pos);
143141
144− if line < self.first_line || line > self.last_line(doc) {
142+ if line < self.offset.row || line > self.last_line(doc) {
145143 // Line is not visible on screen
146144 return None;
147145 }
@@ -161,8 +159,8 @@ impl View {
161159 }
162160
163161 // It is possible for underflow to occur if the buffer length is larger than the terminal width.
164− let row = line.saturating_sub(self.first_line);
165− let col = col.saturating_sub(self.first_col);
162+ let row = line.saturating_sub(self.offset.row);
163+ let col = col.saturating_sub(self.offset.col);
166164
167165 Some(Position::new(row, col))
168166 }
@@ -186,7 +184,7 @@ impl View {
186184 return None;
187185 }
188186
189− let line_number = (row - self.area.y) as usize + self.first_line;
187+ let line_number = (row - self.area.y) as usize + self.offset.row;
190188
191189 if line_number > text.len_lines() - 1 {
192190 return Some(text.len_chars());
@@ -196,7 +194,7 @@ impl View {
196194
197195 let current_line = text.line(line_number);
198196
199− let target = (column - OFFSET - self.area.x) as usize + self.first_col;
197+ let target = (column - OFFSET - self.area.x) as usize + self.offset.col;
200198 let mut selected = 0;
201199
202200 for grapheme in RopeGraphemes::new(current_line) {