Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join empty lines with only one space in join_selections #8989

Merged
merged 7 commits into from
Dec 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4390,28 +4390,39 @@ fn format_selections(cx: &mut Context) {
fn join_selections_impl(cx: &mut Context, select_space: bool) {
use movement::skip_while;
let (view, doc) = current!(cx.editor);
let text = doc.text();
let slice = doc.text().slice(..);

let mut changes = Vec::new();
let fragment = Tendril::from(" ");

for selection in doc.selection(view.id) {
let (start, mut end) = selection.line_range(slice);
if start == end {
end = (end + 1).min(text.len_lines() - 1);
}
let lines = start..end;
let slice = doc.text().slice(..);
let text = doc.text();

changes.reserve(lines.len());
let selected_lines = {
let (start, mut end) = selection.line_range(slice);
if start == end {
end = (end + 1).min(text.len_lines() - 1);
}
start..end
};

the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
for line in lines {
let start = line_end_char_index(&slice, line);
let mut end = text.line_to_char(line + 1);
for selected_line in selected_lines {
let start = line_end_char_index(&slice, selected_line);
let mut end = text.line_to_char(selected_line + 1);
end = skip_while(slice, end, |ch| matches!(ch, ' ' | '\t')).unwrap_or(end);

// need to skip from start, not end
let change = (start, end, Some(fragment.clone()));
let change = {
let separator = {
let line_contains_only_space = text.char(end) == '\n';
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
if line_contains_only_space {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use .then here

None
} else {
Some(Tendril::from(" "))
}
};

(start, end, separator)
};
changes.push(change);
}
}
Expand All @@ -4424,9 +4435,6 @@ fn join_selections_impl(cx: &mut Context, select_space: bool) {
changes.sort_unstable_by_key(|(from, _to, _text)| *from);
changes.dedup();

// TODO: joining multiple empty lines should be replaced by a single space.
// need to merge change ranges that touch

// select inserted spaces
let transaction = if select_space {
let ranges: SmallVec<_> = changes
Expand Down