Skip to content

Commit

Permalink
chore(release): 0.8.0 - Upgrade dprint-core to 0.42.0 (#44)
Browse files Browse the repository at this point in the history
* 0.8.0

* Upgrade to use new PluginHandler trait.

Co-authored-by: David Sherret <[email protected]>
  • Loading branch information
bartlomieju and dsherret authored Jun 6, 2021
1 parent 2946499 commit 63e5efe
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 70 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
edition = "2018"
Expand All @@ -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" }
5 changes: 3 additions & 2 deletions src/format_text.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<dyn Fn(&str, &str, u32) -> Result<String, String>>,
) -> Result<String, String> {
format_code_block_text: impl FnMut(&str, &str, u32) -> Result<String, ErrBox>,
) -> Result<String, ErrBox> {
let (source_file, markdown_text) = match parse_source_file(file_text, config)? {
ParseFileResult::IgnoreFile => return Ok(file_text.to_string()),
ParseFileResult::SourceFile(file) => file,
Expand Down
12 changes: 7 additions & 5 deletions src/parsing/parser_types.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand All @@ -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<dyn Fn(&str, &str, u32) -> Result<String, String>>,
pub format_code_block_text: Box<dyn FnMut(&str, &str, u32) -> Result<String, ErrBox> + 'a>,
pub ignore_regex: Regex,
pub ignore_start_regex: Regex,
pub ignore_end_regex: Regex,
Expand All @@ -20,15 +22,15 @@ impl<'a> Context<'a> {
pub fn new(
file_text: &'a str,
configuration: &'a Configuration,
format_code_block_text: Box<dyn Fn(&str, &str, u32) -> Result<String, String>>
format_code_block_text: impl FnMut(&str, &str, u32) -> Result<String, ErrBox> + 'a,
) -> Context<'a> {
Context {
file_text,
configuration,
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),
Expand All @@ -39,7 +41,7 @@ impl<'a> Context<'a> {
self.is_in_list_count > 0
}

pub fn format_text(&self, tag: &str, text: &str) -> Result<String, String> {
pub fn format_text(&mut self, tag: &str, text: &str) -> Result<String, ErrBox> {
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)
}
Expand Down
117 changes: 70 additions & 47 deletions src/wasm_plugin.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
vec![String::from("md")]
impl MarkdownPluginHandler {
pub const fn new() -> Self {
MarkdownPluginHandler {}
}
}

fn format_text(_: &PathBuf, file_text: &str, config: &Configuration) -> Result<String, String> {
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<Configuration> for MarkdownPluginHandler {
fn resolve_config(
&mut self,
config: ConfigKeyMap,
global_config: &GlobalConfiguration,
) -> ResolveConfigurationResult<Configuration> {
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<String, ErrBox>,
) -> Result<String, ErrBox> {
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());
8 changes: 4 additions & 4 deletions tests/newline_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -18,7 +18,7 @@ fn test_issue26_with_carriage_return_line_feeds() {
let result = format_text(
&"Testing:\r\n<!-- dprint-ignore -->\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<!-- dprint-ignore -->\n```json\ntesting\n```\n");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
8 changes: 4 additions & 4 deletions tests/run_specs_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}),
},
)
}
},
Expand All @@ -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();
}

Expand Down

0 comments on commit 63e5efe

Please sign in to comment.