|
1 |
| -use std::path::PathBuf; |
| 1 | +use std::path::{Path, PathBuf}; |
2 | 2 | use std::collections::HashMap;
|
3 | 3 |
|
4 | 4 | use dprint_core::generate_plugin_code;
|
| 5 | +use dprint_core::configuration::{ConfigKeyMap, GlobalConfiguration, ResolveConfigurationResult}; |
| 6 | +use dprint_core::plugins::{PluginHandler, PluginInfo}; |
| 7 | +use dprint_core::types::ErrBox; |
5 | 8 | use super::configuration::{Configuration, resolve_config};
|
6 | 9 |
|
7 |
| -fn get_plugin_config_key() -> String { |
8 |
| - String::from("markdown") |
| 10 | +struct MarkdownPluginHandler { |
9 | 11 | }
|
10 | 12 |
|
11 |
| -fn get_plugin_file_extensions() -> Vec<String> { |
12 |
| - vec![String::from("md")] |
| 13 | +impl MarkdownPluginHandler { |
| 14 | + pub const fn new() -> Self { |
| 15 | + MarkdownPluginHandler {} |
| 16 | + } |
13 | 17 | }
|
14 | 18 |
|
15 |
| -fn format_text(_: &PathBuf, file_text: &str, config: &Configuration) -> Result<String, String> { |
16 |
| - return super::format_text( |
17 |
| - file_text, |
18 |
| - config, |
19 |
| - Box::new(|tag, file_text, line_width| { |
20 |
| - if let Some(ext) = tag_to_extension(tag) { |
21 |
| - let file_path = PathBuf::from(format!("file.{}", ext)); |
22 |
| - let mut additional_config = HashMap::new(); |
23 |
| - additional_config.insert("lineWidth".into(), (line_width as i32).into()); |
24 |
| - format_with_host(&file_path, file_text.to_string(), &additional_config) |
25 |
| - } else { |
26 |
| - Ok(file_text.to_string()) |
27 |
| - } |
28 |
| - }) |
29 |
| - ); |
30 |
| - |
31 |
| - fn tag_to_extension(tag: &str) -> Option<&'static str> { |
32 |
| - match tag.trim().to_lowercase().as_str() { |
33 |
| - "typescript" | "ts" => Some("ts"), |
34 |
| - "tsx" => Some("tsx"), |
35 |
| - "javascript" | "js" => Some("js"), |
36 |
| - "jsx" => Some("jsx"), |
37 |
| - "json" | "jsonc" => Some("json"), |
38 |
| - "rust" | "rs" => Some("rs"), |
39 |
| - "csharp" | "cs" => Some("cs"), |
40 |
| - "visualbasic" | "vb" => Some("vb"), |
41 |
| - "css" => Some("css"), |
42 |
| - "less" => Some("less"), |
43 |
| - "toml" => Some("toml"), |
44 |
| - "scss" => Some("scss"), |
45 |
| - "vue" => Some("vue"), |
46 |
| - _ => None, |
| 19 | +impl PluginHandler<Configuration> for MarkdownPluginHandler { |
| 20 | + fn resolve_config( |
| 21 | + &mut self, |
| 22 | + config: ConfigKeyMap, |
| 23 | + global_config: &GlobalConfiguration, |
| 24 | + ) -> ResolveConfigurationResult<Configuration> { |
| 25 | + resolve_config(config, global_config) |
| 26 | + } |
| 27 | + |
| 28 | + fn get_plugin_info(&mut self) -> PluginInfo { |
| 29 | + PluginInfo { |
| 30 | + name: env!("CARGO_PKG_NAME").to_string(), |
| 31 | + version: env!("CARGO_PKG_VERSION").to_string(), |
| 32 | + config_key: "markdown".to_string(), |
| 33 | + file_extensions: vec!["md".to_string()], |
| 34 | + help_url: "https://dprint.dev/plugins/markdown".to_string(), |
| 35 | + config_schema_url: "".to_string(), // none until https://github.com/microsoft/vscode/issues/98443 is resolved |
47 | 36 | }
|
48 | 37 | }
|
49 |
| -} |
50 | 38 |
|
51 |
| -fn get_plugin_help_url() -> String { |
52 |
| - String::from("https://dprint.dev/plugins/markdown") |
53 |
| -} |
| 39 | + fn get_license_text(&mut self) -> String { |
| 40 | + std::str::from_utf8(include_bytes!("../LICENSE")).unwrap().into() |
| 41 | + } |
54 | 42 |
|
55 |
| -fn get_plugin_config_schema_url() -> String { |
56 |
| - String::new() // none until https://github.com/microsoft/vscode/issues/98443 is resolved |
57 |
| -} |
| 43 | + fn format_text( |
| 44 | + &mut self, |
| 45 | + _file_path: &Path, |
| 46 | + file_text: &str, |
| 47 | + config: &Configuration, |
| 48 | + mut format_with_host: impl FnMut(&Path, String, &ConfigKeyMap) -> Result<String, ErrBox>, |
| 49 | + ) -> Result<String, ErrBox> { |
| 50 | + return super::format_text( |
| 51 | + file_text, |
| 52 | + config, |
| 53 | + |tag, file_text, line_width| { |
| 54 | + if let Some(ext) = tag_to_extension(tag) { |
| 55 | + let file_path = PathBuf::from(format!("file.{}", ext)); |
| 56 | + let mut additional_config = HashMap::new(); |
| 57 | + additional_config.insert("lineWidth".into(), (line_width as i32).into()); |
| 58 | + format_with_host(&file_path, file_text.to_string(), &additional_config) |
| 59 | + } else { |
| 60 | + Ok(file_text.to_string()) |
| 61 | + } |
| 62 | + } |
| 63 | + ); |
58 | 64 |
|
59 |
| -fn get_plugin_license_text() -> String { |
60 |
| - std::str::from_utf8(include_bytes!("../LICENSE")).unwrap().into() |
| 65 | + fn tag_to_extension(tag: &str) -> Option<&'static str> { |
| 66 | + match tag.trim().to_lowercase().as_str() { |
| 67 | + "typescript" | "ts" => Some("ts"), |
| 68 | + "tsx" => Some("tsx"), |
| 69 | + "javascript" | "js" => Some("js"), |
| 70 | + "jsx" => Some("jsx"), |
| 71 | + "json" | "jsonc" => Some("json"), |
| 72 | + "rust" | "rs" => Some("rs"), |
| 73 | + "csharp" | "cs" => Some("cs"), |
| 74 | + "visualbasic" | "vb" => Some("vb"), |
| 75 | + "css" => Some("css"), |
| 76 | + "less" => Some("less"), |
| 77 | + "toml" => Some("toml"), |
| 78 | + "scss" => Some("scss"), |
| 79 | + "vue" => Some("vue"), |
| 80 | + _ => None, |
| 81 | + } |
| 82 | + } |
| 83 | + } |
61 | 84 | }
|
62 | 85 |
|
63 |
| -generate_plugin_code!(); |
| 86 | +generate_plugin_code!(MarkdownPluginHandler, MarkdownPluginHandler::new()); |
0 commit comments