use super::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum AlignPad {
End,
Front,
}
struct Pad {
row: usize,
col: usize,
text: String,
front: bool,
}
struct Block {
row: usize,
start: usize,
end: usize,
padding_start: usize,
padding_cols: usize,
block_cols: usize,
column: usize,
}
#[derive(Default)]
struct Column {
pad_cols: usize,
block_cols: usize,
}
fn text_cols(text: &str, start_col: usize, width: usize) -> usize {
let width = width.max(1);
let mut cols = 0;
for c in text.chars() {
cols += if c == '\t' {
width - (start_col + cols) % width
} else {
unicode_width::UnicodeWidthChar::width(c).unwrap_or(0)
};
}
cols
}
impl Editor {
pub fn align_cursors(&mut self) -> bool {
self.align_columns(AlignPad::End, None)
}
pub fn align_cursors_pad_front(&mut self) -> bool {
self.align_columns(AlignPad::Front, None)
}
pub fn align_cursors_using_tabs(&mut self, tab_width: usize) -> bool {
self.align_columns(AlignPad::End, Some(tab_width))
}
pub fn align_cursors_using_auto(&mut self) -> bool {
let style = self.indent_style();
if style.use_spaces {
self.align_columns(AlignPad::End, None)
} else {
self.align_columns(AlignPad::End, Some(style.width as usize))
}
}
pub fn align_line_last_cursors(&mut self) -> bool {
let width = self.indent_style().width as usize;
let all = self.caret_selections();
let mut ends: Vec<(usize, usize)> = Vec::new();
for sel in &all {
let (start, end) = sel.normalised();
if start.0 != end.0 {
continue;
}
match ends.iter_mut().find(|(row, _)| *row == start.0) {
Some(slot) => slot.1 = slot.1.max(end.1),
None => ends.push((start.0, end.1)),
}
}
if ends.len() < 2 {
return false;
}
let cols: Vec<(usize, usize)> = ends
.iter()
.map(|&(row, end)| (row, text_cols(&self.text_between(row, 0, end), 0, width)))
.collect();
let target = cols.iter().map(|&(_, c)| c).max().unwrap_or(0);
if target == 0 {
return false;
}
let pads: Vec<Pad> = cols
.iter()
.filter(|&&(_, c)| c < target)
.map(|&(row, c)| Pad {
row,
col: 0,
text: " ".repeat(target - c),
front: false,
})
.collect();
let heads: Vec<((usize, usize), bool)> = all
.iter()
.map(|s| (s.head, s.head == s.normalised().0))
.collect();
let applied = !pads.is_empty();
self.apply_alignment(&pads, &heads);
applied
}
fn text_between(&self, row: usize, from: usize, to: usize) -> String {
self.lines
.get(row)
.map(|l| l.chars().skip(from).take(to.saturating_sub(from)).collect())
.unwrap_or_default()
}
fn align_columns(&mut self, how: AlignPad, tabs: Option<usize>) -> bool {
let width = tabs
.unwrap_or_else(|| self.indent_style().width as usize)
.max(1);
let mut blocks: Vec<Block> = Vec::new();
for sel in self.caret_selections() {
let (start, end) = sel.normalised();
if start.0 != end.0 {
continue;
}
blocks.push(Block {
row: start.0,
start: start.1,
end: end.1,
padding_start: 0,
padding_cols: 0,
block_cols: 0,
column: 0,
});
}
blocks.sort_by_key(|b| (b.row, b.start));
if blocks.len() < 2 {
return false;
}
let mut columns: Vec<Column> = Vec::new();
let (mut cur_row, mut cur_char, mut index) = (blocks[0].row, 0usize, 0usize);
for block in blocks.iter_mut() {
if block.row != cur_row {
cur_row = block.row;
cur_char = 0;
index = 0;
}
if index == columns.len() {
columns.push(Column::default());
}
block.column = index;
block.padding_start = cur_char;
cur_char = block.end;
index += 1;
}
let mut col_start = 0usize;
for (c, column) in columns.iter_mut().enumerate() {
for b in blocks.iter_mut().filter(|b| b.column == c) {
let text = self.text_between(b.row, b.padding_start, b.start);
b.padding_cols = text_cols(&text, col_start, width);
column.pad_cols = column.pad_cols.max(b.padding_cols);
}
if tabs.is_some() {
column.pad_cols = column.pad_cols.div_ceil(width) * width;
}
col_start += column.pad_cols;
for b in blocks.iter_mut().filter(|b| b.column == c) {
let text = self.text_between(b.row, b.start, b.end);
b.block_cols = text_cols(&text, col_start, width);
column.block_cols = column.block_cols.max(b.block_cols);
}
if tabs.is_some() {
column.block_cols = column.block_cols.div_ceil(width) * width;
}
col_start += column.block_cols;
}
let unit = if tabs.is_some() { "\t" } else { " " };
let mut pads: Vec<Pad> = Vec::new();
for b in &blocks {
let column = &columns[b.column];
let mut add_pad = column.pad_cols - b.padding_cols;
let mut add_block = column.block_cols - b.block_cols;
if tabs.is_some() {
add_pad = add_pad.div_ceil(width);
add_block = add_block.div_ceil(width);
}
if add_pad > 0 {
pads.push(Pad {
row: b.row,
col: b.start,
text: unit.repeat(add_pad),
front: true,
});
}
if add_block > 0 {
pads.push(Pad {
row: b.row,
col: if how == AlignPad::Front {
b.start
} else {
b.end
},
text: unit.repeat(add_block),
front: false,
});
}
}
if pads.is_empty() {
return false;
}
let heads: Vec<((usize, usize), bool)> = self
.caret_selections()
.iter()
.map(|s| (s.head, s.head == s.normalised().0))
.collect();
self.apply_alignment(&pads, &heads);
true
}
}
impl Editor {
fn apply_alignment(&mut self, pads: &[Pad], heads: &[((usize, usize), bool)]) {
if pads.is_empty() || self.lines.is_empty() {
return;
}
self.multi_typing = None;
self.pin_on_edit();
self.break_undo_coalescing();
self.push_undo(EditKind::MultiEdit);
let pins: Vec<(bool, (usize, usize))> = heads
.iter()
.enumerate()
.map(|(i, &((row, col), at_start))| {
let right: usize = pads
.iter()
.filter(|p| {
p.row == row && (p.col > col || (p.col == col && p.front && !at_start))
})
.map(|p| p.text.chars().count())
.sum();
let (from_bottom, from_end) = self.pin_from_end((row, col));
(i == 0, (from_bottom, from_end + right))
})
.collect();
let mut order: Vec<usize> = (0..pads.len()).collect();
order.sort_by_key(|&i| std::cmp::Reverse((pads[i].row, pads[i].col, !pads[i].front)));
self.selection = None;
let last = self.lines.len().saturating_sub(1);
for i in order {
let pad = &pads[i];
self.cursor_row = pad.row.min(last);
self.cursor_col = pad.col.min(self.line_char_len(self.cursor_row));
for c in pad.text.chars() {
self.insert_char_raw(c);
}
}
self.selection = None;
self.place_pinned_carets(&pins);
self.recompute_highlights();
}
}