Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand only one file #8

Closed
Yamakaky opened this issue Jan 23, 2017 · 4 comments
Closed

Expand only one file #8

Yamakaky opened this issue Jan 23, 2017 · 4 comments

Comments

@Yamakaky
Copy link

Currently, it displays the expansion of the entire crate. For big crates, that's not very practical. Often, you want to see the expansion of only one file.

@TedDriggs
Copy link

Being able to get the expansion of one type would also be sufficient, if that's easier to implement (after looking at the code, I'm not sure how much control expand has here).

@dtolnay
Copy link
Owner

dtolnay commented Jan 14, 2019

I added support in cargo-expand 0.4 for expanding only one module or item. Check out these examples from the Syn codebase:

$ cargo expand punctuated::printing
    Checking syn v0.15.24
    Finished dev [unoptimized + debuginfo] target(s) in 0.17s

use super::*;
use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt};
impl<T, P> ToTokens for Punctuated<T, P>
where
    T: ToTokens,
    P: ToTokens,
{
    fn to_tokens(&self, tokens: &mut TokenStream) {
        tokens.append_all(self.pairs())
    }
}
impl<T, P> ToTokens for Pair<T, P>
where
    T: ToTokens,
    P: ToTokens,
{
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match *self {
            Pair::Punctuated(ref a, ref b) => {
                a.to_tokens(tokens);
                b.to_tokens(tokens);
            }
            Pair::End(ref a) => a.to_tokens(tokens),
        }
    }
}
$ cargo expand token::FatArrow
    Checking syn v0.15.24
    Finished dev [unoptimized + debuginfo] target(s) in 0.11s

/// `=>`
///
/// Don't try to remember the name of this type -- use the [`Token!`]
/// macro instead.
///
/// [`Token!`]: index.html
#[rustc_copy_clone_marker]
pub struct FatArrow {
    pub spans: [Span; 2],
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub fn FatArrow<S: IntoSpans<[Span; 2]>>(spans: S) -> FatArrow {
    FatArrow {
        spans: spans.into_spans(),
    }
}

@dtolnay dtolnay closed this as completed Jan 14, 2019
@vedantroy
Copy link

Would it be possible to use this to expand code in a test?

@videni
Copy link

videni commented Aug 1, 2021

@dtolnay , do you mean we can expand a specific micro? for example, I am using a micro , lazy_static::static_loader,

then I can expand with cargo expand lazy_static::static_loader, I tried , got

cargo expand lazy_static::static_loader                                                                             

WARNING: no such item: lazy_static::static_loader   


this crate is not usefull for me , be cause it expand lots of micros, it would take hours to find the one I need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants