1 //! This module contains utilities that work with the `SourceMap` from `libsyntax`/`syntex_syntax`.
2 //! This includes extension traits and methods for looking up spans and line ranges for AST nodes.
4 use syntax::source_map::{BytePos, SourceMap, Span};
6 use crate::comment::FindUncommented;
7 use crate::config::file_lines::LineRange;
8 use crate::utils::starts_with_newline;
9 use crate::visitor::SnippetProvider;
11 pub(crate) trait SpanUtils {
12 fn span_after(&self, original: Span, needle: &str) -> BytePos;
13 fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
14 fn span_before(&self, original: Span, needle: &str) -> BytePos;
15 fn span_before_last(&self, original: Span, needle: &str) -> BytePos;
16 fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos>;
17 fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos>;
20 pub(crate) trait LineRangeUtils {
21 /// Returns the `LineRange` that corresponds to `span` in `self`.
25 /// Panics if `span` crosses a file boundary, which shouldn't happen.
26 fn lookup_line_range(&self, span: Span) -> LineRange;
29 impl<'a> SpanUtils for SnippetProvider<'a> {
30 fn span_after(&self, original: Span, needle: &str) -> BytePos {
31 self.opt_span_after(original, needle).unwrap_or_else(|| {
33 "bad span: `{}`: `{}`",
35 self.span_to_snippet(original).unwrap()
40 fn span_after_last(&self, original: Span, needle: &str) -> BytePos {
41 let snippet = self.span_to_snippet(original).unwrap();
44 while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
45 offset += additional_offset + needle.len();
48 original.lo() + BytePos(offset as u32)
51 fn span_before(&self, original: Span, needle: &str) -> BytePos {
52 self.opt_span_before(original, needle).unwrap_or_else(|| {
54 "bad span: `{}`: `{}`",
56 self.span_to_snippet(original).unwrap()
61 fn span_before_last(&self, original: Span, needle: &str) -> BytePos {
62 let snippet = self.span_to_snippet(original).unwrap();
65 while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
66 offset += additional_offset + needle.len();
69 original.lo() + BytePos(offset as u32 - 1)
72 fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
73 self.opt_span_before(original, needle)
74 .map(|bytepos| bytepos + BytePos(needle.len() as u32))
77 fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos> {
78 let snippet = self.span_to_snippet(original)?;
79 let offset = snippet.find_uncommented(needle)?;
81 Some(original.lo() + BytePos(offset as u32))
85 impl LineRangeUtils for SourceMap {
86 fn lookup_line_range(&self, span: Span) -> LineRange {
87 let snippet = self.span_to_snippet(span).unwrap_or_default();
88 let lo = self.lookup_line(span.lo()).unwrap();
89 let hi = self.lookup_line(span.hi()).unwrap();
92 lo.sf.name, hi.sf.name,
93 "span crossed file boundary: lo: {:?}, hi: {:?}",
97 // in case the span starts with a newline, the line range is off by 1 without the
99 let offset = 1 + if starts_with_newline(&snippet) { 1 } else { 0 };
100 // Line numbers start at 1
103 lo: lo.line + offset,
104 hi: hi.line + offset,