|
| 1 | +use crate::{ |
| 2 | + context::{create_indent_trivia, create_newline_trivia, Context}, |
| 3 | + fmt_op, fmt_symbol, |
| 4 | + formatters::{ |
| 5 | + expression::{format_expression, format_var}, |
| 6 | + trivia::{ |
| 7 | + strip_leading_trivia, FormatTriviaType, UpdateLeadingTrivia, UpdateTrailingTrivia, |
| 8 | + }, |
| 9 | + }, |
| 10 | + shape::Shape, |
| 11 | +}; |
| 12 | +use full_moon::{ |
| 13 | + ast::{CompoundAssignment, CompoundOp}, |
| 14 | + tokenizer::TokenReference, |
| 15 | +}; |
| 16 | + |
| 17 | +pub fn format_compound_op(ctx: &Context, compound_op: &CompoundOp, shape: Shape) -> CompoundOp { |
| 18 | + fmt_op!(ctx, CompoundOp, compound_op, shape, { |
| 19 | + PlusEqual = " += ", |
| 20 | + MinusEqual = " -= ", |
| 21 | + StarEqual = " *= ", |
| 22 | + SlashEqual = " /= ", |
| 23 | + #[cfg(feature = "luau")] |
| 24 | + PercentEqual = " %= ", |
| 25 | + CaretEqual = " ^= ", |
| 26 | + #[cfg(feature = "luau")] |
| 27 | + TwoDotsEqual = " ..= ", |
| 28 | + #[cfg(feature = "luau")] |
| 29 | + DoubleSlashEqual = " //= ", |
| 30 | + #[cfg(feature = "cfxlua")] |
| 31 | + DoubleLessThanEqual = " <<= ", |
| 32 | + #[cfg(feature = "cfxlua")] |
| 33 | + DoubleGreaterThanEqual = " >>= ", |
| 34 | + #[cfg(feature = "cfxlua")] |
| 35 | + AmpersandEqual = " &= ", |
| 36 | + #[cfg(feature = "cfxlua")] |
| 37 | + PipeEqual = " |= ", |
| 38 | + }, |other| panic!("unknown node {:?}", other)) |
| 39 | +} |
| 40 | + |
| 41 | +pub fn format_compound_assignment( |
| 42 | + ctx: &Context, |
| 43 | + compound_assignment: &CompoundAssignment, |
| 44 | + shape: Shape, |
| 45 | +) -> CompoundAssignment { |
| 46 | + // Calculate trivia |
| 47 | + let leading_trivia = vec![create_indent_trivia(ctx, shape)]; |
| 48 | + let trailing_trivia = vec![create_newline_trivia(ctx)]; |
| 49 | + |
| 50 | + let lhs = format_var(ctx, compound_assignment.lhs(), shape) |
| 51 | + .update_leading_trivia(FormatTriviaType::Append(leading_trivia)); |
| 52 | + let compound_operator = format_compound_op(ctx, compound_assignment.compound_operator(), shape); |
| 53 | + let shape = shape |
| 54 | + + (strip_leading_trivia(&lhs).to_string().len() + compound_operator.to_string().len()); |
| 55 | + |
| 56 | + let rhs = format_expression(ctx, compound_assignment.rhs(), shape) |
| 57 | + .update_trailing_trivia(FormatTriviaType::Append(trailing_trivia)); |
| 58 | + |
| 59 | + CompoundAssignment::new(lhs, compound_operator, rhs) |
| 60 | +} |
0 commit comments