Skip to content

Commit

Permalink
added tab to insert mode
Browse files Browse the repository at this point in the history
  • Loading branch information
janhrastnik authored and archseer committed Oct 4, 2020
1 parent 956ccc7 commit 4393e1c
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 19 deletions.
2 changes: 1 addition & 1 deletion helix-syntax/languages/tree-sitter-c-sharp
2 changes: 1 addition & 1 deletion helix-syntax/languages/tree-sitter-javascript
33 changes: 19 additions & 14 deletions helix-term/src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::ArgMatches as Args;
use helix_core::{state::coords_at_pos, state::Mode, syntax::HighlightEvent, Range, State};
use helix_core::{state::Mode, syntax::HighlightEvent, Range, State};
use helix_view::{commands, keymap, View};

use std::{
Expand All @@ -24,6 +24,8 @@ use crossterm::{

use tui::{backend::CrosstermBackend, buffer::Buffer as Surface, layout::Rect, style::Style};

const TAB_WIDTH: usize = 4;

type Terminal = tui::Terminal<CrosstermBackend<std::io::Stdout>>;

static EX: smol::Executor = smol::Executor::new();
Expand Down Expand Up @@ -85,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 @@ -172,6 +170,8 @@ impl Editor {
if line >= viewport.height {
break 'outer;
}
} else if grapheme == "\t" {
visual_x += (TAB_WIDTH as u16);
} else {
// Cow will prevent allocations if span contained in a single slice
// which should really be the majority case
Expand Down Expand Up @@ -281,14 +281,19 @@ impl Editor {

// render the cursor
let pos = view.state.selection().cursor();
let coords = coords_at_pos(&view.state.doc().slice(..), pos);
execute!(
stdout,
cursor::MoveTo(
coords.col as u16 + viewport.x,
coords.row as u16 - view.first_line + viewport.y,
)
);

if let (pos, _width) = view
.screen_coords_at_pos(&view.state.doc().slice(..), pos, area)
.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
4 changes: 4 additions & 0 deletions helix-view/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ pub fn insert_char(view: &mut View, c: char) {
// TODO: need to store into history if successful
}

pub fn insert_tab(view: &mut View, _count: usize) {
insert_char(view, '\t');
}

pub fn insert_newline(view: &mut View, _count: usize) {
insert_char(view, '\n');
}
Expand Down
4 changes: 4 additions & 0 deletions helix-view/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ pub fn default() -> Keymaps {
code: KeyCode::Enter,
modifiers: Modifiers::NONE
}] => commands::insert_newline,
vec![Key {
code: KeyCode::Tab,
modifiers: Modifiers::NONE
}] => commands::insert_tab,
)
)
}
42 changes: 41 additions & 1 deletion helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use anyhow::Error;
use std::path::PathBuf;

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

pub struct View {
pub state: State,
Expand Down Expand Up @@ -44,4 +45,43 @@ impl View {
self.first_line = line.saturating_sub(padding);
}
}

/// Calculates the last visible line on screen
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,
)
}

/// Translates a document position to an absolute position in the terminal.
pub fn screen_coords_at_pos(
&self,
text: &RopeSlice,
pos: usize,
viewport: Rect,
) -> Option<(Position, usize)> {
let line = text.char_to_line(pos);

if line < self.first_line as usize || line > self.get_last_line(viewport) {
// 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 character in line_slice.chars() {
if character == '\t' {
col += 4;
} else {
col += grapheme_width(&character.to_string()[..]);
}
}

let row = line - self.first_line as usize;

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

0 comments on commit 4393e1c

Please sign in to comment.