rune/fmt/
mod.rs

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! Helper to format Rune code.

#[cfg(test)]
mod tests;

mod format;
mod output;

use core::fmt;

use crate::alloc;
use crate::alloc::prelude::*;
use crate::ast::Span;
use crate::compile::{ParseOptionError, Result, WithSpan};
use crate::grammar::{Node, Remaining, Stream, Tree};
use crate::{Diagnostics, Options, SourceId, Sources};

use self::output::Comments;
pub(crate) use self::output::Formatter;

const WS: &str = " ";
const NL: &str = "\n";
const NL_CHAR: char = '\n';
const INDENT: &str = "    ";

#[derive(Debug)]
enum FormatErrorKind {
    Build,
    ParseOptionError(ParseOptionError),
    Alloc(alloc::Error),
}

/// Error during formatting.
#[non_exhaustive]
pub struct FormatError {
    kind: FormatErrorKind,
}

impl fmt::Debug for FormatError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.kind.fmt(f)
    }
}

impl fmt::Display for FormatError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.kind {
            FormatErrorKind::Build => write!(f, "Failed to format source"),
            FormatErrorKind::ParseOptionError(error) => error.fmt(f),
            FormatErrorKind::Alloc(error) => error.fmt(f),
        }
    }
}

impl From<ParseOptionError> for FormatError {
    fn from(value: ParseOptionError) -> Self {
        Self {
            kind: FormatErrorKind::ParseOptionError(value),
        }
    }
}

impl From<alloc::Error> for FormatError {
    fn from(value: alloc::Error) -> Self {
        Self {
            kind: FormatErrorKind::Alloc(value),
        }
    }
}

impl core::error::Error for FormatError {}

/// Format the given source.
pub fn prepare(sources: &Sources) -> Prepare<'_> {
    Prepare {
        sources,
        options: None,
        diagnostics: None,
    }
}

/// A prepared formatting operation.
///
/// See [prepare].
pub struct Prepare<'a> {
    sources: &'a Sources,
    options: Option<&'a Options>,
    diagnostics: Option<&'a mut Diagnostics>,
}

impl<'a> Prepare<'a> {
    /// Associate diagnostics with the build.
    pub fn with_diagnostics(mut self, diagnostics: &'a mut Diagnostics) -> Self {
        self.diagnostics = Some(diagnostics);
        self
    }

    /// Associate options with the build.
    pub fn with_options(mut self, options: &'a Options) -> Self {
        self.options = Some(options);
        self
    }

    /// Format the given sources.
    pub fn format(self) -> Result<Vec<(SourceId, String)>, FormatError> {
        let mut local;

        let diagnostics = match self.diagnostics {
            Some(diagnostics) => diagnostics,
            None => {
                local = Diagnostics::new();
                &mut local
            }
        };

        let default_options;

        let options = match self.options {
            Some(options) => options,
            None => {
                default_options = Options::from_default_env()?;
                &default_options
            }
        };

        let mut files = Vec::new();

        for id in self.sources.source_ids() {
            let Some(source) = self.sources.get(id) else {
                continue;
            };

            match layout_source_with(source.as_str(), id, options, diagnostics) {
                Ok(output) => {
                    files.try_push((id, output))?;
                }
                Err(error) => {
                    diagnostics.error(id, error)?;
                }
            }
        }

        if diagnostics.has_error() {
            return Err(FormatError {
                kind: FormatErrorKind::Build,
            });
        }

        Ok(files)
    }
}

/// Format the given source with the specified options.
pub(crate) fn layout_source_with(
    source: &str,
    source_id: SourceId,
    options: &Options,
    diagnostics: &mut Diagnostics,
) -> Result<String> {
    let tree = crate::grammar::text(source_id, source)
        .without_processing()
        .include_whitespace()
        .root()?;

    #[cfg(feature = "std")]
    if options.print_tree {
        tree.print_with_source(
            &Span::empty(),
            format_args!("Formatting source #{source_id}"),
            source,
        )?;
    }

    let mut o = String::new();

    {
        let mut o = Formatter::new(
            Span::new(0, 0),
            source,
            source_id,
            &mut o,
            &options.fmt,
            diagnostics,
        );

        o.flush_prefix_comments(&tree)?;
        format::root(&mut o, &tree)?;
        o.comments(Comments::Line)?;
    }

    if options.fmt.force_newline && !o.ends_with(NL) {
        o.try_push_str(NL)
            .with_span(Span::new(source.len(), source.len()))?;
    }

    Ok(o)
}