|
1 |
| -use rustfmt_nightly::{Input, Session, Config, NewlineStyle, EmitMode, Edition}; |
| 1 | +pub mod config; |
| 2 | +mod format; |
2 | 3 |
|
3 |
| -use std::path::PathBuf; |
4 |
| -use std::collections::HashMap; |
5 |
| -use serde::{Serialize, Deserialize}; |
6 |
| -use dprint_core::generate_plugin_code; |
7 |
| -use dprint_core::configuration::{GlobalConfiguration, ResolveConfigurationResult, NewLineKind, ConfigurationDiagnostic}; |
| 4 | +pub use format::*; |
8 | 5 |
|
9 |
| -#[derive(Clone, Serialize, Deserialize)] |
10 |
| -struct Configuration { |
11 |
| - // Unfortunately no resolved configuration at the moment because serializing |
12 |
| - // rustfmt's PartialConfig configuration kept causing a panic |
13 |
| - #[serde(flatten)] |
14 |
| - config: HashMap<String, String>, |
15 |
| - #[serde(skip_serializing, skip_deserializing)] |
16 |
| - rustfmt_config: Config, |
17 |
| -} |
| 6 | +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] |
| 7 | +mod wasm_plugin; |
18 | 8 |
|
19 |
| -fn resolve_config( |
20 |
| - config: HashMap<String, String>, |
21 |
| - global_config: &GlobalConfiguration, |
22 |
| -) -> ResolveConfigurationResult<Configuration> { |
23 |
| - let mut rustfmt_config = Config::default(); |
24 |
| - let mut diagnostics = Vec::new(); |
25 |
| - |
26 |
| - rustfmt_config.set().edition(Edition::Edition2018); |
27 |
| - |
28 |
| - // set dprint global configuration |
29 |
| - if let Some(line_width) = global_config.line_width { |
30 |
| - rustfmt_config.set().max_width(line_width as usize); |
31 |
| - } |
32 |
| - if let Some(use_tabs) = global_config.use_tabs { |
33 |
| - rustfmt_config.set().hard_tabs(use_tabs); |
34 |
| - } |
35 |
| - if let Some(indent_width) = global_config.indent_width { |
36 |
| - rustfmt_config.set().tab_spaces(indent_width as usize); |
37 |
| - } |
38 |
| - if let Some(new_line_kind) = global_config.new_line_kind { |
39 |
| - rustfmt_config.set().newline_style(match new_line_kind { |
40 |
| - NewLineKind::Auto => NewlineStyle::Auto, |
41 |
| - NewLineKind::LineFeed => NewlineStyle::Unix, |
42 |
| - NewLineKind::CarriageReturnLineFeed => NewlineStyle::Windows, |
43 |
| - NewLineKind::System => NewlineStyle::Native, |
44 |
| - }); |
45 |
| - } |
46 |
| - |
47 |
| - for (key, value) in config.iter() { |
48 |
| - if key == "newLineKind" { |
49 |
| - match value.as_str() { |
50 |
| - "auto" => rustfmt_config.set().newline_style(NewlineStyle::Auto), |
51 |
| - "lf" => rustfmt_config.set().newline_style(NewlineStyle::Unix), |
52 |
| - "crlf" => rustfmt_config.set().newline_style(NewlineStyle::Windows), |
53 |
| - "system" => rustfmt_config.set().newline_style(NewlineStyle::Native), |
54 |
| - _ => { |
55 |
| - diagnostics.push(ConfigurationDiagnostic { |
56 |
| - property_name: String::from(key), |
57 |
| - message: format!("Invalid newline kind: {}", value), |
58 |
| - }); |
59 |
| - } |
60 |
| - } |
61 |
| - continue; |
62 |
| - } |
63 |
| - |
64 |
| - let key = match key.as_str() { |
65 |
| - "lineWidth" => "max_width", |
66 |
| - "useTabs" => "hard_tabs", |
67 |
| - "indentWidth" => "tab_spaces", |
68 |
| - _ => key, |
69 |
| - }; |
70 |
| - if Config::is_valid_key_val(key, value) { |
71 |
| - rustfmt_config.override_value(key, value); |
72 |
| - } else { |
73 |
| - let message = format!("Invalid key or value in configuration. Key: {}, Value: {}", key, value); |
74 |
| - diagnostics.push(ConfigurationDiagnostic { |
75 |
| - property_name: String::from(key), |
76 |
| - message, |
77 |
| - }); |
78 |
| - } |
79 |
| - } |
80 |
| - |
81 |
| - rustfmt_config.set().emit_mode(EmitMode::Stdout); |
82 |
| - |
83 |
| - ResolveConfigurationResult { |
84 |
| - diagnostics, |
85 |
| - config: Configuration { config, rustfmt_config }, |
86 |
| - } |
87 |
| -} |
88 |
| - |
89 |
| -fn get_plugin_config_key() -> String { |
90 |
| - // return the JSON object key name used in the configuration file |
91 |
| - String::from("rustfmt") |
92 |
| -} |
93 |
| - |
94 |
| -fn get_plugin_file_extensions() -> Vec<String> { |
95 |
| - vec![String::from("rs")] |
96 |
| -} |
97 |
| - |
98 |
| -fn get_plugin_help_url() -> String { |
99 |
| - String::from("https://dprint.dev/plugins/rustfmt") |
100 |
| -} |
101 |
| - |
102 |
| -fn get_plugin_config_schema_url() -> String { |
103 |
| - // for now, return an empty string. Return a schema url once VSCode |
104 |
| - // supports $schema properties in descendant objects: |
105 |
| - // https://github.com/microsoft/vscode/issues/98443 |
106 |
| - String::new() |
107 |
| -} |
108 |
| - |
109 |
| -fn get_plugin_license_text() -> String { |
110 |
| - std::str::from_utf8(include_bytes!("../LICENSE")).unwrap().into() |
111 |
| -} |
112 |
| - |
113 |
| -fn format_text( |
114 |
| - _: &PathBuf, |
115 |
| - file_text: &str, |
116 |
| - config: &Configuration, |
117 |
| -) -> Result<String, String> { |
118 |
| - let mut out = Vec::new(); |
119 |
| - { |
120 |
| - let input = Input::Text(String::from(file_text)); |
121 |
| - let mut session = Session::new(config.rustfmt_config.clone(), Some(&mut out)); |
122 |
| - match session.format(input) { |
123 |
| - Err(err) => { |
124 |
| - return Err(err.to_string()); |
125 |
| - }, |
126 |
| - _ => { |
127 |
| - // do nothing |
128 |
| - } |
129 |
| - } |
130 |
| - } |
131 |
| - |
132 |
| - // rustfmt adds this prefix, so just ignore it |
133 |
| - let prefix = "stdin:\n\n"; |
134 |
| - Ok(String::from(std::str::from_utf8(&out[prefix.len()..]).unwrap())) |
135 |
| -} |
136 |
| - |
137 |
| -generate_plugin_code!(); |
| 9 | +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] |
| 10 | +pub use wasm_plugin::*; |
0 commit comments