Skip to content

Commit f28cc74

Browse files
committed
utils: Move codemap related utilities to a dedicated module
This commit adds a `codemap` module, and moves the `CodemapSpanUtils` added in rust-lang#857 to it. This is preparation for adding more `Codemap` specific utilities. Refs rust-lang#434
1 parent 865ae7f commit f28cc74

File tree

10 files changed

+56
-47
lines changed

10 files changed

+56
-47
lines changed

src/codemap.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use syntax::codemap::{BytePos, CodeMap, Span};
2+
3+
use comment::FindUncommented;
4+
5+
pub trait SpanUtils {
6+
fn span_after(&self, original: Span, needle: &str) -> BytePos;
7+
fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
8+
fn span_before(&self, original: Span, needle: &str) -> BytePos;
9+
}
10+
11+
impl SpanUtils for CodeMap {
12+
#[inline]
13+
fn span_after(&self, original: Span, needle: &str) -> BytePos {
14+
let snippet = self.span_to_snippet(original).unwrap();
15+
let offset = snippet.find_uncommented(needle).unwrap() + needle.len();
16+
17+
original.lo + BytePos(offset as u32)
18+
}
19+
20+
#[inline]
21+
fn span_after_last(&self, original: Span, needle: &str) -> BytePos {
22+
let snippet = self.span_to_snippet(original).unwrap();
23+
let mut offset = 0;
24+
25+
while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
26+
offset += additional_offset + needle.len();
27+
}
28+
29+
original.lo + BytePos(offset as u32)
30+
}
31+
32+
#[inline]
33+
fn span_before(&self, original: Span, needle: &str) -> BytePos {
34+
let snippet = self.span_to_snippet(original).unwrap();
35+
let offset = snippet.find_uncommented(needle).unwrap();
36+
37+
original.lo + BytePos(offset as u32)
38+
}
39+
}

src/expr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ use std::iter::ExactSizeIterator;
1616
use std::fmt::Write;
1717

1818
use {Indent, Spanned};
19+
use codemap::SpanUtils;
1920
use rewrite::{Rewrite, RewriteContext};
2021
use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic,
2122
DefinitiveListTactic, definitive_tactic, ListItem, format_item_list};
2223
use string::{StringFormat, rewrite_string};
23-
use utils::{CodeMapSpanUtils, extra_offset, last_line_width, wrap_str, binary_search,
24-
first_line_width, semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr};
24+
use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
25+
semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr};
2526
use visitor::FmtVisitor;
2627
use config::{Config, StructLitStyle, MultilineStyle, ElseIfBraceStyle, ControlBraceStyle};
2728
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};

src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
// except according to those terms.
1010

1111
use Indent;
12+
use codemap::SpanUtils;
1213
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, definitive_tactic};
1314
use types::rewrite_path;
14-
use utils::CodeMapSpanUtils;
1515
use rewrite::{Rewrite, RewriteContext};
1616

1717
use syntax::ast;

src/items.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
// Formatting top-level items - functions, structs, enums, traits, impls.
1212

1313
use Indent;
14-
use utils::{CodeMapSpanUtils, format_mutability, format_visibility, contains_skip, end_typaram,
15-
wrap_str, last_line_width, semicolon_for_expr, format_unsafety, trim_newlines};
14+
use codemap::SpanUtils;
15+
use utils::{format_mutability, format_visibility, contains_skip, end_typaram, wrap_str,
16+
last_line_width, semicolon_for_expr, format_unsafety, trim_newlines};
1617
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic,
1718
DefinitiveListTactic, ListTactic, definitive_tactic, format_item_list};
1819
use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs};

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub use self::summary::Summary;
5151
#[macro_use]
5252
mod utils;
5353
pub mod config;
54+
pub mod codemap;
5455
pub mod filemap;
5556
pub mod visitor;
5657
mod checkstyle;

src/macros.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ use syntax::parse::tts_to_parser;
2525
use syntax::codemap::{mk_sp, BytePos};
2626

2727
use Indent;
28+
use codemap::SpanUtils;
2829
use rewrite::{Rewrite, RewriteContext};
2930
use expr::{rewrite_call, rewrite_array};
3031
use comment::{FindUncommented, contains_comment};
31-
use utils::{CodeMapSpanUtils, wrap_str};
32+
use utils::wrap_str;
3233

3334
const FORCED_BRACKET_MACROS: &'static [&'static str] = &["vec!"];
3435

src/patterns.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
// except according to those terms.
1010

1111
use Indent;
12+
use codemap::SpanUtils;
1213
use rewrite::{Rewrite, RewriteContext};
13-
use utils::{CodeMapSpanUtils, wrap_str, format_mutability};
14+
use utils::{wrap_str, format_mutability};
1415
use lists::{format_item_list, itemize_list};
1516
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
1617
use types::rewrite_path;

src/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ use syntax::codemap::{self, Span, BytePos};
1717
use syntax::abi;
1818

1919
use {Indent, Spanned};
20+
use codemap::SpanUtils;
2021
use lists::{format_item_list, itemize_list, format_fn_args};
2122
use rewrite::{Rewrite, RewriteContext};
22-
use utils::{CodeMapSpanUtils, extra_offset, format_mutability, wrap_str};
23+
use utils::{extra_offset, format_mutability, wrap_str};
2324
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
2425
use config::TypeDensity;
2526

src/utils.rs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,14 @@
1111
use std::cmp::Ordering;
1212

1313
use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItemKind};
14-
use syntax::codemap::{CodeMap, Span, BytePos};
14+
use syntax::codemap::BytePos;
1515
use syntax::abi;
1616

1717
use Indent;
18-
use comment::FindUncommented;
1918
use rewrite::{Rewrite, RewriteContext};
2019

2120
use SKIP_ANNOTATION;
2221

23-
pub trait CodeMapSpanUtils {
24-
fn span_after(&self, original: Span, needle: &str) -> BytePos;
25-
fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
26-
fn span_before(&self, original: Span, needle: &str) -> BytePos;
27-
}
28-
29-
impl CodeMapSpanUtils for CodeMap {
30-
#[inline]
31-
fn span_after(&self, original: Span, needle: &str) -> BytePos {
32-
let snippet = self.span_to_snippet(original).unwrap();
33-
let offset = snippet.find_uncommented(needle).unwrap() + needle.len();
34-
35-
original.lo + BytePos(offset as u32)
36-
}
37-
38-
#[inline]
39-
fn span_after_last(&self, original: Span, needle: &str) -> BytePos {
40-
let snippet = self.span_to_snippet(original).unwrap();
41-
let mut offset = 0;
42-
43-
while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
44-
offset += additional_offset + needle.len();
45-
}
46-
47-
original.lo + BytePos(offset as u32)
48-
}
49-
50-
#[inline]
51-
fn span_before(&self, original: Span, needle: &str) -> BytePos {
52-
let snippet = self.span_to_snippet(original).unwrap();
53-
let offset = snippet.find_uncommented(needle).unwrap();
54-
55-
original.lo + BytePos(offset as u32)
56-
}
57-
}
58-
5922
// Computes the length of a string's last line, minus offset.
6023
#[inline]
6124
pub fn extra_offset(text: &str, offset: Indent) -> usize {

src/visitor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use syntax::parse::ParseSess;
1515
use strings::string_buffer::StringBuffer;
1616

1717
use Indent;
18-
use utils::{self, CodeMapSpanUtils};
18+
use utils;
19+
use codemap::SpanUtils;
1920
use config::Config;
2021
use rewrite::{Rewrite, RewriteContext};
2122
use comment::rewrite_comment;

0 commit comments

Comments
 (0)