mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Merge #11322
11322: Extract function also extracts comments r=Vannevelj a=Vannevelj Fixes #9011 The difficulty I came across is that the original assist works from the concept of a `ast::StmtList`, a node, but that does not allow me to (easily) represent comments, which are tokens. To combat this, I do a whole bunch of roundtrips: from the `ast::StmtList` I retrieve the `NodeOrToken`s it encompasses. I then cast all `Node` ones back to a `Stmt` so I can apply indentation to it, after which it is again parsed as a `NodeOrToken`. Lastly, I add a new `make::` api that accepts `NodeOrToken` rather than `StmtList` so we can write the comment tokens. Co-authored-by: Jeroen Vannevel <jer_vannevel@outlook.com>
This commit is contained in:
commit
34138379b5
3 changed files with 254 additions and 29 deletions
|
@ -329,6 +329,31 @@ pub fn block_expr(
|
|||
ast_from_text(&format!("fn f() {}", buf))
|
||||
}
|
||||
|
||||
/// Ideally this function wouldn't exist since it involves manual indenting.
|
||||
/// It differs from `make::block_expr` by also supporting comments.
|
||||
///
|
||||
/// FIXME: replace usages of this with the mutable syntax tree API
|
||||
pub fn hacky_block_expr_with_comments(
|
||||
elements: impl IntoIterator<Item = crate::SyntaxElement>,
|
||||
tail_expr: Option<ast::Expr>,
|
||||
) -> ast::BlockExpr {
|
||||
let mut buf = "{\n".to_string();
|
||||
for node_or_token in elements.into_iter() {
|
||||
match node_or_token {
|
||||
rowan::NodeOrToken::Node(n) => format_to!(buf, " {}\n", n),
|
||||
rowan::NodeOrToken::Token(t) if t.kind() == SyntaxKind::COMMENT => {
|
||||
format_to!(buf, " {}\n", t)
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
if let Some(tail_expr) = tail_expr {
|
||||
format_to!(buf, " {}\n", tail_expr);
|
||||
}
|
||||
buf += "}";
|
||||
ast_from_text(&format!("fn f() {}", buf))
|
||||
}
|
||||
|
||||
pub fn expr_unit() -> ast::Expr {
|
||||
expr_from_text("()")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue