use super::*;
impl App {
pub fn ensure_comment_vim_editor(&mut self) {
if !self.comment_vim_enabled || self.input_mode != InputMode::Comment {
return;
}
if self.comment_vim_editor.is_none() {
self.comment_vim_editor = Some(CommentVimEditor::from_buffer(
&self.comment_buffer,
self.comment_cursor,
));
}
}
pub fn comment_vim_mode_label(&self) -> Option<(String, bool)> {
if !self.comment_vim_enabled || self.input_mode != InputMode::Comment {
return None;
}
if let Some(cmd) = &self.comment_vim_command {
return Some((format!(":{cmd}"), false));
}
match self.comment_vim_pending {
CommentVimPending::Save => return Some(("Enter again to save".to_string(), false)),
CommentVimPending::Cancel => {
return Some(("Esc/q again to cancel".to_string(), true));
}
CommentVimPending::None => {}
}
Some((
self.comment_vim_editor
.as_ref()
.map_or("INSERT", CommentVimEditor::label)
.to_string(),
false,
))
}
pub fn comment_vim_command_active(&self) -> bool {
self.comment_vim_command.is_some()
}
pub fn comment_vim_enter_normal(&mut self) {
if self.comment_vim_pending == CommentVimPending::Save {
self.comment_vim_pending = CommentVimPending::None;
self.save_comment();
} else {
self.comment_vim_pending = CommentVimPending::Save;
}
}
pub fn comment_vim_esc_normal(&mut self) {
if self.comment_vim_pending == CommentVimPending::Cancel {
self.comment_vim_pending = CommentVimPending::None;
self.exit_comment_mode();
} else {
self.comment_vim_pending = CommentVimPending::Cancel;
}
}
pub fn comment_vim_reset_pending(&mut self) {
self.comment_vim_pending = CommentVimPending::None;
}
pub fn start_comment_vim_command(&mut self) {
self.comment_vim_command = Some(String::new());
}
pub fn comment_vim_command_push(&mut self, c: char) {
if let Some(cmd) = self.comment_vim_command.as_mut() {
cmd.push(c);
}
}
pub fn comment_vim_command_backspace(&mut self) {
if let Some(cmd) = self.comment_vim_command.as_mut() {
if cmd.is_empty() {
self.comment_vim_command = None;
} else {
cmd.pop();
}
}
}
pub fn comment_vim_command_cancel(&mut self) {
self.comment_vim_command = None;
}
pub fn run_comment_vim_command(&mut self) {
let cmd = self.comment_vim_command.take().unwrap_or_default();
match cmd.trim() {
"w" | "wq" | "x" => self.save_comment(),
"q" | "q!" => self.exit_comment_mode(),
"" => {}
other => self.set_warning(format!("Not a comment command: :{other}")),
}
}
pub fn comment_vim_in_normal_mode(&self) -> bool {
self.comment_vim_editor
.as_ref()
.is_some_and(CommentVimEditor::is_normal_mode)
}
pub fn comment_vim_feed_key(&mut self, key: crossterm::event::KeyEvent) {
if let Some(editor) = self.comment_vim_editor.as_mut() {
let (text, cursor) = editor.feed_key(key);
self.comment_buffer = text;
self.comment_cursor = cursor;
}
}
pub fn comment_vim_insert_soft_tab(&mut self) {
let space = crossterm::event::KeyEvent::new(
crossterm::event::KeyCode::Char(' '),
crossterm::event::KeyModifiers::NONE,
);
for _ in 0..self.comment_tab_width {
self.comment_vim_feed_key(space);
}
}
pub fn comment_vim_feed_paste(&mut self, text: String) {
if let Some(editor) = self.comment_vim_editor.as_mut() {
let (text, cursor) = editor.feed_paste(text);
self.comment_buffer = text;
self.comment_cursor = cursor;
}
}
pub fn set_comment_vim(&mut self, enabled: bool) {
self.comment_vim_enabled = enabled;
if !enabled {
self.comment_vim_editor = None;
}
self.set_message(if enabled {
"Vim mode enabled for the comment box"
} else {
"Vim mode disabled for the comment box"
});
}
pub fn toggle_comment_vim(&mut self) {
self.set_comment_vim(!self.comment_vim_enabled);
}
}