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

Implemented VI mode change inside and delete inside functionality #844

Merged
merged 8 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
55 changes: 52 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) if is_valid_change_in(c) => Some(Command::DeleteInside(*c)),
_ => None,
}
} 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) if is_valid_change_in(c) => Some(Command::ChangeInside(*c)),
_ => None,
}
} 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,20 @@ impl Command {
}
}
}

fn right_bracket_for(c: &char) -> char {
match *c {
'(' => ')',
'[' => ']',
'{' => '}',
'<' => '>',
_ => *c,
}
}

fn is_valid_change_in(c: &char) -> bool {
matches!(
c,
'(' | '[' | '{' | ')' | ']' | '}' | '"' | '\'' | '`' | '<'
ayax79 marked this conversation as resolved.
Show resolved Hide resolved
)
}
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)
ayax79 marked this conversation as resolved.
Show resolved Hide resolved
| (Some(Command::AppendToEnd), ParseResult::Incomplete)
| (Some(Command::PrependToStart), ParseResult::Incomplete)
| (Some(Command::RewriteCurrentLine), ParseResult::Incomplete)
Expand Down
Loading