]> git.lizzy.rs Git - rust.git/blob - src/codemap.rs
Remove BlockIndentStyle::Inherit
[rust.git] / src / codemap.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! This module contains utilities that work with the `CodeMap` from libsyntax / syntex_syntax.
12 //! This includes extension traits and methods for looking up spans and line ranges for AST nodes.
13
14 use std::rc::Rc;
15
16 use syntax::codemap::{BytePos, CodeMap, FileMap, Span};
17
18 use comment::FindUncommented;
19
20 /// A range of lines in a file, inclusive of both ends.
21 pub struct LineRange {
22     pub file: Rc<FileMap>,
23     pub lo: usize,
24     pub hi: usize,
25 }
26
27 impl LineRange {
28     pub fn file_name(&self) -> &str {
29         self.file
30             .as_ref()
31             .name
32             .as_str()
33     }
34 }
35
36 pub trait SpanUtils {
37     fn span_after(&self, original: Span, needle: &str) -> BytePos;
38     fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
39     fn span_before(&self, original: Span, needle: &str) -> BytePos;
40 }
41
42 pub trait LineRangeUtils {
43     /// Returns the `LineRange` that corresponds to `span` in `self`.
44     ///
45     /// # Panics
46     ///
47     /// Panics if `span` crosses a file boundary, which shouldn't happen.
48     fn lookup_line_range(&self, span: Span) -> LineRange;
49 }
50
51 impl SpanUtils for CodeMap {
52     fn span_after(&self, original: Span, needle: &str) -> BytePos {
53         let snippet = self.span_to_snippet(original).unwrap();
54         let offset = snippet.find_uncommented(needle).unwrap() + needle.len();
55
56         original.lo + BytePos(offset as u32)
57     }
58
59     fn span_after_last(&self, original: Span, needle: &str) -> BytePos {
60         let snippet = self.span_to_snippet(original).unwrap();
61         let mut offset = 0;
62
63         while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
64             offset += additional_offset + needle.len();
65         }
66
67         original.lo + BytePos(offset as u32)
68     }
69
70     fn span_before(&self, original: Span, needle: &str) -> BytePos {
71         let snippet = self.span_to_snippet(original).unwrap();
72         let offset = snippet.find_uncommented(needle).unwrap();
73
74         original.lo + BytePos(offset as u32)
75     }
76 }
77
78 impl LineRangeUtils for CodeMap {
79     fn lookup_line_range(&self, span: Span) -> LineRange {
80         let lo = self.lookup_char_pos(span.lo);
81         let hi = self.lookup_char_pos(span.hi);
82
83         assert!(lo.file.name == hi.file.name,
84                 "span crossed file boundary: lo: {:?}, hi: {:?}",
85                 lo,
86                 hi);
87
88         LineRange {
89             file: lo.file.clone(),
90             lo: lo.line,
91             hi: hi.line,
92         }
93     }
94 }