Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tab to insert mode #2

Merged
merged 1 commit into from
Oct 4, 2020
Merged

added tab to insert mode #2

merged 1 commit into from
Oct 4, 2020

Conversation

janhrastnik
Copy link
Contributor

Cursor isn't rendering properly when using tabs and I'm not sure why. Should move for 4 characters to the right but only moves for 1.

let line_start = text.line_to_char(line);
let col = text.slice(line_start..pos).len_chars();
let mut x: usize = 0;
let line_slice = text.line(line).slice(..col);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify this:

pub fn screen_coords_at_pos(text: &RopeSlice, pos: usize) -> Coords {
  let line = text.char_to_line(pos);
  let line_start = text.line_to_char(line);
  let line_slice = text.slice(line_start..pos);

  let mut x = 0;
  ...
`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, this returns (screen_x, line_number). What we want is (screen_y, screen_x) so representing (row, col). You should move this to editor.rs screen_coords_at_pos(view: &View, text: &RopeSlice, pos: usize) -> Coords and use view.first_line & last_line on screen to calculate the row.

Add a function called last_line to View

/// Calculates the last visible line on screen
pub fn last_line(&self, viewport: Rect) -> usize {
  std::cmp::min(
      (self.first_line + viewport.height - 1) as usize,
       self.state.doc().len_lines() - 1,
  );
}

Put this in the editor.rs file

/// Translates a document position to an absolute position in the terminal. 
pub fn screen_coords_at_pos(view: &View, text: &RopeSlice, pos: usize) -> Option<Position> {
  let line = text.char_to_line(pos);
  
  if line < self.first_line || line > self.last_line() {
    // Line is not visible on screen
    return None;
  }

  let line_start = text.line_to_char(line);
  let line_slice = text.slice(line_start..pos);

  let mut col = 0;
  
   ... for loop etc

  let row = line - view.first_line;

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

Then you can also replace

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

@archseer archseer merged commit 197651e into master Oct 4, 2020
@archseer archseer deleted the tab-implementation branch October 4, 2020 06:40
SoraTenshi referenced this pull request in SoraTenshi/helix Oct 13, 2022
SoraTenshi referenced this pull request in SoraTenshi/helix Oct 15, 2022
winged pushed a commit to winged/helix-editor that referenced this pull request Oct 18, 2023
add generators, switch statements and cases to ecma queries
voidcontext pushed a commit to voidcontext/helix that referenced this pull request Jul 6, 2024
Add editor-switch-action! command to be able to switch to an existing buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants