Skip to content

Commit 0dc8736

Browse files
committed
Add DelimSpan to hold the 3 different spans of a delimiter
1 parent 1e7fc7b commit 0dc8736

File tree

3 files changed

+114
-4
lines changed

3 files changed

+114
-4
lines changed

src/extra.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//! Items which do not have a correspondence to any API in the proc_macro crate,
2+
//! but are necessary to include in proc-macro2.
3+
4+
use crate::fallback;
5+
use crate::imp;
6+
use crate::marker::Marker;
7+
use crate::Span;
8+
use core::fmt::{self, Debug};
9+
10+
/// An object that holds a [`Group`]'s `span_open()` and `span_close()` together
11+
/// (in a more compact representation than holding those 2 spans individually.
12+
///
13+
/// [`Group`]: crate::Group
14+
#[derive(Copy, Clone)]
15+
pub struct DelimSpan {
16+
inner: DelimSpanEnum,
17+
_marker: Marker,
18+
}
19+
20+
#[derive(Copy, Clone)]
21+
enum DelimSpanEnum {
22+
#[cfg(wrap_proc_macro)]
23+
Compiler {
24+
join: proc_macro::Span,
25+
#[cfg(not(no_group_open_close))]
26+
open: proc_macro::Span,
27+
#[cfg(not(no_group_open_close))]
28+
close: proc_macro::Span,
29+
},
30+
Fallback(fallback::Span),
31+
}
32+
33+
impl DelimSpan {
34+
pub(crate) fn new(group: &imp::Group) -> Self {
35+
#[cfg(wrap_proc_macro)]
36+
let inner = match group {
37+
imp::Group::Compiler(group) => DelimSpanEnum::Compiler {
38+
join: group.span(),
39+
#[cfg(not(no_group_open_close))]
40+
open: group.span_open(),
41+
#[cfg(not(no_group_open_close))]
42+
close: group.span_close(),
43+
},
44+
imp::Group::Fallback(group) => DelimSpanEnum::Fallback(group.span()),
45+
};
46+
47+
#[cfg(not(wrap_proc_macro))]
48+
let inner = DelimSpanEnum::Fallback(group.span());
49+
50+
DelimSpan {
51+
inner,
52+
_marker: Marker,
53+
}
54+
}
55+
56+
/// Returns a span covering the entire delimited group.
57+
pub fn join(&self) -> Span {
58+
match &self.inner {
59+
#[cfg(wrap_proc_macro)]
60+
DelimSpanEnum::Compiler { join, .. } => Span::_new(imp::Span::Compiler(*join)),
61+
DelimSpanEnum::Fallback(span) => Span::_new_stable(*span),
62+
}
63+
}
64+
65+
/// Returns a span for the opening punctuation of the group only.
66+
pub fn open(&self) -> Span {
67+
match &self.inner {
68+
#[cfg(wrap_proc_macro)]
69+
DelimSpanEnum::Compiler {
70+
#[cfg(not(no_group_open_close))]
71+
open,
72+
#[cfg(no_group_open_close)]
73+
join: open,
74+
..
75+
} => Span::_new(imp::Span::Compiler(*open)),
76+
DelimSpanEnum::Fallback(span) => Span::_new_stable(span.first_byte()),
77+
}
78+
}
79+
80+
/// Returns a span for the closing punctuation of the group only.
81+
pub fn close(&self) -> Span {
82+
match &self.inner {
83+
#[cfg(wrap_proc_macro)]
84+
DelimSpanEnum::Compiler {
85+
#[cfg(not(no_group_open_close))]
86+
close,
87+
#[cfg(no_group_open_close)]
88+
join: close,
89+
..
90+
} => Span::_new(imp::Span::Compiler(*close)),
91+
DelimSpanEnum::Fallback(span) => Span::_new_stable(span.last_byte()),
92+
}
93+
}
94+
}
95+
96+
impl Debug for DelimSpan {
97+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98+
Debug::fmt(&self.join(), f)
99+
}
100+
}

src/fallback.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,25 +555,25 @@ impl Span {
555555
}
556556

557557
#[cfg(not(span_locations))]
558-
fn first_byte(self) -> Self {
558+
pub(crate) fn first_byte(self) -> Self {
559559
self
560560
}
561561

562562
#[cfg(span_locations)]
563-
fn first_byte(self) -> Self {
563+
pub(crate) fn first_byte(self) -> Self {
564564
Span {
565565
lo: self.lo,
566566
hi: cmp::min(self.lo.saturating_add(1), self.hi),
567567
}
568568
}
569569

570570
#[cfg(not(span_locations))]
571-
fn last_byte(self) -> Self {
571+
pub(crate) fn last_byte(self) -> Self {
572572
self
573573
}
574574

575575
#[cfg(span_locations)]
576-
fn last_byte(self) -> Self {
576+
pub(crate) fn last_byte(self) -> Self {
577577
Span {
578578
lo: cmp::max(self.hi.saturating_sub(1), self.lo),
579579
hi: self.hi,

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ mod detection;
134134
#[doc(hidden)]
135135
pub mod fallback;
136136

137+
pub mod extra;
138+
137139
#[cfg(not(wrap_proc_macro))]
138140
use crate::fallback as imp;
139141
#[path = "wrapper.rs"]
@@ -143,6 +145,7 @@ mod imp;
143145
#[cfg(span_locations)]
144146
mod location;
145147

148+
use crate::extra::DelimSpan;
146149
use crate::marker::Marker;
147150
use core::cmp::Ordering;
148151
use core::fmt::{self, Debug, Display};
@@ -727,6 +730,13 @@ impl Group {
727730
Span::_new(self.inner.span_close())
728731
}
729732

733+
/// Returns an object that holds this group's `span_open()` and
734+
/// `span_close()` together (in a more compact representation than holding
735+
/// those 2 spans individually).
736+
pub fn delim_span(&self) -> DelimSpan {
737+
DelimSpan::new(&self.inner)
738+
}
739+
730740
/// Configures the span for this `Group`'s delimiters, but not its internal
731741
/// tokens.
732742
///

0 commit comments

Comments
 (0)