4521: Use snippets in add_function r=matklad a=matklad bors r+ 🤖 4522: Explain the purpose of `ast::make` module more clearly r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
a36202390c
@ -25,9 +25,8 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
|
|||||||
let stmt = ctx.find_node_at_offset::<LetStmt>()?;
|
let stmt = ctx.find_node_at_offset::<LetStmt>()?;
|
||||||
let module = ctx.sema.scope(stmt.syntax()).module()?;
|
let module = ctx.sema.scope(stmt.syntax()).module()?;
|
||||||
let expr = stmt.initializer()?;
|
let expr = stmt.initializer()?;
|
||||||
let pat = stmt.pat()?;
|
|
||||||
// Must be a binding
|
// Must be a binding
|
||||||
let pat = match pat {
|
let pat = match stmt.pat()? {
|
||||||
ast::Pat::BindPat(bind_pat) => bind_pat,
|
ast::Pat::BindPat(bind_pat) => bind_pat,
|
||||||
_ => return None,
|
_ => 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
|
// Assist not applicable if the type has already been specified
|
||||||
// and it has no placeholders
|
// and it has no placeholders
|
||||||
let ascribed_ty = stmt.ascribed_type();
|
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() {
|
if ty.syntax().descendants().find_map(ast::PlaceholderType::cast).is_none() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use ra_ide_db::RootDatabase;
|
use ra_ide_db::RootDatabase;
|
||||||
use ra_syntax::ast::{self, AstNode, NameOwner};
|
use ra_syntax::ast::{self, AstNode, NameOwner};
|
||||||
use stdx::format_to;
|
|
||||||
use test_utils::tested_by;
|
use test_utils::tested_by;
|
||||||
|
|
||||||
use crate::{utils::FamousDefs, AssistContext, AssistId, Assists};
|
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 field_type = field_list.fields().next()?.type_ref()?;
|
||||||
let path = match field_type {
|
let path = match field_type {
|
||||||
ast::TypeRef::PathType(p) => p,
|
ast::TypeRef::PathType(it) => it,
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -51,9 +50,7 @@ pub(crate) fn add_from_impl_for_enum(acc: &mut Assists, ctx: &AssistContext) ->
|
|||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
let start_offset = variant.parent_enum().syntax().text_range().end();
|
let start_offset = variant.parent_enum().syntax().text_range().end();
|
||||||
let mut buf = String::new();
|
let buf = format!(
|
||||||
format_to!(
|
|
||||||
buf,
|
|
||||||
r#"
|
r#"
|
||||||
|
|
||||||
impl From<{0}> for {1} {{
|
impl From<{0}> for {1} {{
|
||||||
|
@ -4,13 +4,13 @@
|
|||||||
ast::{
|
ast::{
|
||||||
self,
|
self,
|
||||||
edit::{AstNodeEdit, IndentLevel},
|
edit::{AstNodeEdit, IndentLevel},
|
||||||
ArgListOwner, AstNode, ModuleItemOwner,
|
make, ArgListOwner, AstNode, ModuleItemOwner,
|
||||||
},
|
},
|
||||||
SyntaxKind, SyntaxNode, TextSize,
|
SyntaxKind, SyntaxNode, TextSize,
|
||||||
};
|
};
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, Assists};
|
use crate::{assist_config::SnippetCap, utils::render_snippet, AssistContext, AssistId, Assists};
|
||||||
|
|
||||||
// Assist: add_function
|
// Assist: add_function
|
||||||
//
|
//
|
||||||
@ -33,7 +33,7 @@
|
|||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// fn bar(arg: &str, baz: Baz) {
|
// fn bar(arg: &str, baz: Baz) {
|
||||||
// todo!()
|
// ${0:todo!()}
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// ```
|
// ```
|
||||||
@ -58,21 +58,36 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
|||||||
let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
|
let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
|
||||||
|
|
||||||
let target = call.syntax().text_range();
|
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();
|
let function_template = function_builder.render();
|
||||||
edit.set_file(function_template.file);
|
builder.set_file(function_template.file);
|
||||||
edit.set_cursor(function_template.cursor_offset);
|
let new_fn = function_template.to_string(ctx.config.snippet_cap);
|
||||||
edit.insert(function_template.insert_offset, function_template.fn_def.to_string());
|
match ctx.config.snippet_cap {
|
||||||
|
Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
|
||||||
|
None => builder.insert(function_template.insert_offset, new_fn),
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FunctionTemplate {
|
struct FunctionTemplate {
|
||||||
insert_offset: TextSize,
|
insert_offset: TextSize,
|
||||||
cursor_offset: TextSize,
|
placeholder_expr: ast::MacroCall,
|
||||||
fn_def: ast::SourceFile,
|
leading_ws: String,
|
||||||
|
fn_def: ast::FnDef,
|
||||||
|
trailing_ws: String,
|
||||||
file: FileId,
|
file: FileId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FunctionTemplate {
|
||||||
|
fn to_string(&self, cap: Option<SnippetCap>) -> 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 {
|
struct FunctionBuilder {
|
||||||
target: GeneratedFunctionTarget,
|
target: GeneratedFunctionTarget,
|
||||||
fn_name: ast::Name,
|
fn_name: ast::Name,
|
||||||
@ -110,35 +125,41 @@ fn from_call(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn render(self) -> FunctionTemplate {
|
fn render(self) -> FunctionTemplate {
|
||||||
let placeholder_expr = ast::make::expr_todo();
|
let placeholder_expr = make::expr_todo();
|
||||||
let fn_body = ast::make::block_expr(vec![], Some(placeholder_expr));
|
let fn_body = 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);
|
let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
|
||||||
if self.needs_pub {
|
let mut fn_def =
|
||||||
fn_def = ast::make::add_pub_crate_modifier(fn_def);
|
make::fn_def(visibility, self.fn_name, self.type_params, self.params, fn_body);
|
||||||
}
|
let leading_ws;
|
||||||
|
let trailing_ws;
|
||||||
|
|
||||||
let (fn_def, insert_offset) = match self.target {
|
let insert_offset = match self.target {
|
||||||
GeneratedFunctionTarget::BehindItem(it) => {
|
GeneratedFunctionTarget::BehindItem(it) => {
|
||||||
let with_leading_blank_line = ast::make::add_leading_newlines(2, fn_def);
|
let indent = IndentLevel::from_node(&it);
|
||||||
let indented = with_leading_blank_line.indent(IndentLevel::from_node(&it));
|
leading_ws = format!("\n\n{}", indent);
|
||||||
(indented, it.text_range().end())
|
fn_def = fn_def.indent(indent);
|
||||||
|
trailing_ws = String::new();
|
||||||
|
it.text_range().end()
|
||||||
}
|
}
|
||||||
GeneratedFunctionTarget::InEmptyItemList(it) => {
|
GeneratedFunctionTarget::InEmptyItemList(it) => {
|
||||||
let indent_once = IndentLevel(1);
|
|
||||||
let indent = IndentLevel::from_node(it.syntax());
|
let indent = IndentLevel::from_node(it.syntax());
|
||||||
let fn_def = ast::make::add_leading_newlines(1, fn_def);
|
leading_ws = format!("\n{}", indent + 1);
|
||||||
let fn_def = fn_def.indent(indent_once);
|
fn_def = fn_def.indent(indent + 1);
|
||||||
let fn_def = ast::make::add_trailing_newlines(1, fn_def);
|
trailing_ws = format!("\n{}", indent);
|
||||||
let fn_def = fn_def.indent(indent);
|
it.syntax().text_range().start() + TextSize::of('{')
|
||||||
(fn_def, it.syntax().text_range().start() + TextSize::of('{'))
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let placeholder_expr =
|
let placeholder_expr =
|
||||||
fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
|
fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
|
||||||
let cursor_offset_from_fn_start = placeholder_expr.syntax().text_range().start();
|
FunctionTemplate {
|
||||||
let cursor_offset = insert_offset + cursor_offset_from_fn_start;
|
insert_offset,
|
||||||
FunctionTemplate { insert_offset, cursor_offset, fn_def, file: self.file }
|
placeholder_expr,
|
||||||
|
leading_ws,
|
||||||
|
fn_def,
|
||||||
|
trailing_ws,
|
||||||
|
file: self.file,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,7 +179,7 @@ fn syntax(&self) -> &SyntaxNode {
|
|||||||
|
|
||||||
fn fn_name(call: &ast::Path) -> Option<ast::Name> {
|
fn fn_name(call: &ast::Path) -> Option<ast::Name> {
|
||||||
let name = call.segment()?.syntax().to_string();
|
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
|
/// Computes the type variables and arguments required for the generated function
|
||||||
@ -180,8 +201,8 @@ fn fn_args(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
deduplicate_arg_names(&mut arg_names);
|
deduplicate_arg_names(&mut arg_names);
|
||||||
let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| ast::make::param(name, ty));
|
let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| make::param(name, ty));
|
||||||
Some((None, ast::make::param_list(params)))
|
Some((None, make::param_list(params)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Makes duplicate argument names unique by appending incrementing numbers.
|
/// Makes duplicate argument names unique by appending incrementing numbers.
|
||||||
@ -316,7 +337,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar() {
|
fn bar() {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -343,7 +364,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar() {
|
fn bar() {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -367,7 +388,7 @@ fn foo1() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar() {
|
fn bar() {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn foo2() {}
|
fn foo2() {}
|
||||||
@ -393,7 +414,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar() {
|
fn bar() {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
@ -419,7 +440,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(baz: Baz) {
|
fn bar(baz: Baz) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
@ -452,7 +473,7 @@ fn baz(&self) -> Baz {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(baz: Baz) {
|
fn bar(baz: Baz) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -473,7 +494,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(arg: &str) {
|
fn bar(arg: &str) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
@ -494,7 +515,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(arg: char) {
|
fn bar(arg: char) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
@ -515,7 +536,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(arg: i32) {
|
fn bar(arg: i32) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -536,7 +557,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(arg: u8) {
|
fn bar(arg: u8) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -561,7 +582,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(x: u8) {
|
fn bar(x: u8) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -584,7 +605,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(worble: ()) {
|
fn bar(worble: ()) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -613,7 +634,7 @@ fn baz() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(foo: impl Foo) {
|
fn bar(foo: impl Foo) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -640,7 +661,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(baz: &Baz) {
|
fn bar(baz: &Baz) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -669,7 +690,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(baz: Baz::Bof) {
|
fn bar(baz: Baz::Bof) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -692,7 +713,7 @@ fn foo<T>(t: T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar<T>(t: T) {
|
fn bar<T>(t: T) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -723,7 +744,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(arg: fn() -> Baz) {
|
fn bar(arg: fn() -> Baz) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -748,7 +769,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(closure: impl Fn(i64) -> i64) {
|
fn bar(closure: impl Fn(i64) -> i64) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -769,7 +790,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(baz: ()) {
|
fn bar(baz: ()) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -794,7 +815,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(baz_1: Baz, baz_2: Baz) {
|
fn bar(baz_1: Baz, baz_2: Baz) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -819,7 +840,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) {
|
fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
@ -839,7 +860,7 @@ fn foo() {
|
|||||||
r"
|
r"
|
||||||
mod bar {
|
mod bar {
|
||||||
pub(crate) fn my_fn() {
|
pub(crate) fn my_fn() {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -878,7 +899,7 @@ fn bar() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn baz(foo: foo::Foo) {
|
fn baz(foo: foo::Foo) {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
@ -902,7 +923,7 @@ mod bar {
|
|||||||
fn something_else() {}
|
fn something_else() {}
|
||||||
|
|
||||||
pub(crate) fn my_fn() {
|
pub(crate) fn my_fn() {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -930,7 +951,7 @@ fn foo() {
|
|||||||
mod bar {
|
mod bar {
|
||||||
mod baz {
|
mod baz {
|
||||||
pub(crate) fn my_fn() {
|
pub(crate) fn my_fn() {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -959,7 +980,7 @@ fn main() {
|
|||||||
|
|
||||||
|
|
||||||
pub(crate) fn bar() {
|
pub(crate) fn bar() {
|
||||||
<|>todo!()
|
${0:todo!()}
|
||||||
}",
|
}",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -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 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 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 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]);
|
let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]);
|
||||||
|
@ -78,7 +78,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(arg: &str, baz: Baz) {
|
fn bar(arg: &str, baz: Baz) {
|
||||||
todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
|
|
||||||
"#####,
|
"#####,
|
||||||
|
@ -1,18 +1,44 @@
|
|||||||
//! Assorted functions shared by several assists.
|
//! Assorted functions shared by several assists.
|
||||||
pub(crate) mod insert_use;
|
pub(crate) mod insert_use;
|
||||||
|
|
||||||
use std::iter;
|
use std::{iter, ops};
|
||||||
|
|
||||||
use hir::{Adt, Crate, Semantics, Trait, Type};
|
use hir::{Adt, Crate, Semantics, Trait, Type};
|
||||||
use ra_ide_db::RootDatabase;
|
use ra_ide_db::RootDatabase;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, make, NameOwner},
|
ast::{self, make, NameOwner},
|
||||||
AstNode, T,
|
AstNode, SyntaxNode, T,
|
||||||
};
|
};
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
|
use crate::assist_config::SnippetCap;
|
||||||
|
|
||||||
pub(crate) use insert_use::insert_use_statement;
|
pub(crate) use insert_use::insert_use_statement;
|
||||||
|
|
||||||
|
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<usize> = 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(
|
pub fn get_missing_assoc_items(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
impl_def: &ast::ImplDef,
|
impl_def: &ast::ImplDef,
|
||||||
|
@ -266,6 +266,15 @@ pub fn replace<T: Clone + Into<SyntaxElement>>(&mut self, what: &T, with: &T) {
|
|||||||
let replacement = Replacement::Single(with.clone().into());
|
let replacement = Replacement::Single(with.clone().into());
|
||||||
self.replacements.insert(what, replacement);
|
self.replacements.insert(what, replacement);
|
||||||
}
|
}
|
||||||
|
pub fn replace_with_many<T: Clone + Into<SyntaxElement>>(
|
||||||
|
&mut self,
|
||||||
|
what: &T,
|
||||||
|
with: Vec<SyntaxElement>,
|
||||||
|
) {
|
||||||
|
let what = what.clone().into();
|
||||||
|
let replacement = Replacement::Many(with);
|
||||||
|
self.replacements.insert(what, replacement);
|
||||||
|
}
|
||||||
pub fn replace_ast<T: AstNode>(&mut self, what: &T, with: &T) {
|
pub fn replace_ast<T: AstNode>(&mut self, what: &T, with: &T) {
|
||||||
self.replace(what.syntax(), with.syntax())
|
self.replace(what.syntax(), with.syntax())
|
||||||
}
|
}
|
||||||
@ -302,31 +311,41 @@ fn replacement(&self, element: &SyntaxElement) -> Option<Replacement> {
|
|||||||
|
|
||||||
fn rewrite_children(&self, node: &SyntaxNode) -> SyntaxNode {
|
fn rewrite_children(&self, node: &SyntaxNode) -> SyntaxNode {
|
||||||
// FIXME: this could be made much faster.
|
// FIXME: this could be made much faster.
|
||||||
let new_children =
|
let mut new_children = Vec::new();
|
||||||
node.children_with_tokens().flat_map(|it| self.rewrite_self(&it)).collect::<Vec<_>>();
|
for child in node.children_with_tokens() {
|
||||||
|
self.rewrite_self(&mut new_children, &child);
|
||||||
|
}
|
||||||
with_children(node, new_children)
|
with_children(node, new_children)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_self(
|
fn rewrite_self(
|
||||||
&self,
|
&self,
|
||||||
|
acc: &mut Vec<NodeOrToken<rowan::GreenNode, rowan::GreenToken>>,
|
||||||
element: &SyntaxElement,
|
element: &SyntaxElement,
|
||||||
) -> Option<NodeOrToken<rowan::GreenNode, rowan::GreenToken>> {
|
) {
|
||||||
if let Some(replacement) = self.replacement(&element) {
|
if let Some(replacement) = self.replacement(&element) {
|
||||||
return match replacement {
|
match replacement {
|
||||||
Replacement::Single(NodeOrToken::Node(it)) => {
|
Replacement::Single(NodeOrToken::Node(it)) => {
|
||||||
Some(NodeOrToken::Node(it.green().clone()))
|
acc.push(NodeOrToken::Node(it.green().clone()))
|
||||||
}
|
}
|
||||||
Replacement::Single(NodeOrToken::Token(it)) => {
|
Replacement::Single(NodeOrToken::Token(it)) => {
|
||||||
Some(NodeOrToken::Token(it.green().clone()))
|
acc.push(NodeOrToken::Token(it.green().clone()))
|
||||||
}
|
}
|
||||||
Replacement::Delete => None,
|
Replacement::Many(replacements) => {
|
||||||
|
acc.extend(replacements.iter().map(|it| match it {
|
||||||
|
NodeOrToken::Node(it) => NodeOrToken::Node(it.green().clone()),
|
||||||
|
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
Replacement::Delete => (),
|
||||||
};
|
};
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
let res = match element {
|
let res = match element {
|
||||||
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
|
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
|
||||||
NodeOrToken::Node(it) => NodeOrToken::Node(self.rewrite_children(it).green().clone()),
|
NodeOrToken::Node(it) => NodeOrToken::Node(self.rewrite_children(it).green().clone()),
|
||||||
};
|
};
|
||||||
Some(res)
|
acc.push(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -341,6 +360,7 @@ fn add_assign(&mut self, rhs: SyntaxRewriter) {
|
|||||||
enum Replacement {
|
enum Replacement {
|
||||||
Delete,
|
Delete,
|
||||||
Single(SyntaxElement),
|
Single(SyntaxElement),
|
||||||
|
Many(Vec<SyntaxElement>),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_children(
|
fn with_children(
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
//! This module contains functions for editing syntax trees. As the trees are
|
//! This module contains functions for editing syntax trees. As the trees are
|
||||||
//! immutable, all function here return a fresh copy of the tree, instead of
|
//! immutable, all function here return a fresh copy of the tree, instead of
|
||||||
//! doing an in-place modification.
|
//! doing an in-place modification.
|
||||||
use std::{iter, ops::RangeInclusive};
|
use std::{
|
||||||
|
fmt, iter,
|
||||||
|
ops::{self, RangeInclusive},
|
||||||
|
};
|
||||||
|
|
||||||
use arrayvec::ArrayVec;
|
use arrayvec::ArrayVec;
|
||||||
|
|
||||||
@ -437,6 +440,28 @@ fn from(level: u8) -> IndentLevel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for IndentLevel {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let spaces = " ";
|
||||||
|
let buf;
|
||||||
|
let len = self.0 as usize * 4;
|
||||||
|
let indent = if len <= spaces.len() {
|
||||||
|
&spaces[..len]
|
||||||
|
} else {
|
||||||
|
buf = iter::repeat(' ').take(len).collect::<String>();
|
||||||
|
&buf
|
||||||
|
};
|
||||||
|
fmt::Display::fmt(indent, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ops::Add<u8> for IndentLevel {
|
||||||
|
type Output = IndentLevel;
|
||||||
|
fn add(self, rhs: u8) -> IndentLevel {
|
||||||
|
IndentLevel(self.0 + rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl IndentLevel {
|
impl IndentLevel {
|
||||||
pub fn from_node(node: &SyntaxNode) -> IndentLevel {
|
pub fn from_node(node: &SyntaxNode) -> IndentLevel {
|
||||||
let first_token = match node.first_token() {
|
let first_token = match node.first_token() {
|
||||||
@ -453,6 +478,14 @@ pub fn from_node(node: &SyntaxNode) -> IndentLevel {
|
|||||||
IndentLevel(0)
|
IndentLevel(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// XXX: this intentionally doesn't change the indent of the very first token.
|
||||||
|
/// Ie, in something like
|
||||||
|
/// ```
|
||||||
|
/// fn foo() {
|
||||||
|
/// 92
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
/// if you indent the block, the `{` token would stay put.
|
||||||
fn increase_indent(self, node: SyntaxNode) -> SyntaxNode {
|
fn increase_indent(self, node: SyntaxNode) -> SyntaxNode {
|
||||||
let mut rewriter = SyntaxRewriter::default();
|
let mut rewriter = SyntaxRewriter::default();
|
||||||
node.descendants_with_tokens()
|
node.descendants_with_tokens()
|
||||||
@ -463,12 +496,7 @@ fn increase_indent(self, node: SyntaxNode) -> SyntaxNode {
|
|||||||
text.contains('\n')
|
text.contains('\n')
|
||||||
})
|
})
|
||||||
.for_each(|ws| {
|
.for_each(|ws| {
|
||||||
let new_ws = make::tokens::whitespace(&format!(
|
let new_ws = make::tokens::whitespace(&format!("{}{}", ws.syntax(), self,));
|
||||||
"{}{:width$}",
|
|
||||||
ws.syntax().text(),
|
|
||||||
"",
|
|
||||||
width = self.0 as usize * 4
|
|
||||||
));
|
|
||||||
rewriter.replace(ws.syntax(), &new_ws)
|
rewriter.replace(ws.syntax(), &new_ws)
|
||||||
});
|
});
|
||||||
rewriter.rewrite(&node)
|
rewriter.rewrite(&node)
|
||||||
@ -485,7 +513,7 @@ fn decrease_indent(self, node: SyntaxNode) -> SyntaxNode {
|
|||||||
})
|
})
|
||||||
.for_each(|ws| {
|
.for_each(|ws| {
|
||||||
let new_ws = make::tokens::whitespace(
|
let new_ws = make::tokens::whitespace(
|
||||||
&ws.syntax().text().replace(&format!("\n{:1$}", "", self.0 as usize * 4), "\n"),
|
&ws.syntax().text().replace(&format!("\n{}", self), "\n"),
|
||||||
);
|
);
|
||||||
rewriter.replace(ws.syntax(), &new_ws)
|
rewriter.replace(ws.syntax(), &new_ws)
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
//! This module contains free-standing functions for creating AST fragments out
|
//! This module contains free-standing functions for creating AST fragments out
|
||||||
//! of smaller pieces.
|
//! of smaller pieces.
|
||||||
|
//!
|
||||||
|
//! Note that all functions here intended to be stupid constructors, which just
|
||||||
|
//! assemble a finish node from immediate children. If you want to do something
|
||||||
|
//! smarter than that, it probably doesn't belong in this module.
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use stdx::format_to;
|
use stdx::format_to;
|
||||||
|
|
||||||
@ -95,6 +99,9 @@ pub fn expr_empty_block() -> ast::Expr {
|
|||||||
pub fn expr_unimplemented() -> ast::Expr {
|
pub fn expr_unimplemented() -> ast::Expr {
|
||||||
expr_from_text("unimplemented!()")
|
expr_from_text("unimplemented!()")
|
||||||
}
|
}
|
||||||
|
pub fn expr_unreachable() -> ast::Expr {
|
||||||
|
expr_from_text("unreachable!()")
|
||||||
|
}
|
||||||
pub fn expr_todo() -> ast::Expr {
|
pub fn expr_todo() -> ast::Expr {
|
||||||
expr_from_text("todo!()")
|
expr_from_text("todo!()")
|
||||||
}
|
}
|
||||||
@ -264,10 +271,6 @@ pub fn token(kind: SyntaxKind) -> SyntaxToken {
|
|||||||
.unwrap_or_else(|| panic!("unhandled token: {:?}", kind))
|
.unwrap_or_else(|| panic!("unhandled token: {:?}", kind))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unreachable_macro_call() -> ast::MacroCall {
|
|
||||||
ast_from_text(&format!("unreachable!()"))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn param(name: String, ty: String) -> ast::Param {
|
pub fn param(name: String, ty: String) -> ast::Param {
|
||||||
ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty))
|
ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty))
|
||||||
}
|
}
|
||||||
@ -277,7 +280,12 @@ pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList
|
|||||||
ast_from_text(&format!("fn f({}) {{ }}", args))
|
ast_from_text(&format!("fn f({}) {{ }}", args))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn visibility_pub_crate() -> ast::Visibility {
|
||||||
|
ast_from_text("pub(crate) struct S")
|
||||||
|
}
|
||||||
|
|
||||||
pub fn fn_def(
|
pub fn fn_def(
|
||||||
|
visibility: Option<ast::Visibility>,
|
||||||
fn_name: ast::Name,
|
fn_name: ast::Name,
|
||||||
type_params: Option<ast::TypeParamList>,
|
type_params: Option<ast::TypeParamList>,
|
||||||
params: ast::ParamList,
|
params: ast::ParamList,
|
||||||
@ -285,21 +293,11 @@ pub fn fn_def(
|
|||||||
) -> ast::FnDef {
|
) -> ast::FnDef {
|
||||||
let type_params =
|
let type_params =
|
||||||
if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() };
|
if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() };
|
||||||
ast_from_text(&format!("fn {}{}{} {}", fn_name, type_params, params, body))
|
let visibility = match visibility {
|
||||||
}
|
None => String::new(),
|
||||||
|
Some(it) => format!("{} ", it),
|
||||||
pub fn add_leading_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile {
|
};
|
||||||
let newlines = "\n".repeat(amount_of_newlines);
|
ast_from_text(&format!("{}fn {}{}{} {}", visibility, fn_name, type_params, params, body))
|
||||||
ast_from_text(&format!("{}{}", newlines, t.syntax()))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_trailing_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile {
|
|
||||||
let newlines = "\n".repeat(amount_of_newlines);
|
|
||||||
ast_from_text(&format!("{}{}", t.syntax(), newlines))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_pub_crate_modifier(fn_def: ast::FnDef) -> ast::FnDef {
|
|
||||||
ast_from_text(&format!("pub(crate) {}", fn_def))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
||||||
|
@ -639,7 +639,7 @@ fn main() <fold>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn code_action(world: &WorldSnapshot, assist: Assist) -> Result<lsp_ext::CodeAction> {
|
pub(crate) fn code_action(world: &WorldSnapshot, assist: Assist) -> Result<lsp_ext::CodeAction> {
|
||||||
let res = if assist.source_change.is_snippet {
|
let res = if assist.source_change.cursor_position.is_none() {
|
||||||
lsp_ext::CodeAction {
|
lsp_ext::CodeAction {
|
||||||
title: assist.label,
|
title: assist.label,
|
||||||
kind: Some(String::new()),
|
kind: Some(String::new()),
|
||||||
@ -647,6 +647,7 @@ pub(crate) fn code_action(world: &WorldSnapshot, assist: Assist) -> Result<lsp_e
|
|||||||
command: None,
|
command: None,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
assert!(!assist.source_change.is_snippet);
|
||||||
let source_change = source_change(&world, assist.source_change)?;
|
let source_change = source_change(&world, assist.source_change)?;
|
||||||
let arg = serde_json::to_value(source_change)?;
|
let arg = serde_json::to_value(source_change)?;
|
||||||
let title = assist.label;
|
let title = assist.label;
|
||||||
|
@ -116,3 +116,11 @@ pub fn to_lower_snake_case(s: &str) -> String {
|
|||||||
}
|
}
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn replace(buf: &mut String, from: char, to: &str) {
|
||||||
|
if !buf.contains(from) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// FIXME: do this in place.
|
||||||
|
*buf = buf.replace(from, to)
|
||||||
|
}
|
||||||
|
@ -77,7 +77,7 @@ fn foo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bar(arg: &str, baz: Baz) {
|
fn bar(arg: &str, baz: Baz) {
|
||||||
todo!()
|
${0:todo!()}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user