Skip to content

Commit e6fc0bd

Browse files
committed
Moderate cleanup of add_function
1 parent 8eb3272 commit e6fc0bd

File tree

5 files changed

+128
-65
lines changed

5 files changed

+128
-65
lines changed

crates/ra_assists/src/handlers/add_function.rs

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use ra_syntax::{
44
ast::{
55
self,
66
edit::{AstNodeEdit, IndentLevel},
7-
ArgListOwner, AstNode, ModuleItemOwner,
7+
make, ArgListOwner, AstNode, ModuleItemOwner,
88
},
99
SyntaxKind, SyntaxNode, TextSize,
1010
};
1111
use rustc_hash::{FxHashMap, FxHashSet};
1212

13-
use crate::{utils::render_snippet, AssistContext, AssistId, Assists};
13+
use crate::{assist_config::SnippetCap, utils::render_snippet, AssistContext, AssistId, Assists};
1414

1515
// Assist: add_function
1616
//
@@ -61,27 +61,33 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
6161
acc.add(AssistId("add_function"), "Add function", target, |builder| {
6262
let function_template = function_builder.render();
6363
builder.set_file(function_template.file);
64+
let new_fn = function_template.to_string(ctx.config.snippet_cap);
6465
match ctx.config.snippet_cap {
65-
Some(cap) => {
66-
let snippet = render_snippet(
67-
function_template.fn_def.syntax(),
68-
function_template.placeholder_expr.syntax(),
69-
);
70-
builder.insert_snippet(cap, function_template.insert_offset, snippet)
71-
}
72-
None => builder
73-
.insert(function_template.insert_offset, function_template.fn_def.to_string()),
66+
Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
67+
None => builder.insert(function_template.insert_offset, new_fn),
7468
}
7569
})
7670
}
7771

7872
struct FunctionTemplate {
7973
insert_offset: TextSize,
80-
fn_def: ast::SourceFile,
8174
placeholder_expr: ast::MacroCall,
75+
leading_ws: String,
76+
fn_def: ast::FnDef,
77+
trailing_ws: String,
8278
file: FileId,
8379
}
8480

81+
impl FunctionTemplate {
82+
fn to_string(&self, cap: Option<SnippetCap>) -> String {
83+
let f = match cap {
84+
Some(cap) => render_snippet(cap, self.fn_def.syntax(), self.placeholder_expr.syntax()),
85+
None => self.fn_def.to_string(),
86+
};
87+
format!("{}{}{}", self.leading_ws, f, self.trailing_ws)
88+
}
89+
}
90+
8591
struct FunctionBuilder {
8692
target: GeneratedFunctionTarget,
8793
fn_name: ast::Name,
@@ -119,33 +125,41 @@ impl FunctionBuilder {
119125
}
120126

121127
fn render(self) -> FunctionTemplate {
122-
let placeholder_expr = ast::make::expr_todo();
123-
let fn_body = ast::make::block_expr(vec![], Some(placeholder_expr));
124-
let mut fn_def = ast::make::fn_def(self.fn_name, self.type_params, self.params, fn_body);
125-
if self.needs_pub {
126-
fn_def = ast::make::add_pub_crate_modifier(fn_def);
127-
}
128-
129-
let (fn_def, insert_offset) = match self.target {
128+
let placeholder_expr = make::expr_todo();
129+
let fn_body = make::block_expr(vec![], Some(placeholder_expr));
130+
let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
131+
let mut fn_def =
132+
make::fn_def(visibility, self.fn_name, self.type_params, self.params, fn_body);
133+
let leading_ws;
134+
let trailing_ws;
135+
136+
let insert_offset = match self.target {
130137
GeneratedFunctionTarget::BehindItem(it) => {
131-
let with_leading_blank_line = ast::make::add_leading_newlines(2, fn_def);
132-
let indented = with_leading_blank_line.indent(IndentLevel::from_node(&it));
133-
(indented, it.text_range().end())
138+
let indent = IndentLevel::from_node(&it);
139+
leading_ws = format!("\n\n{}", indent);
140+
fn_def = fn_def.indent(indent);
141+
trailing_ws = String::new();
142+
it.text_range().end()
134143
}
135144
GeneratedFunctionTarget::InEmptyItemList(it) => {
136-
let indent_once = IndentLevel(1);
137145
let indent = IndentLevel::from_node(it.syntax());
138-
let fn_def = ast::make::add_leading_newlines(1, fn_def);
139-
let fn_def = fn_def.indent(indent_once);
140-
let fn_def = ast::make::add_trailing_newlines(1, fn_def);
141-
let fn_def = fn_def.indent(indent);
142-
(fn_def, it.syntax().text_range().start() + TextSize::of('{'))
146+
leading_ws = format!("\n{}", indent + 1);
147+
fn_def = fn_def.indent(indent + 1);
148+
trailing_ws = format!("\n{}", indent);
149+
it.syntax().text_range().start() + TextSize::of('{')
143150
}
144151
};
145152

146153
let placeholder_expr =
147154
fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
148-
FunctionTemplate { insert_offset, placeholder_expr, fn_def, file: self.file }
155+
FunctionTemplate {
156+
insert_offset,
157+
placeholder_expr,
158+
leading_ws,
159+
fn_def,
160+
trailing_ws,
161+
file: self.file,
162+
}
149163
}
150164
}
151165

@@ -165,7 +179,7 @@ impl GeneratedFunctionTarget {
165179

166180
fn fn_name(call: &ast::Path) -> Option<ast::Name> {
167181
let name = call.segment()?.syntax().to_string();
168-
Some(ast::make::name(&name))
182+
Some(make::name(&name))
169183
}
170184

171185
/// Computes the type variables and arguments required for the generated function
@@ -187,8 +201,8 @@ fn fn_args(
187201
});
188202
}
189203
deduplicate_arg_names(&mut arg_names);
190-
let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| ast::make::param(name, ty));
191-
Some((None, ast::make::param_list(params)))
204+
let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| make::param(name, ty));
205+
Some((None, make::param_list(params)))
192206
}
193207

194208
/// Makes duplicate argument names unique by appending incrementing numbers.

crates/ra_assists/src/utils.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ use ra_syntax::{
1111
};
1212
use rustc_hash::FxHashSet;
1313

14+
use crate::assist_config::SnippetCap;
15+
1416
pub(crate) use insert_use::insert_use_statement;
1517

16-
pub(crate) fn render_snippet(node: &SyntaxNode, placeholder: &SyntaxNode) -> String {
18+
pub(crate) fn render_snippet(
19+
_cap: SnippetCap,
20+
node: &SyntaxNode,
21+
placeholder: &SyntaxNode,
22+
) -> String {
1723
assert!(placeholder.ancestors().any(|it| it == *node));
1824
let range = placeholder.text_range() - node.text_range().start();
1925
let range: ops::Range<usize> = range.into();

crates/ra_syntax/src/algo.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ impl<'a> SyntaxRewriter<'a> {
266266
let replacement = Replacement::Single(with.clone().into());
267267
self.replacements.insert(what, replacement);
268268
}
269+
pub fn replace_with_many<T: Clone + Into<SyntaxElement>>(
270+
&mut self,
271+
what: &T,
272+
with: Vec<SyntaxElement>,
273+
) {
274+
let what = what.clone().into();
275+
let replacement = Replacement::Many(with);
276+
self.replacements.insert(what, replacement);
277+
}
269278
pub fn replace_ast<T: AstNode>(&mut self, what: &T, with: &T) {
270279
self.replace(what.syntax(), with.syntax())
271280
}
@@ -302,31 +311,41 @@ impl<'a> SyntaxRewriter<'a> {
302311

303312
fn rewrite_children(&self, node: &SyntaxNode) -> SyntaxNode {
304313
// FIXME: this could be made much faster.
305-
let new_children =
306-
node.children_with_tokens().flat_map(|it| self.rewrite_self(&it)).collect::<Vec<_>>();
314+
let mut new_children = Vec::new();
315+
for child in node.children_with_tokens() {
316+
self.rewrite_self(&mut new_children, &child);
317+
}
307318
with_children(node, new_children)
308319
}
309320

310321
fn rewrite_self(
311322
&self,
323+
acc: &mut Vec<NodeOrToken<rowan::GreenNode, rowan::GreenToken>>,
312324
element: &SyntaxElement,
313-
) -> Option<NodeOrToken<rowan::GreenNode, rowan::GreenToken>> {
325+
) {
314326
if let Some(replacement) = self.replacement(&element) {
315-
return match replacement {
327+
match replacement {
316328
Replacement::Single(NodeOrToken::Node(it)) => {
317-
Some(NodeOrToken::Node(it.green().clone()))
329+
acc.push(NodeOrToken::Node(it.green().clone()))
318330
}
319331
Replacement::Single(NodeOrToken::Token(it)) => {
320-
Some(NodeOrToken::Token(it.green().clone()))
332+
acc.push(NodeOrToken::Token(it.green().clone()))
321333
}
322-
Replacement::Delete => None,
334+
Replacement::Many(replacements) => {
335+
acc.extend(replacements.iter().map(|it| match it {
336+
NodeOrToken::Node(it) => NodeOrToken::Node(it.green().clone()),
337+
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
338+
}))
339+
}
340+
Replacement::Delete => (),
323341
};
342+
return;
324343
}
325344
let res = match element {
326345
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().clone()),
327346
NodeOrToken::Node(it) => NodeOrToken::Node(self.rewrite_children(it).green().clone()),
328347
};
329-
Some(res)
348+
acc.push(res)
330349
}
331350
}
332351

@@ -341,6 +360,7 @@ impl ops::AddAssign for SyntaxRewriter<'_> {
341360
enum Replacement {
342361
Delete,
343362
Single(SyntaxElement),
363+
Many(Vec<SyntaxElement>),
344364
}
345365

346366
fn with_children(

crates/ra_syntax/src/ast/edit.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! This module contains functions for editing syntax trees. As the trees are
22
//! immutable, all function here return a fresh copy of the tree, instead of
33
//! doing an in-place modification.
4-
use std::{iter, ops::RangeInclusive};
4+
use std::{
5+
fmt, iter,
6+
ops::{self, RangeInclusive},
7+
};
58

69
use arrayvec::ArrayVec;
710

@@ -437,6 +440,28 @@ impl From<u8> for IndentLevel {
437440
}
438441
}
439442

443+
impl fmt::Display for IndentLevel {
444+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
445+
let spaces = " ";
446+
let buf;
447+
let len = self.0 as usize * 4;
448+
let indent = if len <= spaces.len() {
449+
&spaces[..len]
450+
} else {
451+
buf = iter::repeat(' ').take(len).collect::<String>();
452+
&buf
453+
};
454+
fmt::Display::fmt(indent, f)
455+
}
456+
}
457+
458+
impl ops::Add<u8> for IndentLevel {
459+
type Output = IndentLevel;
460+
fn add(self, rhs: u8) -> IndentLevel {
461+
IndentLevel(self.0 + rhs)
462+
}
463+
}
464+
440465
impl IndentLevel {
441466
pub fn from_node(node: &SyntaxNode) -> IndentLevel {
442467
let first_token = match node.first_token() {
@@ -453,6 +478,14 @@ impl IndentLevel {
453478
IndentLevel(0)
454479
}
455480

481+
/// XXX: this intentionally doesn't change the indent of the very first token.
482+
/// Ie, in something like
483+
/// ```
484+
/// fn foo() {
485+
/// 92
486+
/// }
487+
/// ```
488+
/// if you indent the block, the `{` token would stay put.
456489
fn increase_indent(self, node: SyntaxNode) -> SyntaxNode {
457490
let mut rewriter = SyntaxRewriter::default();
458491
node.descendants_with_tokens()
@@ -463,12 +496,7 @@ impl IndentLevel {
463496
text.contains('\n')
464497
})
465498
.for_each(|ws| {
466-
let new_ws = make::tokens::whitespace(&format!(
467-
"{}{:width$}",
468-
ws.syntax().text(),
469-
"",
470-
width = self.0 as usize * 4
471-
));
499+
let new_ws = make::tokens::whitespace(&format!("{}{}", ws.syntax(), self,));
472500
rewriter.replace(ws.syntax(), &new_ws)
473501
});
474502
rewriter.rewrite(&node)
@@ -485,7 +513,7 @@ impl IndentLevel {
485513
})
486514
.for_each(|ws| {
487515
let new_ws = make::tokens::whitespace(
488-
&ws.syntax().text().replace(&format!("\n{:1$}", "", self.0 as usize * 4), "\n"),
516+
&ws.syntax().text().replace(&format!("\n{}", self), "\n"),
489517
);
490518
rewriter.replace(ws.syntax(), &new_ws)
491519
});

crates/ra_syntax/src/ast/make.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -277,29 +277,24 @@ pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList
277277
ast_from_text(&format!("fn f({}) {{ }}", args))
278278
}
279279

280+
pub fn visibility_pub_crate() -> ast::Visibility {
281+
ast_from_text("pub(crate) struct S")
282+
}
283+
280284
pub fn fn_def(
285+
visibility: Option<ast::Visibility>,
281286
fn_name: ast::Name,
282287
type_params: Option<ast::TypeParamList>,
283288
params: ast::ParamList,
284289
body: ast::BlockExpr,
285290
) -> ast::FnDef {
286291
let type_params =
287292
if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() };
288-
ast_from_text(&format!("fn {}{}{} {}", fn_name, type_params, params, body))
289-
}
290-
291-
pub fn add_leading_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile {
292-
let newlines = "\n".repeat(amount_of_newlines);
293-
ast_from_text(&format!("{}{}", newlines, t.syntax()))
294-
}
295-
296-
pub fn add_trailing_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile {
297-
let newlines = "\n".repeat(amount_of_newlines);
298-
ast_from_text(&format!("{}{}", t.syntax(), newlines))
299-
}
300-
301-
pub fn add_pub_crate_modifier(fn_def: ast::FnDef) -> ast::FnDef {
302-
ast_from_text(&format!("pub(crate) {}", fn_def))
293+
let visibility = match visibility {
294+
None => String::new(),
295+
Some(it) => format!("{} ", it),
296+
};
297+
ast_from_text(&format!("{}fn {}{}{} {}", visibility, fn_name, type_params, params, body))
303298
}
304299

305300
fn ast_from_text<N: AstNode>(text: &str) -> N {

0 commit comments

Comments
 (0)