diff --git a/Cargo.lock b/Cargo.lock index 6767a654..9f378ce1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -377,9 +377,9 @@ checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" [[package]] name = "full_moon" -version = "1.2.0" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a02c056a5966f6db8e663ee48093280f098d68d97763a94490ee63178d54776" +checksum = "7e5544e0a9eb14156c6bdc12bfd3bdc35bef1a927e7376eb17626e5d8baec3a3" dependencies = [ "bytecount", "cfg-if", diff --git a/Cargo.toml b/Cargo.toml index 71a51f23..c96d318c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ console = "0.15.7" crossbeam-channel = "0.5.15" ec4rs = { version = "1.0.2", optional = true } env_logger = { version = "0.10.0", default-features = false } -full_moon = "1.2.0" +full_moon = "2.0.0" globset = "0.4.13" ignore = "0.4.20" lazy_static = "1.4.0" diff --git a/src/formatters/functions.rs b/src/formatters/functions.rs index 4ae91066..03b038ce 100644 --- a/src/formatters/functions.rs +++ b/src/formatters/functions.rs @@ -1,9 +1,9 @@ use full_moon::ast::{ punctuated::{Pair, Punctuated}, span::ContainedSpan, - Block, Call, Expression, Field, FunctionArgs, FunctionBody, FunctionCall, FunctionDeclaration, - FunctionName, Index, LastStmt, LocalFunction, MethodCall, Parameter, Prefix, Stmt, Suffix, - TableConstructor, Var, + AnonymousFunction, Block, Call, Expression, Field, FunctionArgs, FunctionBody, FunctionCall, + FunctionDeclaration, FunctionName, Index, LastStmt, LocalFunction, MethodCall, Parameter, + Prefix, Stmt, Suffix, TableConstructor, Var, }; use full_moon::tokenizer::{Token, TokenKind, TokenReference, TokenType}; @@ -42,17 +42,25 @@ use super::expression::process_dot_name; /// This doesn't have its own struct, but it is part of Value::Function pub fn format_anonymous_function( ctx: &Context, - anonymous_function: &(TokenReference, FunctionBody), + anonymous_function: &AnonymousFunction, shape: Shape, -) -> Box<(TokenReference, FunctionBody)> { +) -> Box { const FUNCTION_LEN: usize = "function".len(); let function_definition_trivia = vec![create_function_definition_trivia(ctx)]; - let function_token = fmt_symbol!(ctx, &anonymous_function.0, "function", shape) + let function_token = fmt_symbol!(ctx, anonymous_function.function_token(), "function", shape) .update_trailing_trivia(FormatTriviaType::Append(function_definition_trivia)); - let function_body = - format_function_body(ctx, &anonymous_function.1, shape.add_width(FUNCTION_LEN)); + let function_body = format_function_body( + ctx, + anonymous_function.body(), + shape.add_width(FUNCTION_LEN), + ); - Box::new((function_token, function_body)) + Box::new( + anonymous_function + .clone() + .with_function_token(function_token) + .with_body(function_body), + ) } /// An enum providing information regarding the next AST node after a function call. @@ -230,7 +238,7 @@ fn function_args_multiline_heuristic( match argument { Expression::Function(anonymous_function) => { // Check to see whether it has been expanded - let is_expanded = !should_collapse_function_body(ctx, &anonymous_function.1); + let is_expanded = !should_collapse_function_body(ctx, anonymous_function.body()); if is_expanded { // If we have a mixture of multiline args, and other arguments // Then the function args should be expanded diff --git a/src/formatters/luau.rs b/src/formatters/luau.rs index 2cd023a8..99a5384c 100644 --- a/src/formatters/luau.rs +++ b/src/formatters/luau.rs @@ -26,12 +26,13 @@ use crate::{ }; use full_moon::ast::{ luau::{ - CompoundAssignment, CompoundOp, ExportedTypeDeclaration, ExportedTypeFunction, - GenericDeclaration, GenericDeclarationParameter, GenericParameterInfo, IndexedTypeInfo, - TypeArgument, TypeAssertion, TypeDeclaration, TypeField, TypeFieldKey, TypeFunction, - TypeInfo, TypeIntersection, TypeSpecifier, TypeUnion, + ExportedTypeDeclaration, ExportedTypeFunction, GenericDeclaration, + GenericDeclarationParameter, GenericParameterInfo, IndexedTypeInfo, TypeArgument, + TypeAssertion, TypeDeclaration, TypeField, TypeFieldKey, TypeFunction, TypeInfo, + TypeIntersection, TypeSpecifier, TypeUnion, }, punctuated::Pair, + CompoundAssignment, CompoundOp, }; use full_moon::ast::{punctuated::Punctuated, span::ContainedSpan}; use full_moon::tokenizer::{Token, TokenReference, TokenType}; diff --git a/src/formatters/stmt.rs b/src/formatters/stmt.rs index ce312ba7..e60ff7ec 100644 --- a/src/formatters/stmt.rs +++ b/src/formatters/stmt.rs @@ -944,11 +944,9 @@ pub(crate) mod stmt_block { expression: Box::new(format_expression_block(ctx, expression, shape)), }, Expression::Function(anonymous_function) => { - let block = format_block(ctx, anonymous_function.1.block(), shape); - Expression::Function(Box::new(( - anonymous_function.0.to_owned(), - anonymous_function.1.to_owned().with_block(block), - ))) + let block = format_block(ctx, anonymous_function.body().block(), shape); + let body = anonymous_function.body().clone().with_block(block); + Expression::Function(Box::new(anonymous_function.clone().with_body(body))) } Expression::FunctionCall(function_call) => { Expression::FunctionCall(format_function_call_block(ctx, function_call, shape)) diff --git a/src/formatters/table.rs b/src/formatters/table.rs index 6c31f7a8..831c3954 100644 --- a/src/formatters/table.rs +++ b/src/formatters/table.rs @@ -408,7 +408,7 @@ where fn expression_is_multiline_function(ctx: &Context, expression: &Expression) -> bool { if let Expression::Function(anonymous_function) = expression { - return !should_collapse_function_body(ctx, &anonymous_function.1); + return !should_collapse_function_body(ctx, anonymous_function.body()); } false } diff --git a/src/formatters/trivia.rs b/src/formatters/trivia.rs index 2e276696..d8769caa 100644 --- a/src/formatters/trivia.rs +++ b/src/formatters/trivia.rs @@ -8,9 +8,9 @@ use full_moon::ast::luau::{ TypeIntersection, TypeSpecifier, TypeUnion, }; use full_moon::ast::{ - punctuated::Punctuated, span::ContainedSpan, BinOp, Call, Expression, FunctionArgs, - FunctionBody, FunctionCall, FunctionName, Index, LastStmt, MethodCall, Parameter, Prefix, - Return, Stmt, Suffix, TableConstructor, UnOp, Var, VarExpression, + punctuated::Punctuated, span::ContainedSpan, AnonymousFunction, BinOp, Call, Expression, + FunctionArgs, FunctionBody, FunctionCall, FunctionName, Index, LastStmt, MethodCall, Parameter, + Prefix, Return, Stmt, Suffix, TableConstructor, UnOp, Var, VarExpression, }; use full_moon::ast::{Assignment, If, LocalAssignment}; use full_moon::tokenizer::{Token, TokenReference}; @@ -224,6 +224,12 @@ define_update_trivia!(Call, |this, leading, trailing| { } }); +define_update_trivia!(AnonymousFunction, |this, leading, trailing| { + this.clone() + .with_function_token(this.function_token().update_leading_trivia(leading)) + .with_body(this.body().update_trailing_trivia(trailing)) +}); + define_update_leading_trivia!(Expression, |this, leading| { match this { Expression::Parentheses { @@ -242,10 +248,9 @@ define_update_leading_trivia!(Expression, |this, leading| { binop: binop.to_owned(), rhs: rhs.to_owned(), }, - Expression::Function(anonymous_function) => Expression::Function(Box::new(( - anonymous_function.0.update_leading_trivia(leading), - anonymous_function.1.to_owned(), - ))), + Expression::Function(anonymous_function) => { + Expression::Function(Box::new(anonymous_function.update_leading_trivia(leading))) + } Expression::FunctionCall(function_call) => { Expression::FunctionCall(function_call.update_leading_trivia(leading)) } @@ -284,10 +289,9 @@ define_update_leading_trivia!(Expression, |this, leading| { define_update_trailing_trivia!(Expression, |this, trailing| { match this { - Expression::Function(anonymous_function) => Expression::Function(Box::new(( - anonymous_function.0.to_owned(), - anonymous_function.1.update_trailing_trivia(trailing), - ))), + Expression::Function(anonymous_function) => Expression::Function(Box::new( + anonymous_function.update_trailing_trivia(trailing), + )), Expression::FunctionCall(function_call) => { Expression::FunctionCall(function_call.update_trailing_trivia(trailing)) } diff --git a/src/formatters/trivia_util.rs b/src/formatters/trivia_util.rs index abf53084..341fb6ff 100644 --- a/src/formatters/trivia_util.rs +++ b/src/formatters/trivia_util.rs @@ -11,9 +11,9 @@ use full_moon::ast::luau::{ use full_moon::{ ast::{ punctuated::{Pair, Punctuated}, - BinOp, Block, Call, Expression, Field, FunctionArgs, FunctionBody, Index, LastStmt, - LocalAssignment, Parameter, Prefix, Stmt, Suffix, TableConstructor, UnOp, Var, - VarExpression, + AnonymousFunction, BinOp, Block, Call, Expression, Field, FunctionArgs, FunctionBody, + Index, LastStmt, LocalAssignment, Parameter, Prefix, Stmt, Suffix, TableConstructor, UnOp, + Var, VarExpression, }, node::Node, tokenizer::{Token, TokenKind, TokenReference, TokenType}, @@ -368,6 +368,18 @@ impl GetTrailingTrivia for Var { } } +impl GetLeadingTrivia for AnonymousFunction { + fn leading_trivia(&self) -> Vec { + GetLeadingTrivia::leading_trivia(self.function_token()) + } +} + +impl GetTrailingTrivia for AnonymousFunction { + fn trailing_trivia(&self) -> Vec { + self.body().trailing_trivia() + } +} + impl GetLeadingTrivia for Expression { fn leading_trivia(&self) -> Vec { match self { @@ -383,9 +395,7 @@ impl GetLeadingTrivia for Expression { other => panic!("unknown node {:?}", other), }, Expression::BinaryOperator { lhs, .. } => lhs.leading_trivia(), - Expression::Function(anonymous_function) => { - GetLeadingTrivia::leading_trivia(&anonymous_function.0) - } + Expression::Function(anonymous_function) => anonymous_function.leading_trivia(), Expression::FunctionCall(function_call) => function_call.prefix().leading_trivia(), #[cfg(feature = "luau")] Expression::IfExpression(if_expression) => { @@ -421,9 +431,7 @@ impl GetTrailingTrivia for Expression { } Expression::UnaryOperator { expression, .. } => expression.trailing_trivia(), Expression::BinaryOperator { rhs, .. } => rhs.trailing_trivia(), - Expression::Function(anonymous_function) => { - GetTrailingTrivia::trailing_trivia(anonymous_function.1.end_token()) - } + Expression::Function(anonymous_function) => anonymous_function.trailing_trivia(), Expression::FunctionCall(function_call) => function_call .suffixes() .last()