diff options
| author | topecongiro <seuchida@gmail.com> | 2018-02-07 22:48:05 +0900 |
|---|---|---|
| committer | topecongiro <seuchida@gmail.com> | 2018-02-07 22:48:05 +0900 |
| commit | 4af2aa3a9e2ab32e584c3d7bc97d74bdc8b35836 (patch) | |
| tree | ab74c699216d5b6bcafb21dcf6b09a2878efa05d /src/codemap.rs | |
| parent | c9e250a1ab4983875cba2dcd5f083a6694d70e8f (diff) | |
Create rustfmt_core crate
Diffstat (limited to 'src/codemap.rs')
| -rw-r--r-- | src/codemap.rs | 100 |
1 files changed, 0 insertions, 100 deletions
diff --git a/src/codemap.rs b/src/codemap.rs deleted file mode 100644 index d74d24439d9..00000000000 --- a/src/codemap.rs +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright 2016 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! 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, FileName, 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) -> &FileName { - &self.file.name - } -} - -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>; -} - -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 { - fn span_after(&self, original: Span, needle: &str) -> BytePos { - let snippet = self.span_to_snippet(original).expect("Bad snippet"); - let offset = snippet.find_uncommented(needle).expect("Bad offset") + needle.len(); - - original.lo() + BytePos(offset as u32) - } - - fn span_after_last(&self, original: Span, needle: &str) -> BytePos { - let snippet = self.span_to_snippet(original).unwrap(); - let mut offset = 0; - - while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) { - offset += additional_offset + needle.len(); - } - - original.lo() + BytePos(offset as u32) - } - - 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) - } - - 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(); - - 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()); - - assert_eq!( - 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, - } - } -} |
