-
-
Notifications
You must be signed in to change notification settings - Fork 376
/
lib.rs
396 lines (343 loc) · 13 KB
/
lib.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
mod comments;
pub mod context;
mod css;
mod cst;
mod generated;
mod prelude;
mod separated;
mod utils;
use std::borrow::Cow;
use crate::comments::CssCommentStyle;
pub(crate) use crate::context::CssFormatContext;
use crate::context::CssFormatOptions;
use crate::cst::FormatCssSyntaxNode;
use biome_css_syntax::{
AnyCssDeclarationBlock, AnyCssRule, AnyCssRuleBlock, AnyCssValue, CssLanguage, CssSyntaxKind,
CssSyntaxNode, CssSyntaxToken,
};
use biome_formatter::comments::Comments;
use biome_formatter::prelude::*;
use biome_formatter::token::string::ToAsciiLowercaseCow;
use biome_formatter::trivia::format_skipped_token_trivia;
use biome_formatter::{
write, CstFormatContext, FormatContext, FormatLanguage, FormatOwnedWithRule, FormatRefWithRule,
TransformSourceMap,
};
use biome_formatter::{Formatted, Printed};
use biome_rowan::{AstNode, SyntaxNode, TextRange};
/// Used to get an object that knows how to format this object.
pub(crate) trait AsFormat<Context> {
type Format<'a>: biome_formatter::Format<Context>
where
Self: 'a;
/// Returns an object that is able to format this object.
fn format(&self) -> Self::Format<'_>;
}
/// Implement [AsFormat] for references to types that implement [AsFormat].
impl<T, C> AsFormat<C> for &T
where
T: AsFormat<C>,
{
type Format<'a> = T::Format<'a> where Self: 'a;
fn format(&self) -> Self::Format<'_> {
AsFormat::format(&**self)
}
}
/// Implement [AsFormat] for [SyntaxResult] where `T` implements [AsFormat].
///
/// Useful to format mandatory AST fields without having to unwrap the value first.
impl<T, C> AsFormat<C> for biome_rowan::SyntaxResult<T>
where
T: AsFormat<C>,
{
type Format<'a> = biome_rowan::SyntaxResult<T::Format<'a>> where Self: 'a;
fn format(&self) -> Self::Format<'_> {
match self {
Ok(value) => Ok(value.format()),
Err(err) => Err(*err),
}
}
}
/// Implement [AsFormat] for [Option] when `T` implements [AsFormat]
///
/// Allows to call format on optional AST fields without having to unwrap the field first.
impl<T, C> AsFormat<C> for Option<T>
where
T: AsFormat<C>,
{
type Format<'a> = Option<T::Format<'a>> where Self: 'a;
fn format(&self) -> Self::Format<'_> {
self.as_ref().map(|value| value.format())
}
}
/// Used to convert this object into an object that can be formatted.
///
/// The difference to [AsFormat] is that this trait takes ownership of `self`.
pub(crate) trait IntoFormat<Context> {
type Format: biome_formatter::Format<Context>;
fn into_format(self) -> Self::Format;
}
impl<T, Context> IntoFormat<Context> for biome_rowan::SyntaxResult<T>
where
T: IntoFormat<Context>,
{
type Format = biome_rowan::SyntaxResult<T::Format>;
fn into_format(self) -> Self::Format {
self.map(IntoFormat::into_format)
}
}
/// Implement [IntoFormat] for [Option] when `T` implements [IntoFormat]
///
/// Allows to call format on optional AST fields without having to unwrap the field first.
impl<T, Context> IntoFormat<Context> for Option<T>
where
T: IntoFormat<Context>,
{
type Format = Option<T::Format>;
fn into_format(self) -> Self::Format {
self.map(IntoFormat::into_format)
}
}
/// Formatting specific [Iterator] extensions
pub(crate) trait FormattedIterExt {
/// Converts every item to an object that knows how to format it.
fn formatted<Context>(self) -> FormattedIter<Self, Self::Item, Context>
where
Self: Iterator + Sized,
Self::Item: IntoFormat<Context>,
{
FormattedIter {
inner: self,
options: std::marker::PhantomData,
}
}
}
impl<I> FormattedIterExt for I where I: std::iter::Iterator {}
pub(crate) struct FormattedIter<Iter, Item, Context>
where
Iter: Iterator<Item = Item>,
{
inner: Iter,
options: std::marker::PhantomData<Context>,
}
impl<Iter, Item, Context> std::iter::Iterator for FormattedIter<Iter, Item, Context>
where
Iter: Iterator<Item = Item>,
Item: IntoFormat<Context>,
{
type Item = Item::Format;
fn next(&mut self) -> Option<Self::Item> {
Some(self.inner.next()?.into_format())
}
}
impl<Iter, Item, Context> std::iter::FusedIterator for FormattedIter<Iter, Item, Context>
where
Iter: std::iter::FusedIterator<Item = Item>,
Item: IntoFormat<Context>,
{
}
impl<Iter, Item, Context> std::iter::ExactSizeIterator for FormattedIter<Iter, Item, Context>
where
Iter: Iterator<Item = Item> + std::iter::ExactSizeIterator,
Item: IntoFormat<Context>,
{
}
pub(crate) type CssFormatter<'buf> = Formatter<'buf, CssFormatContext>;
/// Format a [CssSyntaxNode]
pub(crate) trait FormatNodeRule<N>
where
N: AstNode<Language = CssLanguage>,
{
fn fmt(&self, node: &N, f: &mut CssFormatter) -> FormatResult<()> {
if self.is_suppressed(node, f) {
return write!(f, [format_suppressed_node(node.syntax())]);
}
self.fmt_leading_comments(node, f)?;
self.fmt_fields(node, f)?;
self.fmt_dangling_comments(node, f)?;
self.fmt_trailing_comments(node, f)
}
fn fmt_fields(&self, node: &N, f: &mut CssFormatter) -> FormatResult<()>;
/// Returns `true` if the node has a suppression comment and should use the same formatting as in the source document.
fn is_suppressed(&self, node: &N, f: &CssFormatter) -> bool {
f.context().comments().is_suppressed(node.syntax())
}
/// Formats the [leading comments](biome_formatter::comments#leading-comments) of the node.
///
/// You may want to override this method if you want to manually handle the formatting of comments
/// inside of the `fmt_fields` method or customize the formatting of the leading comments.
fn fmt_leading_comments(&self, node: &N, f: &mut CssFormatter) -> FormatResult<()> {
format_leading_comments(node.syntax()).fmt(f)
}
/// Formats the [dangling comments](biome_formatter::comments#dangling-comments) of the node.
///
/// You should override this method if the node handled by this rule can have dangling comments because the
/// default implementation formats the dangling comments at the end of the node, which isn't ideal but ensures that
/// no comments are dropped.
///
/// A node can have dangling comments if all its children are tokens or if all node childrens are optional.
fn fmt_dangling_comments(&self, node: &N, f: &mut CssFormatter) -> FormatResult<()> {
format_dangling_comments(node.syntax())
.with_soft_block_indent()
.fmt(f)
}
/// Formats the [trailing comments](biome_formatter::comments#trailing-comments) of the node.
///
/// You may want to override this method if you want to manually handle the formatting of comments
/// inside of the `fmt_fields` method or customize the formatting of the trailing comments.
fn fmt_trailing_comments(&self, node: &N, f: &mut CssFormatter) -> FormatResult<()> {
format_trailing_comments(node.syntax()).fmt(f)
}
}
/// Rule for formatting an bogus nodes.
pub(crate) trait FormatBogusNodeRule<N>
where
N: AstNode<Language = CssLanguage>,
{
fn fmt(&self, node: &N, f: &mut CssFormatter) -> FormatResult<()> {
format_bogus_node(node.syntax()).fmt(f)
}
}
#[derive(Debug, Default, Clone)]
pub struct CssFormatLanguage {
options: CssFormatOptions,
}
impl CssFormatLanguage {
pub fn new(options: CssFormatOptions) -> Self {
Self { options }
}
}
impl FormatLanguage for CssFormatLanguage {
type SyntaxLanguage = CssLanguage;
type Context = CssFormatContext;
type FormatRule = FormatCssSyntaxNode;
// For CSS, range formatting allows:
// - any block of rules or declarations
// - any individual rule or declaration
// - any individual value
// - a complete value definition for a declaration
fn is_range_formatting_node(&self, node: &SyntaxNode<Self::SyntaxLanguage>) -> bool {
AnyCssDeclarationBlock::can_cast(node.kind())
|| AnyCssRuleBlock::can_cast(node.kind())
|| AnyCssValue::can_cast(node.kind())
|| AnyCssRule::can_cast(node.kind())
|| matches!(
node.kind(),
CssSyntaxKind::CSS_DECLARATION
| CssSyntaxKind::CSS_COMPONENT_VALUE_LIST
| CssSyntaxKind::CSS_SELECTOR_LIST
)
}
fn options(&self) -> &<Self::Context as FormatContext>::Options {
&self.options
}
fn create_context(
self,
root: &CssSyntaxNode,
source_map: Option<TransformSourceMap>,
) -> Self::Context {
let comments = Comments::from_node(root, &CssCommentStyle, source_map.as_ref());
CssFormatContext::new(self.options, comments).with_source_map(source_map)
}
}
/// Format implementation specific to CSS tokens.
///
/// This re-implementation of FormatToken allows the formatter to automatically
/// rewrite all keywords in lowercase, since they are case-insensitive. Other
/// tokens like identifiers handle lowercasing themselves.
#[derive(Default, Debug, Clone, Copy)]
pub(crate) struct FormatCssSyntaxToken;
impl FormatRule<CssSyntaxToken> for FormatCssSyntaxToken {
type Context = CssFormatContext;
fn fmt(&self, token: &CssSyntaxToken, f: &mut Formatter<Self::Context>) -> FormatResult<()> {
f.state_mut().track_token(token);
write!(f, [format_skipped_token_trivia(token)])?;
if token.kind().is_contextual_keyword() {
let original = token.text_trimmed();
match original.to_ascii_lowercase_cow() {
Cow::Borrowed(_) => write!(f, [format_trimmed_token(token)]),
Cow::Owned(lowercase) => write!(
f,
[dynamic_text(&lowercase, token.text_trimmed_range().start())]
),
}
} else {
write!(f, [format_trimmed_token(token)])
}
}
}
impl AsFormat<CssFormatContext> for CssSyntaxToken {
type Format<'a> = FormatRefWithRule<'a, CssSyntaxToken, FormatCssSyntaxToken>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(self, FormatCssSyntaxToken)
}
}
impl IntoFormat<CssFormatContext> for CssSyntaxToken {
type Format = FormatOwnedWithRule<CssSyntaxToken, FormatCssSyntaxToken>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(self, FormatCssSyntaxToken)
}
}
/// Formats a range within a file, supported by Biome
///
/// This runs a simple heuristic to determine the initial indentation
/// level of the node based on the provided [CssFormatOptions], which
/// must match currently the current initial of the file. Additionally,
/// because the reformatting happens only locally the resulting code
/// will be indented with the same level as the original selection,
/// even if it's a mismatch from the rest of the block the selection is in
///
/// It returns a [Printed] result with a range corresponding to the
/// range of the input that was effectively overwritten by the formatter
pub fn format_range(
options: CssFormatOptions,
root: &CssSyntaxNode,
range: TextRange,
) -> FormatResult<Printed> {
biome_formatter::format_range(root, range, CssFormatLanguage::new(options))
}
/// Formats a CSS syntax tree.
///
/// It returns the [Formatted] document that can be printed to a string.
pub fn format_node(
options: CssFormatOptions,
root: &CssSyntaxNode,
) -> FormatResult<Formatted<CssFormatContext>> {
biome_formatter::format_node(root, CssFormatLanguage::new(options))
}
/// Formats a single node within a file, supported by Biome.
///
/// This runs a simple heuristic to determine the initial indentation
/// level of the node based on the provided [CssFormatOptions], which
/// must match currently the current initial of the file. Additionally,
/// because the reformatting happens only locally the resulting code
/// will be indented with the same level as the original selection,
/// even if it's a mismatch from the rest of the block the selection is in
///
/// Returns the [Printed] code.
pub fn format_sub_tree(options: CssFormatOptions, root: &CssSyntaxNode) -> FormatResult<Printed> {
biome_formatter::format_sub_tree(root, CssFormatLanguage::new(options))
}
/// Whether the CSS formatter is allowed to be used at all.
///
/// Until the formatter is sufficiently ready, we're disabling its use in any
/// external projects, but we still want to be able to run test suites in the
/// meantime. This flag controls that visibility, and only returns true for
/// non-release builds.
pub const fn can_format_css_yet() -> bool {
cfg!(debug_assertions) || cfg!(feature = "format_css")
}
#[cfg(test)]
mod tests {
use crate::context::CssFormatOptions;
use crate::format_node;
use biome_css_parser::{parse_css, CssParserOptions};
#[test]
fn smoke_test() {
let src = r#"html {}"#;
let parse = parse_css(src, CssParserOptions::default());
let options = CssFormatOptions::default();
let formatted = format_node(options, &parse.syntax()).unwrap();
assert_eq!(formatted.print().unwrap().as_code(), "html {\n}\n");
}
}