diff options
| author | Kamal Marhubi <kamal@marhubi.com> | 2016-03-07 13:41:32 -0500 |
|---|---|---|
| committer | Kamal Marhubi <kamal@marhubi.com> | 2016-03-13 18:14:07 -0400 |
| commit | d82d9fc808bfa3f99844ed40f622c8a53e2d267e (patch) | |
| tree | 7ab35a3dce6c6039d64d7bb2fc5043ea2d21c379 /src/utils.rs | |
| parent | 80db099558e1a15352b7e7911aa3e3d002baa210 (diff) | |
utils: Add CodeMapSpanUtils trait for span_* methods
This commit adds a CodeMapSpanUtils extension trait on CodeMap, and moves some functions to methods there: - span_after - span_after_last - span_before This better reflects them being lookup methods on the codemap.
Diffstat (limited to 'src/utils.rs')
| -rw-r--r-- | src/utils.rs | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/src/utils.rs b/src/utils.rs index 3b03ede2be3..5c2bca73432 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -20,42 +20,50 @@ use rewrite::{Rewrite, RewriteContext}; use SKIP_ANNOTATION; -// Computes the length of a string's last line, minus offset. -#[inline] -pub fn extra_offset(text: &str, offset: Indent) -> usize { - match text.rfind('\n') { - // 1 for newline character - Some(idx) => text.len() - idx - 1 - offset.width(), - None => text.len(), - } +pub trait CodeMapSpanUtils { + 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; } -#[inline] -pub fn span_after(original: Span, needle: &str, codemap: &CodeMap) -> BytePos { - let snippet = codemap.span_to_snippet(original).unwrap(); - let offset = snippet.find_uncommented(needle).unwrap() + needle.len(); +impl CodeMapSpanUtils 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) -} + original.lo + BytePos(offset as u32) + } -#[inline] -pub fn span_before(original: Span, needle: &str, codemap: &CodeMap) -> BytePos { - let snippet = codemap.span_to_snippet(original).unwrap(); - let offset = snippet.find_uncommented(needle).unwrap(); + #[inline] + fn span_after_last(&self, original: Span, needle: &str) -> BytePos { + let snippet = self.span_to_snippet(original).unwrap(); + let mut offset = 0; - original.lo + BytePos(offset as u32) -} + while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) { + offset += additional_offset + needle.len(); + } -#[inline] -pub fn span_after_last(original: Span, needle: &str, codemap: &CodeMap) -> BytePos { - let snippet = codemap.span_to_snippet(original).unwrap(); - let mut offset = 0; + 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(); - while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) { - offset += additional_offset + needle.len(); + original.lo + BytePos(offset as u32) } +} - original.lo + BytePos(offset as u32) +// Computes the length of a string's last line, minus offset. +#[inline] +pub fn extra_offset(text: &str, offset: Indent) -> usize { + match text.rfind('\n') { + // 1 for newline character + Some(idx) => text.len() - idx - 1 - offset.width(), + None => text.len(), + } } #[inline] |
