Skip to content

Commit

Permalink
introduce helper functions on Document .view_offset and .set_view_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
intarga committed Apr 26, 2024
1 parent 6494301 commit 23ee43a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
12 changes: 5 additions & 7 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ fn goto_window(cx: &mut Context, align: Align) {
let count = cx.count() - 1;
let config = cx.editor.config();
let (view, doc) = current!(cx.editor);
let view_offset = doc.view_data_mut(view.id).view_position;
let view_offset = doc.view_offset(view.id);

let height = view.inner_height();

Expand Down Expand Up @@ -1668,7 +1668,7 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor
use Direction::*;
let config = cx.editor.config();
let (view, doc) = current!(cx.editor);
let view_offset = doc.view_data_mut(view.id).view_position;
let mut view_offset = doc.view_offset(view.id).to_owned();

let range = doc.selection(view.id).primary();
let text = doc.text().slice(..);
Expand All @@ -1686,17 +1686,15 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor
let viewport = view.inner_area(doc);
let text_fmt = doc.text_format(viewport.width, None);
let annotations = view.text_annotations(&*doc, None);
let (new_anchor, new_vertical_offset) = char_idx_at_visual_offset(
(view_offset.anchor, view_offset.vertical_offset) = char_idx_at_visual_offset(
doc_text,
view_offset.anchor,
view_offset.vertical_offset as isize + offset,
0,
&text_fmt,
&annotations,
);
let view_data = doc.view_data_mut(view.id);
view_data.view_position.anchor = new_anchor;
view_data.view_position.vertical_offset = new_vertical_offset;
doc.set_view_offset(view.id, view_offset);

let doc_text = doc.text().slice(..);
let mut annotations = view.text_annotations(&*doc, None);
Expand Down Expand Up @@ -1725,7 +1723,7 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor
return;
}

let view_offset = doc.view_data(view.id).view_position;
let view_offset = doc.view_offset(view.id);

let mut head;
match direction {
Expand Down
8 changes: 4 additions & 4 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl EditorView {
let theme = &editor.theme;
let config = editor.config();

let view_offset = doc.view_data(view.id).view_position;
let view_offset = doc.view_offset(view.id);

let text_annotations = view.text_annotations(doc, Some(theme));
let mut line_decorations: Vec<Box<dyn LineDecoration>> = Vec::new();
Expand Down Expand Up @@ -198,7 +198,7 @@ impl EditorView {
surface,
inner,
doc,
view_offset,
view_offset.to_owned(),
&text_annotations,
syntax_highlights,
overlay_highlights,
Expand Down Expand Up @@ -251,7 +251,7 @@ impl EditorView {
.and_then(|config| config.rulers.as_ref())
.unwrap_or(editor_rulers);

let view_offset = doc.view_data(view.id).view_position;
let view_offset = doc.view_offset(view.id);

rulers
.iter()
Expand Down Expand Up @@ -827,7 +827,7 @@ impl EditorView {
let inner_area = view.inner_area(doc);

let selection = doc.selection(view.id);
let view_offset = doc.view_data(view.id).view_position;
let view_offset = doc.view_offset(view.id);
let primary = selection.primary();
let text_format = doc.text_format(viewport.width, None);
for range in selection.iter() {
Expand Down
8 changes: 8 additions & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,14 @@ impl Document {
self.view_data.entry(view_id).or_default()
}

pub fn view_offset(&self, view_id: ViewId) -> &ViewPosition {
&self.view_data(view_id).view_position
}

pub fn set_view_offset(&mut self, view_id: ViewId, new_offset: ViewPosition) {
self.view_data_mut(view_id).view_position = new_offset;
}

pub fn relative_path(&self) -> Option<Cow<Path>> {
self.path
.as_deref()
Expand Down
6 changes: 3 additions & 3 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ 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_data(self.id).view_position;
let view_offset = doc.view_offset(self.id);

// last visual line in view is trivial to compute
let visual_height =
Expand Down Expand Up @@ -378,7 +378,7 @@ impl View {
text: RopeSlice,
pos: usize,
) -> Option<Position> {
let view_offset = doc.view_data(self.id).view_position;
let view_offset = doc.view_offset(self.id);

if pos < view_offset.anchor {
// Line is not visible on screen
Expand Down Expand Up @@ -500,7 +500,7 @@ impl View {
ignore_virtual_text: bool,
) -> Option<usize> {
let text = doc.text().slice(..);
let view_offset = doc.view_data(self.id).view_position;
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;
Expand Down

0 comments on commit 23ee43a

Please sign in to comment.