use {
reovim_driver_command::{
ArgKind, ArgSpec, Command, CommandContext, CommandHandler, CommandResult,
},
reovim_driver_session::{
OperatorPendingState, SessionRuntime, TextObjRange,
api::{ExtensionApi, ModeApi, Selection, SelectionMode},
},
reovim_kernel::api::v1::{CommandId, Position, TextObject, TextObjectEngine},
};
use crate::ids;
fn is_visual_mode(runtime: &SessionRuntime<'_>) -> bool {
let mode = runtime.current_mode();
let mode_name = mode.name();
mode_name == "visual" || mode_name == "visual-line" || mode_name == "visual-block"
}
fn visual_selection_mode(runtime: &SessionRuntime<'_>) -> SelectionMode {
let mode = runtime.current_mode();
match mode.name() {
"visual-line" => SelectionMode::Line,
"visual-block" => SelectionMode::Block,
_ => SelectionMode::Character,
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
fn execute_quote_textobj(
runtime: &mut SessionRuntime<'_>,
args: &CommandContext,
text_object: TextObject,
) -> CommandResult {
let Some(buffer_id) = args.buffer_id() else {
return CommandResult::error("No active buffer");
};
let count = args.count().unwrap_or(1);
let Some(window) = runtime.windows().active() else {
return CommandResult::error("No active window");
};
let pos = Position::new(window.cursor.line, window.cursor.column);
let range_result = runtime.with_buffer_read(buffer_id, |buffer| {
TextObjectEngine::range(buffer, pos, text_object, count)
});
let Some(range_result) = range_result else {
return CommandResult::error("Buffer not found");
};
let Some((start, end)) = range_result else {
return CommandResult::Success; };
let end_exclusive = Position::new(end.line, end.column + 1);
if is_visual_mode(runtime) {
let sel_mode = visual_selection_mode(runtime);
if let Some(window) = runtime.windows_mut().active_mut() {
window.selection = Some(Selection::new(start, end_exclusive, sel_mode));
window.cursor = end.into();
}
} else {
let state = runtime.ext_mut::<OperatorPendingState>();
state.set_textobj_range(TextObjRange::characterwise(start, end_exclusive));
}
CommandResult::Success
}
#[derive(Debug, Clone, Copy, Default)]
pub struct InnerDoubleQuote;
impl Command for InnerDoubleQuote {
fn id(&self) -> CommandId {
ids::INNER_DOUBLE_QUOTE
}
fn description(&self) -> &'static str {
"Inner double quote text object"
}
fn args(&self) -> Vec<ArgSpec> {
vec![ArgSpec::optional(
"count",
ArgKind::Count,
"Quote nesting level",
)]
}
}
impl CommandHandler for InnerDoubleQuote {
fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
execute_quote_textobj(runtime, args, TextObject::InnerQuote('"'))
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct AroundDoubleQuote;
impl Command for AroundDoubleQuote {
fn id(&self) -> CommandId {
ids::AROUND_DOUBLE_QUOTE
}
fn description(&self) -> &'static str {
"Around double quote text object"
}
fn args(&self) -> Vec<ArgSpec> {
vec![ArgSpec::optional(
"count",
ArgKind::Count,
"Quote nesting level",
)]
}
}
impl CommandHandler for AroundDoubleQuote {
fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
execute_quote_textobj(runtime, args, TextObject::AQuote('"'))
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct InnerSingleQuote;
impl Command for InnerSingleQuote {
fn id(&self) -> CommandId {
ids::INNER_SINGLE_QUOTE
}
fn description(&self) -> &'static str {
"Inner single quote text object"
}
fn args(&self) -> Vec<ArgSpec> {
vec![ArgSpec::optional(
"count",
ArgKind::Count,
"Quote nesting level",
)]
}
}
impl CommandHandler for InnerSingleQuote {
fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
execute_quote_textobj(runtime, args, TextObject::InnerQuote('\''))
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct AroundSingleQuote;
impl Command for AroundSingleQuote {
fn id(&self) -> CommandId {
ids::AROUND_SINGLE_QUOTE
}
fn description(&self) -> &'static str {
"Around single quote text object"
}
fn args(&self) -> Vec<ArgSpec> {
vec![ArgSpec::optional(
"count",
ArgKind::Count,
"Quote nesting level",
)]
}
}
impl CommandHandler for AroundSingleQuote {
fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
execute_quote_textobj(runtime, args, TextObject::AQuote('\''))
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct InnerBacktick;
impl Command for InnerBacktick {
fn id(&self) -> CommandId {
ids::INNER_BACKTICK
}
fn description(&self) -> &'static str {
"Inner backtick text object"
}
fn args(&self) -> Vec<ArgSpec> {
vec![ArgSpec::optional(
"count",
ArgKind::Count,
"Quote nesting level",
)]
}
}
impl CommandHandler for InnerBacktick {
fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
execute_quote_textobj(runtime, args, TextObject::InnerQuote('`'))
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct AroundBacktick;
impl Command for AroundBacktick {
fn id(&self) -> CommandId {
ids::AROUND_BACKTICK
}
fn description(&self) -> &'static str {
"Around backtick text object"
}
fn args(&self) -> Vec<ArgSpec> {
vec![ArgSpec::optional(
"count",
ArgKind::Count,
"Quote nesting level",
)]
}
}
impl CommandHandler for AroundBacktick {
fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
execute_quote_textobj(runtime, args, TextObject::AQuote('`'))
}
}
#[must_use]
pub fn all_commands() -> Vec<Box<dyn CommandHandler>> {
vec![
Box::new(InnerDoubleQuote),
Box::new(AroundDoubleQuote),
Box::new(InnerSingleQuote),
Box::new(AroundSingleQuote),
Box::new(InnerBacktick),
Box::new(AroundBacktick),
]
}