diff --git a/kakplugin/src/lib.rs b/kakplugin/src/lib.rs index 431cdea..58d5058 100644 --- a/kakplugin/src/lib.rs +++ b/kakplugin/src/lib.rs @@ -24,6 +24,22 @@ pub fn get_selections(keys: Option<&'_ str>) -> Result, KakError> /// Will return `Err` if command fifo could not be opened, read from, or written to // TODO: Use AsRef pub fn get_selections_desc(keys: Option) -> Result, KakError> +where + S: AsRef, +{ + let mut ret = response("%val{selections_desc}", keys.as_ref())? + .iter() + .map(|sd| SelectionDesc::from_str(sd).map(|x| x.sort())) + .collect::, KakError>>()?; + ret.sort(); + Ok(ret) +} + +/// # Errors +/// +/// Will return `Err` if command fifo could not be opened, read from, or written to +// TODO: Use AsRef +pub fn get_selections_desc_unsorted(keys: Option) -> Result, KakError> where S: AsRef, { @@ -52,7 +68,7 @@ where /// or if `selections.len() != selections_desc.len` pub fn get_selections_with_desc(keys: Option<&'_ str>) -> Result, KakError> { let mut selections = get_selections(keys)?; - let selections_desc = get_selections_desc(keys)?; + let selections_desc = get_selections_desc_unsorted(keys)?; if selections.len() != selections_desc.len() { return Err(KakError::KakResponse(format!( diff --git a/src/box_.rs b/src/box_.rs index bf6a7fb..3923501 100644 --- a/src/box_.rs +++ b/src/box_.rs @@ -63,18 +63,10 @@ where /// 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, KakError> { // The selections we want to box, one per box - let selections_desc = { - let mut ret = get_selections_desc::<&str>(None)?; - ret.sort(); - ret - }; + let selections_desc = get_selections_desc::<&str>(None)?; // Whole-row selections split on newline - let selections_desc_rows = { - let mut ret = get_selections_desc(Some(""))?; - ret.sort(); - ret - }; + let selections_desc_rows = get_selections_desc(Some(""))?; Ok(selections_desc .iter() diff --git a/src/invert.rs b/src/invert.rs index 648bd3a..d275192 100644 --- a/src/invert.rs +++ b/src/invert.rs @@ -9,20 +9,20 @@ pub fn invert(_options: &Options) -> Result { // The selections to invert let mut split_selections_desc: Vec<(usize, Vec)> = { // Split by multiline so subtraction is defined (see below) - let mut ret: Vec = get_selections_desc(Some(""))?.into_iter().collect(); - ret.sort(); // Group by row, so for a given document row, subtraction can iterate over the Vec - ret.group_by(|a, b| a.left.row == b.left.row) + get_selections_desc(Some(""))? + .group_by(|a, b| a.left.row == b.left.row) .map(|sds| (sds[0].left.row, sds.to_vec())) .collect() }; + let count_selections = split_selections_desc.len(); + let document_descs: Vec = { // 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.) - let mut ret = get_selections_desc(Some("%"))?; - ret.sort(); - ret.into_iter() + get_selections_desc(Some("%"))? + .into_iter() // dd - The full row selectiondesc, spanning from col 1 to the rightmost col, for every row in the file .map(|dd: SelectionDesc| { // For every line, if there are selections to subtract, subtract them all @@ -47,10 +47,7 @@ pub fn invert(_options: &Options) -> Result { kakplugin::cmd("exec ''")?; - Ok(format!( - "Inverted {} selections", - split_selections_desc.len() - )) + Ok(format!("Inverted {} selections", count_selections)) } /// Subtract an iterator of `SelectionDesc`s from a given SelectionDesc