1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use crate::{context::Context, shape::Shape, Config};
use full_moon::ast::Ast;

pub mod assignment;
pub mod block;
pub mod general;
#[macro_use]
pub mod expression;
pub mod functions;
#[cfg(feature = "lua52")]
pub mod lua52;
#[cfg(feature = "lua54")]
pub mod lua54;
#[cfg(feature = "luau")]
pub mod luau;
pub mod stmt;
pub mod table;
pub mod trivia;
pub mod trivia_util;

use block::format_block;
use general::format_eof;

pub struct CodeFormatter {
    /// The formatting context
    context: Context,
}

impl CodeFormatter {
    /// Creates a new CodeFormatter, with the given configuration
    pub fn new(config: Config, range: Option<crate::Range>) -> Self {
        CodeFormatter {
            context: Context::new(config, range),
        }
    }

    /// Runs the formatter over the given AST
    pub fn format(&self, ast: Ast) -> Ast {
        let shape = Shape::new(&self.context);
        let new_block = format_block(&self.context, ast.nodes(), shape);
        let new_eof = format_eof(&self.context, ast.eof(), shape);

        ast.with_nodes(new_block).with_eof(new_eof)
    }
}