diff --git a/Cargo.lock b/Cargo.lock index 9299039..a4b3d0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,9 +38,9 @@ dependencies = [ [[package]] name = "dprint-core" -version = "0.39.0" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "558e1faeec0ba98e38d61ec6c71a3bb80e68d6fde96b5663a739c79f43eba058" +checksum = "690b3f34a0c0825e5ebbc58f0f0ddf979a6f5fff32f7317886d4917507035760" dependencies = [ "bumpalo", "fnv", @@ -49,9 +49,9 @@ dependencies = [ [[package]] name = "dprint-development" -version = "0.3.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8241dcdcf5dff04c1eb8268411d127fdaf846ea4605cb2daf58351ed4ff33a36" +checksum = "11257da1cf6728775dbae962a9c65c9745e8d7c749e3e41beca18a734279630e" dependencies = [ "console", "similar", @@ -59,7 +59,7 @@ dependencies = [ [[package]] name = "dprint-plugin-markdown" -version = "0.7.1" +version = "0.8.0" dependencies = [ "dprint-core", "dprint-development", diff --git a/Cargo.toml b/Cargo.toml index c4ce130..926f367 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "dprint-plugin-markdown" description = "Markdown formatter for dprint." -version = "0.7.1" +version = "0.8.0" authors = ["David Sherret "] license = "MIT" edition = "2018" @@ -24,12 +24,12 @@ wasm = ["serde_json", "dprint-core/wasm"] tracing = ["dprint-core/tracing"] [dependencies] -dprint-core = { version = "0.39.0", features = ["formatting"] } +dprint-core = { version = "0.42.0", features = ["formatting"] } pulldown-cmark = { version = "0.8.0", default-features = false } serde = { version = "1.0.88", features = ["derive"] } serde_json = { version = "1.0", optional = true } regex = "1" [dev-dependencies] -dprint-development = "0.3.0" +dprint-development = "0.4.0" serde_json = { version = "1.0" } diff --git a/src/format_text.rs b/src/format_text.rs index eb96ed3..0b64b26 100644 --- a/src/format_text.rs +++ b/src/format_text.rs @@ -1,5 +1,6 @@ use dprint_core::formatting::*; use dprint_core::configuration::{resolve_new_line_kind}; +use dprint_core::types::ErrBox; use super::configuration::Configuration; use super::parsing::{parse_cmark_ast, parse_yaml_header, parse_node, Context, file_has_ignore_file_directive}; @@ -10,8 +11,8 @@ use super::parsing::{parse_cmark_ast, parse_yaml_header, parse_node, Context, fi pub fn format_text( file_text: &str, config: &Configuration, - format_code_block_text: Box Result>, -) -> Result { + format_code_block_text: impl FnMut(&str, &str, u32) -> Result, +) -> Result { let (source_file, markdown_text) = match parse_source_file(file_text, config)? { ParseFileResult::IgnoreFile => return Ok(file_text.to_string()), ParseFileResult::SourceFile(file) => file, diff --git a/src/parsing/parser_types.rs b/src/parsing/parser_types.rs index fd6e075..70e9f6a 100644 --- a/src/parsing/parser_types.rs +++ b/src/parsing/parser_types.rs @@ -1,5 +1,7 @@ use regex::Regex; -use super::super::configuration::Configuration; +use dprint_core::types::ErrBox; + +use crate::configuration::Configuration; use super::utils::*; pub struct Context<'a> { @@ -10,7 +12,7 @@ pub struct Context<'a> { /** The current indentation level within the file being formatted. */ pub raw_indent_level: u32, pub is_in_list_count: u32, - pub format_code_block_text: Box Result>, + pub format_code_block_text: Box Result + 'a>, pub ignore_regex: Regex, pub ignore_start_regex: Regex, pub ignore_end_regex: Regex, @@ -20,7 +22,7 @@ impl<'a> Context<'a> { pub fn new( file_text: &'a str, configuration: &'a Configuration, - format_code_block_text: Box Result> + format_code_block_text: impl FnMut(&str, &str, u32) -> Result + 'a, ) -> Context<'a> { Context { file_text, @@ -28,7 +30,7 @@ impl<'a> Context<'a> { indent_level: 0, raw_indent_level: 0, is_in_list_count: 0, - format_code_block_text, + format_code_block_text: Box::new(format_code_block_text), ignore_regex: get_ignore_comment_regex(&configuration.ignore_directive), ignore_start_regex: get_ignore_comment_regex(&configuration.ignore_start_directive), ignore_end_regex: get_ignore_comment_regex(&configuration.ignore_end_directive), @@ -39,7 +41,7 @@ impl<'a> Context<'a> { self.is_in_list_count > 0 } - pub fn format_text(&self, tag: &str, text: &str) -> Result { + pub fn format_text(&mut self, tag: &str, text: &str) -> Result { let line_width = std::cmp::max(10, self.configuration.line_width as i32 - self.indent_level as i32) as u32; (self.format_code_block_text)(tag, text, line_width) } diff --git a/src/wasm_plugin.rs b/src/wasm_plugin.rs index fe54cfe..636cfab 100644 --- a/src/wasm_plugin.rs +++ b/src/wasm_plugin.rs @@ -1,63 +1,86 @@ -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::collections::HashMap; use dprint_core::generate_plugin_code; +use dprint_core::configuration::{ConfigKeyMap, GlobalConfiguration, ResolveConfigurationResult}; +use dprint_core::plugins::{PluginHandler, PluginInfo}; +use dprint_core::types::ErrBox; use super::configuration::{Configuration, resolve_config}; -fn get_plugin_config_key() -> String { - String::from("markdown") +struct MarkdownPluginHandler { } -fn get_plugin_file_extensions() -> Vec { - vec![String::from("md")] +impl MarkdownPluginHandler { + pub const fn new() -> Self { + MarkdownPluginHandler {} + } } -fn format_text(_: &PathBuf, file_text: &str, config: &Configuration) -> Result { - return super::format_text( - file_text, - config, - Box::new(|tag, file_text, line_width| { - if let Some(ext) = tag_to_extension(tag) { - let file_path = PathBuf::from(format!("file.{}", ext)); - let mut additional_config = HashMap::new(); - additional_config.insert("lineWidth".into(), (line_width as i32).into()); - format_with_host(&file_path, file_text.to_string(), &additional_config) - } else { - Ok(file_text.to_string()) - } - }) - ); - - fn tag_to_extension(tag: &str) -> Option<&'static str> { - match tag.trim().to_lowercase().as_str() { - "typescript" | "ts" => Some("ts"), - "tsx" => Some("tsx"), - "javascript" | "js" => Some("js"), - "jsx" => Some("jsx"), - "json" | "jsonc" => Some("json"), - "rust" | "rs" => Some("rs"), - "csharp" | "cs" => Some("cs"), - "visualbasic" | "vb" => Some("vb"), - "css" => Some("css"), - "less" => Some("less"), - "toml" => Some("toml"), - "scss" => Some("scss"), - "vue" => Some("vue"), - _ => None, +impl PluginHandler for MarkdownPluginHandler { + fn resolve_config( + &mut self, + config: ConfigKeyMap, + global_config: &GlobalConfiguration, + ) -> ResolveConfigurationResult { + resolve_config(config, global_config) + } + + fn get_plugin_info(&mut self) -> PluginInfo { + PluginInfo { + name: env!("CARGO_PKG_NAME").to_string(), + version: env!("CARGO_PKG_VERSION").to_string(), + config_key: "markdown".to_string(), + file_extensions: vec!["md".to_string()], + help_url: "https://dprint.dev/plugins/markdown".to_string(), + config_schema_url: "".to_string(), // none until https://github.com/microsoft/vscode/issues/98443 is resolved } } -} -fn get_plugin_help_url() -> String { - String::from("https://dprint.dev/plugins/markdown") -} + fn get_license_text(&mut self) -> String { + std::str::from_utf8(include_bytes!("../LICENSE")).unwrap().into() + } -fn get_plugin_config_schema_url() -> String { - String::new() // none until https://github.com/microsoft/vscode/issues/98443 is resolved -} + fn format_text( + &mut self, + _file_path: &Path, + file_text: &str, + config: &Configuration, + mut format_with_host: impl FnMut(&Path, String, &ConfigKeyMap) -> Result, + ) -> Result { + return super::format_text( + file_text, + config, + |tag, file_text, line_width| { + if let Some(ext) = tag_to_extension(tag) { + let file_path = PathBuf::from(format!("file.{}", ext)); + let mut additional_config = HashMap::new(); + additional_config.insert("lineWidth".into(), (line_width as i32).into()); + format_with_host(&file_path, file_text.to_string(), &additional_config) + } else { + Ok(file_text.to_string()) + } + } + ); -fn get_plugin_license_text() -> String { - std::str::from_utf8(include_bytes!("../LICENSE")).unwrap().into() + fn tag_to_extension(tag: &str) -> Option<&'static str> { + match tag.trim().to_lowercase().as_str() { + "typescript" | "ts" => Some("ts"), + "tsx" => Some("tsx"), + "javascript" | "js" => Some("js"), + "jsx" => Some("jsx"), + "json" | "jsonc" => Some("json"), + "rust" | "rs" => Some("rs"), + "csharp" | "cs" => Some("cs"), + "visualbasic" | "vb" => Some("vb"), + "css" => Some("css"), + "less" => Some("less"), + "toml" => Some("toml"), + "scss" => Some("scss"), + "vue" => Some("vue"), + _ => None, + } + } + } } -generate_plugin_code!(); +generate_plugin_code!(MarkdownPluginHandler, MarkdownPluginHandler::new()); diff --git a/tests/newline_test.rs b/tests/newline_test.rs index dfddaa1..fe62f1b 100644 --- a/tests/newline_test.rs +++ b/tests/newline_test.rs @@ -7,7 +7,7 @@ fn test_issue22_with_carriage_return_line_feeds() { let result = format_text( &"```\r\ntest\r\n\r\ntest\r\n```\r\n", &config, - Box::new(|_, file_text, _| Ok(file_text.to_string())), + |_, file_text, _| Ok(file_text.to_string()), ).unwrap(); assert_eq!(result, "```\ntest\n\ntest\n```\n"); } @@ -18,7 +18,7 @@ fn test_issue26_with_carriage_return_line_feeds() { let result = format_text( &"Testing:\r\n\r\n```json\r\ntesting\r\n```\r\n", &config, - Box::new(|_, file_text, _| Ok(file_text.to_string())), + |_, file_text, _| Ok(file_text.to_string()), ).unwrap(); assert_eq!(result, "Testing:\n\n\n```json\ntesting\n```\n"); } @@ -31,7 +31,7 @@ fn test_issue35_convert_two_spaces_end_of_line_to_hard_break() { let result = format_text( &"testing \nasdf", &config, - Box::new(|_, file_text, _| Ok(file_text.to_string())), + |_, file_text, _| Ok(file_text.to_string()), ).unwrap(); assert_eq!(result, "testing\\\nasdf\n"); } @@ -42,7 +42,7 @@ fn test_issue35_ignore_two_spaces_before_hard_break() { let result = format_text( &"testing \\\nasdf", &config, - Box::new(|_, file_text, _| Ok(file_text.to_string())), + |_, file_text, _| Ok(file_text.to_string()), ).unwrap(); assert_eq!(result, "testing\\\nasdf\n"); } diff --git a/tests/run_specs_test.rs b/tests/run_specs_test.rs index cb060f4..d430e15 100644 --- a/tests/run_specs_test.rs +++ b/tests/run_specs_test.rs @@ -27,14 +27,14 @@ fn test_specs() { format_text( &file_text, &config_result.config, - Box::new(|tag, file_text, line_width| { + |tag, file_text, line_width| { let end = format!("_formatted_{}", line_width); if tag == "format" && !file_text.ends_with(&end) { Ok(format!("{}{}\n\n", file_text.to_string(), end)) } else { Ok(file_text.to_string()) } - }), + }, ) } }, @@ -46,14 +46,14 @@ fn test_specs() { return serde_json::to_string(&trace_file( &_file_text, &config_result.config, - Box::new(|tag, file_text, line_width| { + |tag, file_text, line_width| { let end = format!("_formatted_{}", line_width); if tag == "format" && !file_text.ends_with(&end) { Ok(format!("{}{}\n\n", file_text.to_string(), end)) } else { Ok(file_text.to_string()) } - }), + }, )).unwrap(); }