Skip to main content

lax_css/
format_text.rs

1use std::path::Path;
2
3use anyhow::Result;
4use dprint_core::configuration::resolve_new_line_kind;
5use dprint_core::formatting::PrintOptions;
6
7use crate::configuration::Configuration;
8use crate::generation;
9
10pub fn format_text(_path: &Path, text: &str, config: &Configuration) -> Result<Option<String>> {
11  let result = format_text_inner(text, config)?;
12  if result == text { Ok(None) } else { Ok(Some(result)) }
13}
14
15fn format_text_inner(text: &str, config: &Configuration) -> Result<String> {
16  let text = text.strip_prefix('\u{FEFF}').unwrap_or(text);
17  let tokens = generation::tokenize(text);
18  if has_ignore_file_comment(&tokens, &config.ignore_file_comment_text) {
19    return Ok(text.to_string());
20  }
21  let statements = generation::parse(&tokens, text);
22  if statements.is_empty() {
23    return Ok(String::new());
24  }
25  if config.single_line {
26    // keep the newline-terminated contract of the block formatter; consumers
27    // that splice the result into an inline context (e.g. a style attribute)
28    // trim it
29    let mut out = generation::generate_inline(&statements, text);
30    out.push('\n');
31    return Ok(out);
32  }
33  Ok(dprint_core::formatting::format(
34    || generation::generate(&statements, text, config),
35    PrintOptions {
36      indent_width: config.indent_width,
37      max_width: config.line_width,
38      use_tabs: config.use_tabs,
39      new_line_text: resolve_new_line_kind(text, config.new_line_kind),
40    },
41  ))
42}
43
44fn has_ignore_file_comment(tokens: &[generation::Token], directive: &str) -> bool {
45  lax_core::has_ignore_file_comment(
46    tokens.iter().map(|token| match token.kind {
47      generation::TokenKind::Whitespace { newlines } => lax_core::HeaderToken::Whitespace { newlines },
48      generation::TokenKind::LineComment | generation::TokenKind::BlockComment => {
49        lax_core::HeaderToken::Comment(token.text)
50      }
51      _ => lax_core::HeaderToken::Other,
52    }),
53    directive,
54  )
55}