lax_markup/
format_text.rs1use 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 events = generation::tokenize(text);
18 if has_ignore_file_comment(&events, &config.ignore_file_comment_text) {
19 return Ok(text.to_string());
20 }
21 let nodes = generation::parse(events);
22 if nodes.is_empty() {
23 return Ok(String::new());
24 }
25 let formatted = dprint_core::formatting::format(
26 || generation::generate(&nodes, text, config),
27 PrintOptions {
28 indent_width: config.indent_width,
29 max_width: config.line_width,
30 use_tabs: config.use_tabs,
31 new_line_text: resolve_new_line_kind(text, config.new_line_kind),
32 },
33 );
34 Ok(format!("{}\n", formatted.trim_end()))
37}
38
39fn has_ignore_file_comment(events: &[generation::Event], directive: &str) -> bool {
40 lax_core::has_ignore_file_comment(
41 events.iter().map(|event| match &event.kind {
42 generation::EventKind::Whitespace { newlines } => lax_core::HeaderToken::Whitespace { newlines: *newlines },
43 generation::EventKind::Comment { text } => lax_core::HeaderToken::Comment(text),
44 _ => lax_core::HeaderToken::Other,
45 }),
46 directive,
47 )
48}