Skip to content

Commit 0b36b20

Browse files
committed
CodeMap functions refactored.
- Using `span_take_while` to implement others.
1 parent 74a4928 commit 0b36b20

File tree

1 file changed

+38
-50
lines changed

1 file changed

+38
-50
lines changed

src/libsyntax/codemap.rs

+38-50
Original file line numberDiff line numberDiff line change
@@ -597,21 +597,6 @@ impl CodeMap {
597597
self.span_to_source(sp, |src, start_index, _| src[..start_index].to_string())
598598
}
599599

600-
/// Given a `Span`, try to get a shorter span ending before the first occurrence of `c` `char`
601-
pub fn span_until_char(&self, sp: Span, c: char) -> Span {
602-
match self.span_to_snippet(sp) {
603-
Ok(snippet) => {
604-
let snippet = snippet.split(c).nth(0).unwrap_or("").trim_right();
605-
if !snippet.is_empty() && !snippet.contains('\n') {
606-
sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32))
607-
} else {
608-
sp
609-
}
610-
}
611-
_ => sp,
612-
}
613-
}
614-
615600
/// Extend the given `Span` to just after the previous occurrence of `c`. Return the same span
616601
/// if no character could be found or if an error occurred while retrieving the code snippet.
617602
pub fn span_extend_to_prev_char(&self, sp: Span, c: char) -> Span {
@@ -646,55 +631,58 @@ impl CodeMap {
646631
sp
647632
}
648633

634+
/// Given a `Span`, try to get a shorter span ending before the first occurrence of `c` `char`
635+
pub fn span_until_char(&self, sp: Span, c: char) -> Span {
636+
match self.span_to_snippet(sp) {
637+
Ok(snippet) => {
638+
let snippet = snippet.split(c).nth(0).unwrap_or("").trim_right();
639+
if !snippet.is_empty() && !snippet.contains('\n') {
640+
sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32))
641+
} else {
642+
sp
643+
}
644+
}
645+
_ => sp,
646+
}
647+
}
648+
649+
/// Given a `Span`, try to get a shorter span ending just after the first occurrence of `char`
650+
/// `c`.
651+
pub fn span_through_char(&self, sp: Span, c: char) -> Span {
652+
if let Ok(snippet) = self.span_to_snippet(sp) {
653+
if let Some(offset) = snippet.find(c) {
654+
return sp.with_hi(BytePos(sp.lo().0 + (offset + c.len_utf8()) as u32));
655+
}
656+
}
657+
sp
658+
}
659+
649660
/// Given a `Span`, get a new `Span` covering the first token and all its trailing whitespace or
650661
/// the original `Span`.
651662
///
652663
/// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned.
653664
pub fn span_until_non_whitespace(&self, sp: Span) -> Span {
654-
if let Ok(snippet) = self.span_to_snippet(sp) {
655-
let mut offset = 0;
656-
// get the bytes width of all the non-whitespace characters
657-
for c in snippet.chars().take_while(|c| !c.is_whitespace()) {
658-
offset += c.len_utf8();
659-
}
660-
// get the bytes width of all the whitespace characters after that
661-
for c in snippet[offset..].chars().take_while(|c| c.is_whitespace()) {
662-
offset += c.len_utf8();
665+
let mut whitespace_found = false;
666+
667+
self.span_take_while(sp, |c| {
668+
if !whitespace_found && c.is_whitespace() {
669+
whitespace_found = true;
663670
}
664-
if offset > 1 {
665-
return sp.with_hi(BytePos(sp.lo().0 + offset as u32));
671+
672+
if whitespace_found && !c.is_whitespace() {
673+
false
674+
} else {
675+
true
666676
}
667-
}
668-
sp
677+
})
669678
}
670679

671680
/// Given a `Span`, get a new `Span` covering the first token without its trailing whitespace or
672681
/// the original `Span` in case of error.
673682
///
674683
/// If `sp` points to `"let mut x"`, then a span pointing at `"let"` will be returned.
675684
pub fn span_until_whitespace(&self, sp: Span) -> Span {
676-
if let Ok(snippet) = self.span_to_snippet(sp) {
677-
let mut offset = 0;
678-
// Get the bytes width of all the non-whitespace characters
679-
for c in snippet.chars().take_while(|c| !c.is_whitespace()) {
680-
offset += c.len_utf8();
681-
}
682-
if offset > 1 {
683-
return sp.with_hi(BytePos(sp.lo().0 + offset as u32));
684-
}
685-
}
686-
sp
687-
}
688-
689-
/// Given a `Span`, try to get a shorter span ending just after the first occurrence of `char`
690-
/// `c`.
691-
pub fn span_through_char(&self, sp: Span, c: char) -> Span {
692-
if let Ok(snippet) = self.span_to_snippet(sp) {
693-
if let Some(offset) = snippet.find(c) {
694-
return sp.with_hi(BytePos(sp.lo().0 + (offset + c.len_utf8()) as u32));
695-
}
696-
}
697-
sp
685+
self.span_take_while(sp, |c| !c.is_whitespace())
698686
}
699687

700688
/// Given a `Span`, get a shorter one until `predicate` yields false.

0 commit comments

Comments
 (0)