Move rustfmt changes out

Now in https://github.com/rust-lang/rustfmt/pull/6154
This commit is contained in:
George Bateman 2024-04-28 21:38:55 +01:00
parent e606bb626e
commit 997d5f2bec
4 changed files with 33 additions and 20 deletions

@ -44,6 +44,7 @@ dirs = "4.0"
getopts = "0.2" getopts = "0.2"
ignore = "0.4" ignore = "0.4"
itertools = "0.11" itertools = "0.11"
lazy_static = "1.4"
regex = "1.7" regex = "1.7"
serde = { version = "1.0.160", features = ["derive"] } serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"

@ -3,6 +3,8 @@
use std::{borrow::Cow, iter}; use std::{borrow::Cow, iter};
use itertools::{multipeek, MultiPeek}; use itertools::{multipeek, MultiPeek};
use lazy_static::lazy_static;
use regex::Regex;
use rustc_span::Span; use rustc_span::Span;
use crate::config::Config; use crate::config::Config;
@ -15,6 +17,17 @@ use crate::utils::{
}; };
use crate::{ErrorKind, FormattingError}; use crate::{ErrorKind, FormattingError};
lazy_static! {
/// A regex matching reference doc links.
///
/// ```markdown
/// /// An [example].
/// ///
/// /// [example]: this::is::a::link
/// ```
static ref REFERENCE_LINK_URL: Regex = Regex::new(r"^\[.+\]\s?:").unwrap();
}
fn is_custom_comment(comment: &str) -> bool { fn is_custom_comment(comment: &str) -> bool {
if !comment.starts_with("//") { if !comment.starts_with("//") {
false false
@ -967,16 +980,11 @@ fn trim_custom_comment_prefix(s: &str) -> String {
/// Returns `true` if the given string MAY include URLs or alike. /// Returns `true` if the given string MAY include URLs or alike.
fn has_url(s: &str) -> bool { fn has_url(s: &str) -> bool {
// This function may return false positive, but should get its job done in most cases. // This function may return false positive, but should get its job done in most cases.
// The regex is indended to capture text such as the below.
//
// /// An [example].
// ///
// /// [example]: this::is::a::link
s.contains("https://") s.contains("https://")
|| s.contains("http://") || s.contains("http://")
|| s.contains("ftp://") || s.contains("ftp://")
|| s.contains("file://") || s.contains("file://")
|| static_regex!(r"^\[.+\]\s?:").is_match(s) || REFERENCE_LINK_URL.is_match(s)
} }
/// Returns true if the given string may be part of a Markdown table. /// Returns true if the given string may be part of a Markdown table.

@ -5,6 +5,9 @@
#![allow(clippy::match_like_matches_macro)] #![allow(clippy::match_like_matches_macro)]
#![allow(unreachable_pub)] #![allow(unreachable_pub)]
#[cfg(test)]
#[macro_use]
extern crate lazy_static;
#[macro_use] #[macro_use]
extern crate tracing; extern crate tracing;
@ -59,13 +62,6 @@ pub use crate::rustfmt_diff::{ModifiedChunk, ModifiedLines};
#[macro_use] #[macro_use]
mod utils; mod utils;
macro_rules! static_regex {
($re:literal) => {{
static RE: ::std::sync::OnceLock<::regex::Regex> = ::std::sync::OnceLock::new();
RE.get_or_init(|| ::regex::Regex::new($re).unwrap())
}};
}
mod attr; mod attr;
mod chains; mod chains;
mod closures; mod closures;

@ -24,6 +24,19 @@ impl ConfigurationSection {
fn get_section<I: Iterator<Item = String>>( fn get_section<I: Iterator<Item = String>>(
file: &mut Enumerate<I>, file: &mut Enumerate<I>,
) -> Option<ConfigurationSection> { ) -> Option<ConfigurationSection> {
lazy_static! {
static ref CONFIG_NAME_REGEX: regex::Regex =
regex::Regex::new(r"^## `([^`]+)`").expect("failed creating configuration pattern");
// Configuration values, which will be passed to `from_str`:
//
// - must be prefixed with `####`
// - must be wrapped in backticks
// - may by wrapped in double quotes (which will be stripped)
static ref CONFIG_VALUE_REGEX: regex::Regex =
regex::Regex::new(r#"^#### `"?([^`]+?)"?`"#)
.expect("failed creating configuration value pattern");
}
loop { loop {
match file.next() { match file.next() {
Some((i, line)) => { Some((i, line)) => {
@ -40,14 +53,9 @@ impl ConfigurationSection {
let start_line = (i + 2) as u32; let start_line = (i + 2) as u32;
return Some(ConfigurationSection::CodeBlock((block, start_line))); return Some(ConfigurationSection::CodeBlock((block, start_line)));
} else if let Some(c) = static_regex!(r"^## `([^`]+)`").captures(&line) { } else if let Some(c) = CONFIG_NAME_REGEX.captures(&line) {
return Some(ConfigurationSection::ConfigName(String::from(&c[1]))); return Some(ConfigurationSection::ConfigName(String::from(&c[1])));
} else if let Some(c) = static_regex!(r#"^#### `"?([^`]+?)"?`"#).captures(&line) { } else if let Some(c) = CONFIG_VALUE_REGEX.captures(&line) {
// Configuration values, which will be passed to `from_str`
//
// - must be prefixed with `####`
// - must be wrapped in backticks
// - may by wrapped in double quotes (which will be stripped)
return Some(ConfigurationSection::ConfigValue(String::from(&c[1]))); return Some(ConfigurationSection::ConfigValue(String::from(&c[1])));
} }
} }