aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/Cargo.toml4
-rw-r--r--crates/ra_assists/src/handlers/add_custom_impl.rs14
-rw-r--r--crates/ra_assists/src/handlers/add_impl.rs9
-rw-r--r--crates/ra_assists/src/handlers/add_new.rs39
-rw-r--r--crates/ra_assists/src/handlers/introduce_variable.rs4
5 files changed, 31 insertions, 39 deletions
diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml
index a87f4052a..3bcf58ba4 100644
--- a/crates/ra_assists/Cargo.toml
+++ b/crates/ra_assists/Cargo.toml
@@ -8,12 +8,12 @@ authors = ["rust-analyzer developers"]
8doctest = false 8doctest = false
9 9
10[dependencies] 10[dependencies]
11format-buf = "1.0.0"
12join_to_string = "0.1.3"
13rustc-hash = "1.1.0" 11rustc-hash = "1.1.0"
14itertools = "0.9.0" 12itertools = "0.9.0"
15either = "1.5.3" 13either = "1.5.3"
16 14
15stdx = { path = "../stdx" }
16
17ra_syntax = { path = "../ra_syntax" } 17ra_syntax = { path = "../ra_syntax" }
18ra_text_edit = { path = "../ra_text_edit" } 18ra_text_edit = { path = "../ra_text_edit" }
19ra_fmt = { path = "../ra_fmt" } 19ra_fmt = { path = "../ra_fmt" }
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
3use join_to_string::join;
4use ra_syntax::{ 1use 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};
7use stdx::SepBy;
10 8
11use crate::{Assist, AssistCtx, AssistId}; 9use crate::{Assist, AssistCtx, AssistId};
12 10
13const 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)]
102mod tests { 97mod 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 @@
1use format_buf::format;
2use join_to_string::join;
3use ra_syntax::{ 1use ra_syntax::{
4 ast::{self, AstNode, NameOwner, TypeParamsOwner}, 2 ast::{self, AstNode, NameOwner, TypeParamsOwner},
5 TextUnit, 3 TextUnit,
6}; 4};
5use stdx::{format_to, SepBy};
7 6
8use crate::{Assist, AssistCtx, AssistId}; 7use 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 @@
1use std::fmt::Write;
2
3use format_buf::format;
4use hir::Adt; 1use hir::Adt;
5use join_to_string::join;
6use ra_syntax::{ 2use 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};
8use stdx::{format_to, SepBy};
12 9
13use crate::{Assist, AssistCtx, AssistId}; 10use 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 @@
1use format_buf::format;
2use ra_syntax::{ 1use 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};
9use stdx::format_to;
10use test_utils::tested_by; 10use test_utils::tested_by;
11 11
12use crate::{Assist, AssistCtx, AssistId}; 12use 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())