Skip to content

Commit

Permalink
Implemented change in and delete in functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ayax79 committed Oct 11, 2024
1 parent 5e556bf commit 96e7f17
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/edit_mode/vi/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ where
match input.peek() {
Some('d') => {
let _ = input.next();
Some(Command::Delete)
if let Some('i') = input.peek() {
let _ = input.next();
match input.next() {
Some(c) => Some(Command::DeleteInside(*c)),
None => Some(Command::Incomplete),
}
} else {
Some(Command::Delete)
}
}
Some('p') => {
let _ = input.next();
Expand All @@ -33,7 +41,15 @@ where
}
Some('c') => {
let _ = input.next();
Some(Command::Change)
if let Some('i') = input.peek() {
let _ = input.next();
match input.next() {
Some(c) => Some(Command::ChangeInside(*c)),
None => Some(Command::Incomplete),
}
} else {
Some(Command::Change)
}
}
Some('x') => {
let _ = input.next();
Expand Down Expand Up @@ -107,6 +123,8 @@ pub enum Command {
HistorySearch,
Switchcase,
RepeatLastAction,
ChangeInside(char),
DeleteInside(char),
}

impl Command {
Expand Down Expand Up @@ -150,10 +168,24 @@ impl Command {
// Whenever a motion is required to finish the command we must be in visual mode
Self::Delete | Self::Change => vec![ReedlineOption::Edit(EditCommand::CutSelection)],
Self::Incomplete => vec![ReedlineOption::Incomplete],
Command::RepeatLastAction => match &vi_state.previous {
Self::RepeatLastAction => match &vi_state.previous {
Some(event) => vec![ReedlineOption::Event(event.clone())],
None => vec![],
},
Self::ChangeInside(char) => {
let right = right_bracket_for(char);
vec![
ReedlineOption::Edit(EditCommand::CutLeftBefore(*char)),
ReedlineOption::Edit(EditCommand::CutRightBefore(right)),
]
}
Self::DeleteInside(char) => {
let right = right_bracket_for(char);
vec![
ReedlineOption::Edit(EditCommand::CutLeftBefore(*char)),
ReedlineOption::Edit(EditCommand::CutRightBefore(right)),
]
}
}
}

Expand Down Expand Up @@ -276,3 +308,12 @@ impl Command {
}
}
}

fn right_bracket_for(c: &char) -> char {
match *c {
'(' => ')',
'[' => ']',
'{' => '}',
_ => *c,
}
}
1 change: 1 addition & 0 deletions src/edit_mode/vi/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl ParsedViSequence {
(Some(Command::EnterViInsert), ParseResult::Incomplete)
| (Some(Command::EnterViAppend), ParseResult::Incomplete)
| (Some(Command::ChangeToLineEnd), ParseResult::Incomplete)
| (Some(Command::ChangeInside(_)), ParseResult::Incomplete)
| (Some(Command::AppendToEnd), ParseResult::Incomplete)
| (Some(Command::PrependToStart), ParseResult::Incomplete)
| (Some(Command::RewriteCurrentLine), ParseResult::Incomplete)
Expand Down

0 comments on commit 96e7f17

Please sign in to comment.