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 use_tabs: global_config.use_tabs,
43 };
44
45 if let Some(value) = config
46 .get("indentWidth")
47 .and_then(|value| value.as_number())
48 {
49 resolved.indent_width = Some(value as usize);
50 }
51
52 if let Some(value) = config.get("useTabs").and_then(|value| value.as_bool()) {
53 resolved.use_tabs = Some(value);
54 }
55
56 PluginResolveConfigurationResult {
57 config: resolved,
58 diagnostics: Vec::new(),
59 file_matching: FileMatchingInfo {
60 file_extensions: vec![String::from("pug")],
61 file_names: Vec::new(),
62 },
63 }
64 }
65
66 fn license_text(&mut self) -> String {
67 String::from("MIT")
68 }
69
70 fn check_config_updates(
71 &self,
72 _message: CheckConfigUpdatesMessage,
73 ) -> anyhow::Result<Vec<ConfigChange>> {
74 Ok(Vec::new())
75 }
76
77 fn format(
78 &mut self,
79 request: SyncFormatRequest<Configuration>,
80 _format_with_host: impl FnMut(SyncHostFormatRequest) -> dprint_core::plugins::FormatResult,
81 ) -> dprint_core::plugins::FormatResult {
82 let file_text = String::from_utf8(request.file_bytes)?;
83 let lexed = lexer::lex(&file_text);
84 let document = parser::parse(&lexed);
85 let formatted = formatter::format(&document, request.config);
86
87 if formatted == file_text {
88 Ok(None)
89 } else {
90 Ok(Some(formatted.into_bytes()))
91 }
92 }
93}
94
95#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
96generate_plugin_code!(PugPluginHandler, PugPluginHandler);