on master help sign in
refactor align_selection using kakoune logic
commit1c1aee74b4ecaf5a04d88ce01fdaa9d4601bc970 authorBob Qi <baobin.qi@gmail.com> dateThu 17 Feb 2022, 10:37 (5 years ago) parent5c810e5
1 file changed+36−38
helix-term/src/commands.rs+36−38
helix-term/src/commands.rs+36−38
@@ -772,18 +772,11 @@ fn trim_selections(cx: &mut Context) {
772772
773773// align text in selection
774774fn align_selections(cx: &mut Context) {
775− let align_style = cx.count();
776− if align_style > 3 {
777− cx.editor
778− .set_error("align only accept 1,2,3 as count to set left/center/right align");
779− return;
780− }
781−
782775 let (view, doc) = current!(cx.editor);
783776 let text = doc.text().slice(..);
784777 let selection = doc.selection(view.id);
785− let mut column_widths = vec![];
786− let mut last_line = text.len_lines();
778+ let mut column_widths: Vec<Vec<_>> = vec![];
779+ let mut last_line = text.len_lines() + 1;
787780 let mut column = 0;
788781 // first of all, we need compute all column's width, let use max width of the selections in a column
789782 for sel in selection {
@@ -797,47 +790,52 @@ fn align_selections(cx: &mut Context) {
797790 column = if l1 != last_line { 0 } else { column + 1 };
798791 last_line = l1;
799792
793+ // save all max line cursor of selections
794+ let cursor = sel.to() - text.line_to_char(l1);
800795 if column < column_widths.len() {
801− if sel.to() - sel.from() > column_widths[column] {
802− column_widths[column] = sel.to() - sel.from();
803− }
796+ column_widths[column].push((l1, cursor));
804797 } else {
805− // a new column, current selection width is the temp width of the column
806− column_widths.push(sel.to() - sel.from());
798+ column_widths.push(vec![(l1, cursor)]);
799+ }
800+ }
801+ let mut max_curs = vec![];
802+ for i in 0..column_widths.len() {
803+ // compute max cursor of current column
804+ let max_cur = column_widths[i]
805+ .iter()
806+ .fold(0, |max, &(_, cur)| max.max(cur));
807+ max_curs.push(max_cur);
808+
809+ let mut pads = std::collections::HashMap::new();
810+ for item in column_widths[i].iter_mut() {
811+ pads.insert(item.0, max_cur - item.1);
812+ }
813+ // add pad to right columns
814+ for col in column_widths[i + 1..].iter_mut() {
815+ for item in col.iter_mut() {
816+ item.1 += pads.get(&item.0).unwrap_or(&0);
817+ }
807818 }
808819 }
820+
809821 last_line = text.len_lines();
810− // once we get the with of each column, we transform each selection with to it's column width based on the align style
811822 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
812− let l = range.cursor_line(text);
813− column = if l != last_line { 0 } else { column + 1 };
814− last_line = l;
823+ let line = range.cursor_line(text);
824+ column = if line != last_line { 0 } else { column + 1 };
825+ last_line = line;
826+ let max_cur = max_curs[column];
827+ let new_cur = column_widths[column]
828+ .iter()
829+ .fold(max_cur, |c, (l, cur)| if *l == line { *cur } else { c });
830+ let mut new_str = " ".repeat(max_cur - new_cur);
831+ new_str.push_str(&range.fragment(text));
815832
816− (
817− range.from(),
818− range.to(),
819− Some(
820− align_fragment_to_width(&range.fragment(text), column_widths[column], align_style)
821− .into(),
822− ),
823− )
833+ (range.from(), range.to(), Some(new_str.into()))
824834 });
825835
826836 doc.apply(&transaction, view.id);
827837}
828838
829−fn align_fragment_to_width(fragment: &str, width: usize, align_style: usize) -> String {
830− let trimed = fragment.trim_matches(|c| c == ' ');
831− let mut s = " ".repeat(width - trimed.chars().count());
832− match align_style {
833− 1 => s.insert_str(0, trimed), // left align
834− 2 => s.insert_str(s.len() / 2, trimed), // center align
835− 3 => s.push_str(trimed), // right align
836− n => unimplemented!("{}", n),
837− }
838− s
839−}
840−
841839fn goto_window(cx: &mut Context, align: Align) {
842840 let count = cx.count() - 1;
843841 let (view, doc) = current!(cx.editor);