Skip to content

Commit

Permalink
Add flag to disable implicit markdown formatting for non-doc line com…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
andrew committed Feb 2, 2025
1 parent c06d730 commit 0ae7103
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
7 changes: 7 additions & 0 deletions crates/genemichaels-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub mod utils;
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum CommentMode {
Normal,
ExplicitNormal,
DocInner,
DocOuter,
Verbatim,
Expand Down Expand Up @@ -473,6 +474,7 @@ pub struct FormatConfig {
pub comment_errors_fatal: bool,
pub keep_max_blank_lines: usize,
pub indent_spaces: usize,
pub explicit_markdown_comments: bool,
}

impl Default for FormatConfig {
Expand All @@ -487,6 +489,7 @@ impl Default for FormatConfig {
comment_errors_fatal: false,
keep_max_blank_lines: 0,
indent_spaces: 4,
explicit_markdown_comments:false,
}
}
}
Expand Down Expand Up @@ -791,6 +794,7 @@ pub fn format_ast(
}
let prefix = format!("{}//{} ", " ".repeat(b.get(&config)), match comment.mode {

Check warning on line 795 in crates/genemichaels-lib/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> crates/genemichaels-lib/src/lib.rs:795:86 | 795 | ... let prefix = format!("{}//{} ", " ".repeat(b.get(&config)), match comment.mode { | ^^^^^^^ help: change this to: `config` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

Check warning on line 795 in crates/genemichaels-lib/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> crates/genemichaels-lib/src/lib.rs:795:86 | 795 | ... let prefix = format!("{}//{} ", " ".repeat(b.get(&config)), match comment.mode { | ^^^^^^^ help: change this to: `config` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

Check warning on line 795 in crates/genemichaels-lib/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> crates/genemichaels-lib/src/lib.rs:795:86 | 795 | ... let prefix = format!("{}//{} ", " ".repeat(b.get(&config)), match comment.mode { | ^^^^^^^ help: change this to: `config` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
CommentMode::Normal => "",
CommentMode::ExplicitNormal => "?",
CommentMode::DocInner => "!",
CommentMode::DocOuter => "/",
CommentMode::Verbatim => ".",
Expand All @@ -799,6 +803,9 @@ pub fn format_ast(
CommentMode::Verbatim => {
true
},
CommentMode::Normal if config.explicit_markdown_comments => {
true
},
_ => {
match format_md(
&mut rendered,
Expand Down
2 changes: 1 addition & 1 deletion crates/genemichaels-lib/src/sg_general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ pub(crate) fn append_macro_body(
};

Check warning on line 447 in crates/genemichaels-lib/src/sg_general.rs

View workflow job for this annotation

GitHub Actions / clippy

match expression looks like `matches!` macro

warning: match expression looks like `matches!` macro --> crates/genemichaels-lib/src/sg_general.rs:441:28 | 441 | return match p.as_char() { | ____________________________^ 442 | | '.' => true, 443 | | '\'' => true, 444 | | '$' => true, 445 | | '#' => true, 446 | | _ => false, 447 | | }; | |_____________________^ help: try: `matches!(p.as_char(), '.' | '\'' | '$' | '#')` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro = note: `#[warn(clippy::match_like_matches_macro)]` on by default

Check warning on line 447 in crates/genemichaels-lib/src/sg_general.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> crates/genemichaels-lib/src/sg_general.rs:441:21 | 441 | / return match p.as_char() { 442 | | '.' => true, 443 | | '\'' => true, 444 | | '$' => true, 445 | | '#' => true, 446 | | _ => false, 447 | | }; | |_____________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return help: remove `return` | 441 ~ match p.as_char() { 442 + '.' => true, 443 + '\'' => true, 444 + '$' => true, 445 + '#' => true, 446 + _ => false, 447 ~ } |

Check warning on line 447 in crates/genemichaels-lib/src/sg_general.rs

View workflow job for this annotation

GitHub Actions / clippy

match expression looks like `matches!` macro

warning: match expression looks like `matches!` macro --> crates/genemichaels-lib/src/sg_general.rs:441:28 | 441 | return match p.as_char() { | ____________________________^ 442 | | '.' => true, 443 | | '\'' => true, 444 | | '$' => true, 445 | | '#' => true, 446 | | _ => false, 447 | | }; | |_____________________^ help: try: `matches!(p.as_char(), '.' | '\'' | '$' | '#')` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro = note: `#[warn(clippy::match_like_matches_macro)]` on by default

Check warning on line 447 in crates/genemichaels-lib/src/sg_general.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> crates/genemichaels-lib/src/sg_general.rs:441:21 | 441 | / return match p.as_char() { 442 | | '.' => true, 443 | | '\'' => true, 444 | | '$' => true, 445 | | '#' => true, 446 | | _ => false, 447 | | }; | |_____________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return help: remove `return` | 441 ~ match p.as_char() { 442 + '.' => true, 443 + '\'' => true, 444 + '$' => true, 445 + '#' => true, 446 + _ => false, 447 ~ } |

Check warning on line 447 in crates/genemichaels-lib/src/sg_general.rs

View workflow job for this annotation

GitHub Actions / clippy

match expression looks like `matches!` macro

warning: match expression looks like `matches!` macro --> crates/genemichaels-lib/src/sg_general.rs:441:28 | 441 | return match p.as_char() { | ____________________________^ 442 | | '.' => true, 443 | | '\'' => true, 444 | | '$' => true, 445 | | '#' => true, 446 | | _ => false, 447 | | }; | |_____________________^ help: try: `matches!(p.as_char(), '.' | '\'' | '$' | '#')` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro = note: `#[warn(clippy::match_like_matches_macro)]` on by default

Check warning on line 447 in crates/genemichaels-lib/src/sg_general.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> crates/genemichaels-lib/src/sg_general.rs:441:21 | 441 | / return match p.as_char() { 442 | | '.' => true, 443 | | '\'' => true, 444 | | '$' => true, 445 | | '#' => true, 446 | | _ => false, 447 | | }; | |_____________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return help: remove `return` | 441 ~ match p.as_char() { 442 + '.' => true, 443 + '\'' => true, 444 + '$' => true, 445 + '#' => true, 446 + _ => false, 447 ~ } |
}

// With exceptions, the default heterogenous adjacent token tree behavior is to
// With exceptions, the default heterogeneous adjacent token tree behavior is to
// push. For punctuation-adjacent, it depends on the punctuation type.
fn is_hetero_push_next(prev: &Option<TokenTree>) -> bool {
return match &prev {
Expand Down
3 changes: 2 additions & 1 deletion crates/genemichaels-lib/src/whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn extract_whitespaces(
let start_re =
&self
.start_re
.get_or_insert_with(|| Regex::new(r#"(?:(//)(/|!|\.)?)|(/\*\*/)|(?:(/\*)(\*|!)?)"#).unwrap());
.get_or_insert_with(|| Regex::new(r#"(?:(//)(/|!|\.|\?)?)|(/\*\*/)|(?:(/\*)(\*|!)?)"#).unwrap());
let block_event_re =
&self.block_event_re.get_or_insert_with(|| Regex::new(r#"((?:/\*)|(?:\*/))"#).unwrap());

Expand Down Expand Up @@ -168,6 +168,7 @@ pub fn extract_whitespaces(
"/" => CommentMode::DocOuter,
"!" => CommentMode::DocInner,
"." => CommentMode::Verbatim,
"?" => CommentMode::ExplicitNormal,
_ => unreachable!(),
}, start_suffix_match.end()),
None => (CommentMode::Normal, start_prefix_match.end()),
Expand Down
51 changes: 39 additions & 12 deletions crates/genemichaels-lib/tests/oneway.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
use genemichaels_lib::{
FormatConfig,
format_str,
use {
genemichaels_lib::{
FormatConfig,
format_str,
},
};

fn ow(before: &str, want_after: &str) {
let res = format_str(before, &FormatConfig {
max_width: 120,
..Default::default()
}).unwrap();
fn owc(before: &str, want_after: &str, config: &FormatConfig) {
let res = format_str(before, config).unwrap();
assert!(
want_after == res.rendered,
"Formatted text changed:\n\nWant after:\n{}\n\nAfter:\n{}\n",
want_after
.lines()
.split("\n")
.enumerate()
.map(|(line, text)| format!("{:>03} {}", line, text))
.map(|(line, text)| format!("{:>03} [{}]", line, text))
.collect::<Vec<String>>()
.join("\n"),
res
.rendered
.lines()
.split("\n")
.enumerate()
.map(|(line, text)| format!("{:>03} {}", line, text))
.map(|(line, text)| format!("{:>03} [{}]", line, text))
.collect::<Vec<String>>()
.join("\n")
);
}

fn ow(before: &str, want_after: &str) {
owc(before, want_after, &FormatConfig {
max_width: 120,
..Default::default()
});
}

#[test]
fn ow_remove_blanks1() {
ow(r#"fn main() {
Expand All @@ -49,3 +55,24 @@ fn ow_remove_blanks2() {
}
"#);
}

#[test]
fn ow_format_explicit_normal() {
// Before/after newlines inconsequential
ow(r#"
//? remove extra spaces"#, r#"
//? remove extra spaces
"#);
}

#[test]
fn ow_dont_format_when_explicit_normal() {
// Before/after newlines inconsequential
owc(r#"
// remove extra spaces"#, r#"
// remove extra spaces"#, &FormatConfig {
max_width: 120,
explicit_markdown_comments: true,
..Default::default()
});
}
4 changes: 4 additions & 0 deletions readme_genemichaels.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ Here's the config file. All values shown are defaults and the keys can be omitte
"keep_max_blank_lines": 0,
// The number of spaces to indent by at each level.
"indent_spaces": 4,
// `//` (plain line-comments) won't be treated implicitly as markdown. In this case you can
// use `//?` for explicitly markdown-formatted line-comments (these comments will work
// regardless of the setting)
"explicit_markdown_comments": false,
}
```

Expand Down

0 comments on commit 0ae7103

Please sign in to comment.