From b75cb33b939df3e28e546273c5a34c6cc4800dc4 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 14 Apr 2021 19:39:08 -0400 Subject: [PATCH] feat: Support dprint-ignore-file directive. Closes #40. --- src/configuration/builder.rs | 15 ++++++-- src/configuration/resolve_config.rs | 1 + src/configuration/types.rs | 1 + src/format_text.rs | 8 ++++- src/parsing/parse.rs | 6 ++-- src/parsing/parser_types.rs | 11 +++--- src/parsing/utils.rs | 15 ++++---- .../IgnoreComments/IgnoreComments_All.txt | 34 +++++++++++++++++++ .../IgnoreComments/IgnoreComments_Custom.txt | 14 +++++++- 9 files changed, 83 insertions(+), 22 deletions(-) diff --git a/src/configuration/builder.rs b/src/configuration/builder.rs index a84309a..bf3a70a 100644 --- a/src/configuration/builder.rs +++ b/src/configuration/builder.rs @@ -80,6 +80,12 @@ impl ConfigurationBuilder { self.insert("ignoreDirective", value.to_string().into()) } + /// The directive used to ignore a file. + /// Default: `dprint-ignore-file` + pub fn ignore_file_directive(&mut self, value: &str) -> &mut Self { + self.insert("ignoreFileDirective", value.to_string().into()) + } + /// The directive used to mark start of ignored section. /// Default: `dprint-ignore-start` pub fn ignore_start_directive(&mut self, value: &str) -> &mut Self { @@ -97,6 +103,7 @@ impl ConfigurationBuilder { .ignore_directive("deno-fmt-ignore") .ignore_start_directive("deno-fmt-ignore-start") .ignore_end_directive("deno-fmt-ignore-end") + .ignore_file_directive("deno-fmt-ignore-file") } #[cfg(test)] @@ -124,10 +131,14 @@ mod tests { .line_width(90) .text_wrap(TextWrap::Always) .emphasis_kind(EmphasisKind::Asterisks) - .strong_kind(StrongKind::Underscores); + .strong_kind(StrongKind::Underscores) + .ignore_directive("test") + .ignore_file_directive("test") + .ignore_start_directive("test") + .ignore_end_directive("test"); let inner_config = config.get_inner_config(); - assert_eq!(inner_config.len(), 5); + assert_eq!(inner_config.len(), 9); let diagnostics = resolve_config(inner_config, &resolve_global_config(HashMap::new()).config).diagnostics; assert_eq!(diagnostics.len(), 0); } diff --git a/src/configuration/resolve_config.rs b/src/configuration/resolve_config.rs index f91ef27..3ed44a0 100644 --- a/src/configuration/resolve_config.rs +++ b/src/configuration/resolve_config.rs @@ -40,6 +40,7 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration) emphasis_kind: get_value(&mut config, "emphasisKind", EmphasisKind::Underscores, &mut diagnostics), strong_kind: get_value(&mut config, "strongKind", StrongKind::Asterisks, &mut diagnostics), ignore_directive: get_value(&mut config, "ignoreDirective", "dprint-ignore".to_string(), &mut diagnostics), + ignore_file_directive: get_value(&mut config, "ignoreFileDirective", "dprint-ignore-file".to_string(), &mut diagnostics), ignore_start_directive: get_value(&mut config, "ignoreStartDirective", "dprint-ignore-start".to_string(), &mut diagnostics), ignore_end_directive: get_value(&mut config, "ignoreEndDirective", "dprint-ignore-end".to_string(), &mut diagnostics), }; diff --git a/src/configuration/types.rs b/src/configuration/types.rs index f1d6dfc..abb1a3c 100644 --- a/src/configuration/types.rs +++ b/src/configuration/types.rs @@ -12,6 +12,7 @@ pub struct Configuration { pub emphasis_kind: EmphasisKind, pub strong_kind: StrongKind, pub ignore_directive: String, + pub ignore_file_directive: String, pub ignore_start_directive: String, pub ignore_end_directive: String, } diff --git a/src/format_text.rs b/src/format_text.rs index ea6b652..89090ff 100644 --- a/src/format_text.rs +++ b/src/format_text.rs @@ -2,7 +2,7 @@ use dprint_core::formatting::*; use dprint_core::configuration::{resolve_new_line_kind}; use super::configuration::Configuration; -use super::parsing::{parse_cmark_ast, parse_yaml_header, parse_node, Context}; +use super::parsing::{parse_cmark_ast, parse_yaml_header, parse_node, Context, file_has_ignore_file_directive}; /// Formats a file. /// @@ -17,6 +17,12 @@ pub fn format_text( Some(yaml_header) => &file_text[yaml_header.range.end..], None => file_text, }; + + // check for the presence of an dprint-ignore-file comment before parsing + if file_has_ignore_file_directive(&markdown_text, &config.ignore_file_directive) { + return Ok(file_text.to_string()); + } + let source_file = match parse_cmark_ast(markdown_text) { Ok(source_file) => { let mut source_file = source_file; diff --git a/src/parsing/parse.rs b/src/parsing/parse.rs index d791dc8..366aeed 100644 --- a/src/parsing/parse.rs +++ b/src/parsing/parse.rs @@ -157,7 +157,7 @@ fn parse_nodes(nodes: &Vec, context: &mut Context) -> PrintItems { // check for ignore comment if let Node::Html(html) = node { - if utils::is_ignore_comment(&context.ignore_regex, &html.text) { + if context.ignore_regex.is_match(&html.text) { items.push_signal(Signal::NewLine); if let Some(node) = node_iterator.next() { if utils::has_leading_blankline(node.range().start, context.file_text) { @@ -166,14 +166,14 @@ fn parse_nodes(nodes: &Vec, context: &mut Context) -> PrintItems { items.extend(parser_helpers::parse_raw_string(node.text(context).trim_end())); last_node = Some(node); } - } else if utils::is_ignore_start_comment(&context.ignore_start_regex, &html.text) { + } else if context.ignore_start_regex.is_match(&html.text) { let mut range: Option = None; let mut end_comment = None; while let Some(node) = node_iterator.next() { last_node = Some(node); if let Node::Html(html) = node { - if utils::is_ignore_end_comment(&context.ignore_end_regex, &html.text) { + if context.ignore_end_regex.is_match(&html.text) { end_comment = Some(html); break; } diff --git a/src/parsing/parser_types.rs b/src/parsing/parser_types.rs index d6f20ff..fd6e075 100644 --- a/src/parsing/parser_types.rs +++ b/src/parsing/parser_types.rs @@ -1,5 +1,6 @@ use regex::Regex; use super::super::configuration::Configuration; +use super::utils::*; pub struct Context<'a> { pub file_text: &'a str, @@ -21,10 +22,6 @@ impl<'a> Context<'a> { configuration: &'a Configuration, format_code_block_text: Box Result> ) -> Context<'a> { - let ignore_regex = format!(r"\s*\s*", configuration.ignore_directive); - let ignore_start_regex = format!(r"\s*\s*", configuration.ignore_start_directive); - let ignore_end_regex = format!(r"\s*\s*", configuration.ignore_end_directive); - Context { file_text, configuration, @@ -32,9 +29,9 @@ impl<'a> Context<'a> { raw_indent_level: 0, is_in_list_count: 0, format_code_block_text, - ignore_regex: Regex::new(&ignore_regex).unwrap(), - ignore_start_regex: Regex::new(&ignore_start_regex).unwrap(), - ignore_end_regex: Regex::new(&ignore_end_regex).unwrap(), + 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), } } diff --git a/src/parsing/utils.rs b/src/parsing/utils.rs index d1582dd..18c84f7 100644 --- a/src/parsing/utils.rs +++ b/src/parsing/utils.rs @@ -51,16 +51,15 @@ pub fn has_leading_blankline(index: usize, text: &str) -> bool { false } -pub fn is_ignore_comment(is_ignore_regex: &Regex, text: &str) -> bool { - is_ignore_regex.is_match(text) +pub fn file_has_ignore_file_directive(file_text: &str, directive_inner_text: &str) -> bool { + let ignore_regex = get_ignore_comment_regex(directive_inner_text); + ignore_regex.is_match(file_text) } -pub fn is_ignore_start_comment(is_ignore_start_regex: &Regex, text: &str) -> bool { - is_ignore_start_regex.is_match(text) -} - -pub fn is_ignore_end_comment(is_ignore_end_regex: &Regex, text: &str) -> bool { - is_ignore_end_regex.is_match(text) +pub fn get_ignore_comment_regex(inner_text: &str) -> Regex { + // todo: don't use regex + let text = format!(r"\s*\s*", inner_text); + Regex::new(&text).unwrap() } pub fn safe_subtract_to_zero(a: u32, b: u32) -> u32 { diff --git a/tests/specs/IgnoreComments/IgnoreComments_All.txt b/tests/specs/IgnoreComments/IgnoreComments_All.txt index fca0799..3990406 100644 --- a/tests/specs/IgnoreComments/IgnoreComments_All.txt +++ b/tests/specs/IgnoreComments/IgnoreComments_All.txt @@ -85,3 +85,37 @@ test ```ts testing ``` + +!! should handle ignore file comments !! + +testing this out + +testing this out + +[expect] + +testing this out + +testing this out + +!! should handle ignore file comments in a file with a yaml header !! +--- +title: Some Title +description: Some description +--- + + +testing this out + +testing this out + +[expect] +--- +title: Some Title +description: Some description +--- + + +testing this out + +testing this out diff --git a/tests/specs/IgnoreComments/IgnoreComments_Custom.txt b/tests/specs/IgnoreComments/IgnoreComments_Custom.txt index 3fd22b7..cd69794 100644 --- a/tests/specs/IgnoreComments/IgnoreComments_Custom.txt +++ b/tests/specs/IgnoreComments/IgnoreComments_Custom.txt @@ -1,4 +1,4 @@ -~~ ignoreDirective: foo-ignore, ignoreStartDirective: foo-ignore-start, ignoreEndDirective: foo-ignore-end ~~ +~~ ignoreDirective: foo-ignore, ignoreStartDirective: foo-ignore-start, ignoreEndDirective: foo-ignore-end, ignoreFileDirective: foo-ignore-file ~~ !! should handle ignore comments !! testing this out @@ -86,3 +86,15 @@ test ```ts testing ``` + +!! should handle ignore file comments !! + +testing this out + +testing this out + +[expect] + +testing this out + +testing this out