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

Support meaningful spans in the stable version of proc-macro2 #36

Merged
merged 11 commits into from
Dec 31, 2017
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ doctest = false

[dependencies]
unicode-xid = "0.1"
memchr = "2.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With @Manishearth's work on improving the performance of the find method in rust-lang/rust#46735, it probably won't be useful to have this dependency on memchr anymore, so we might want to drop it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's why you wanted this.

Sneaky, sneaky 😺


[features]
unstable = []
58 changes: 58 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

extern crate proc_macro;

#[cfg(not(feature = "unstable"))]
extern crate memchr;

#[cfg(not(feature = "unstable"))]
extern crate unicode_xid;

Expand Down Expand Up @@ -103,6 +106,43 @@ impl TokenStream {
}
}

#[derive(Clone, PartialEq, Eq)]
pub struct SourceFile(imp::SourceFile);

impl SourceFile {
/// Get the path to this source file as a string.
pub fn as_str(&self) -> &str {
self.0.as_str()
}

pub fn is_real(&self) -> bool {
self.0.is_real()
}
}

impl AsRef<str> for SourceFile {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}

impl PartialEq<str> for SourceFile {
fn eq(&self, other: &str) -> bool {
self.0.eq(other)
}
}

impl fmt::Debug for SourceFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}

// NOTE: We can't easily wrap LineColumn right now, as the version in proc-macro
// doesn't actually expose the internal `line` and `column` fields, making it
// mostly useless.
pub use imp::LineColumn;

#[derive(Copy, Clone)]
pub struct Span(imp::Span);

Expand All @@ -121,6 +161,24 @@ impl Span {
pub fn def_site() -> Span {
Span(imp::Span::def_site())
}

pub fn source_file(&self) -> SourceFile {
SourceFile(self.0.source_file())
}

pub fn start(&self) -> LineColumn {
// XXX(nika): We can't easily wrap LineColumn right now
self.0.start()
}

pub fn end(&self) -> LineColumn {
// XXX(nika): We can't easily wrap LineColumn right now
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of rust-lang/rust#46690, the fields have been made public in the proc-macro implementation, so we can probably do a wrap of this type now.

self.0.end()
}

pub fn join(&self, other: Span) -> Option<Span> {
self.0.join(other.0).map(Span)
}
}

#[derive(Clone, Debug)]
Expand Down
Loading