Skip to content

Commit 59019a8

Browse files
committed
chore: upgrade to dprint-core 0.54.1
1 parent d8b0658 commit 59019a8

8 files changed

+53
-60
lines changed

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dprint-plugin-markdown"
3-
version = "0.12.2"
3+
version = "0.13.0"
44
authors = ["David Sherret <[email protected]>"]
55
edition = "2021"
66
homepage = "https://github.com/dprint/dprint-plugin-markdown"
@@ -25,12 +25,12 @@ tracing = ["dprint-core/tracing"]
2525

2626
[dependencies]
2727
anyhow = "1.0.56"
28-
dprint-core = { version = "0.51.0", features = ["formatting"] }
28+
dprint-core = { version = "0.54.1", features = ["formatting"] }
2929
pulldown-cmark = { version = "0.9.1", default-features = false }
3030
regex = "1"
3131
serde = { version = "1.0.136", features = ["derive"] }
3232
serde_json = { version = "1.0", optional = true }
3333

3434
[dev-dependencies]
35-
dprint-development = "0.7.0"
35+
dprint-development = "0.8.0"
3636
serde_json = { version = "1.0" }

dprint.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"**/target"
1313
],
1414
"plugins": [
15-
"https://plugins.dprint.dev/typescript-0.64.2.wasm",
15+
"https://plugins.dprint.dev/typescript-0.65.1.wasm",
1616
"https://plugins.dprint.dev/json-0.14.1.wasm",
1717
"https://plugins.dprint.dev/markdown-0.12.2.wasm",
1818
"https://plugins.dprint.dev/toml-0.5.4.wasm",
19-
"https://plugins.dprint.dev/rustfmt-0.4.0.exe-plugin@c6bb223ef6e5e87580177f6461a0ab0554ac9ea6b54f78ea7ae8bf63b14f5bc2"
19+
"https://plugins.dprint.dev/rustfmt-0.6.1.exe-plugin@99b89a0599fd3a63e597e03436862157901f3facae2f0c2fbd0b9f656cdbc2a5"
2020
]
2121
}

src/format_text.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use std::borrow::Cow;
2-
31
use anyhow::bail;
42
use anyhow::Result;
53
use dprint_core::configuration::resolve_new_line_kind;
64
use dprint_core::formatting::*;
5+
use dprint_core::plugins::FormatResult;
76

87
use super::configuration::Configuration;
98
use super::generation::file_has_ignore_file_directive;
@@ -15,17 +14,13 @@ use super::generation::Context;
1514
/// Formats a file.
1615
///
1716
/// Returns the file text or an error when it failed to parse.
18-
pub fn format_text(
19-
file_text: &str,
20-
config: &Configuration,
21-
format_code_block_text: impl for<'a> FnMut(&str, &'a str, u32) -> Result<Cow<'a, str>>,
22-
) -> Result<String> {
17+
pub fn format_text(file_text: &str, config: &Configuration, format_code_block_text: impl for<'a> FnMut(&str, &'a str, u32) -> FormatResult) -> FormatResult {
2318
let (source_file, markdown_text) = match parse_source_file(file_text, config)? {
24-
ParseFileResult::IgnoreFile => return Ok(file_text.to_string()),
19+
ParseFileResult::IgnoreFile => return Ok(None),
2520
ParseFileResult::SourceFile(file) => file,
2621
};
2722

28-
Ok(dprint_core::formatting::format(
23+
let result = dprint_core::formatting::format(
2924
|| {
3025
let mut context = Context::new(markdown_text, config, format_code_block_text);
3126
#[allow(clippy::let_and_return)]
@@ -34,23 +29,28 @@ pub fn format_text(
3429
print_items
3530
},
3631
config_to_print_options(file_text, config),
37-
))
32+
);
33+
if result == file_text {
34+
Ok(None)
35+
} else {
36+
Ok(Some(result))
37+
}
3838
}
3939

4040
#[cfg(feature = "tracing")]
4141
pub fn trace_file(
4242
file_text: &str,
4343
config: &Configuration,
44-
format_code_block_text: Box<dyn Fn(&str, &str, u32) -> Result<String>>,
44+
format_code_block_text: impl for<'a> FnMut(&str, &'a str, u32) -> FormatResult,
4545
) -> dprint_core::formatting::TracingResult {
4646
let (source_file, markdown_text) = match parse_source_file(file_text, config).unwrap() {
4747
ParseFileResult::IgnoreFile => panic!("Cannot trace file because it has an ignore file comment."),
4848
ParseFileResult::SourceFile(file) => file,
4949
};
5050
dprint_core::formatting::trace_printing(
5151
|| {
52-
let mut context = Context::new(&markdown_text, config, format_code_block_text);
53-
let print_items = parse_node(&source_file.into(), &mut context);
52+
let mut context = Context::new(markdown_text, config, format_code_block_text);
53+
let print_items = generate(&source_file.into(), &mut context);
5454
// println!("{}", print_items.get_as_text());
5555
print_items
5656
},

src/generation/gen_types.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use std::borrow::Cow;
2-
3-
use anyhow::Result;
1+
use dprint_core::plugins::FormatResult;
42
use regex::Regex;
53

64
use super::utils::*;
@@ -15,7 +13,7 @@ pub struct Context<'a> {
1513
pub raw_indent_level: u32,
1614
is_in_list_count: u32,
1715
text_wrap_disabled_count: u32,
18-
pub format_code_block_text: Box<dyn for<'b> FnMut(&str, &'b str, u32) -> Result<Cow<'b, str>> + 'a>,
16+
pub format_code_block_text: Box<dyn for<'b> FnMut(&str, &'b str, u32) -> FormatResult + 'a>,
1917
pub ignore_regex: Regex,
2018
pub ignore_start_regex: Regex,
2119
pub ignore_end_regex: Regex,
@@ -25,7 +23,7 @@ impl<'a> Context<'a> {
2523
pub fn new(
2624
file_text: &'a str,
2725
configuration: &'a Configuration,
28-
format_code_block_text: impl for<'b> FnMut(&str, &'b str, u32) -> Result<Cow<'b, str>> + 'a,
26+
format_code_block_text: impl for<'b> FnMut(&str, &'b str, u32) -> FormatResult + 'a,
2927
) -> Context<'a> {
3028
Context {
3129
file_text,
@@ -63,7 +61,7 @@ impl<'a> Context<'a> {
6361
self.text_wrap_disabled_count > 0
6462
}
6563

66-
pub fn format_text<'b>(&mut self, tag: &str, text: &'b str) -> Result<Cow<'b, str>> {
64+
pub fn format_text<'b>(&mut self, tag: &str, text: &'b str) -> FormatResult {
6765
let line_width = std::cmp::max(10, self.configuration.line_width as i32 - self.indent_level as i32) as u32;
6866
(self.format_code_block_text)(tag, text, line_width)
6967
}

src/generation/generate.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use dprint_core::formatting::conditions::*;
77
use dprint_core::formatting::ir_helpers::*;
88
use dprint_core::formatting::*;
99
use std::borrow::Cow;
10+
use std::rc::Rc;
1011

1112
pub fn generate(node: &Node, context: &mut Context) -> PrintItems {
1213
// println!("Kind: {:?}", node.kind());
@@ -61,7 +62,7 @@ fn gen_source_file(source_file: &SourceFile, context: &mut Context) -> PrintItem
6162

6263
items.push_condition(if_true(
6364
"endOfFileNewLine",
64-
|context| Some(context.writer_info.column_number > 0 || context.writer_info.line_number > 0),
65+
Rc::new(|context| Some(context.writer_info.column_number > 0 || context.writer_info.line_number > 0)),
6566
Signal::NewLine.into(),
6667
));
6768

@@ -266,15 +267,15 @@ fn gen_block_quote(block_quote: &BlockQuote, context: &mut Context) -> PrintItem
266267
PrintItem::String(text) => {
267268
items.push_condition(if_true(
268269
"angleBracketIfStartOfLine",
269-
|context| Some(context.writer_info.is_start_of_line()),
270+
condition_resolvers::is_start_of_line(),
270271
"> ".into(),
271272
));
272273
items.push_item(PrintItem::String(text));
273274
}
274275
PrintItem::Signal(Signal::NewLine) => {
275276
items.push_condition(if_true(
276277
"angleBracketIfStartOfLine",
277-
|context| Some(context.writer_info.is_start_of_line()),
278+
condition_resolvers::is_start_of_line(),
278279
">".into(),
279280
));
280281
items.push_signal(Signal::NewLine);
@@ -325,8 +326,8 @@ fn gen_code_block(code_block: &CodeBlock, context: &mut Context) -> PrintItems {
325326
let start_pos = get_code_block_start_pos(code);
326327
let code = code[start_pos..].trim_end();
327328
if let Some(tag) = &code_block.tag {
328-
if let Ok(text) = context.format_text(tag, code) {
329-
return text;
329+
if let Ok(Some(text)) = context.format_text(tag, code) {
330+
return Cow::Owned(text);
330331
}
331332
}
332333
Cow::Borrowed(code)
@@ -626,7 +627,7 @@ fn gen_list(list: &List, is_alternate: bool, context: &mut Context) -> PrintItem
626627
let after_child = Info::new("afterChild");
627628
items.push_condition(if_true(
628629
"spaceIfHasChild",
629-
move |context| Some(!condition_resolvers::is_at_same_position(context, &after_child)?),
630+
Rc::new(move |context| Some(!condition_helpers::is_at_same_position(context, &after_child)?)),
630631
Signal::SpaceIfNotTrailing.into(),
631632
));
632633
items.extend(with_indent_times(generate(child, context), indent_increment));

src/wasm_plugin.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use std::borrow::Cow;
21
use std::path::Path;
32
use std::path::PathBuf;
43

5-
use anyhow::Result;
64
use dprint_core::configuration::ConfigKeyMap;
75
use dprint_core::configuration::GlobalConfiguration;
86
use dprint_core::configuration::ResolveConfigurationResult;
97
use dprint_core::generate_plugin_code;
10-
use dprint_core::plugins::PluginHandler;
8+
use dprint_core::plugins::FormatResult;
119
use dprint_core::plugins::PluginInfo;
10+
use dprint_core::plugins::SyncPluginHandler;
1211

1312
use super::configuration::resolve_config;
1413
use super::configuration::Configuration;
@@ -21,15 +20,15 @@ impl MarkdownPluginHandler {
2120
}
2221
}
2322

24-
impl PluginHandler<Configuration> for MarkdownPluginHandler {
23+
impl SyncPluginHandler<Configuration> for MarkdownPluginHandler {
2524
fn resolve_config(&mut self, config: ConfigKeyMap, global_config: &GlobalConfiguration) -> ResolveConfigurationResult<Configuration> {
2625
resolve_config(config, global_config)
2726
}
2827

2928
// Markdown extensions: markdown, mdown, mkdn, mdwn, mkd, md
3029
// ref: https://superuser.com/questions/249436/file-extension-for-markdown-files/285878#285878
3130
// ref: https://github.com/denoland/deno_registry2/issues/206
32-
fn get_plugin_info(&mut self) -> PluginInfo {
31+
fn plugin_info(&mut self) -> PluginInfo {
3332
let version = env!("CARGO_PKG_VERSION").to_string();
3433
PluginInfo {
3534
name: env!("CARGO_PKG_NAME").to_string(),
@@ -50,25 +49,25 @@ impl PluginHandler<Configuration> for MarkdownPluginHandler {
5049
}
5150
}
5251

53-
fn get_license_text(&mut self) -> String {
52+
fn license_text(&mut self) -> String {
5453
std::str::from_utf8(include_bytes!("../LICENSE")).unwrap().into()
5554
}
5655

57-
fn format_text(
56+
fn format(
5857
&mut self,
5958
_file_path: &Path,
6059
file_text: &str,
6160
config: &Configuration,
62-
mut format_with_host: impl FnMut(&Path, String, &ConfigKeyMap) -> Result<String>,
63-
) -> Result<String> {
61+
mut format_with_host: impl FnMut(&Path, String, &ConfigKeyMap) -> FormatResult,
62+
) -> FormatResult {
6463
return super::format_text(file_text, config, |tag, file_text, line_width| {
6564
if let Some(ext) = tag_to_extension(tag) {
6665
let file_path = PathBuf::from(format!("file.{}", ext));
6766
let mut additional_config = ConfigKeyMap::new();
6867
additional_config.insert("lineWidth".into(), (line_width as i32).into());
69-
format_with_host(&file_path, file_text.to_string(), &additional_config).map(|text| Cow::Owned(text))
68+
format_with_host(&file_path, file_text.to_string(), &additional_config)
7069
} else {
71-
Ok(Cow::Borrowed(file_text))
70+
Ok(None)
7271
}
7372
});
7473

tests/newline_test.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
1-
use std::borrow::Cow;
2-
31
use dprint_plugin_markdown::configuration::*;
42
use dprint_plugin_markdown::*;
53

64
#[test]
75
fn test_issue22_with_carriage_return_line_feeds() {
86
let config = ConfigurationBuilder::new().build();
9-
let result = format_text(&"```\r\ntest\r\n\r\ntest\r\n```\r\n", &config, |_, file_text, _| Ok(Cow::Borrowed(file_text))).unwrap();
10-
assert_eq!(result, "```\ntest\n\ntest\n```\n");
7+
let result = format_text(&"```\r\ntest\r\n\r\ntest\r\n```\r\n", &config, |_, _, _| Ok(None)).unwrap();
8+
assert_eq!(result.unwrap(), "```\ntest\n\ntest\n```\n");
119
}
1210

1311
#[test]
1412
fn test_issue26_with_carriage_return_line_feeds() {
1513
let config = ConfigurationBuilder::new().build();
16-
let result = format_text(
17-
&"Testing:\r\n<!-- dprint-ignore -->\r\n```json\r\ntesting\r\n```\r\n",
18-
&config,
19-
|_, file_text, _| Ok(Cow::Borrowed(file_text)),
20-
)
14+
let result = format_text(&"Testing:\r\n<!-- dprint-ignore -->\r\n```json\r\ntesting\r\n```\r\n", &config, |_, _, _| {
15+
Ok(None)
16+
})
2117
.unwrap();
22-
assert_eq!(result, "Testing:\n\n<!-- dprint-ignore -->\n```json\ntesting\n```\n");
18+
assert_eq!(result.unwrap(), "Testing:\n\n<!-- dprint-ignore -->\n```json\ntesting\n```\n");
2319
}
2420

2521
#[test]
2622
fn test_issue35_convert_two_spaces_end_of_line_to_hard_break() {
2723
// In markdown, two spaces at the end of the line is a hard break.
2824
// To make this easier to see, we convert it to a hard break.
2925
let config = ConfigurationBuilder::new().build();
30-
let result = format_text(&"testing \nasdf", &config, |_, file_text, _| Ok(Cow::Borrowed(file_text))).unwrap();
31-
assert_eq!(result, "testing\\\nasdf\n");
26+
let result = format_text(&"testing \nasdf", &config, |_, _, _| Ok(None)).unwrap();
27+
assert_eq!(result.unwrap(), "testing\\\nasdf\n");
3228
}
3329

3430
#[test]
3531
fn test_issue35_ignore_two_spaces_before_hard_break() {
3632
let config = ConfigurationBuilder::new().build();
37-
let result = format_text(&"testing \\\nasdf", &config, |_, file_text, _| Ok(Cow::Borrowed(file_text))).unwrap();
38-
assert_eq!(result, "testing\\\nasdf\n");
33+
let result = format_text(&"testing \\\nasdf", &config, |_, _, _| Ok(None)).unwrap();
34+
assert_eq!(result.unwrap(), "testing\\\nasdf\n");
3935
}

tests/run_specs_test.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
extern crate dprint_development;
22
extern crate dprint_plugin_markdown;
33

4-
use std::borrow::Cow;
54
use std::path::PathBuf;
65

76
use dprint_core::configuration::*;
@@ -30,9 +29,9 @@ fn test_specs() {
3029
format_text(&file_text, &config_result.config, |tag, file_text, line_width| {
3130
let end = format!("_formatted_{}", line_width);
3231
if tag == "format" && !file_text.ends_with(&end) {
33-
Ok(Cow::Owned(format!("{}{}\n\n", file_text.to_string(), end)))
32+
Ok(Some(format!("{}{}\n\n", file_text.to_string(), end)))
3433
} else {
35-
Ok(Cow::Borrowed(file_text))
34+
Ok(None)
3635
}
3736
})
3837
}
@@ -45,9 +44,9 @@ fn test_specs() {
4544
return serde_json::to_string(&trace_file(&_file_text, &config_result.config, |tag, file_text, line_width| {
4645
let end = format!("_formatted_{}", line_width);
4746
if tag == "format" && !file_text.ends_with(&end) {
48-
Ok(format!("{}{}\n\n", file_text.to_string(), end))
47+
Ok(Some(format!("{}{}\n\n", file_text.to_string(), end)))
4948
} else {
50-
Ok(file_text.to_string())
49+
Ok(None)
5150
}
5251
}))
5352
.unwrap();

0 commit comments

Comments
 (0)