Skip to content

Commit 85c30b1

Browse files
committed
Continue multiline non-doc comment blocks
1 parent 90fe534 commit 85c30b1

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

crates/ra_ide/src/typing/on_enter.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour
3232
return None;
3333
}
3434

35-
// Continuing non-doc line comments (like this one :) ) is annoying
36-
if prefix == "//" && comment_range.end() == position.offset {
35+
// Continuing single-line non-doc comments (like this one :) ) is annoying
36+
if prefix == "//" && comment_range.end() == position.offset && !followed_by_comment(&comment) {
3737
return None;
3838
}
3939

@@ -51,6 +51,17 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour
5151
)
5252
}
5353

54+
fn followed_by_comment(comment: &ast::Comment) -> bool {
55+
let ws = match comment.syntax().next_token().and_then(ast::Whitespace::cast) {
56+
Some(it) => it,
57+
None => return false,
58+
};
59+
if ws.spans_multiple_lines() {
60+
return false;
61+
}
62+
ws.syntax().next_token().and_then(ast::Comment::cast).is_some()
63+
}
64+
5465
fn node_indent(file: &SourceFile, token: &SyntaxToken) -> Option<SmolStr> {
5566
let ws = match file.syntax().token_at_offset(token.text_range().start()) {
5667
TokenAtOffset::Between(l, r) => {
@@ -152,7 +163,7 @@ fn foo() {
152163
}
153164

154165
#[test]
155-
fn continues_code_comment_in_the_middle() {
166+
fn continues_code_comment_in_the_middle_of_line() {
156167
do_check(
157168
r"
158169
fn main() {
@@ -170,6 +181,27 @@ fn main() {
170181
);
171182
}
172183

184+
#[test]
185+
fn continues_code_comment_in_the_middle_several_lines() {
186+
do_check(
187+
r"
188+
fn main() {
189+
// Fix<|>
190+
// me
191+
let x = 1 + 1;
192+
}
193+
",
194+
r"
195+
fn main() {
196+
// Fix
197+
// <|>
198+
// me
199+
let x = 1 + 1;
200+
}
201+
",
202+
);
203+
}
204+
173205
#[test]
174206
fn does_not_continue_end_of_code_comment() {
175207
do_check_noop(

0 commit comments

Comments
 (0)