From 6c41a205a908eb94f139f968f803e728fc3418c6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 28 Aug 2018 14:21:37 +0300 Subject: join any block --- crates/libeditor/src/typing.rs | 5 ++--- crates/libeditor/tests/test.rs | 9 +++++++++ crates/libsyntax2/src/ast/generated.rs | 21 +++++++++++++++++++++ crates/libsyntax2/src/grammar.ron | 2 ++ 4 files changed, 34 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/libeditor/src/typing.rs b/crates/libeditor/src/typing.rs index 3a3776c26..daa57983f 100644 --- a/crates/libeditor/src/typing.rs +++ b/crates/libeditor/src/typing.rs @@ -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(), diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs index 2be54215a..440afe92d 100644 --- a/crates/libeditor/tests/test.rs +++ b/crates/libeditor/tests/test.rs @@ -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] diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index 6181aada8..b24fd2aba 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -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 { + 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> { diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index 7217a4633..c3a29c10c 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -384,6 +384,7 @@ Grammar( "PrefixExpr": (), "RangeExpr": (), "BinExpr": (), + "Literal": (), "Expr": ( enum: [ @@ -418,6 +419,7 @@ Grammar( "PrefixExpr", "RangeExpr", "BinExpr", + "Literal", ], ), -- cgit v1.2.3