//! This module contains utilities that work with the `CodeMap` from `libsyntax` / `syntex_syntax`.
//! This includes extension traits and methods for looking up spans and line ranges for AST nodes.
-use std::rc::Rc;
-
-use syntax::codemap::{BytePos, CodeMap, FileMap, Span};
+use config::file_lines::LineRange;
+use syntax::codemap::{BytePos, CodeMap, Span};
+use visitor::SnippetProvider;
use comment::FindUncommented;
-/// A range of lines in a file, inclusive of both ends.
-pub struct LineRange {
- pub file: Rc<FileMap>,
- pub lo: usize,
- pub hi: usize,
-}
-
-impl LineRange {
- pub fn file_name(&self) -> &str {
- self.file.as_ref().name.as_str()
- }
-}
-
pub trait SpanUtils {
fn span_after(&self, original: Span, needle: &str) -> BytePos;
fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
fn span_before(&self, original: Span, needle: &str) -> BytePos;
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos>;
+ fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos>;
}
pub trait LineRangeUtils {
fn lookup_line_range(&self, span: Span) -> LineRange;
}
-impl SpanUtils for CodeMap {
+impl<'a> SpanUtils for SnippetProvider<'a> {
fn span_after(&self, original: Span, needle: &str) -> BytePos {
- let snippet = self.span_to_snippet(original).unwrap();
- let offset = snippet.find_uncommented(needle).unwrap() + needle.len();
-
- original.lo() + BytePos(offset as u32)
+ self.opt_span_after(original, needle).expect("bad span")
}
fn span_after_last(&self, original: Span, needle: &str) -> BytePos {
}
fn span_before(&self, original: Span, needle: &str) -> BytePos {
- let snippet = self.span_to_snippet(original).unwrap();
- let offset = snippet.find_uncommented(needle).unwrap();
-
- original.lo() + BytePos(offset as u32)
+ self.opt_span_before(original, needle).expect(&format!(
+ "bad span: {}: {}",
+ needle,
+ self.span_to_snippet(original).unwrap()
+ ))
}
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
- let snippet = self.span_to_snippet(original).ok()?;
- let offset = snippet.find_uncommented(needle)? + needle.len();
+ self.opt_span_before(original, needle)
+ .map(|bytepos| bytepos + BytePos(needle.len() as u32))
+ }
+
+ fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos> {
+ let snippet = self.span_to_snippet(original)?;
+ let offset = snippet.find_uncommented(needle)?;
Some(original.lo() + BytePos(offset as u32))
}
impl LineRangeUtils for CodeMap {
fn lookup_line_range(&self, span: Span) -> LineRange {
- let lo = self.lookup_char_pos(span.lo());
- let hi = self.lookup_char_pos(span.hi());
+ let lo = self.lookup_line(span.lo()).unwrap();
+ let hi = self.lookup_line(span.hi()).unwrap();
- assert_eq!(
- lo.file.name,
- hi.file.name,
+ debug_assert_eq!(
+ lo.fm.name, hi.fm.name,
"span crossed file boundary: lo: {:?}, hi: {:?}",
- lo,
- hi
+ lo, hi
);
+ // Line numbers start at 1
LineRange {
- file: lo.file.clone(),
- lo: lo.line,
- hi: hi.line,
+ file: lo.fm.clone(),
+ lo: lo.line + 1,
+ hi: hi.line + 1,
}
}
}