From 0146a95a3987bc8d1f68fddfc39e8a5458feae0d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 21:40:47 +0200 Subject: Cleanup --- crates/ra_assists/src/handlers/add_explicit_type.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs index 0c7d5e355..770049212 100644 --- a/crates/ra_assists/src/handlers/add_explicit_type.rs +++ b/crates/ra_assists/src/handlers/add_explicit_type.rs @@ -25,9 +25,8 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio let stmt = ctx.find_node_at_offset::()?; let module = ctx.sema.scope(stmt.syntax()).module()?; let expr = stmt.initializer()?; - let pat = stmt.pat()?; // Must be a binding - let pat = match pat { + let pat = match stmt.pat()? { ast::Pat::BindPat(bind_pat) => bind_pat, _ => return None, }; @@ -46,7 +45,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio // Assist not applicable if the type has already been specified // and it has no placeholders let ascribed_ty = stmt.ascribed_type(); - if let Some(ref ty) = ascribed_ty { + if let Some(ty) = &ascribed_ty { if ty.syntax().descendants().find_map(ast::PlaceholderType::cast).is_none() { return None; } -- cgit v1.2.3 From 9c3acd3028d32b6cf099e8d5dffe435f15f241a2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 21:43:14 +0200 Subject: Cleanup --- crates/ra_assists/src/handlers/add_from_impl_for_enum.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs index 6a49b7dbd..eb57b7231 100644 --- a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs +++ b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs @@ -1,6 +1,5 @@ use ra_ide_db::RootDatabase; use ra_syntax::ast::{self, AstNode, NameOwner}; -use stdx::format_to; use test_utils::tested_by; use crate::{utils::FamousDefs, AssistContext, AssistId, Assists}; @@ -35,7 +34,7 @@ pub(crate) fn add_from_impl_for_enum(acc: &mut Assists, ctx: &AssistContext) -> } let field_type = field_list.fields().next()?.type_ref()?; let path = match field_type { - ast::TypeRef::PathType(p) => p, + ast::TypeRef::PathType(it) => it, _ => return None, }; @@ -51,9 +50,7 @@ pub(crate) fn add_from_impl_for_enum(acc: &mut Assists, ctx: &AssistContext) -> target, |edit| { let start_offset = variant.parent_enum().syntax().text_range().end(); - let mut buf = String::new(); - format_to!( - buf, + let buf = format!( r#" impl From<{0}> for {1} {{ -- cgit v1.2.3 From 8eb3272ad6f774bccb967ee640b72a9a17273e7b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 22:25:07 +0200 Subject: Use snippets in add function --- crates/ra_assists/src/handlers/add_function.rs | 79 ++++++++++++++------------ crates/ra_assists/src/tests/generated.rs | 2 +- crates/ra_assists/src/utils.rs | 24 +++++++- 3 files changed, 66 insertions(+), 39 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs index de016ae4e..69fede00f 100644 --- a/crates/ra_assists/src/handlers/add_function.rs +++ b/crates/ra_assists/src/handlers/add_function.rs @@ -10,7 +10,7 @@ use ra_syntax::{ }; use rustc_hash::{FxHashMap, FxHashSet}; -use crate::{AssistContext, AssistId, Assists}; +use crate::{utils::render_snippet, AssistContext, AssistId, Assists}; // Assist: add_function // @@ -33,7 +33,7 @@ use crate::{AssistContext, AssistId, Assists}; // } // // fn bar(arg: &str, baz: Baz) { -// todo!() +// ${0:todo!()} // } // // ``` @@ -58,18 +58,27 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?; let target = call.syntax().text_range(); - acc.add(AssistId("add_function"), "Add function", target, |edit| { + acc.add(AssistId("add_function"), "Add function", target, |builder| { let function_template = function_builder.render(); - edit.set_file(function_template.file); - edit.set_cursor(function_template.cursor_offset); - edit.insert(function_template.insert_offset, function_template.fn_def.to_string()); + builder.set_file(function_template.file); + match ctx.config.snippet_cap { + Some(cap) => { + let snippet = render_snippet( + function_template.fn_def.syntax(), + function_template.placeholder_expr.syntax(), + ); + builder.insert_snippet(cap, function_template.insert_offset, snippet) + } + None => builder + .insert(function_template.insert_offset, function_template.fn_def.to_string()), + } }) } struct FunctionTemplate { insert_offset: TextSize, - cursor_offset: TextSize, fn_def: ast::SourceFile, + placeholder_expr: ast::MacroCall, file: FileId, } @@ -136,9 +145,7 @@ impl FunctionBuilder { let placeholder_expr = fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); - let cursor_offset_from_fn_start = placeholder_expr.syntax().text_range().start(); - let cursor_offset = insert_offset + cursor_offset_from_fn_start; - FunctionTemplate { insert_offset, cursor_offset, fn_def, file: self.file } + FunctionTemplate { insert_offset, placeholder_expr, fn_def, file: self.file } } } @@ -316,7 +323,7 @@ fn foo() { } fn bar() { - <|>todo!() + ${0:todo!()} } ", ) @@ -343,7 +350,7 @@ impl Foo { } fn bar() { - <|>todo!() + ${0:todo!()} } ", ) @@ -367,7 +374,7 @@ fn foo1() { } fn bar() { - <|>todo!() + ${0:todo!()} } fn foo2() {} @@ -393,7 +400,7 @@ mod baz { } fn bar() { - <|>todo!() + ${0:todo!()} } } ", @@ -419,7 +426,7 @@ fn foo() { } fn bar(baz: Baz) { - <|>todo!() + ${0:todo!()} } ", ); @@ -452,7 +459,7 @@ impl Baz { } fn bar(baz: Baz) { - <|>todo!() + ${0:todo!()} } ", ) @@ -473,7 +480,7 @@ fn foo() { } fn bar(arg: &str) { - <|>todo!() + ${0:todo!()} } "#, ) @@ -494,7 +501,7 @@ fn foo() { } fn bar(arg: char) { - <|>todo!() + ${0:todo!()} } "#, ) @@ -515,7 +522,7 @@ fn foo() { } fn bar(arg: i32) { - <|>todo!() + ${0:todo!()} } ", ) @@ -536,7 +543,7 @@ fn foo() { } fn bar(arg: u8) { - <|>todo!() + ${0:todo!()} } ", ) @@ -561,7 +568,7 @@ fn foo() { } fn bar(x: u8) { - <|>todo!() + ${0:todo!()} } ", ) @@ -584,7 +591,7 @@ fn foo() { } fn bar(worble: ()) { - <|>todo!() + ${0:todo!()} } ", ) @@ -613,7 +620,7 @@ fn baz() { } fn bar(foo: impl Foo) { - <|>todo!() + ${0:todo!()} } ", ) @@ -640,7 +647,7 @@ fn foo() { } fn bar(baz: &Baz) { - <|>todo!() + ${0:todo!()} } ", ) @@ -669,7 +676,7 @@ fn foo() { } fn bar(baz: Baz::Bof) { - <|>todo!() + ${0:todo!()} } ", ) @@ -692,7 +699,7 @@ fn foo(t: T) { } fn bar(t: T) { - <|>todo!() + ${0:todo!()} } ", ) @@ -723,7 +730,7 @@ fn foo() { } fn bar(arg: fn() -> Baz) { - <|>todo!() + ${0:todo!()} } ", ) @@ -748,7 +755,7 @@ fn foo() { } fn bar(closure: impl Fn(i64) -> i64) { - <|>todo!() + ${0:todo!()} } ", ) @@ -769,7 +776,7 @@ fn foo() { } fn bar(baz: ()) { - <|>todo!() + ${0:todo!()} } ", ) @@ -794,7 +801,7 @@ fn foo() { } fn bar(baz_1: Baz, baz_2: Baz) { - <|>todo!() + ${0:todo!()} } ", ) @@ -819,7 +826,7 @@ fn foo() { } fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) { - <|>todo!() + ${0:todo!()} } "#, ) @@ -839,7 +846,7 @@ fn foo() { r" mod bar { pub(crate) fn my_fn() { - <|>todo!() + ${0:todo!()} } } @@ -878,7 +885,7 @@ fn bar() { } fn baz(foo: foo::Foo) { - <|>todo!() + ${0:todo!()} } ", ) @@ -902,7 +909,7 @@ mod bar { fn something_else() {} pub(crate) fn my_fn() { - <|>todo!() + ${0:todo!()} } } @@ -930,7 +937,7 @@ fn foo() { mod bar { mod baz { pub(crate) fn my_fn() { - <|>todo!() + ${0:todo!()} } } } @@ -959,7 +966,7 @@ fn main() { pub(crate) fn bar() { - <|>todo!() + ${0:todo!()} }", ) } diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index 32fbcdef4..1d82c245d 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs @@ -78,7 +78,7 @@ fn foo() { } fn bar(arg: &str, baz: Baz) { - todo!() + ${0:todo!()} } "#####, diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index f3fc92ebf..bb9749b06 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -1,18 +1,38 @@ //! Assorted functions shared by several assists. pub(crate) mod insert_use; -use std::iter; +use std::{iter, ops}; use hir::{Adt, Crate, Semantics, Trait, Type}; use ra_ide_db::RootDatabase; use ra_syntax::{ ast::{self, make, NameOwner}, - AstNode, T, + AstNode, SyntaxNode, T, }; use rustc_hash::FxHashSet; pub(crate) use insert_use::insert_use_statement; +pub(crate) fn render_snippet(node: &SyntaxNode, placeholder: &SyntaxNode) -> String { + assert!(placeholder.ancestors().any(|it| it == *node)); + let range = placeholder.text_range() - node.text_range().start(); + let range: ops::Range = range.into(); + + let mut placeholder = placeholder.to_string(); + escape(&mut placeholder); + let tab_stop = format!("${{0:{}}}", placeholder); + + let mut buf = node.to_string(); + buf.replace_range(range, &tab_stop); + return buf; + + fn escape(buf: &mut String) { + stdx::replace(buf, '{', r"\{"); + stdx::replace(buf, '}', r"\}"); + stdx::replace(buf, '$', r"\$"); + } +} + pub fn get_missing_assoc_items( sema: &Semantics, impl_def: &ast::ImplDef, -- cgit v1.2.3 From 4de2749db8281c00aba37270fa9ae8d4bd2572d8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 01:28:11 +0200 Subject: Explain the purpose of `ast::make` module more clearly --- crates/ra_assists/src/handlers/replace_unwrap_with_match.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index c4b56f6e9..b379b55a8 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs @@ -51,7 +51,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a"))); let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); - let unreachable_call = make::unreachable_macro_call().into(); + let unreachable_call = make::expr_unreachable(); let err_arm = make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call); let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); -- cgit v1.2.3 From e6fc0bdffb213f6e94c5bb4081e6d175ccbd518f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 23:12:01 +0200 Subject: Moderate cleanup of add_function --- crates/ra_assists/src/handlers/add_function.rs | 80 +++++++++++++++----------- crates/ra_assists/src/utils.rs | 8 ++- 2 files changed, 54 insertions(+), 34 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs index 69fede00f..a0709630d 100644 --- a/crates/ra_assists/src/handlers/add_function.rs +++ b/crates/ra_assists/src/handlers/add_function.rs @@ -4,13 +4,13 @@ use ra_syntax::{ ast::{ self, edit::{AstNodeEdit, IndentLevel}, - ArgListOwner, AstNode, ModuleItemOwner, + make, ArgListOwner, AstNode, ModuleItemOwner, }, SyntaxKind, SyntaxNode, TextSize, }; use rustc_hash::{FxHashMap, FxHashSet}; -use crate::{utils::render_snippet, AssistContext, AssistId, Assists}; +use crate::{assist_config::SnippetCap, utils::render_snippet, AssistContext, AssistId, Assists}; // Assist: add_function // @@ -61,27 +61,33 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> acc.add(AssistId("add_function"), "Add function", target, |builder| { let function_template = function_builder.render(); builder.set_file(function_template.file); + let new_fn = function_template.to_string(ctx.config.snippet_cap); match ctx.config.snippet_cap { - Some(cap) => { - let snippet = render_snippet( - function_template.fn_def.syntax(), - function_template.placeholder_expr.syntax(), - ); - builder.insert_snippet(cap, function_template.insert_offset, snippet) - } - None => builder - .insert(function_template.insert_offset, function_template.fn_def.to_string()), + Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn), + None => builder.insert(function_template.insert_offset, new_fn), } }) } struct FunctionTemplate { insert_offset: TextSize, - fn_def: ast::SourceFile, placeholder_expr: ast::MacroCall, + leading_ws: String, + fn_def: ast::FnDef, + trailing_ws: String, file: FileId, } +impl FunctionTemplate { + fn to_string(&self, cap: Option) -> String { + let f = match cap { + Some(cap) => render_snippet(cap, self.fn_def.syntax(), self.placeholder_expr.syntax()), + None => self.fn_def.to_string(), + }; + format!("{}{}{}", self.leading_ws, f, self.trailing_ws) + } +} + struct FunctionBuilder { target: GeneratedFunctionTarget, fn_name: ast::Name, @@ -119,33 +125,41 @@ impl FunctionBuilder { } fn render(self) -> FunctionTemplate { - let placeholder_expr = ast::make::expr_todo(); - let fn_body = ast::make::block_expr(vec![], Some(placeholder_expr)); - let mut fn_def = ast::make::fn_def(self.fn_name, self.type_params, self.params, fn_body); - if self.needs_pub { - fn_def = ast::make::add_pub_crate_modifier(fn_def); - } - - let (fn_def, insert_offset) = match self.target { + let placeholder_expr = make::expr_todo(); + let fn_body = make::block_expr(vec![], Some(placeholder_expr)); + let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None }; + let mut fn_def = + make::fn_def(visibility, self.fn_name, self.type_params, self.params, fn_body); + let leading_ws; + let trailing_ws; + + let insert_offset = match self.target { GeneratedFunctionTarget::BehindItem(it) => { - let with_leading_blank_line = ast::make::add_leading_newlines(2, fn_def); - let indented = with_leading_blank_line.indent(IndentLevel::from_node(&it)); - (indented, it.text_range().end()) + let indent = IndentLevel::from_node(&it); + leading_ws = format!("\n\n{}", indent); + fn_def = fn_def.indent(indent); + trailing_ws = String::new(); + it.text_range().end() } GeneratedFunctionTarget::InEmptyItemList(it) => { - let indent_once = IndentLevel(1); let indent = IndentLevel::from_node(it.syntax()); - let fn_def = ast::make::add_leading_newlines(1, fn_def); - let fn_def = fn_def.indent(indent_once); - let fn_def = ast::make::add_trailing_newlines(1, fn_def); - let fn_def = fn_def.indent(indent); - (fn_def, it.syntax().text_range().start() + TextSize::of('{')) + leading_ws = format!("\n{}", indent + 1); + fn_def = fn_def.indent(indent + 1); + trailing_ws = format!("\n{}", indent); + it.syntax().text_range().start() + TextSize::of('{') } }; let placeholder_expr = fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); - FunctionTemplate { insert_offset, placeholder_expr, fn_def, file: self.file } + FunctionTemplate { + insert_offset, + placeholder_expr, + leading_ws, + fn_def, + trailing_ws, + file: self.file, + } } } @@ -165,7 +179,7 @@ impl GeneratedFunctionTarget { fn fn_name(call: &ast::Path) -> Option { let name = call.segment()?.syntax().to_string(); - Some(ast::make::name(&name)) + Some(make::name(&name)) } /// Computes the type variables and arguments required for the generated function @@ -187,8 +201,8 @@ fn fn_args( }); } deduplicate_arg_names(&mut arg_names); - let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| ast::make::param(name, ty)); - Some((None, ast::make::param_list(params))) + let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| make::param(name, ty)); + Some((None, make::param_list(params))) } /// Makes duplicate argument names unique by appending incrementing numbers. diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index bb9749b06..8a26a6808 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -11,9 +11,15 @@ use ra_syntax::{ }; use rustc_hash::FxHashSet; +use crate::assist_config::SnippetCap; + pub(crate) use insert_use::insert_use_statement; -pub(crate) fn render_snippet(node: &SyntaxNode, placeholder: &SyntaxNode) -> String { +pub(crate) fn render_snippet( + _cap: SnippetCap, + node: &SyntaxNode, + placeholder: &SyntaxNode, +) -> String { assert!(placeholder.ancestors().any(|it| it == *node)); let range = placeholder.text_range() - node.text_range().start(); let range: ops::Range = range.into(); -- cgit v1.2.3