fix: keep whitespace in extract function handler

This commit is contained in:
bvanjoi 2023-01-04 22:10:17 +08:00
parent a97c71f92d
commit ae73628f6b
2 changed files with 17 additions and 9 deletions

View file

@ -339,10 +339,10 @@ pub fn tail_only_block_expr(tail_expr: ast::Expr) -> ast::BlockExpr {
}
/// Ideally this function wouldn't exist since it involves manual indenting.
/// It differs from `make::block_expr` by also supporting comments.
/// It differs from `make::block_expr` by also supporting comments and whitespace.
///
/// FIXME: replace usages of this with the mutable syntax tree API
pub fn hacky_block_expr_with_comments(
pub fn hacky_block_expr(
elements: impl IntoIterator<Item = crate::SyntaxElement>,
tail_expr: Option<ast::Expr>,
) -> ast::BlockExpr {
@ -350,10 +350,17 @@ pub fn hacky_block_expr_with_comments(
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, " {t}\n")
rowan::NodeOrToken::Token(t) => {
let kind = t.kind();
if kind == SyntaxKind::COMMENT {
format_to!(buf, " {t}\n")
} else if kind == SyntaxKind::WHITESPACE {
let content = t.text().trim_matches(|c| c != '\n');
if content.len() >= 1 {
format_to!(buf, "{}", &content[1..])
}
}
}
_ => (),
}
}
if let Some(tail_expr) = tail_expr {