Skip to content

Commit beacd8a

Browse files
committed
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 #22071
1 parent 26d7ba3 commit beacd8a

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

crates/ide-assists/src/handlers/convert_comment_block.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,21 @@ fn main() {
381381
);
382382
}
383383

384+
#[test]
385+
fn empty_block_to_line() {
386+
check_assist(
387+
convert_comment_block,
388+
r#"
389+
/**/$0
390+
fn main() {}
391+
"#,
392+
r#"
393+
394+
fn main() {}
395+
"#,
396+
);
397+
}
398+
384399
#[test]
385400
fn end_of_line_block_to_line() {
386401
check_assist_not_applicable(

crates/syntax/src/ast/token_ext.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ impl ast::Comment {
3232
}
3333

3434
pub fn prefix(&self) -> &'static str {
35-
let &(prefix, _kind) = CommentKind::BY_PREFIX
36-
.iter()
37-
.find(|&(prefix, kind)| self.kind() == *kind && self.text().starts_with(prefix))
38-
.unwrap();
39-
prefix
35+
self.kind().prefix()
4036
}
4137

4238
/// Returns the textual content of a doc comment node as a single string with prefix and suffix

0 commit comments

Comments
 (0)