From 75473ee2ea442a5be0a64f40ec23709507db09b6 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Fri, 20 Sep 2024 23:23:12 -0700 Subject: [PATCH] Join single-line comments with J. Fixes #8565. --- helix-term/src/commands.rs | 17 +++++++++++++++++ helix-term/tests/test/commands.rs | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 6e037a471ffc8..4ac4d58f1177a 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -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) { @@ -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 diff --git a/helix-term/tests/test/commands.rs b/helix-term/tests/test/commands.rs index 9f196827faf3e..eb308a1cf55e2 100644 --- a/helix-term/tests/test/commands.rs +++ b/helix-term/tests/test/commands.rs @@ -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 rustJ", + indoc! {"\ + /// #[a|]#bc def + "}, + )) + .await?; + Ok(()) +} + #[tokio::test(flavor = "multi_thread")] async fn test_read_file() -> anyhow::Result<()> { let mut file = tempfile::NamedTempFile::new()?;