join any block

This commit is contained in:
Aleksey Kladov 2018-08-28 14:21:37 +03:00
parent 288c9d1ac6
commit 6c41a205a9
4 changed files with 34 additions and 3 deletions

View File

@ -84,7 +84,7 @@ fn remove_newline(
offset: TextUnit,
) {
if node.kind() == WHITESPACE && node_text.bytes().filter(|&b| b == b'\n').count() == 1 {
if join_lambda_body(edit, node).is_some() {
if join_single_expr_block(edit, node).is_some() {
return
}
match (node.prev_sibling(), node.next_sibling()) {
@ -118,13 +118,12 @@ fn remove_newline(
);
}
fn join_lambda_body(
fn join_single_expr_block(
edit: &mut EditBuilder,
node: SyntaxNodeRef,
) -> Option<()> {
let block = ast::Block::cast(node.parent()?)?;
let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
let _lambda = ast::LambdaExpr::cast(block_expr.syntax().parent()?)?;
let expr = single_expr(block)?;
edit.replace(
block_expr.syntax().range(),

View File

@ -222,6 +222,15 @@ pub fn reparse(&self, edit: &AtomEdit) -> File {
<|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
}
");
do_check(r"
fn foo() {
foo(<|>{
92
})
}", r"
fn foo() {
foo(<|>92)
}");
}
#[test]

View File

@ -339,6 +339,7 @@ pub enum Expr<'a> {
PrefixExpr(PrefixExpr<'a>),
RangeExpr(RangeExpr<'a>),
BinExpr(BinExpr<'a>),
Literal(Literal<'a>),
}
impl<'a> AstNode<'a> for Expr<'a> {
@ -375,6 +376,7 @@ impl<'a> AstNode<'a> for Expr<'a> {
PREFIX_EXPR => Some(Expr::PrefixExpr(PrefixExpr { syntax })),
RANGE_EXPR => Some(Expr::RangeExpr(RangeExpr { syntax })),
BIN_EXPR => Some(Expr::BinExpr(BinExpr { syntax })),
LITERAL => Some(Expr::Literal(Literal { syntax })),
_ => None,
}
}
@ -411,6 +413,7 @@ impl<'a> AstNode<'a> for Expr<'a> {
Expr::PrefixExpr(inner) => inner.syntax(),
Expr::RangeExpr(inner) => inner.syntax(),
Expr::BinExpr(inner) => inner.syntax(),
Expr::Literal(inner) => inner.syntax(),
}
}
}
@ -726,6 +729,24 @@ impl<'a> LetStmt<'a> {
}
}
// Literal
#[derive(Debug, Clone, Copy)]
pub struct Literal<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for Literal<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
LITERAL => Some(Literal { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> Literal<'a> {}
// LoopExpr
#[derive(Debug, Clone, Copy)]
pub struct LoopExpr<'a> {

View File

@ -384,6 +384,7 @@ Grammar(
"PrefixExpr": (),
"RangeExpr": (),
"BinExpr": (),
"Literal": (),
"Expr": (
enum: [
@ -418,6 +419,7 @@ Grammar(
"PrefixExpr",
"RangeExpr",
"BinExpr",
"Literal",
],
),