Skip to main content

dprint_plugin_pug/
lib.rs

1#![cfg_attr(
2    not(all(target_arch = "wasm32", target_os = "unknown")),
3    allow(dead_code)
4)]
5
6mod ast;
7mod config;
8mod formatter;
9mod lexer;
10mod parser;
11
12use config::Configuration;
13use dprint_core::configuration::{ConfigKeyMap, GlobalConfiguration};
14#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
15use dprint_core::generate_plugin_code;
16use dprint_core::plugins::{
17    CheckConfigUpdatesMessage, ConfigChange, FileMatchingInfo, PluginInfo,
18    PluginResolveConfigurationResult, SyncFormatRequest, SyncHostFormatRequest, SyncPluginHandler,
19};
20
21struct PugPluginHandler;
22
23impl SyncPluginHandler<Configuration> for PugPluginHandler {
24    fn plugin_info(&mut self) -> PluginInfo {
25        PluginInfo {
26            name: String::from("pug"),
27            version: env!("CARGO_PKG_VERSION").to_string(),
28            config_key: String::from("pug"),
29            help_url: String::from("https://pugjs.org/"),
30            config_schema_url: String::new(),
31            update_url: None,
32        }
33    }
34
35    fn resolve_config(
36        &mut self,
37        config: ConfigKeyMap,
38        global_config: &GlobalConfiguration,
39    ) -> PluginResolveConfigurationResult<Configuration> {
40        let mut resolved = Configuration {
41            indent_width: global_config.indent_width.map(|value| value as usize),
42            line_width: global_config.line_width.map(|value| value as usize),
43            quote_style: None,
44            use_tabs: global_config.use_tabs,
45        };
46
47        if let Some(value) = config
48            .get("indentWidth")
49            .and_then(|value| value.as_number())
50        {
51            resolved.indent_width = Some(value as usize);
52        }
53
54        if let Some(value) = config.get("lineWidth").and_then(|value| value.as_number()) {
55            resolved.line_width = Some(value as usize);
56        }
57
58        if let Some(value) = config.get("quoteStyle").and_then(|value| value.as_string()) {
59            resolved.quote_style = match value.as_str() {
60                "double" => Some(crate::ast::QuoteStyle::Double),
61                "single" => Some(crate::ast::QuoteStyle::Single),
62                _ => None,
63            };
64        }
65
66        if let Some(value) = config.get("useTabs").and_then(|value| value.as_bool()) {
67            resolved.use_tabs = Some(value);
68        }
69
70        PluginResolveConfigurationResult {
71            config: resolved,
72            diagnostics: Vec::new(),
73            file_matching: FileMatchingInfo {
74                file_extensions: vec![String::from("pug")],
75                file_names: Vec::new(),
76            },
77        }
78    }
79
80    fn license_text(&mut self) -> String {
81        String::from("MIT")
82    }
83
84    fn check_config_updates(
85        &self,
86        _message: CheckConfigUpdatesMessage,
87    ) -> anyhow::Result<Vec<ConfigChange>> {
88        Ok(Vec::new())
89    }
90
91    fn format(
92        &mut self,
93        request: SyncFormatRequest<Configuration>,
94        _format_with_host: impl FnMut(SyncHostFormatRequest) -> dprint_core::plugins::FormatResult,
95    ) -> dprint_core::plugins::FormatResult {
96        let file_text = String::from_utf8(request.file_bytes)?;
97        let lexed = lexer::lex(&file_text);
98        let document = parser::parse(&lexed);
99        let formatted = formatter::format(&document, request.config);
100
101        if formatted == file_text {
102            Ok(None)
103        } else {
104            Ok(Some(formatted.into_bytes()))
105        }
106    }
107}
108
109#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
110generate_plugin_code!(PugPluginHandler, PugPluginHandler);