]> git.lizzy.rs Git - rust.git/blob - src/source_map.rs
Merge pull request #3472 from devinalvaro/add-print-current-config
[rust.git] / src / source_map.rs
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.
3
4 use syntax::source_map::{BytePos, SourceMap, Span};
5
6 use crate::comment::FindUncommented;
7 use crate::config::file_lines::LineRange;
8 use crate::visitor::SnippetProvider;
9
10 pub trait SpanUtils {
11     fn span_after(&self, original: Span, needle: &str) -> BytePos;
12     fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
13     fn span_before(&self, original: Span, needle: &str) -> BytePos;
14     fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos>;
15     fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos>;
16 }
17
18 pub trait LineRangeUtils {
19     /// Returns the `LineRange` that corresponds to `span` in `self`.
20     ///
21     /// # Panics
22     ///
23     /// Panics if `span` crosses a file boundary, which shouldn't happen.
24     fn lookup_line_range(&self, span: Span) -> LineRange;
25 }
26
27 impl<'a> SpanUtils for SnippetProvider<'a> {
28     fn span_after(&self, original: Span, needle: &str) -> BytePos {
29         self.opt_span_after(original, needle).unwrap_or_else(|| {
30             panic!(
31                 "bad span: `{}`: `{}`",
32                 needle,
33                 self.span_to_snippet(original).unwrap()
34             )
35         })
36     }
37
38     fn span_after_last(&self, original: Span, needle: &str) -> BytePos {
39         let snippet = self.span_to_snippet(original).unwrap();
40         let mut offset = 0;
41
42         while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
43             offset += additional_offset + needle.len();
44         }
45
46         original.lo() + BytePos(offset as u32)
47     }
48
49     fn span_before(&self, original: Span, needle: &str) -> BytePos {
50         self.opt_span_before(original, needle).unwrap_or_else(|| {
51             panic!(
52                 "bad span: `{}`: `{}`",
53                 needle,
54                 self.span_to_snippet(original).unwrap()
55             )
56         })
57     }
58
59     fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
60         self.opt_span_before(original, needle)
61             .map(|bytepos| bytepos + BytePos(needle.len() as u32))
62     }
63
64     fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos> {
65         let snippet = self.span_to_snippet(original)?;
66         let offset = snippet.find_uncommented(needle)?;
67
68         Some(original.lo() + BytePos(offset as u32))
69     }
70 }
71
72 impl LineRangeUtils for SourceMap {
73     fn lookup_line_range(&self, span: Span) -> LineRange {
74         let snippet = self.span_to_snippet(span).unwrap_or(String::new());
75         let lo = self.lookup_line(span.lo()).unwrap();
76         let hi = self.lookup_line(span.hi()).unwrap();
77
78         debug_assert_eq!(
79             lo.sf.name, hi.sf.name,
80             "span crossed file boundary: lo: {:?}, hi: {:?}",
81             lo, hi
82         );
83
84         // in case the span starts with a newline, the line range is off by 1 without the
85         // adjustment below
86         let offset = 1 + if snippet.starts_with('\n') { 1 } else { 0 };
87         // Line numbers start at 1
88         LineRange {
89             file: lo.sf.clone(),
90             lo: lo.line + offset,
91             hi: hi.line + offset,
92         }
93     }
94 }