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

Implement visual mode support to editor #35

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub enum KeyEvent {
P,
S,
U,
V,
W,
X,
Y,
Expand Down
67 changes: 52 additions & 15 deletions core/src/state/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use {
consume::{directory, note, traverse},
inner_state::{
InnerState::{self, *},
VimState,
VimNormalState, VimVisualState,
},
};

Expand Down Expand Up @@ -92,22 +92,22 @@ impl NotebookState {
NoteTreeNumber(n) => {
format!("Steps: '{n}' selected")
}
EditingNormalMode(VimState::Idle) => {
EditingNormalMode(VimNormalState::Idle) => {
let name = &self.get_selected_note()?.name;

format!("Note '{name}' normal mode")
}
EditingNormalMode(VimState::Numbering(n)) => {
EditingNormalMode(VimNormalState::Numbering(n)) => {
let name = &self.get_selected_note()?.name;

format!("Note '{name}' normal mode, steps: '{n}'")
}
EditingNormalMode(VimState::Gateway) => {
EditingNormalMode(VimNormalState::Gateway) => {
let name = &self.get_selected_note()?.name;

format!("Note '{name}' normal mode - gateway")
}
EditingNormalMode(VimState::Yank(n)) => {
EditingNormalMode(VimNormalState::Yank(n)) => {
let name = &self.get_selected_note()?.name;

let n = if *n >= 2 {
Expand All @@ -117,7 +117,7 @@ impl NotebookState {
};
format!("Note '{name}' normal mode - yank '{n}y'")
}
EditingNormalMode(VimState::Yank2(n1, n2)) => {
EditingNormalMode(VimNormalState::Yank2(n1, n2)) => {
let name = &self.get_selected_note()?.name;
let n1 = if *n1 >= 2 {
format!("{n1}")
Expand All @@ -132,7 +132,7 @@ impl NotebookState {

format!("Note '{name}' normal mode - yank '{n1}y{n2}'")
}
EditingNormalMode(VimState::Delete(n)) => {
EditingNormalMode(VimNormalState::Delete(n)) => {
let name = &self.get_selected_note()?.name;

let n = if *n >= 2 {
Expand All @@ -142,7 +142,7 @@ impl NotebookState {
};
format!("Note '{name}' normal mode - delete '{n}d'")
}
EditingNormalMode(VimState::Delete2(n1, n2)) => {
EditingNormalMode(VimNormalState::Delete2(n1, n2)) => {
let name = &self.get_selected_note()?.name;
let n1 = if *n1 >= 2 {
format!("{n1}")
Expand All @@ -157,6 +157,21 @@ impl NotebookState {

format!("Note '{name}' normal mode - delete '{n1}d{n2}'")
}
EditingVisualMode(VimVisualState::Idle) => {
let name = &self.get_selected_note()?.name;

format!("Note '{name}' visual mode")
}
EditingVisualMode(VimVisualState::Numbering(n)) => {
let name = &self.get_selected_note()?.name;

format!("Note '{name}' visual mode, input: '{n}'")
}
EditingVisualMode(VimVisualState::Gateway) => {
let name = &self.get_selected_note()?.name;

format!("Note '{name}' visual mode - gateway")
}
EditingInsertMode => {
let name = &self.get_selected_note()?.name;

Expand Down Expand Up @@ -197,7 +212,7 @@ impl NotebookState {
"[Esc] Cancel".to_owned(),
]
}
EditingNormalMode(VimState::Idle) => {
EditingNormalMode(VimNormalState::Idle) => {
/* TODO:
[o] insert new line below
[O] insert new line above
Expand All @@ -216,7 +231,7 @@ impl NotebookState {
"[Esc] Quit".to_owned(),
]
}
EditingNormalMode(VimState::Numbering(n)) => {
EditingNormalMode(VimNormalState::Numbering(n)) => {
// TODO: s, S, x, y, d

vec![
Expand All @@ -227,20 +242,20 @@ impl NotebookState {
"[Esc] Cancel".to_owned(),
]
}
EditingNormalMode(VimState::Gateway) => {
EditingNormalMode(VimNormalState::Gateway) => {
vec![
"[g] Move cursor to top".to_owned(),
"[Esc] Cancel".to_owned(),
]
}
EditingNormalMode(VimState::Yank(n)) => {
EditingNormalMode(VimNormalState::Yank(n)) => {
vec![
format!("[y] Yank {n} lines"),
"[1-9] Append steps".to_owned(),
"[Esc] Cancel".to_owned(),
]
}
EditingNormalMode(VimState::Yank2(n1, n2)) => {
EditingNormalMode(VimNormalState::Yank2(n1, n2)) => {
vec![
if *n1 == 1 {
format!("[y] Yank {n2} lines")
Expand All @@ -251,14 +266,14 @@ impl NotebookState {
"[Esc] Cancel".to_owned(),
]
}
EditingNormalMode(VimState::Delete(n)) => {
EditingNormalMode(VimNormalState::Delete(n)) => {
vec![
format!("[d] Delete {n} lines"),
"[1-9] Append steps".to_owned(),
"[Esc] Cancel".to_owned(),
]
}
EditingNormalMode(VimState::Delete2(n1, n2)) => {
EditingNormalMode(VimNormalState::Delete2(n1, n2)) => {
vec![
if *n1 == 1 {
format!("[d] Delete {n2} lines")
Expand All @@ -269,6 +284,28 @@ impl NotebookState {
"[Esc] Cancel".to_owned(),
]
}
EditingVisualMode(VimVisualState::Idle) => {
//todo
vec![
"[hjkl] Move".to_owned(),
"[1-9] Append steps".to_owned(),
"[Esc] Cancel".to_owned(),
]
}
EditingVisualMode(VimVisualState::Numbering(n)) => {
//todo
vec![
format!("[h|j|k|l] Move cursor {n} steps"),
"[1-9] Append steps".to_owned(),
"[Esc] Cancel".to_owned(),
]
}
EditingVisualMode(VimVisualState::Gateway) => {
vec![
"[g] Move cursor to top".to_owned(),
"[Esc] Cancel".to_owned(),
]
}
EditingInsertMode => {
vec![
"[Esc] Save note & Normal mode".to_owned(),
Expand Down
6 changes: 3 additions & 3 deletions core/src/state/notebook/consume/note.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
data::{Directory, Note},
db::Db,
state::notebook::{DirectoryItem, InnerState, NotebookState, SelectedItem, VimState},
state::notebook::{DirectoryItem, InnerState, NotebookState, SelectedItem, VimNormalState},
Error, NotebookTransition, Result,
};

Expand Down Expand Up @@ -93,15 +93,15 @@ pub async fn open(
let content = db.fetch_note_content(note.id.clone()).await?;

state.editing = Some(note.clone());
state.inner_state = InnerState::EditingNormalMode(VimState::Idle);
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

Ok(NotebookTransition::OpenNote { note, content })
}

pub async fn view(state: &mut NotebookState) -> Result<NotebookTransition> {
let note = state.get_editing()?.clone();

state.inner_state = InnerState::EditingNormalMode(VimState::Idle);
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

Ok(NotebookTransition::ViewMode(note))
}
Expand Down
10 changes: 8 additions & 2 deletions core/src/state/notebook/inner_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ mod directory_more_actions;
mod directory_selected;
mod editing_insert_mode;
mod editing_normal_mode;
mod editing_visual_mode;
mod note_more_actions;
mod note_selected;
mod note_tree_number;

use crate::{db::Db, state::notebook::NotebookState, Event, NotebookTransition, Result};
pub use editing_normal_mode::VimState;
pub use editing_normal_mode::VimNormalState;
pub use editing_visual_mode::VimVisualState;

#[derive(Clone)]
pub enum InnerState {
Expand All @@ -16,7 +18,8 @@ pub enum InnerState {
DirectorySelected,
DirectoryMoreActions,
NoteTreeNumber(usize),
EditingNormalMode(VimState),
EditingNormalMode(VimNormalState),
EditingVisualMode(VimVisualState),
EditingInsertMode,
}

Expand All @@ -36,6 +39,9 @@ pub async fn consume(
EditingNormalMode(vim_state) => {
editing_normal_mode::consume(db, state, *vim_state, event).await
}
EditingVisualMode(vim_state) => {
editing_visual_mode::consume(db, state, *vim_state, event).await
}
EditingInsertMode => editing_insert_mode::consume(db, state, event).await,
}
}
Loading