Skip to content

Commit f5cde97

Browse files
committed
Apply edits
1 parent 9eecba4 commit f5cde97

File tree

1 file changed

+54
-51
lines changed

1 file changed

+54
-51
lines changed

crates/ide_assists/src/handlers/convert_comment_block.rs

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
use ast::{Comment, CommentShape};
21
use itertools::Itertools;
32
use std::convert::identity;
43
use syntax::{
5-
ast::{self, edit::IndentLevel, CommentKind, Whitespace},
4+
ast::{
5+
self,
6+
edit::IndentLevel,
7+
Comment, CommentKind,
8+
CommentPlacement::{Inner, Outer},
9+
CommentShape::{self, Block, Line},
10+
Whitespace,
11+
},
612
AstToken, Direction, SyntaxElement, TextRange,
713
};
814

@@ -42,34 +48,37 @@ pub(crate) fn convert_comment_block(acc: &mut Assists, ctx: &AssistContext) -> O
4248
}
4349

4450
fn block_to_line(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
45-
let indentation = IndentLevel::from_token(comment.syntax());
46-
let line_prefix =
47-
comment_kind_prefix(CommentKind { shape: CommentShape::Line, ..comment.kind() });
48-
49-
let text = comment.text();
50-
let text = &text[comment.prefix().len()..(text.len() - "*/".len())].trim();
51-
52-
let lines = text.lines().peekable();
53-
54-
let indent_spaces = indentation.to_string();
55-
let output = lines
56-
.map(|l| l.trim_start_matches(&indent_spaces))
57-
.map(|l| {
58-
// Don't introduce trailing whitespace
59-
if l.is_empty() {
60-
line_prefix.to_string()
61-
} else {
62-
format!("{} {}", line_prefix, l.trim_start_matches(&indent_spaces))
63-
}
64-
})
65-
.join(&format!("\n{}", indent_spaces));
66-
6751
let target = comment.syntax().text_range();
52+
6853
acc.add(
6954
AssistId("block_to_line", AssistKind::RefactorRewrite),
7055
"Replace block comment with line comments",
7156
target,
72-
|edit| edit.replace(target, output),
57+
|edit| {
58+
let indentation = IndentLevel::from_token(comment.syntax());
59+
let line_prefix =
60+
comment_kind_prefix(CommentKind { shape: CommentShape::Line, ..comment.kind() });
61+
62+
let text = comment.text();
63+
let text = &text[comment.prefix().len()..(text.len() - "*/".len())].trim();
64+
65+
let lines = text.lines().peekable();
66+
67+
let indent_spaces = indentation.to_string();
68+
let output = lines
69+
.map(|l| l.trim_start_matches(&indent_spaces))
70+
.map(|l| {
71+
// Don't introduce trailing whitespace
72+
if l.is_empty() {
73+
line_prefix.to_string()
74+
} else {
75+
format!("{} {}", line_prefix, l.trim_start_matches(&indent_spaces))
76+
}
77+
})
78+
.join(&format!("\n{}", indent_spaces));
79+
80+
edit.replace(target, output)
81+
},
7382
)
7483
}
7584

@@ -83,24 +92,27 @@ fn line_to_block(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
8392
comments.last().unwrap().syntax().text_range().end(),
8493
);
8594

86-
// We pick a single indentation level for the whole block comment based on the
87-
// comment where the assist was invoked. This will be prepended to the
88-
// contents of each line comment when they're put into the block comment.
89-
let indentation = IndentLevel::from_token(&comment.syntax());
90-
91-
let block_comment_body =
92-
comments.into_iter().map(|c| line_comment_text(indentation, c)).join("\n");
93-
94-
let block_prefix =
95-
comment_kind_prefix(CommentKind { shape: CommentShape::Block, ..comment.kind() });
96-
97-
let output = format!("{}\n{}\n{}*/", block_prefix, block_comment_body, indentation.to_string());
98-
9995
acc.add(
10096
AssistId("line_to_block", AssistKind::RefactorRewrite),
10197
"Replace line comments with a single block comment",
10298
target,
103-
|edit| edit.replace(target, output),
99+
|edit| {
100+
// We pick a single indentation level for the whole block comment based on the
101+
// comment where the assist was invoked. This will be prepended to the
102+
// contents of each line comment when they're put into the block comment.
103+
let indentation = IndentLevel::from_token(&comment.syntax());
104+
105+
let block_comment_body =
106+
comments.into_iter().map(|c| line_comment_text(indentation, c)).join("\n");
107+
108+
let block_prefix =
109+
comment_kind_prefix(CommentKind { shape: CommentShape::Block, ..comment.kind() });
110+
111+
let output =
112+
format!("{}\n{}\n{}*/", block_prefix, block_comment_body, indentation.to_string());
113+
114+
edit.replace(target, output)
115+
},
104116
)
105117
}
106118

@@ -160,27 +172,18 @@ fn relevant_line_comments(comment: &ast::Comment) -> Vec<Comment> {
160172
//
161173
// But since such comments aren't idiomatic we're okay with this.
162174
fn line_comment_text(indentation: IndentLevel, comm: ast::Comment) -> String {
163-
let contents = trim_one(comm.text().strip_prefix(comm.prefix()).unwrap()).to_owned();
175+
let contents_without_prefix = comm.text().strip_prefix(comm.prefix()).unwrap();
176+
let contents = contents_without_prefix.strip_prefix(' ').unwrap_or(contents_without_prefix);
164177

165178
// Don't add the indentation if the line is empty
166179
if contents.is_empty() {
167-
contents
180+
contents.to_owned()
168181
} else {
169182
indentation.to_string() + &contents
170183
}
171184
}
172185

173-
fn trim_one(text: &str) -> &str {
174-
if text.starts_with(' ') {
175-
&text[1..]
176-
} else {
177-
text
178-
}
179-
}
180-
181186
fn comment_kind_prefix(ck: ast::CommentKind) -> &'static str {
182-
use ast::CommentPlacement::{Inner, Outer};
183-
use ast::CommentShape::{Block, Line};
184187
match (ck.shape, ck.doc) {
185188
(Line, Some(Inner)) => "//!",
186189
(Line, Some(Outer)) => "///",

0 commit comments

Comments
 (0)