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
Show file tree
Hide file tree
Changes from 6 commits
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
24 changes: 16 additions & 8 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4391,10 +4391,9 @@ 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 slice = 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);
Expand All @@ -4411,7 +4410,19 @@ fn join_selections_impl(cx: &mut Context, select_space: bool) {
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_end_index = line_end_char_index(&slice, line + 1);
let line_contains_only_space = end == line_end_index;
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);
TornaxO7 marked this conversation as resolved.
Show resolved Hide resolved
}
}
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 All @@ -4438,9 +4446,9 @@ fn join_selections_impl(cx: &mut Context, select_space: bool) {
})
.collect();
let selection = Selection::new(ranges, 0);
Transaction::change(doc.text(), changes.into_iter()).with_selection(selection)
Transaction::change(text, changes.into_iter()).with_selection(selection)
} else {
Transaction::change(doc.text(), changes.into_iter())
Transaction::change(text, changes.into_iter())
};

doc.apply(&transaction, view.id);
Expand Down
46 changes: 46 additions & 0 deletions helix-term/tests/test/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,49 @@ fn bar() {#(\n|)#\

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_join_selections() -> anyhow::Result<()> {
// normal join
test((
platform_line(indoc! {"\
#[a|]#bc
def
"}),
"J",
platform_line(indoc! {"\
#[a|]#bc def
"}),
))
.await?;

// join with empty line
test((
platform_line(indoc! {"\
#[a|]#bc

def
"}),
"JJ",
platform_line(indoc! {"\
#[a|]#bc def
"}),
))
.await?;

// join with additional space in non-empty line
test((
platform_line(indoc! {"\
#[a|]#bc

def
"}),
"JJ",
platform_line(indoc! {"\
#[a|]#bc def
"}),
))
.await?;

Ok(())
}
Loading