Skip to content

Commit 63e5efe

Browse files
chore(release): 0.8.0 - Upgrade dprint-core to 0.42.0 (#44)
* 0.8.0 * Upgrade to use new PluginHandler trait. Co-authored-by: David Sherret <[email protected]>
1 parent 2946499 commit 63e5efe

File tree

7 files changed

+96
-70
lines changed

7 files changed

+96
-70
lines changed

Cargo.lock

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "dprint-plugin-markdown"
33
description = "Markdown formatter for dprint."
4-
version = "0.7.1"
4+
version = "0.8.0"
55
authors = ["David Sherret <[email protected]>"]
66
license = "MIT"
77
edition = "2018"
@@ -24,12 +24,12 @@ wasm = ["serde_json", "dprint-core/wasm"]
2424
tracing = ["dprint-core/tracing"]
2525

2626
[dependencies]
27-
dprint-core = { version = "0.39.0", features = ["formatting"] }
27+
dprint-core = { version = "0.42.0", features = ["formatting"] }
2828
pulldown-cmark = { version = "0.8.0", default-features = false }
2929
serde = { version = "1.0.88", features = ["derive"] }
3030
serde_json = { version = "1.0", optional = true }
3131
regex = "1"
3232

3333
[dev-dependencies]
34-
dprint-development = "0.3.0"
34+
dprint-development = "0.4.0"
3535
serde_json = { version = "1.0" }

src/format_text.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use dprint_core::formatting::*;
22
use dprint_core::configuration::{resolve_new_line_kind};
3+
use dprint_core::types::ErrBox;
34

45
use super::configuration::Configuration;
56
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
1011
pub fn format_text(
1112
file_text: &str,
1213
config: &Configuration,
13-
format_code_block_text: Box<dyn Fn(&str, &str, u32) -> Result<String, String>>,
14-
) -> Result<String, String> {
14+
format_code_block_text: impl FnMut(&str, &str, u32) -> Result<String, ErrBox>,
15+
) -> Result<String, ErrBox> {
1516
let (source_file, markdown_text) = match parse_source_file(file_text, config)? {
1617
ParseFileResult::IgnoreFile => return Ok(file_text.to_string()),
1718
ParseFileResult::SourceFile(file) => file,

src/parsing/parser_types.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use regex::Regex;
2-
use super::super::configuration::Configuration;
2+
use dprint_core::types::ErrBox;
3+
4+
use crate::configuration::Configuration;
35
use super::utils::*;
46

57
pub struct Context<'a> {
@@ -10,7 +12,7 @@ pub struct Context<'a> {
1012
/** The current indentation level within the file being formatted. */
1113
pub raw_indent_level: u32,
1214
pub is_in_list_count: u32,
13-
pub format_code_block_text: Box<dyn Fn(&str, &str, u32) -> Result<String, String>>,
15+
pub format_code_block_text: Box<dyn FnMut(&str, &str, u32) -> Result<String, ErrBox> + 'a>,
1416
pub ignore_regex: Regex,
1517
pub ignore_start_regex: Regex,
1618
pub ignore_end_regex: Regex,
@@ -20,15 +22,15 @@ impl<'a> Context<'a> {
2022
pub fn new(
2123
file_text: &'a str,
2224
configuration: &'a Configuration,
23-
format_code_block_text: Box<dyn Fn(&str, &str, u32) -> Result<String, String>>
25+
format_code_block_text: impl FnMut(&str, &str, u32) -> Result<String, ErrBox> + 'a,
2426
) -> Context<'a> {
2527
Context {
2628
file_text,
2729
configuration,
2830
indent_level: 0,
2931
raw_indent_level: 0,
3032
is_in_list_count: 0,
31-
format_code_block_text,
33+
format_code_block_text: Box::new(format_code_block_text),
3234
ignore_regex: get_ignore_comment_regex(&configuration.ignore_directive),
3335
ignore_start_regex: get_ignore_comment_regex(&configuration.ignore_start_directive),
3436
ignore_end_regex: get_ignore_comment_regex(&configuration.ignore_end_directive),
@@ -39,7 +41,7 @@ impl<'a> Context<'a> {
3941
self.is_in_list_count > 0
4042
}
4143

42-
pub fn format_text(&self, tag: &str, text: &str) -> Result<String, String> {
44+
pub fn format_text(&mut self, tag: &str, text: &str) -> Result<String, ErrBox> {
4345
let line_width = std::cmp::max(10, self.configuration.line_width as i32 - self.indent_level as i32) as u32;
4446
(self.format_code_block_text)(tag, text, line_width)
4547
}

src/wasm_plugin.rs

+70-47
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,86 @@
1-
use std::path::PathBuf;
1+
use std::path::{Path, PathBuf};
22
use std::collections::HashMap;
33

44
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;
58
use super::configuration::{Configuration, resolve_config};
69

7-
fn get_plugin_config_key() -> String {
8-
String::from("markdown")
10+
struct MarkdownPluginHandler {
911
}
1012

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+
}
1317
}
1418

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
4736
}
4837
}
49-
}
5038

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+
}
5442

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+
);
5864

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+
}
6184
}
6285

63-
generate_plugin_code!();
86+
generate_plugin_code!(MarkdownPluginHandler, MarkdownPluginHandler::new());

tests/newline_test.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn test_issue22_with_carriage_return_line_feeds() {
77
let result = format_text(
88
&"```\r\ntest\r\n\r\ntest\r\n```\r\n",
99
&config,
10-
Box::new(|_, file_text, _| Ok(file_text.to_string())),
10+
|_, file_text, _| Ok(file_text.to_string()),
1111
).unwrap();
1212
assert_eq!(result, "```\ntest\n\ntest\n```\n");
1313
}
@@ -18,7 +18,7 @@ fn test_issue26_with_carriage_return_line_feeds() {
1818
let result = format_text(
1919
&"Testing:\r\n<!-- dprint-ignore -->\r\n```json\r\ntesting\r\n```\r\n",
2020
&config,
21-
Box::new(|_, file_text, _| Ok(file_text.to_string())),
21+
|_, file_text, _| Ok(file_text.to_string()),
2222
).unwrap();
2323
assert_eq!(result, "Testing:\n\n<!-- dprint-ignore -->\n```json\ntesting\n```\n");
2424
}
@@ -31,7 +31,7 @@ fn test_issue35_convert_two_spaces_end_of_line_to_hard_break() {
3131
let result = format_text(
3232
&"testing \nasdf",
3333
&config,
34-
Box::new(|_, file_text, _| Ok(file_text.to_string())),
34+
|_, file_text, _| Ok(file_text.to_string()),
3535
).unwrap();
3636
assert_eq!(result, "testing\\\nasdf\n");
3737
}
@@ -42,7 +42,7 @@ fn test_issue35_ignore_two_spaces_before_hard_break() {
4242
let result = format_text(
4343
&"testing \\\nasdf",
4444
&config,
45-
Box::new(|_, file_text, _| Ok(file_text.to_string())),
45+
|_, file_text, _| Ok(file_text.to_string()),
4646
).unwrap();
4747
assert_eq!(result, "testing\\\nasdf\n");
4848
}

tests/run_specs_test.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ fn test_specs() {
2727
format_text(
2828
&file_text,
2929
&config_result.config,
30-
Box::new(|tag, file_text, line_width| {
30+
|tag, file_text, line_width| {
3131
let end = format!("_formatted_{}", line_width);
3232
if tag == "format" && !file_text.ends_with(&end) {
3333
Ok(format!("{}{}\n\n", file_text.to_string(), end))
3434
} else {
3535
Ok(file_text.to_string())
3636
}
37-
}),
37+
},
3838
)
3939
}
4040
},
@@ -46,14 +46,14 @@ fn test_specs() {
4646
return serde_json::to_string(&trace_file(
4747
&_file_text,
4848
&config_result.config,
49-
Box::new(|tag, file_text, line_width| {
49+
|tag, file_text, line_width| {
5050
let end = format!("_formatted_{}", line_width);
5151
if tag == "format" && !file_text.ends_with(&end) {
5252
Ok(format!("{}{}\n\n", file_text.to_string(), end))
5353
} else {
5454
Ok(file_text.to_string())
5555
}
56-
}),
56+
},
5757
)).unwrap();
5858
}
5959

0 commit comments

Comments
 (0)