Skip to content

Commit

Permalink
feat: Support dprint-ignore-file directive.
Browse files Browse the repository at this point in the history
Closes #40.
  • Loading branch information
dsherret committed Apr 14, 2021
1 parent 2ffe601 commit b75cb33
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 22 deletions.
15 changes: 13 additions & 2 deletions src/configuration/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)]
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand Down
1 change: 1 addition & 0 deletions src/configuration/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
8 changes: 7 additions & 1 deletion src/format_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/parsing/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn parse_nodes(nodes: &Vec<Node>, 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) {
Expand All @@ -166,14 +166,14 @@ fn parse_nodes(nodes: &Vec<Node>, 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<Range> = 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;
}
Expand Down
11 changes: 4 additions & 7 deletions src/parsing/parser_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use regex::Regex;
use super::super::configuration::Configuration;
use super::utils::*;

pub struct Context<'a> {
pub file_text: &'a str,
Expand All @@ -21,20 +22,16 @@ impl<'a> Context<'a> {
configuration: &'a Configuration,
format_code_block_text: Box<dyn Fn(&str, &str, u32) -> Result<String, String>>
) -> Context<'a> {
let ignore_regex = format!(r"\s*<!\-\-\s*{}\s*\-\->\s*", configuration.ignore_directive);
let ignore_start_regex = format!(r"\s*<!\-\-\s*{}\s*\-\->\s*", configuration.ignore_start_directive);
let ignore_end_regex = format!(r"\s*<!\-\-\s*{}\s*\-\->\s*", configuration.ignore_end_directive);

Context {
file_text,
configuration,
indent_level: 0,
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),
}
}

Expand Down
15 changes: 7 additions & 8 deletions src/parsing/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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*{}\s*\-\->\s*", inner_text);
Regex::new(&text).unwrap()
}

pub fn safe_subtract_to_zero(a: u32, b: u32) -> u32 {
Expand Down
34 changes: 34 additions & 0 deletions tests/specs/IgnoreComments/IgnoreComments_All.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,37 @@ test
```ts
testing
```

!! should handle ignore file comments !!
<!-- dprint-ignore-file -->
testing this out

testing this out

[expect]
<!-- dprint-ignore-file -->
testing this out

testing this out

!! should handle ignore file comments in a file with a yaml header !!
---
title: Some Title
description: Some description
---

<!-- dprint-ignore-file -->
testing this out

testing this out

[expect]
---
title: Some Title
description: Some description
---

<!-- dprint-ignore-file -->
testing this out

testing this out
14 changes: 13 additions & 1 deletion tests/specs/IgnoreComments/IgnoreComments_Custom.txt
Original file line number Diff line number Diff line change
@@ -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 !!
<!-- foo-ignore -->
testing this out
Expand Down Expand Up @@ -86,3 +86,15 @@ test
```ts
testing
```

!! should handle ignore file comments !!
<!-- foo-ignore-file -->
testing this out

testing this out

[expect]
<!-- foo-ignore-file -->
testing this out

testing this out

0 comments on commit b75cb33

Please sign in to comment.