diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-28 11:28:31 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-28 11:28:31 +0000 |
commit | c30425dc96895117b644f29b758cee9dac36839b (patch) | |
tree | d3ccef4aa8f681cc9de29f0435ad20e87911a6ba /crates/ra_assists/src/handlers | |
parent | a1fea0d34ee8f3436aefd87d4c133a7ff50ffbb0 (diff) | |
parent | 311cbbdad599d51c6f08f7dd72c299f7c0128bb2 (diff) |
Merge #3753
3753: Introduce stdx crate r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r-- | crates/ra_assists/src/handlers/add_custom_impl.rs | 14 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/add_impl.rs | 9 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/add_new.rs | 39 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/introduce_variable.rs | 4 |
4 files changed, 29 insertions, 37 deletions
diff --git a/crates/ra_assists/src/handlers/add_custom_impl.rs b/crates/ra_assists/src/handlers/add_custom_impl.rs index dd2bed25a..15f9b216b 100644 --- a/crates/ra_assists/src/handlers/add_custom_impl.rs +++ b/crates/ra_assists/src/handlers/add_custom_impl.rs | |||
@@ -1,17 +1,13 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use join_to_string::join; | ||
4 | use ra_syntax::{ | 1 | use ra_syntax::{ |
5 | ast::{self, AstNode}, | 2 | ast::{self, AstNode}, |
6 | Direction, SmolStr, | 3 | Direction, SmolStr, |
7 | SyntaxKind::{IDENT, WHITESPACE}, | 4 | SyntaxKind::{IDENT, WHITESPACE}, |
8 | TextRange, TextUnit, | 5 | TextRange, TextUnit, |
9 | }; | 6 | }; |
7 | use stdx::SepBy; | ||
10 | 8 | ||
11 | use crate::{Assist, AssistCtx, AssistId}; | 9 | use crate::{Assist, AssistCtx, AssistId}; |
12 | 10 | ||
13 | const DERIVE_TRAIT: &str = "derive"; | ||
14 | |||
15 | // Assist: add_custom_impl | 11 | // Assist: add_custom_impl |
16 | // | 12 | // |
17 | // Adds impl block for derived trait. | 13 | // Adds impl block for derived trait. |
@@ -38,7 +34,7 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> { | |||
38 | .descendants_with_tokens() | 34 | .descendants_with_tokens() |
39 | .filter(|t| t.kind() == IDENT) | 35 | .filter(|t| t.kind() == IDENT) |
40 | .find_map(|i| i.into_token()) | 36 | .find_map(|i| i.into_token()) |
41 | .filter(|t| *t.text() == DERIVE_TRAIT)? | 37 | .filter(|t| *t.text() == "derive")? |
42 | .text() | 38 | .text() |
43 | .clone(); | 39 | .clone(); |
44 | 40 | ||
@@ -63,8 +59,7 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> { | |||
63 | .filter(|t| t != trait_token.text()) | 59 | .filter(|t| t != trait_token.text()) |
64 | .collect::<Vec<SmolStr>>(); | 60 | .collect::<Vec<SmolStr>>(); |
65 | let has_more_derives = !new_attr_input.is_empty(); | 61 | let has_more_derives = !new_attr_input.is_empty(); |
66 | let new_attr_input = | 62 | let new_attr_input = new_attr_input.iter().sep_by(", ").surround_with("(", ")").to_string(); |
67 | join(new_attr_input.iter()).separator(", ").surround_with("(", ")").to_string(); | ||
68 | let new_attr_input_len = new_attr_input.len(); | 63 | let new_attr_input_len = new_attr_input.len(); |
69 | 64 | ||
70 | let mut buf = String::new(); | 65 | let mut buf = String::new(); |
@@ -100,9 +95,10 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> { | |||
100 | 95 | ||
101 | #[cfg(test)] | 96 | #[cfg(test)] |
102 | mod tests { | 97 | mod tests { |
103 | use super::*; | ||
104 | use crate::helpers::{check_assist, check_assist_not_applicable}; | 98 | use crate::helpers::{check_assist, check_assist_not_applicable}; |
105 | 99 | ||
100 | use super::*; | ||
101 | |||
106 | #[test] | 102 | #[test] |
107 | fn add_custom_impl_for_unique_input() { | 103 | fn add_custom_impl_for_unique_input() { |
108 | check_assist( | 104 | check_assist( |
diff --git a/crates/ra_assists/src/handlers/add_impl.rs b/crates/ra_assists/src/handlers/add_impl.rs index afae7d385..6622eadb2 100644 --- a/crates/ra_assists/src/handlers/add_impl.rs +++ b/crates/ra_assists/src/handlers/add_impl.rs | |||
@@ -1,9 +1,8 @@ | |||
1 | use format_buf::format; | ||
2 | use join_to_string::join; | ||
3 | use ra_syntax::{ | 1 | use ra_syntax::{ |
4 | ast::{self, AstNode, NameOwner, TypeParamsOwner}, | 2 | ast::{self, AstNode, NameOwner, TypeParamsOwner}, |
5 | TextUnit, | 3 | TextUnit, |
6 | }; | 4 | }; |
5 | use stdx::{format_to, SepBy}; | ||
7 | 6 | ||
8 | use crate::{Assist, AssistCtx, AssistId}; | 7 | use crate::{Assist, AssistCtx, AssistId}; |
9 | 8 | ||
@@ -36,7 +35,7 @@ pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> { | |||
36 | let mut buf = String::new(); | 35 | let mut buf = String::new(); |
37 | buf.push_str("\n\nimpl"); | 36 | buf.push_str("\n\nimpl"); |
38 | if let Some(type_params) = &type_params { | 37 | if let Some(type_params) = &type_params { |
39 | format!(buf, "{}", type_params.syntax()); | 38 | format_to!(buf, "{}", type_params.syntax()); |
40 | } | 39 | } |
41 | buf.push_str(" "); | 40 | buf.push_str(" "); |
42 | buf.push_str(name.text().as_str()); | 41 | buf.push_str(name.text().as_str()); |
@@ -47,7 +46,9 @@ pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> { | |||
47 | .map(|it| it.text().clone()); | 46 | .map(|it| it.text().clone()); |
48 | let type_params = | 47 | let type_params = |
49 | type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone()); | 48 | type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone()); |
50 | join(lifetime_params.chain(type_params)).surround_with("<", ">").to_buf(&mut buf); | 49 | |
50 | let generic_params = lifetime_params.chain(type_params).sep_by(", "); | ||
51 | format_to!(buf, "<{}>", generic_params) | ||
51 | } | 52 | } |
52 | buf.push_str(" {\n"); | 53 | buf.push_str(" {\n"); |
53 | edit.set_cursor(start_offset + TextUnit::of_str(&buf)); | 54 | edit.set_cursor(start_offset + TextUnit::of_str(&buf)); |
diff --git a/crates/ra_assists/src/handlers/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs index 729a223e0..240b19fa3 100644 --- a/crates/ra_assists/src/handlers/add_new.rs +++ b/crates/ra_assists/src/handlers/add_new.rs | |||
@@ -1,14 +1,11 @@ | |||
1 | use std::fmt::Write; | ||
2 | |||
3 | use format_buf::format; | ||
4 | use hir::Adt; | 1 | use hir::Adt; |
5 | use join_to_string::join; | ||
6 | use ra_syntax::{ | 2 | use ra_syntax::{ |
7 | ast::{ | 3 | ast::{ |
8 | self, AstNode, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner, VisibilityOwner, | 4 | self, AstNode, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner, VisibilityOwner, |
9 | }, | 5 | }, |
10 | TextUnit, T, | 6 | TextUnit, T, |
11 | }; | 7 | }; |
8 | use stdx::{format_to, SepBy}; | ||
12 | 9 | ||
13 | use crate::{Assist, AssistCtx, AssistId}; | 10 | use crate::{Assist, AssistCtx, AssistId}; |
14 | 11 | ||
@@ -53,24 +50,22 @@ pub(crate) fn add_new(ctx: AssistCtx) -> Option<Assist> { | |||
53 | buf.push('\n'); | 50 | buf.push('\n'); |
54 | } | 51 | } |
55 | 52 | ||
56 | let vis = strukt.visibility().map(|v| format!("{} ", v.syntax())); | 53 | let vis = strukt.visibility().map(|v| format!("{} ", v)); |
57 | let vis = vis.as_deref().unwrap_or(""); | 54 | let vis = vis.as_deref().unwrap_or(""); |
58 | write!(&mut buf, " {}fn new(", vis).unwrap(); | ||
59 | |||
60 | join(field_list.fields().filter_map(|f| { | ||
61 | Some(format!("{}: {}", f.name()?.syntax().text(), f.ascribed_type()?.syntax().text())) | ||
62 | })) | ||
63 | .separator(", ") | ||
64 | .to_buf(&mut buf); | ||
65 | 55 | ||
66 | buf.push_str(") -> Self { Self {"); | 56 | let params = field_list |
67 | 57 | .fields() | |
68 | join(field_list.fields().filter_map(|f| Some(f.name()?.syntax().text()))) | 58 | .filter_map(|f| { |
69 | .separator(", ") | 59 | Some(format!( |
70 | .surround_with(" ", " ") | 60 | "{}: {}", |
71 | .to_buf(&mut buf); | 61 | f.name()?.syntax().text(), |
62 | f.ascribed_type()?.syntax().text() | ||
63 | )) | ||
64 | }) | ||
65 | .sep_by(", "); | ||
66 | let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", "); | ||
72 | 67 | ||
73 | buf.push_str("} }"); | 68 | format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields); |
74 | 69 | ||
75 | let (start_offset, end_offset) = impl_def | 70 | let (start_offset, end_offset) = impl_def |
76 | .and_then(|impl_def| { | 71 | .and_then(|impl_def| { |
@@ -103,7 +98,7 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String { | |||
103 | let mut buf = String::with_capacity(code.len()); | 98 | let mut buf = String::with_capacity(code.len()); |
104 | buf.push_str("\n\nimpl"); | 99 | buf.push_str("\n\nimpl"); |
105 | if let Some(type_params) = &type_params { | 100 | if let Some(type_params) = &type_params { |
106 | format!(buf, "{}", type_params.syntax()); | 101 | format_to!(buf, "{}", type_params.syntax()); |
107 | } | 102 | } |
108 | buf.push_str(" "); | 103 | buf.push_str(" "); |
109 | buf.push_str(strukt.name().unwrap().text().as_str()); | 104 | buf.push_str(strukt.name().unwrap().text().as_str()); |
@@ -114,10 +109,10 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String { | |||
114 | .map(|it| it.text().clone()); | 109 | .map(|it| it.text().clone()); |
115 | let type_params = | 110 | let type_params = |
116 | type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone()); | 111 | type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone()); |
117 | join(lifetime_params.chain(type_params)).surround_with("<", ">").to_buf(&mut buf); | 112 | format_to!(buf, "<{}>", lifetime_params.chain(type_params).sep_by(", ")) |
118 | } | 113 | } |
119 | 114 | ||
120 | format!(&mut buf, " {{\n{}\n}}\n", code); | 115 | format_to!(buf, " {{\n{}\n}}\n", code); |
121 | 116 | ||
122 | buf | 117 | buf |
123 | } | 118 | } |
diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs index b453c51fb..1edbdc14c 100644 --- a/crates/ra_assists/src/handlers/introduce_variable.rs +++ b/crates/ra_assists/src/handlers/introduce_variable.rs | |||
@@ -1,4 +1,3 @@ | |||
1 | use format_buf::format; | ||
2 | use ra_syntax::{ | 1 | use ra_syntax::{ |
3 | ast::{self, AstNode}, | 2 | ast::{self, AstNode}, |
4 | SyntaxKind::{ | 3 | SyntaxKind::{ |
@@ -7,6 +6,7 @@ use ra_syntax::{ | |||
7 | }, | 6 | }, |
8 | SyntaxNode, TextUnit, | 7 | SyntaxNode, TextUnit, |
9 | }; | 8 | }; |
9 | use stdx::format_to; | ||
10 | use test_utils::tested_by; | 10 | use test_utils::tested_by; |
11 | 11 | ||
12 | use crate::{Assist, AssistCtx, AssistId}; | 12 | use crate::{Assist, AssistCtx, AssistId}; |
@@ -52,7 +52,7 @@ pub(crate) fn introduce_variable(ctx: AssistCtx) -> Option<Assist> { | |||
52 | buf.push_str("let var_name = "); | 52 | buf.push_str("let var_name = "); |
53 | TextUnit::of_str("let ") | 53 | TextUnit::of_str("let ") |
54 | }; | 54 | }; |
55 | format!(buf, "{}", expr.syntax()); | 55 | format_to!(buf, "{}", expr.syntax()); |
56 | let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone()); | 56 | let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone()); |
57 | let is_full_stmt = if let Some(expr_stmt) = &full_stmt { | 57 | let is_full_stmt = if let Some(expr_stmt) = &full_stmt { |
58 | Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone()) | 58 | Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone()) |