Add get_selections_desc_unsorted

This commit is contained in:
Austen Adler 2022-08-14 20:44:44 -04:00
parent e3bdb745bf
commit 82dacf2482
3 changed files with 26 additions and 21 deletions

View File

@ -24,6 +24,22 @@ pub fn get_selections(keys: Option<&'_ str>) -> Result<Vec<Selection>, KakError>
/// Will return `Err` if command fifo could not be opened, read from, or written to /// Will return `Err` if command fifo could not be opened, read from, or written to
// TODO: Use AsRef // TODO: Use AsRef
pub fn get_selections_desc<S>(keys: Option<S>) -> Result<Vec<SelectionDesc>, KakError> pub fn get_selections_desc<S>(keys: Option<S>) -> Result<Vec<SelectionDesc>, KakError>
where
S: AsRef<str>,
{
let mut ret = response("%val{selections_desc}", keys.as_ref())?
.iter()
.map(|sd| SelectionDesc::from_str(sd).map(|x| x.sort()))
.collect::<Result<Vec<_>, 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<S>(keys: Option<S>) -> Result<Vec<SelectionDesc>, KakError>
where where
S: AsRef<str>, S: AsRef<str>,
{ {
@ -52,7 +68,7 @@ where
/// or if `selections.len() != selections_desc.len` /// or if `selections.len() != selections_desc.len`
pub fn get_selections_with_desc(keys: Option<&'_ str>) -> Result<Vec<SelectionWithDesc>, KakError> { pub fn get_selections_with_desc(keys: Option<&'_ str>) -> Result<Vec<SelectionWithDesc>, KakError> {
let mut selections = get_selections(keys)?; 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() { if selections.len() != selections_desc.len() {
return Err(KakError::KakResponse(format!( return Err(KakError::KakResponse(format!(

View File

@ -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` /// 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 = { let selections_desc = get_selections_desc::<&str>(None)?;
let mut ret = get_selections_desc::<&str>(None)?;
ret.sort();
ret
};
// Whole-row selections split on newline // Whole-row selections split on newline
let selections_desc_rows = { let selections_desc_rows = get_selections_desc(Some("<a-x><a-s>"))?;
let mut ret = get_selections_desc(Some("<a-x><a-s>"))?;
ret.sort();
ret
};
Ok(selections_desc Ok(selections_desc
.iter() .iter()

View File

@ -9,20 +9,20 @@ 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)
let mut ret: Vec<SelectionDesc> = get_selections_desc(Some("<a-s>"))?.into_iter().collect();
ret.sort();
// Group by row, so for a given document row, subtraction can iterate over the Vec // 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("<a-s>"))?
.group_by(|a, b| a.left.row == b.left.row)
.map(|sds| (sds[0].left.row, sds.to_vec())) .map(|sds| (sds[0].left.row, sds.to_vec()))
.collect() .collect()
}; };
let count_selections = split_selections_desc.len();
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.<?>)
let mut ret = get_selections_desc(Some("%<a-s>"))?; get_selections_desc(Some("%<a-s>"))?
ret.sort(); .into_iter()
ret.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| {
// For every line, if there are selections to subtract, subtract them all // For every line, if there are selections to subtract, subtract them all
@ -47,10 +47,7 @@ pub fn invert(_options: &Options) -> Result<String, KakError> {
kakplugin::cmd("exec '<a-_>'")?; kakplugin::cmd("exec '<a-_>'")?;
Ok(format!( Ok(format!("Inverted {} selections", count_selections))
"Inverted {} selections",
split_selections_desc.len()
))
} }
/// Subtract an iterator of `SelectionDesc`s from a given SelectionDesc /// Subtract an iterator of `SelectionDesc`s from a given SelectionDesc