Skip to content

Commit

Permalink
screen_coords_at_pos fix
Browse files Browse the repository at this point in the history
  • Loading branch information
janhrastnik committed Oct 3, 2020
1 parent 7de98c2 commit 54f618a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
26 changes: 12 additions & 14 deletions helix-term/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ impl Editor {
// TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
let source_code = view.state.doc().to_string();

let last_line = std::cmp::min(
(view.first_line + viewport.height - 1) as usize,
view.state.doc().len_lines() - 1,
);

let last_line = view.get_last_line(viewport);
let range = {
// calculate viewport byte ranges
let start = view.state.doc().line_to_byte(view.first_line.into());
Expand Down Expand Up @@ -286,16 +282,18 @@ impl Editor {
// render the cursor
let pos = view.state.selection().cursor();

let coords = view
if let (pos, _width) = view
.screen_coords_at_pos(&view.state.doc().slice(..), pos, area)
.unwrap();
execute!(
stdout,
cursor::MoveTo(
coords.row as u16 + viewport.x,
coords.col as u16 - view.first_line + viewport.y,
)
);
.expect("Cursor is out of bounds.")
{
execute!(
stdout,
cursor::MoveTo(
pos.col as u16 + viewport.x,
pos.row as u16 - view.first_line + viewport.y,
)
);
}
}
None => (),
}
Expand Down
12 changes: 6 additions & 6 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::Error;
use std::path::PathBuf;

use crate::theme::Theme;
use helix_core::{Position, RopeSlice, State};
use helix_core::{graphemes::grapheme_width, Position, RopeSlice, State};
use tui::layout::Rect;

pub struct View {
Expand Down Expand Up @@ -47,7 +47,7 @@ impl View {
}

/// Calculates the last visible line on screen
pub fn last_line(&self, viewport: Rect) -> usize {
pub fn get_last_line(&self, viewport: Rect) -> usize {
std::cmp::min(
(self.first_line + viewport.height - 1) as usize,
self.state.doc().len_lines() - 1,
Expand All @@ -60,10 +60,10 @@ impl View {
text: &RopeSlice,
pos: usize,
viewport: Rect,
) -> Option<Position> {
) -> Option<(Position, usize)> {
let line = text.char_to_line(pos);

if line < self.first_line as usize || line > self.last_line(viewport) {
if line < self.first_line as usize || line > self.get_last_line(viewport) {
// Line is not visible on screen
return None;
}
Expand All @@ -76,12 +76,12 @@ impl View {
if character == '\t' {
col += 4;
} else {
col += 1;
col += grapheme_width(&character.to_string()[..]);
}
}

let row = line - self.first_line as usize;

Some(Position::new(row, col))
Some((Position::new(row, col), 0))
}
}

0 comments on commit 54f618a

Please sign in to comment.