Add no newline option for box and invert

This commit is contained in:
Austen Adler 2022-08-15 20:40:04 -04:00
parent 82dacf2482
commit ffb978a60d
3 changed files with 30 additions and 7 deletions

View File

@ -4,9 +4,12 @@ use kakplugin::{
use std::cmp::{max, min}; use std::cmp::{max, min};
#[derive(clap::StructOpt, Debug)] #[derive(clap::StructOpt, Debug)]
pub struct Options { pub struct Options {
// /// Bounding box mode, which selects the largest box to contain everything /// Bounding box mode, which selects the largest box to contain everything
#[clap(short, long, help = "Select the bonding box of all selections")] #[clap(short, long, help = "Select the bonding box of all selections")]
bounding_box: bool, bounding_box: bool,
/// Excludes newlines from resulting selection
#[clap(short, long, help = "Do not include newlines")]
no_newline: bool,
} }
pub fn box_(options: &Options) -> Result<String, KakError> { pub fn box_(options: &Options) -> Result<String, KakError> {
@ -61,12 +64,20 @@ where
/// Implementation that converts each selection to a box with the top left corner at min(anchor.col, cursor.col) and bottom right at max(anchor.col, cursor.col) /// Implementation that converts each selection to a box with the top left corner at min(anchor.col, cursor.col) and bottom right at max(anchor.col, cursor.col)
/// ///
/// Do this by getting each selection, then getting each whole-row (col 0 to col max) and passing the range of whole-rows into helper `to_boxed_selections` /// Do this by getting each selection, then getting each whole-row (col 0 to col max) and passing the range of whole-rows into helper `to_boxed_selections`
fn boxed_selections(_options: &Options) -> Result<Vec<SelectionDesc>, KakError> { fn boxed_selections(options: &Options) -> Result<Vec<SelectionDesc>, KakError> {
// The selections we want to box, one per box // The selections we want to box, one per box
let selections_desc = get_selections_desc::<&str>(None)?; let selections_desc = get_selections_desc::<&str>(None)?;
let whole_line_selection_command = if options.no_newline {
// Select everything and only keep non-newlines
"<a-x>s^[^\\n]+<ret>"
} else {
// Select everything and split
"<a-x><a-s>"
};
// Whole-row selections split on newline // Whole-row selections split on newline
let selections_desc_rows = get_selections_desc(Some("<a-x><a-s>"))?; let selections_desc_rows = get_selections_desc(Some(whole_line_selection_command))?;
Ok(selections_desc Ok(selections_desc
.iter() .iter()

View File

@ -4,8 +4,12 @@ use kakplugin::{
}; };
use std::{fs, str::FromStr}; use std::{fs, str::FromStr};
#[derive(clap::StructOpt, Debug)] #[derive(clap::StructOpt, Debug)]
pub struct Options; pub struct Options {
pub fn invert(_options: &Options) -> Result<String, KakError> { #[clap(short, long, help = "Do not include newlines")]
no_newline: bool,
}
pub fn invert(options: &Options) -> Result<String, KakError> {
// The selections to invert // The selections to invert
let mut split_selections_desc: Vec<(usize, Vec<SelectionDesc>)> = { let mut split_selections_desc: Vec<(usize, Vec<SelectionDesc>)> = {
// Split by multiline so subtraction is defined (see below) // Split by multiline so subtraction is defined (see below)
@ -18,10 +22,18 @@ pub fn invert(_options: &Options) -> Result<String, KakError> {
let count_selections = split_selections_desc.len(); let count_selections = split_selections_desc.len();
let whole_document_selection_command = if options.no_newline {
// Select everything and only keep non-newlines
"%s^[^\\n]+<ret>"
} else {
// Select everything and split
"%<a-s>"
};
let document_descs: Vec<SelectionDesc> = { let document_descs: Vec<SelectionDesc> = {
// Every line in the document as a selectiondesc // Every line in the document as a selectiondesc
// Split by line because subtracting cross-multiline is not always defined for multiline selection descs (ex: 1.1,3.3 - 2.1,3.3 = 1.1,1.<?>) // Split by line because subtracting cross-multiline is not always defined for multiline selection descs (ex: 1.1,3.3 - 2.1,3.3 = 1.1,1.<?>)
get_selections_desc(Some("%<a-s>"))? get_selections_desc(Some(whole_document_selection_command))?
.into_iter() .into_iter()
// dd - The full row selectiondesc, spanning from col 1 to the rightmost col, for every row in the file // dd - The full row selectiondesc, spanning from col 1 to the rightmost col, for every row in the file
.map(|dd: SelectionDesc| { .map(|dd: SelectionDesc| {

View File

@ -48,7 +48,7 @@ enum Commands {
Shuf(shuf::Options), Shuf(shuf::Options),
#[clap(about = "Find unique selections based on optional regex match")] #[clap(about = "Find unique selections based on optional regex match")]
Uniq(uniq::Options), Uniq(uniq::Options),
#[clap(about = "Invert all selections", visible_aliases = &["inverse"])] #[clap(about = "Invert all selections", visible_aliases = &["inv", "inverse"])]
Invert(invert::Options), Invert(invert::Options),
#[clap(about = "Evaluate selections as a math expression", visible_aliases = &["bc", "eval"])] #[clap(about = "Evaluate selections as a math expression", visible_aliases = &["bc", "eval"])]
MathEval(math_eval::Options), MathEval(math_eval::Options),