Skip to content

Commit 9e90cfb

Browse files
authored
Merge pull request #6690 from shulaoda/10-10-support-lazy_static!-with-crate-name-format
Support formatting for `lazy_static::lazy_static!`
2 parents 85d72bc + 7cb4a36 commit 9e90cfb

File tree

5 files changed

+194
-6
lines changed

5 files changed

+194
-6
lines changed

src/macros.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,12 @@ fn rewrite_macro_inner(
226226
};
227227
}
228228
// Format well-known macros which cannot be parsed as a valid AST.
229-
if macro_name == "lazy_static!" && !has_comment {
230-
match format_lazy_static(context, shape, ts.clone(), mac.span()) {
229+
if (macro_name == "lazy_static!"
230+
|| (context.config.style_edition() >= StyleEdition::Edition2027
231+
&& macro_name == "lazy_static::lazy_static!"))
232+
&& !has_comment
233+
{
234+
match format_lazy_static(context, shape, ts.clone(), mac.span(), &macro_name) {
231235
Ok(rw) => return Ok(rw),
232236
Err(err) => match err {
233237
// We will move on to parsing macro args just like other macros
@@ -1394,7 +1398,8 @@ impl MacroBranch {
13941398
}
13951399
}
13961400

1397-
/// Format `lazy_static!` from <https://crates.io/crates/lazy_static>.
1401+
/// Format `lazy_static!` and `lazy_static::lazy_static!`
1402+
/// from <https://crates.io/crates/lazy_static>.
13981403
///
13991404
/// # Expected syntax
14001405
///
@@ -1405,19 +1410,28 @@ impl MacroBranch {
14051410
/// ...
14061411
/// [pub] static ref NAME_N: TYPE_N = EXPR_N;
14071412
/// }
1413+
///
1414+
/// lazy_static::lazy_static! {
1415+
/// [pub] static ref NAME_1: TYPE_1 = EXPR_1;
1416+
/// [pub] static ref NAME_2: TYPE_2 = EXPR_2;
1417+
/// ...
1418+
/// [pub] static ref NAME_N: TYPE_N = EXPR_N;
1419+
/// }
14081420
/// ```
14091421
fn format_lazy_static(
14101422
context: &RewriteContext<'_>,
14111423
shape: Shape,
14121424
ts: TokenStream,
14131425
span: Span,
1426+
macro_name: &str,
14141427
) -> RewriteResult {
14151428
let mut result = String::with_capacity(1024);
14161429
let nested_shape = shape
14171430
.block_indent(context.config.tab_spaces())
14181431
.with_max_width(context.config);
14191432

1420-
result.push_str("lazy_static! {");
1433+
result.push_str(macro_name);
1434+
result.push_str(" {");
14211435
result.push_str(&nested_shape.indent.to_string_with_newline(context.config));
14221436

14231437
let parsed_elems =
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
lazy_static::lazy_static! {
2+
static ref CONFIG_NAME_REGEX: regex::Regex =
3+
regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern");
4+
static ref CONFIG_VALUE_REGEX: regex::Regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#)
5+
.expect("Failed creating configuration value pattern");
6+
}
7+
8+
// We need to be able to format `lazy_static::lazy_static!` without known syntax.
9+
lazy_static::lazy_static!(
10+
xxx,
11+
yyyy ,
12+
zzzzz
13+
);
14+
15+
lazy_static::lazy_static!{
16+
}
17+
18+
// #2354
19+
lazy_static::lazy_static ! {
20+
pub static ref Sbase64_encode_string : :: lisp :: LispSubrRef = {
21+
let subr = :: remacs_sys :: Lisp_Subr {
22+
header : :: remacs_sys :: Lisp_Vectorlike_Header {
23+
size : (
24+
( :: remacs_sys :: PseudovecType :: PVEC_SUBR as :: libc :: ptrdiff_t ) << ::
25+
remacs_sys :: PSEUDOVECTOR_AREA_BITS ) , } , function : self ::
26+
Fbase64_encode_string as * const :: libc :: c_void , min_args : 1i16 ,
27+
max_args : 2i16 , symbol_name : ( b"base64-encode-string\x00" ) . as_ptr ( )
28+
as * const :: libc :: c_char , intspec : :: std :: ptr :: null ( ) , doc : ::
29+
std :: ptr :: null ( ) , lang : :: remacs_sys :: Lisp_Subr_Lang_Rust , } ;
30+
unsafe {
31+
let ptr = :: remacs_sys :: xmalloc (
32+
:: std :: mem :: size_of :: < :: remacs_sys :: Lisp_Subr > ( ) ) as * mut ::
33+
remacs_sys :: Lisp_Subr ; :: std :: ptr :: copy_nonoverlapping (
34+
& subr , ptr , 1 ) ; :: std :: mem :: forget ( subr ) ; :: lisp :: ExternalPtr
35+
:: new ( ptr ) } } ; }
36+
37+
38+
lazy_static::lazy_static! {
39+
static ref FOO: HashMap<String,
40+
(&'static str,
41+
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
42+
),> = HashMap::new();
43+
}

tests/source/lazy_static.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Format `lazy_static!`.
1+
// rustfmt-style_edition: 2027
2+
// Format `lazy_static!` and `lazy_static::lazy_static!`.
23

34
lazy_static! {
45
static ref CONFIG_NAME_REGEX: regex::Regex =
@@ -43,3 +44,47 @@ static ref FOO: HashMap<String,
4344
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
4445
),> = HashMap::new();
4546
}
47+
48+
lazy_static::lazy_static! {
49+
static ref CONFIG_NAME_REGEX: regex::Regex =
50+
regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern");
51+
static ref CONFIG_VALUE_REGEX: regex::Regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#)
52+
.expect("Failed creating configuration value pattern");
53+
}
54+
55+
// We need to be able to format `lazy_static::lazy_static!` without known syntax.
56+
lazy_static::lazy_static!(
57+
xxx,
58+
yyyy ,
59+
zzzzz
60+
);
61+
62+
lazy_static::lazy_static!{
63+
}
64+
65+
// #2354
66+
lazy_static::lazy_static ! {
67+
pub static ref Sbase64_encode_string : :: lisp :: LispSubrRef = {
68+
let subr = :: remacs_sys :: Lisp_Subr {
69+
header : :: remacs_sys :: Lisp_Vectorlike_Header {
70+
size : (
71+
( :: remacs_sys :: PseudovecType :: PVEC_SUBR as :: libc :: ptrdiff_t ) << ::
72+
remacs_sys :: PSEUDOVECTOR_AREA_BITS ) , } , function : self ::
73+
Fbase64_encode_string as * const :: libc :: c_void , min_args : 1i16 ,
74+
max_args : 2i16 , symbol_name : ( b"base64-encode-string\x00" ) . as_ptr ( )
75+
as * const :: libc :: c_char , intspec : :: std :: ptr :: null ( ) , doc : ::
76+
std :: ptr :: null ( ) , lang : :: remacs_sys :: Lisp_Subr_Lang_Rust , } ;
77+
unsafe {
78+
let ptr = :: remacs_sys :: xmalloc (
79+
:: std :: mem :: size_of :: < :: remacs_sys :: Lisp_Subr > ( ) ) as * mut ::
80+
remacs_sys :: Lisp_Subr ; :: std :: ptr :: copy_nonoverlapping (
81+
& subr , ptr , 1 ) ; :: std :: mem :: forget ( subr ) ; :: lisp :: ExternalPtr
82+
:: new ( ptr ) } } ; }
83+
84+
85+
lazy_static::lazy_static! {
86+
static ref FOO: HashMap<String,
87+
(&'static str,
88+
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
89+
),> = HashMap::new();
90+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
lazy_static::lazy_static! {
2+
static ref CONFIG_NAME_REGEX: regex::Regex =
3+
regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern");
4+
static ref CONFIG_VALUE_REGEX: regex::Regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#)
5+
.expect("Failed creating configuration value pattern");
6+
}
7+
8+
// We need to be able to format `lazy_static::lazy_static!` without known syntax.
9+
lazy_static::lazy_static!(xxx, yyyy, zzzzz);
10+
11+
lazy_static::lazy_static! {}
12+
13+
// #2354
14+
lazy_static::lazy_static! {
15+
pub static ref Sbase64_encode_string : :: lisp :: LispSubrRef = {
16+
let subr = :: remacs_sys :: Lisp_Subr {
17+
header : :: remacs_sys :: Lisp_Vectorlike_Header {
18+
size : (
19+
( :: remacs_sys :: PseudovecType :: PVEC_SUBR as :: libc :: ptrdiff_t ) << ::
20+
remacs_sys :: PSEUDOVECTOR_AREA_BITS ) , } , function : self ::
21+
Fbase64_encode_string as * const :: libc :: c_void , min_args : 1i16 ,
22+
max_args : 2i16 , symbol_name : ( b"base64-encode-string\x00" ) . as_ptr ( )
23+
as * const :: libc :: c_char , intspec : :: std :: ptr :: null ( ) , doc : ::
24+
std :: ptr :: null ( ) , lang : :: remacs_sys :: Lisp_Subr_Lang_Rust , } ;
25+
unsafe {
26+
let ptr = :: remacs_sys :: xmalloc (
27+
:: std :: mem :: size_of :: < :: remacs_sys :: Lisp_Subr > ( ) ) as * mut ::
28+
remacs_sys :: Lisp_Subr ; :: std :: ptr :: copy_nonoverlapping (
29+
& subr , ptr , 1 ) ; :: std :: mem :: forget ( subr ) ; :: lisp :: ExternalPtr
30+
:: new ( ptr ) } } ; }
31+
32+
lazy_static::lazy_static! {
33+
static ref FOO: HashMap<String,
34+
(&'static str,
35+
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
36+
),> = HashMap::new();
37+
}

tests/target/lazy_static.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Format `lazy_static!`.
1+
// rustfmt-style_edition: 2027
2+
// Format `lazy_static!` and `lazy_static::lazy_static!`.
23

34
lazy_static! {
45
static ref CONFIG_NAME_REGEX: regex::Regex =
@@ -47,3 +48,51 @@ lazy_static! {
4748
),
4849
> = HashMap::new();
4950
}
51+
52+
lazy_static::lazy_static! {
53+
static ref CONFIG_NAME_REGEX: regex::Regex =
54+
regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern");
55+
static ref CONFIG_VALUE_REGEX: regex::Regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#)
56+
.expect("Failed creating configuration value pattern");
57+
}
58+
59+
// We need to be able to format `lazy_static::lazy_static!` without known syntax.
60+
lazy_static::lazy_static!(xxx, yyyy, zzzzz);
61+
62+
lazy_static::lazy_static! {}
63+
64+
// #2354
65+
lazy_static::lazy_static! {
66+
pub static ref Sbase64_encode_string: ::lisp::LispSubrRef = {
67+
let subr = ::remacs_sys::Lisp_Subr {
68+
header: ::remacs_sys::Lisp_Vectorlike_Header {
69+
size: ((::remacs_sys::PseudovecType::PVEC_SUBR as ::libc::ptrdiff_t)
70+
<< ::remacs_sys::PSEUDOVECTOR_AREA_BITS),
71+
},
72+
function: self::Fbase64_encode_string as *const ::libc::c_void,
73+
min_args: 1i16,
74+
max_args: 2i16,
75+
symbol_name: (b"base64-encode-string\x00").as_ptr() as *const ::libc::c_char,
76+
intspec: ::std::ptr::null(),
77+
doc: ::std::ptr::null(),
78+
lang: ::remacs_sys::Lisp_Subr_Lang_Rust,
79+
};
80+
unsafe {
81+
let ptr = ::remacs_sys::xmalloc(::std::mem::size_of::<::remacs_sys::Lisp_Subr>())
82+
as *mut ::remacs_sys::Lisp_Subr;
83+
::std::ptr::copy_nonoverlapping(&subr, ptr, 1);
84+
::std::mem::forget(subr);
85+
::lisp::ExternalPtr::new(ptr)
86+
}
87+
};
88+
}
89+
90+
lazy_static::lazy_static! {
91+
static ref FOO: HashMap<
92+
String,
93+
(
94+
&'static str,
95+
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
96+
),
97+
> = HashMap::new();
98+
}

0 commit comments

Comments
 (0)