Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils: Move codemap related utilities to a dedicated module
This commit adds a `codemap` module, and moves the `CodemapSpanUtils` added in #857 to it. This is preparation for adding more `Codemap` specific utilities. Refs #434
- Loading branch information
1 parent
80c56a0
commit bd10af1
Showing
10 changed files
with
56 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use syntax::codemap::{BytePos, CodeMap, Span}; | ||
|
||
use comment::FindUncommented; | ||
|
||
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; | ||
} | ||
|
||
impl SpanUtils for CodeMap { | ||
#[inline] | ||
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) | ||
} | ||
|
||
#[inline] | ||
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) | ||
} | ||
|
||
#[inline] | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters