Skip to content

Commit

Permalink
apply trim to selections only
Browse files Browse the repository at this point in the history
  • Loading branch information
kirawi committed Nov 23, 2022
1 parent 90b9681 commit ec65e57
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1794,49 +1794,52 @@ fn trim(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) ->
let mode = args.get(0).unwrap_or(&Cow::Borrowed("all"));
let (view, doc) = current!(cx.editor);

let selections = doc.selection(view.id)

let selection = doc.selection(view.id);
if mode == "all" || mode == "spaces" {

let mut pos = 0;
let transaction = Transaction::change(
doc.text(),
doc.text().lines().filter_map(|line| {
// The index of the last non-whitespace character in the line.
let mut changes = Vec::new();
for range in selection {
let doc = range.slice(doc.text().slice(..));
let mut pos = range.from();
changes.extend(doc.lines().filter_map(|line| {
let start = pos
+ movement::backwards_skip_while(
line,
line_ending::rope_end_without_line_ending(&line),
|x| x.is_whitespace(),
)
.unwrap_or(0);

// The index of the last character in the line, excluding the line ending.
let end = pos + line_ending::rope_end_without_line_ending(&line);
pos += line.len_chars();
(start != end).then(|| (start, end, None))
}),
);
}));
}
let transaction = Transaction::change(doc.text(), changes.into_iter());
doc.apply(&transaction, view.id);
}

if mode == "all" || mode == "lines" {
let mut lines = doc.text().lines_at(doc.text().len_lines()).reversed();
let mut pos = doc.text().len_chars();

// The last non-empty line char index.
let start = lines
.find_map(|line| {
if line_ending::rope_end_without_line_ending(&line) != 0 {
Some(pos)
} else {
pos -= line.len_chars();
None
}
})
.unwrap_or(0);
let end = doc.text().len_chars();
let transaction = Transaction::change(doc.text(), [(start, end, None)].into_iter());
let mut changes = Vec::new();
let selection = doc.selection(view.id);
for range in selection {
let doc = range.slice(doc.text().slice(..));
let mut lines = doc.lines_at(doc.len_lines()).reversed();
let mut pos = range.to();

// The last non-empty line char index.
let start = lines
.find_map(|line| {
if line_ending::rope_end_without_line_ending(&line) != 0 {
Some(pos)
} else {
pos -= line.len_chars();
None
}
})
.unwrap_or(0);
let end = range.to();
changes.push((start, end, None));
}
let transaction = Transaction::change(doc.text(), changes.into_iter());
doc.apply(&transaction, view.id);
}

Expand Down

0 comments on commit ec65e57

Please sign in to comment.