Skip to content

Commit

Permalink
Merge pull request #3 from helix-editor/goto-implementation
Browse files Browse the repository at this point in the history
Goto mode implementation
  • Loading branch information
archseer authored Oct 7, 2020
2 parents b7e1c0c + 7f07e66 commit 6848702
Show file tree
Hide file tree
Showing 8 changed files with 1,133 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions helix-core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::path::PathBuf;
pub enum Mode {
Normal,
Insert,
Goto,
}

/// A state represents the current editor state of a single buffer.
Expand Down Expand Up @@ -287,9 +288,6 @@ impl State {
}
}

/// Coordinates are a 0-indexed line and column pair.
pub type Coords = (usize, usize); // line, col

/// Convert a character index to (line, column) coordinates.
pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Position {
let line = text.char_to_line(pos);
Expand Down
2 changes: 1 addition & 1 deletion helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ pub struct LanguageLayer {
tree: Option<Tree>,
}

use crate::state::{coords_at_pos, Coords};
use crate::state::coords_at_pos;
use crate::transaction::{ChangeSet, Operation};
use crate::Tendril;

Expand Down
22 changes: 19 additions & 3 deletions helix-term/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +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 = view.last_line(viewport);
let last_line = view.last_line();

let range = {
// calculate viewport byte ranges
Expand Down Expand Up @@ -219,7 +219,7 @@ impl Editor {
}

let style: Style = view.theme.get("ui.linenr");
for (i, line) in (view.first_line..(last_line as u16)).enumerate() {
for (i, line) in (view.first_line..last_line).enumerate() {
self.surface
.set_stringn(0, i as u16, format!("{:>5}", line + 1), 5, style);
// lavender
Expand Down Expand Up @@ -254,6 +254,7 @@ impl Editor {
let mode = match view.state.mode() {
Mode::Insert => "INS",
Mode::Normal => "NOR",
Mode::Goto => "GOTO",
};
self.surface.set_style(
Rect::new(0, self.size.1 - 1, self.size.0, 1),
Expand All @@ -278,13 +279,14 @@ impl Editor {
match view.state.mode() {
Mode::Insert => write!(stdout, "\x1B[6 q"),
Mode::Normal => write!(stdout, "\x1B[2 q"),
Mode::Goto => write!(stdout, "\x1B[2 q"),
};

// render the cursor
let pos = view.state.selection().cursor();

let pos = view
.screen_coords_at_pos(&view.state.doc().slice(..), pos, area)
.screen_coords_at_pos(&view.state.doc().slice(..), pos)
.expect("Cursor is out of bounds.");

execute!(
Expand Down Expand Up @@ -326,6 +328,7 @@ impl Editor {
}))) => {
break;
}

Some(Ok(Event::Key(event))) => {
if let Some(view) = &mut self.view {
match view.state.mode() {
Expand Down Expand Up @@ -356,6 +359,19 @@ impl Editor {
// TODO: simplistic ensure cursor in view for now
view.ensure_cursor_in_view();

self.render();
}
}
Mode::Goto => {
// TODO: handle modes and sequences (`gg`)
let keys = vec![event];
if let Some(command) = keymap[&Mode::Goto].get(&keys) {
// TODO: handle count other than 1
command(view, 1);

// TODO: simplistic ensure cursor in view for now
view.ensure_cursor_in_view();

self.render();
}
}
Expand Down
Loading

0 comments on commit 6848702

Please sign in to comment.