Skip to content

Commit

Permalink
Join single-line comments with J.
Browse files Browse the repository at this point in the history
Fixes #8565.
  • Loading branch information
Rose Hogenson committed Sep 21, 2024
1 parent 5717aa8 commit 75473ee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4626,6 +4626,15 @@ fn join_selections_impl(cx: &mut Context, select_space: bool) {
let text = doc.text();
let slice = text.slice(..);

let comment_prefixes = doc
.language_config()
.and_then(|config| config.comment_tokens.as_ref())
.map(|pfxs| &pfxs[..])
.unwrap_or(&[]);
// Sort by length to handle Rust's /// vs //
let mut comment_prefixes = Vec::from(comment_prefixes);
comment_prefixes.sort_unstable_by_key(|x| std::cmp::Reverse(x.len()));

let mut changes = Vec::new();

for selection in doc.selection(view.id) {
Expand All @@ -4641,6 +4650,14 @@ fn join_selections_impl(cx: &mut Context, select_space: bool) {
let start = line_end_char_index(&slice, line);
let mut end = text.line_to_char(line + 1);
end = skip_while(slice, end, |ch| matches!(ch, ' ' | '\t')).unwrap_or(end);
let slice_from_end = slice.slice(end..);
for pfx in comment_prefixes.iter() {
if slice_from_end.starts_with(pfx) {
end += pfx.len();
break;
}
}
end = skip_while(slice, end, |ch| matches!(ch, ' ' | '\t')).unwrap_or(end);

let separator = if end == line_end_char_index(&slice, line + 1) {
// the joining line contains only space-characters => don't include a whitespace when joining
Expand Down
16 changes: 16 additions & 0 deletions helix-term/tests/test/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,22 @@ async fn test_join_selections_space() -> anyhow::Result<()> {
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_join_selections_comment() -> anyhow::Result<()> {
test((
indoc! {"\
/// #[a|]#bc
/// def
"},
":lang rust<ret>J",
indoc! {"\
/// #[a|]#bc def
"},
))
.await?;
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_read_file() -> anyhow::Result<()> {
let mut file = tempfile::NamedTempFile::new()?;
Expand Down

0 comments on commit 75473ee

Please sign in to comment.