mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-17 09:23:05 +00:00
codemap: Add utilities for looking up line ranges of spans
This commit adds extension methods to `Codemap` to allow looking up line ranges for spans. Refs #434
This commit is contained in:
parent
bd10af127e
commit
ed27b4799a
@ -1,13 +1,37 @@
|
||||
use syntax::codemap::{BytePos, CodeMap, Span};
|
||||
use std::rc::Rc;
|
||||
|
||||
use syntax::codemap::{BytePos, CodeMap, FileMap, Span};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
pub trait LineRangeUtils {
|
||||
/// Returns the `LineRange` that corresponds to `span` in `self`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `span` crosses a file boundary, which shouldn't happen.
|
||||
fn lookup_line_range(&self, span: Span) -> LineRange;
|
||||
}
|
||||
|
||||
impl SpanUtils for CodeMap {
|
||||
#[inline]
|
||||
fn span_after(&self, original: Span, needle: &str) -> BytePos {
|
||||
@ -37,3 +61,21 @@ impl SpanUtils for CodeMap {
|
||||
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);
|
||||
|
||||
assert!(lo.file.name == hi.file.name,
|
||||
"span crossed file boundary: lo: {:?}, hi: {:?}",
|
||||
lo,
|
||||
hi);
|
||||
|
||||
LineRange {
|
||||
file: lo.file.clone(),
|
||||
lo: lo.line,
|
||||
hi: hi.line,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user