Unnamed repository; edit this file 'description' to name the repository.
fix: return canonical short prefix from `Comment::prefix`
`Comment::prefix` iterated `CommentKind::BY_PREFIX` forward, so `/**/` matched the `/**/` entry itself and returned the full 4-char string. `block_to_line` then computed `&text[4..text.len()-2]` = `&text[4..2]` and panicked. Delegate `Comment::prefix` to `CommentKind::prefix`, which already iterates in reverse and returns the canonical short form (`/*` for non-doc block comments). The `/**/` and `/***` entries in `BY_PREFIX` are still needed for `from_text` to classify them as non-doc blocks. fixes rust-lang/rust-analyzer#22071
albab-hasan 5 weeks ago
parent 26d7ba3 · commit beacd8a
-rw-r--r--crates/ide-assists/src/handlers/convert_comment_block.rs15
-rw-r--r--crates/syntax/src/ast/token_ext.rs6
2 files changed, 16 insertions, 5 deletions
diff --git a/crates/ide-assists/src/handlers/convert_comment_block.rs b/crates/ide-assists/src/handlers/convert_comment_block.rs
index 0d36a5ddb3..f242fe8314 100644
--- a/crates/ide-assists/src/handlers/convert_comment_block.rs
+++ b/crates/ide-assists/src/handlers/convert_comment_block.rs
@@ -382,6 +382,21 @@ fn main() {
}
#[test]
+ fn empty_block_to_line() {
+ check_assist(
+ convert_comment_block,
+ r#"
+/**/$0
+fn main() {}
+"#,
+ r#"
+
+fn main() {}
+"#,
+ );
+ }
+
+ #[test]
fn end_of_line_block_to_line() {
check_assist_not_applicable(
convert_comment_block,
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs
index 83ab87c1c6..29b3b0930a 100644
--- a/crates/syntax/src/ast/token_ext.rs
+++ b/crates/syntax/src/ast/token_ext.rs
@@ -32,11 +32,7 @@ impl ast::Comment {
}
pub fn prefix(&self) -> &'static str {
- let &(prefix, _kind) = CommentKind::BY_PREFIX
- .iter()
- .find(|&(prefix, kind)| self.kind() == *kind && self.text().starts_with(prefix))
- .unwrap();
- prefix
+ self.kind().prefix()
}
/// Returns the textual content of a doc comment node as a single string with prefix and suffix