Moderate cleanup of add_function

This commit is contained in:
Aleksey Kladov 2020-05-19 23:12:01 +02:00
parent 8eb3272ad6
commit e6fc0bdffb
5 changed files with 127 additions and 64 deletions

View file

@ -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<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 {
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 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 (fn_def, insert_offset) = match self.target {
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<ast::Name> {
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.

View file

@ -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<usize> = range.into();

View file

@ -266,6 +266,15 @@ impl<'a> SyntaxRewriter<'a> {
let replacement = Replacement::Single(with.clone().into());
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) {
self.replace(what.syntax(), with.syntax())
}
@ -302,31 +311,41 @@ impl<'a> SyntaxRewriter<'a> {
fn rewrite_children(&self, node: &SyntaxNode) -> SyntaxNode {
// FIXME: this could be made much faster.
let new_children =
node.children_with_tokens().flat_map(|it| self.rewrite_self(&it)).collect::<Vec<_>>();
let mut new_children = Vec::new();
for child in node.children_with_tokens() {
self.rewrite_self(&mut new_children, &child);
}
with_children(node, new_children)
}
fn rewrite_self(
&self,
acc: &mut Vec<NodeOrToken<rowan::GreenNode, rowan::GreenToken>>,
element: &SyntaxElement,
) -> Option<NodeOrToken<rowan::GreenNode, rowan::GreenToken>> {
) {
if let Some(replacement) = self.replacement(&element) {
return match replacement {
match replacement {
Replacement::Single(NodeOrToken::Node(it)) => {
Some(NodeOrToken::Node(it.green().clone()))
acc.push(NodeOrToken::Node(it.green().clone()))
}
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 {
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
NodeOrToken::Node(it) => NodeOrToken::Node(self.rewrite_children(it).green().clone()),
};
Some(res)
acc.push(res)
}
}
@ -341,6 +360,7 @@ impl ops::AddAssign for SyntaxRewriter<'_> {
enum Replacement {
Delete,
Single(SyntaxElement),
Many(Vec<SyntaxElement>),
}
fn with_children(

View file

@ -1,7 +1,10 @@
//! 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
//! doing an in-place modification.
use std::{iter, ops::RangeInclusive};
use std::{
fmt, iter,
ops::{self, RangeInclusive},
};
use arrayvec::ArrayVec;
@ -437,6 +440,28 @@ impl From<u8> for 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 {
pub fn from_node(node: &SyntaxNode) -> IndentLevel {
let first_token = match node.first_token() {
@ -453,6 +478,14 @@ impl IndentLevel {
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 {
let mut rewriter = SyntaxRewriter::default();
node.descendants_with_tokens()
@ -463,12 +496,7 @@ impl IndentLevel {
text.contains('\n')
})
.for_each(|ws| {
let new_ws = make::tokens::whitespace(&format!(
"{}{:width$}",
ws.syntax().text(),
"",
width = self.0 as usize * 4
));
let new_ws = make::tokens::whitespace(&format!("{}{}", ws.syntax(), self,));
rewriter.replace(ws.syntax(), &new_ws)
});
rewriter.rewrite(&node)
@ -485,7 +513,7 @@ impl IndentLevel {
})
.for_each(|ws| {
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)
});

View file

@ -277,7 +277,12 @@ pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList
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(
visibility: Option<ast::Visibility>,
fn_name: ast::Name,
type_params: Option<ast::TypeParamList>,
params: ast::ParamList,
@ -285,21 +290,11 @@ pub fn fn_def(
) -> ast::FnDef {
let type_params =
if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() };
ast_from_text(&format!("fn {}{}{} {}", fn_name, type_params, params, body))
}
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!("{}{}", 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))
let visibility = match visibility {
None => String::new(),
Some(it) => format!("{} ", it),
};
ast_from_text(&format!("{}fn {}{}{} {}", visibility, fn_name, type_params, params, body))
}
fn ast_from_text<N: AstNode>(text: &str) -> N {