aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/handlers
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-03-23 16:41:15 +0000
committerAleksey Kladov <[email protected]>2021-03-23 16:41:15 +0000
commite33959a8889104f6bda06755df9ade933aadcf4f (patch)
treeae7c4eaf788a52a98cc887c658437259902ea909 /crates/ide_assists/src/handlers
parent860e069d4dd141ad8d2f2f77a09b9a3ac762ce34 (diff)
Simplify code
changelog: skip
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r--crates/ide_assists/src/handlers/add_missing_impl_members.rs6
-rw-r--r--crates/ide_assists/src/handlers/convert_comment_block.rs46
2 files changed, 15 insertions, 37 deletions
diff --git a/crates/ide_assists/src/handlers/add_missing_impl_members.rs b/crates/ide_assists/src/handlers/add_missing_impl_members.rs
index 63cea754d..0148635f9 100644
--- a/crates/ide_assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ide_assists/src/handlers/add_missing_impl_members.rs
@@ -3,9 +3,9 @@ use syntax::ast::{self, AstNode};
3 3
4use crate::{ 4use crate::{
5 assist_context::{AssistContext, Assists}, 5 assist_context::{AssistContext, Assists},
6 utils::add_trait_assoc_items_to_impl, 6 utils::{
7 utils::DefaultMethods, 7 add_trait_assoc_items_to_impl, filter_assoc_items, render_snippet, Cursor, DefaultMethods,
8 utils::{filter_assoc_items, render_snippet, Cursor}, 8 },
9 AssistId, AssistKind, 9 AssistId, AssistKind,
10}; 10};
11 11
diff --git a/crates/ide_assists/src/handlers/convert_comment_block.rs b/crates/ide_assists/src/handlers/convert_comment_block.rs
index 9dc3ee28f..d202a85f9 100644
--- a/crates/ide_assists/src/handlers/convert_comment_block.rs
+++ b/crates/ide_assists/src/handlers/convert_comment_block.rs
@@ -1,13 +1,6 @@
1use itertools::Itertools; 1use itertools::Itertools;
2use syntax::{ 2use syntax::{
3 ast::{ 3 ast::{self, edit::IndentLevel, Comment, CommentKind, CommentShape, Whitespace},
4 self,
5 edit::IndentLevel,
6 Comment, CommentKind,
7 CommentPlacement::{Inner, Outer},
8 CommentShape::{self, Block, Line},
9 Whitespace,
10 },
11 AstToken, Direction, SyntaxElement, TextRange, 4 AstToken, Direction, SyntaxElement, TextRange,
12}; 5};
13 6
@@ -29,21 +22,18 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
29/// */ 22/// */
30/// ``` 23/// ```
31pub(crate) fn convert_comment_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 24pub(crate) fn convert_comment_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
32 if let Some(comment) = ctx.find_token_at_offset::<ast::Comment>() { 25 let comment = ctx.find_token_at_offset::<ast::Comment>()?;
33 // Only allow comments which are alone on their line 26 // Only allow comments which are alone on their line
34 if let Some(prev) = comment.syntax().prev_token() { 27 if let Some(prev) = comment.syntax().prev_token() {
35 if Whitespace::cast(prev).filter(|w| w.text().contains('\n')).is_none() { 28 if Whitespace::cast(prev).filter(|w| w.text().contains('\n')).is_none() {
36 return None; 29 return None;
37 }
38 } 30 }
39
40 return match comment.kind().shape {
41 ast::CommentShape::Block => block_to_line(acc, comment),
42 ast::CommentShape::Line => line_to_block(acc, comment),
43 };
44 } 31 }
45 32
46 return None; 33 match comment.kind().shape {
34 ast::CommentShape::Block => block_to_line(acc, comment),
35 ast::CommentShape::Line => line_to_block(acc, comment),
36 }
47} 37}
48 38
49fn block_to_line(acc: &mut Assists, comment: ast::Comment) -> Option<()> { 39fn block_to_line(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
@@ -55,8 +45,7 @@ fn block_to_line(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
55 target, 45 target,
56 |edit| { 46 |edit| {
57 let indentation = IndentLevel::from_token(comment.syntax()); 47 let indentation = IndentLevel::from_token(comment.syntax());
58 let line_prefix = 48 let line_prefix = CommentKind { shape: CommentShape::Line, ..comment.kind() }.prefix();
59 comment_kind_prefix(CommentKind { shape: CommentShape::Line, ..comment.kind() });
60 49
61 let text = comment.text(); 50 let text = comment.text();
62 let text = &text[comment.prefix().len()..(text.len() - "*/".len())].trim(); 51 let text = &text[comment.prefix().len()..(text.len() - "*/".len())].trim();
@@ -105,7 +94,7 @@ fn line_to_block(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
105 comments.into_iter().map(|c| line_comment_text(indentation, c)).join("\n"); 94 comments.into_iter().map(|c| line_comment_text(indentation, c)).join("\n");
106 95
107 let block_prefix = 96 let block_prefix =
108 comment_kind_prefix(CommentKind { shape: CommentShape::Block, ..comment.kind() }); 97 CommentKind { shape: CommentShape::Block, ..comment.kind() }.prefix();
109 98
110 let output = 99 let output =
111 format!("{}\n{}\n{}*/", block_prefix, block_comment_body, indentation.to_string()); 100 format!("{}\n{}\n{}*/", block_prefix, block_comment_body, indentation.to_string());
@@ -182,17 +171,6 @@ fn line_comment_text(indentation: IndentLevel, comm: ast::Comment) -> String {
182 } 171 }
183} 172}
184 173
185fn comment_kind_prefix(ck: ast::CommentKind) -> &'static str {
186 match (ck.shape, ck.doc) {
187 (Line, Some(Inner)) => "//!",
188 (Line, Some(Outer)) => "///",
189 (Line, None) => "//",
190 (Block, Some(Inner)) => "/*!",
191 (Block, Some(Outer)) => "/**",
192 (Block, None) => "/*",
193 }
194}
195
196#[cfg(test)] 174#[cfg(test)]
197mod tests { 175mod tests {
198 use crate::tests::{check_assist, check_assist_not_applicable}; 176 use crate::tests::{check_assist, check_assist_not_applicable};