mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-26 11:23:31 +04:00
Merge pull request #3 from helix-editor/goto-implementation
Goto mode implementation
This commit is contained in:
commit
6848702b1f
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -225,6 +225,7 @@ checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
|
"lazy_static",
|
||||||
"os_str_bytes",
|
"os_str_bytes",
|
||||||
"textwrap",
|
"textwrap",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
pub enum Mode {
|
pub enum Mode {
|
||||||
Normal,
|
Normal,
|
||||||
Insert,
|
Insert,
|
||||||
|
Goto,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A state represents the current editor state of a single buffer.
|
/// A state represents the current editor state of a single buffer.
|
||||||
@ -287,9 +288,6 @@ pub fn extend_selection(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Coordinates are a 0-indexed line and column pair.
|
|
||||||
pub type Coords = (usize, usize); // line, col
|
|
||||||
|
|
||||||
/// Convert a character index to (line, column) coordinates.
|
/// Convert a character index to (line, column) coordinates.
|
||||||
pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Position {
|
pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Position {
|
||||||
let line = text.char_to_line(pos);
|
let line = text.char_to_line(pos);
|
||||||
|
@ -312,7 +312,7 @@ pub struct LanguageLayer {
|
|||||||
tree: Option<Tree>,
|
tree: Option<Tree>,
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::state::{coords_at_pos, Coords};
|
use crate::state::coords_at_pos;
|
||||||
use crate::transaction::{ChangeSet, Operation};
|
use crate::transaction::{ChangeSet, Operation};
|
||||||
use crate::Tendril;
|
use crate::Tendril;
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ fn render(&mut self) {
|
|||||||
// TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
|
// TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
|
||||||
let source_code = view.state.doc().to_string();
|
let source_code = view.state.doc().to_string();
|
||||||
|
|
||||||
let last_line = view.last_line(viewport);
|
let last_line = view.last_line();
|
||||||
|
|
||||||
let range = {
|
let range = {
|
||||||
// calculate viewport byte ranges
|
// calculate viewport byte ranges
|
||||||
@ -219,7 +219,7 @@ fn render(&mut self) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let style: Style = view.theme.get("ui.linenr");
|
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
|
self.surface
|
||||||
.set_stringn(0, i as u16, format!("{:>5}", line + 1), 5, style);
|
.set_stringn(0, i as u16, format!("{:>5}", line + 1), 5, style);
|
||||||
// lavender
|
// lavender
|
||||||
@ -254,6 +254,7 @@ fn render(&mut self) {
|
|||||||
let mode = match view.state.mode() {
|
let mode = match view.state.mode() {
|
||||||
Mode::Insert => "INS",
|
Mode::Insert => "INS",
|
||||||
Mode::Normal => "NOR",
|
Mode::Normal => "NOR",
|
||||||
|
Mode::Goto => "GOTO",
|
||||||
};
|
};
|
||||||
self.surface.set_style(
|
self.surface.set_style(
|
||||||
Rect::new(0, self.size.1 - 1, self.size.0, 1),
|
Rect::new(0, self.size.1 - 1, self.size.0, 1),
|
||||||
@ -278,13 +279,14 @@ fn render(&mut self) {
|
|||||||
match view.state.mode() {
|
match view.state.mode() {
|
||||||
Mode::Insert => write!(stdout, "\x1B[6 q"),
|
Mode::Insert => write!(stdout, "\x1B[6 q"),
|
||||||
Mode::Normal => write!(stdout, "\x1B[2 q"),
|
Mode::Normal => write!(stdout, "\x1B[2 q"),
|
||||||
|
Mode::Goto => write!(stdout, "\x1B[2 q"),
|
||||||
};
|
};
|
||||||
|
|
||||||
// render the cursor
|
// render the cursor
|
||||||
let pos = view.state.selection().cursor();
|
let pos = view.state.selection().cursor();
|
||||||
|
|
||||||
let pos = view
|
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.");
|
.expect("Cursor is out of bounds.");
|
||||||
|
|
||||||
execute!(
|
execute!(
|
||||||
@ -326,6 +328,7 @@ pub async fn event_loop(&mut self) {
|
|||||||
}))) => {
|
}))) => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(Ok(Event::Key(event))) => {
|
Some(Ok(Event::Key(event))) => {
|
||||||
if let Some(view) = &mut self.view {
|
if let Some(view) = &mut self.view {
|
||||||
match view.state.mode() {
|
match view.state.mode() {
|
||||||
@ -356,6 +359,19 @@ pub async fn event_loop(&mut self) {
|
|||||||
// TODO: simplistic ensure cursor in view for now
|
// TODO: simplistic ensure cursor in view for now
|
||||||
view.ensure_cursor_in_view();
|
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();
|
self.render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1000
helix-term/test.txt
Normal file
1000
helix-term/test.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -13,6 +13,8 @@
|
|||||||
/// state (usually by creating and applying a transaction).
|
/// state (usually by creating and applying a transaction).
|
||||||
pub type Command = fn(view: &mut View, count: usize);
|
pub type Command = fn(view: &mut View, count: usize);
|
||||||
|
|
||||||
|
const PADDING: usize = 5;
|
||||||
|
|
||||||
pub fn move_char_left(view: &mut View, count: usize) {
|
pub fn move_char_left(view: &mut View, count: usize) {
|
||||||
// TODO: use a transaction
|
// TODO: use a transaction
|
||||||
let selection = view
|
let selection = view
|
||||||
@ -117,6 +119,74 @@ pub fn move_next_word_end(view: &mut View, count: usize) {
|
|||||||
view.state.selection = Selection::single(pos, pos);
|
view.state.selection = Selection::single(pos, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn move_file_start(view: &mut View, _count: usize) {
|
||||||
|
// TODO: use a transaction
|
||||||
|
view.state.selection = Selection::single(0, 0);
|
||||||
|
|
||||||
|
view.state.mode = Mode::Normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn move_file_end(view: &mut View, _count: usize) {
|
||||||
|
// TODO: use a transaction
|
||||||
|
let text = &view.state.doc;
|
||||||
|
let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
|
||||||
|
view.state.selection = Selection::single(last_line, last_line);
|
||||||
|
|
||||||
|
view.state.mode = Mode::Normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check_cursor_in_view(view: &mut View) -> bool {
|
||||||
|
let cursor = view.state.selection().cursor();
|
||||||
|
let line = view.state.doc().char_to_line(cursor);
|
||||||
|
let document_end = view.first_line + view.size.1.saturating_sub(1) as usize;
|
||||||
|
|
||||||
|
if (line > document_end.saturating_sub(PADDING)) | (line < view.first_line + PADDING) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn page_up(view: &mut View, _count: usize) {
|
||||||
|
view.first_line = view.first_line.saturating_sub(view.size.1 as usize);
|
||||||
|
|
||||||
|
if !check_cursor_in_view(view) {
|
||||||
|
let text = view.state.doc();
|
||||||
|
let pos = text.line_to_char(view.last_line().saturating_sub(PADDING as usize));
|
||||||
|
view.state.selection = Selection::single(pos, pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn page_down(view: &mut View, _count: usize) {
|
||||||
|
view.first_line += view.size.1 as usize + PADDING;
|
||||||
|
|
||||||
|
if view.first_line < view.state.doc().len_lines() {
|
||||||
|
let text = view.state.doc();
|
||||||
|
let pos = text.line_to_char(view.first_line as usize);
|
||||||
|
view.state.selection = Selection::single(pos, pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn half_page_up(view: &mut View, _count: usize) {
|
||||||
|
view.first_line = view.first_line.saturating_sub(view.size.1 as usize / 2);
|
||||||
|
|
||||||
|
if !check_cursor_in_view(view) {
|
||||||
|
let text = &view.state.doc;
|
||||||
|
let pos = text.line_to_char(view.last_line() - PADDING as usize);
|
||||||
|
view.state.selection = Selection::single(pos, pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn half_page_down(view: &mut View, _count: usize) {
|
||||||
|
let lines = view.state.doc().len_lines();
|
||||||
|
if view.first_line < lines.saturating_sub(view.size.1 as usize) {
|
||||||
|
view.first_line += view.size.1 as usize / 2;
|
||||||
|
}
|
||||||
|
if !check_cursor_in_view(view) {
|
||||||
|
let text = view.state.doc();
|
||||||
|
let pos = text.line_to_char(view.first_line as usize);
|
||||||
|
view.state.selection = Selection::single(pos, pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
// avoid select by default by having a visual mode switch that makes movements into selects
|
// avoid select by default by having a visual mode switch that makes movements into selects
|
||||||
|
|
||||||
pub fn extend_char_left(view: &mut View, count: usize) {
|
pub fn extend_char_left(view: &mut View, count: usize) {
|
||||||
@ -292,6 +362,10 @@ pub fn normal_mode(view: &mut View, _count: usize) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn goto_mode(view: &mut View, _count: usize) {
|
||||||
|
view.state.mode = Mode::Goto;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: insert means add text just before cursor, on exit we should be on the last letter.
|
// TODO: insert means add text just before cursor, on exit we should be on the last letter.
|
||||||
pub fn insert_char(view: &mut View, c: char) {
|
pub fn insert_char(view: &mut View, c: char) {
|
||||||
let c = Tendril::from_char(c);
|
let c = Tendril::from_char(c);
|
||||||
|
@ -108,6 +108,15 @@ macro_rules! shift {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! ctrl {
|
||||||
|
($ch:expr) => {
|
||||||
|
Key {
|
||||||
|
code: KeyCode::Char($ch),
|
||||||
|
modifiers: Modifiers::CONTROL,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub fn default() -> Keymaps {
|
pub fn default() -> Keymaps {
|
||||||
hashmap!(
|
hashmap!(
|
||||||
state::Mode::Normal =>
|
state::Mode::Normal =>
|
||||||
@ -126,6 +135,7 @@ pub fn default() -> Keymaps {
|
|||||||
vec![key!('w')] => commands::move_next_word_start,
|
vec![key!('w')] => commands::move_next_word_start,
|
||||||
vec![key!('b')] => commands::move_prev_word_start,
|
vec![key!('b')] => commands::move_prev_word_start,
|
||||||
vec![key!('e')] => commands::move_next_word_end,
|
vec![key!('e')] => commands::move_next_word_end,
|
||||||
|
vec![key!('g')] => commands::goto_mode,
|
||||||
vec![key!('i')] => commands::insert_mode,
|
vec![key!('i')] => commands::insert_mode,
|
||||||
vec![shift!('I')] => commands::prepend_to_line,
|
vec![shift!('I')] => commands::prepend_to_line,
|
||||||
vec![key!('a')] => commands::append_mode,
|
vec![key!('a')] => commands::append_mode,
|
||||||
@ -139,6 +149,16 @@ pub fn default() -> Keymaps {
|
|||||||
code: KeyCode::Esc,
|
code: KeyCode::Esc,
|
||||||
modifiers: Modifiers::NONE
|
modifiers: Modifiers::NONE
|
||||||
}] => commands::normal_mode,
|
}] => commands::normal_mode,
|
||||||
|
vec![Key {
|
||||||
|
code: KeyCode::PageUp,
|
||||||
|
modifiers: Modifiers::NONE
|
||||||
|
}] => commands::page_up,
|
||||||
|
vec![Key {
|
||||||
|
code: KeyCode::PageDown,
|
||||||
|
modifiers: Modifiers::NONE
|
||||||
|
}] => commands::page_down,
|
||||||
|
vec![ctrl!('u')] => commands::half_page_up,
|
||||||
|
vec![ctrl!('d')] => commands::half_page_down,
|
||||||
),
|
),
|
||||||
state::Mode::Insert => hashmap!(
|
state::Mode::Insert => hashmap!(
|
||||||
vec![Key {
|
vec![Key {
|
||||||
@ -161,6 +181,14 @@ pub fn default() -> Keymaps {
|
|||||||
code: KeyCode::Tab,
|
code: KeyCode::Tab,
|
||||||
modifiers: Modifiers::NONE
|
modifiers: Modifiers::NONE
|
||||||
}] => commands::insert_tab,
|
}] => commands::insert_tab,
|
||||||
|
),
|
||||||
|
state::Mode::Goto => hashmap!(
|
||||||
|
vec![Key {
|
||||||
|
code: KeyCode::Esc,
|
||||||
|
modifiers: Modifiers::NONE
|
||||||
|
}] => commands::normal_mode as Command,
|
||||||
|
vec![key!('g')] => commands::move_file_start as Command,
|
||||||
|
vec![key!('e')] => commands::move_file_end as Command,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
pub struct View {
|
pub struct View {
|
||||||
pub state: State,
|
pub state: State,
|
||||||
pub first_line: u16,
|
pub first_line: usize,
|
||||||
pub size: (u16, u16),
|
pub size: (u16, u16),
|
||||||
pub theme: Theme, // TODO: share one instance
|
pub theme: Theme, // TODO: share one instance
|
||||||
}
|
}
|
||||||
@ -33,10 +33,10 @@ pub fn open(path: PathBuf, size: (u16, u16)) -> Result<Self, Error> {
|
|||||||
|
|
||||||
pub fn ensure_cursor_in_view(&mut self) {
|
pub fn ensure_cursor_in_view(&mut self) {
|
||||||
let cursor = self.state.selection().cursor();
|
let cursor = self.state.selection().cursor();
|
||||||
let line = self.state.doc().char_to_line(cursor) as u16;
|
let line = self.state.doc().char_to_line(cursor);
|
||||||
let document_end = self.first_line + self.size.1.saturating_sub(1) - 1;
|
let document_end = self.first_line + (self.size.1 as usize).saturating_sub(1);
|
||||||
|
|
||||||
let padding = 5u16;
|
let padding = 5usize;
|
||||||
|
|
||||||
// TODO: side scroll
|
// TODO: side scroll
|
||||||
|
|
||||||
@ -51,9 +51,10 @@ pub fn ensure_cursor_in_view(&mut self) {
|
|||||||
|
|
||||||
/// Calculates the last visible line on screen
|
/// Calculates the last visible line on screen
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn last_line(&self, viewport: Rect) -> usize {
|
pub fn last_line(&self) -> usize {
|
||||||
|
let viewport = Rect::new(6, 0, self.size.0, self.size.1 - 1); // - 1 for statusline
|
||||||
std::cmp::min(
|
std::cmp::min(
|
||||||
(self.first_line + viewport.height) as usize,
|
self.first_line + (viewport.height as usize),
|
||||||
self.state.doc().len_lines() - 1,
|
self.state.doc().len_lines() - 1,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -61,15 +62,10 @@ pub fn last_line(&self, viewport: Rect) -> usize {
|
|||||||
/// Translates a document position to an absolute position in the terminal.
|
/// Translates a document position to an absolute position in the terminal.
|
||||||
/// Returns a (line, col) position if the position is visible on screen.
|
/// Returns a (line, col) position if the position is visible on screen.
|
||||||
// TODO: Could return width as well for the character width at cursor.
|
// TODO: Could return width as well for the character width at cursor.
|
||||||
pub fn screen_coords_at_pos(
|
pub fn screen_coords_at_pos(&self, text: &RopeSlice, pos: usize) -> Option<Position> {
|
||||||
&self,
|
|
||||||
text: &RopeSlice,
|
|
||||||
pos: usize,
|
|
||||||
viewport: Rect,
|
|
||||||
) -> Option<Position> {
|
|
||||||
let line = text.char_to_line(pos);
|
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.last_line() {
|
||||||
// Line is not visible on screen
|
// Line is not visible on screen
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user