Skip to content

Commit 2b0f7b0

Browse files
committed
Support formatting for lazy_static::lazy_static!
1 parent 0332da0 commit 2b0f7b0

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

src/macros.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ 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!" || macro_name == "lazy_static::lazy_static!") && !has_comment {
230+
match format_lazy_static(context, shape, ts.clone(), mac.span(), &macro_name) {
231231
Ok(rw) => return Ok(rw),
232232
Err(err) => match err {
233233
// We will move on to parsing macro args just like other macros
@@ -1394,7 +1394,8 @@ impl MacroBranch {
13941394
}
13951395
}
13961396

1397-
/// Format `lazy_static!` from <https://crates.io/crates/lazy_static>.
1397+
/// Format `lazy_static!` and `lazy_static::lazy_static!`
1398+
/// from <https://crates.io/crates/lazy_static>.
13981399
///
13991400
/// # Expected syntax
14001401
///
@@ -1405,19 +1406,28 @@ impl MacroBranch {
14051406
/// ...
14061407
/// [pub] static ref NAME_N: TYPE_N = EXPR_N;
14071408
/// }
1409+
///
1410+
/// lazy_static::lazy_static! {
1411+
/// [pub] static ref NAME_1: TYPE_1 = EXPR_1;
1412+
/// [pub] static ref NAME_2: TYPE_2 = EXPR_2;
1413+
/// ...
1414+
/// [pub] static ref NAME_N: TYPE_N = EXPR_N;
1415+
/// }
14081416
/// ```
14091417
fn format_lazy_static(
14101418
context: &RewriteContext<'_>,
14111419
shape: Shape,
14121420
ts: TokenStream,
14131421
span: Span,
1422+
macro_name: &str,
14141423
) -> RewriteResult {
14151424
let mut result = String::with_capacity(1024);
14161425
let nested_shape = shape
14171426
.block_indent(context.config.tab_spaces())
14181427
.with_max_width(context.config);
14191428

1420-
result.push_str("lazy_static! {");
1429+
result.push_str(macro_name);
1430+
result.push_str(" {");
14211431
result.push_str(&nested_shape.indent.to_string_with_newline(context.config));
14221432

14231433
let parsed_elems =

tests/source/lazy_static.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Format `lazy_static!`.
1+
// Format `lazy_static!` and `lazy_static::lazy_static!`.
22

33
lazy_static! {
44
static ref CONFIG_NAME_REGEX: regex::Regex =
@@ -43,3 +43,47 @@ static ref FOO: HashMap<String,
4343
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
4444
),> = HashMap::new();
4545
}
46+
47+
lazy_static::lazy_static! {
48+
static ref CONFIG_NAME_REGEX: regex::Regex =
49+
regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern");
50+
static ref CONFIG_VALUE_REGEX: regex::Regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#)
51+
.expect("Failed creating configuration value pattern");
52+
}
53+
54+
// We need to be able to format `lazy_static::lazy_static!` without known syntax.
55+
lazy_static::lazy_static!(
56+
xxx,
57+
yyyy ,
58+
zzzzz
59+
);
60+
61+
lazy_static::lazy_static!{
62+
}
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 : (
70+
( :: remacs_sys :: PseudovecType :: PVEC_SUBR as :: libc :: ptrdiff_t ) << ::
71+
remacs_sys :: PSEUDOVECTOR_AREA_BITS ) , } , function : self ::
72+
Fbase64_encode_string as * const :: libc :: c_void , min_args : 1i16 ,
73+
max_args : 2i16 , symbol_name : ( b"base64-encode-string\x00" ) . as_ptr ( )
74+
as * const :: libc :: c_char , intspec : :: std :: ptr :: null ( ) , doc : ::
75+
std :: ptr :: null ( ) , lang : :: remacs_sys :: Lisp_Subr_Lang_Rust , } ;
76+
unsafe {
77+
let ptr = :: remacs_sys :: xmalloc (
78+
:: std :: mem :: size_of :: < :: remacs_sys :: Lisp_Subr > ( ) ) as * mut ::
79+
remacs_sys :: Lisp_Subr ; :: std :: ptr :: copy_nonoverlapping (
80+
& subr , ptr , 1 ) ; :: std :: mem :: forget ( subr ) ; :: lisp :: ExternalPtr
81+
:: new ( ptr ) } } ; }
82+
83+
84+
lazy_static::lazy_static! {
85+
static ref FOO: HashMap<String,
86+
(&'static str,
87+
fn(Foo) -> Result<Box<Bar>, Either<FooError, BarError>>
88+
),> = HashMap::new();
89+
}

tests/target/lazy_static.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Format `lazy_static!`.
1+
// Format `lazy_static!` and `lazy_static::lazy_static!`.
22

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

0 commit comments

Comments
 (0)