Skip to content

Commit d82d9fc

Browse files
committed
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.
1 parent 80db099 commit d82d9fc

File tree

8 files changed

+75
-70
lines changed

8 files changed

+75
-70
lines changed

src/expr.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rewrite::{Rewrite, RewriteContext};
2020
use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic,
2121
DefinitiveListTactic, definitive_tactic, ListItem, format_fn_args};
2222
use string::{StringFormat, rewrite_string};
23-
use utils::{span_after, span_before, extra_offset, last_line_width, wrap_str, binary_search,
23+
use utils::{CodeMapSpanUtils, extra_offset, last_line_width, wrap_str, binary_search,
2424
first_line_width, semicolon_for_stmt};
2525
use visitor::FmtVisitor;
2626
use config::{Config, StructLitStyle, MultilineStyle};
@@ -39,7 +39,7 @@ impl Rewrite for ast::Expr {
3939
let result = match self.node {
4040
ast::Expr_::ExprVec(ref expr_vec) => {
4141
rewrite_array(expr_vec.iter().map(|e| &**e),
42-
mk_sp(span_after(self.span, "[", context.codemap), self.span.hi),
42+
mk_sp(context.codemap.span_after(self.span, "["), self.span.hi),
4343
context,
4444
width,
4545
offset)
@@ -332,7 +332,7 @@ fn rewrite_closure(capture: ast::CaptureClause,
332332
|arg| span_lo_for_arg(arg),
333333
|arg| span_hi_for_arg(arg),
334334
|arg| arg.rewrite(context, budget, argument_offset),
335-
span_after(span, "|", context.codemap),
335+
context.codemap.span_after(span, "|"),
336336
body.span.lo);
337337
let item_vec = arg_items.collect::<Vec<_>>();
338338
let tactic = definitive_tactic(&item_vec, ListTactic::HorizontalVertical, horizontal_budget);
@@ -660,9 +660,9 @@ fn rewrite_if_else(context: &RewriteContext,
660660

661661
let if_block_string = try_opt!(if_block.rewrite(context, width, offset));
662662

663-
let between_if_cond = mk_sp(span_after(span, "if", context.codemap),
663+
let between_if_cond = mk_sp(context.codemap.span_after(span, "if"),
664664
pat.map_or(cond.span.lo,
665-
|_| span_before(span, "let", context.codemap)));
665+
|_| context.codemap.span_before(span, "let")));
666666

667667
let between_if_cond_comment = extract_comment(between_if_cond, &context, offset, width);
668668

@@ -707,17 +707,17 @@ fn rewrite_if_else(context: &RewriteContext,
707707
};
708708

709709
let between_if_else_block = mk_sp(if_block.span.hi,
710-
span_before(mk_sp(if_block.span.hi, else_block.span.lo),
711-
"else",
712-
context.codemap));
710+
context.codemap.span_before(mk_sp(if_block.span.hi,
711+
else_block.span.lo),
712+
"else"));
713713
let between_if_else_block_comment = extract_comment(between_if_else_block,
714714
&context,
715715
offset,
716716
width);
717717

718-
let after_else = mk_sp(span_after(mk_sp(if_block.span.hi, else_block.span.lo),
719-
"else",
720-
context.codemap),
718+
let after_else = mk_sp(context.codemap
719+
.span_after(mk_sp(if_block.span.hi, else_block.span.lo),
720+
"else"),
721721
else_block.span.lo);
722722
let after_else_comment = extract_comment(after_else, &context, offset, width);
723723

@@ -863,9 +863,8 @@ fn rewrite_match(context: &RewriteContext,
863863
let arm_indent = nested_context.block_indent;
864864
let arm_indent_str = arm_indent.to_string(context.config);
865865

866-
let open_brace_pos = span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])),
867-
"{",
868-
context.codemap);
866+
let open_brace_pos = context.codemap
867+
.span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])), "{");
869868

870869
for (i, arm) in arms.iter().enumerate() {
871870
// Make sure we get the stuff between arms.
@@ -1275,7 +1274,7 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
12751274
None => return Err(Ordering::Greater),
12761275
};
12771276

1278-
let span_lo = span_after(span, "(", context.codemap);
1277+
let span_lo = context.codemap.span_after(span, "(");
12791278
let span = mk_sp(span_lo, span.hi);
12801279

12811280
let extra_offset = extra_offset(&callee_str, offset);
@@ -1461,7 +1460,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
14611460
}
14621461
}
14631462
},
1464-
span_after(span, "{", context.codemap),
1463+
context.codemap.span_after(span, "{"),
14651464
span.hi);
14661465
let item_vec = items.collect::<Vec<_>>();
14671466

@@ -1569,7 +1568,7 @@ pub fn rewrite_tuple<'a, I>(context: &RewriteContext,
15691568
return items.next().unwrap().rewrite(context, budget, indent).map(|s| format!("({},)", s));
15701569
}
15711570

1572-
let list_lo = span_after(span, "(", context.codemap);
1571+
let list_lo = context.codemap.span_after(span, "(");
15731572
let items = itemize_list(context.codemap,
15741573
items,
15751574
")",

src/imports.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Indent;
1212
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, definitive_tactic};
1313
use types::rewrite_path;
14-
use utils::span_after;
14+
use utils::CodeMapSpanUtils;
1515
use rewrite::{Rewrite, RewriteContext};
1616

1717
use syntax::ast;
@@ -130,7 +130,7 @@ pub fn rewrite_use_list(width: usize,
130130
|vpi| vpi.span.lo,
131131
|vpi| vpi.span.hi,
132132
rewrite_path_item,
133-
span_after(span, "{", context.codemap),
133+
context.codemap.span_after(span, "{"),
134134
span.hi);
135135
items.extend(iter);
136136
items

src/items.rs

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

1313
use Indent;
14-
use utils::{format_mutability, format_visibility, contains_skip, span_after, end_typaram,
15-
wrap_str, last_line_width, semicolon_for_expr, format_unsafety, trim_newlines,
16-
span_after_last};
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};
1716
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic,
1817
DefinitiveListTactic, definitive_tactic, format_item_list};
1918
use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs};
@@ -452,7 +451,7 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
452451
result.push_str(format_unsafety(unsafety));
453452
result.push_str("impl");
454453

455-
let lo = span_after(item.span, "impl", context.codemap);
454+
let lo = context.codemap.span_after(item.span, "impl");
456455
let hi = match *trait_ref {
457456
Some(ref tr) => tr.path.span.lo,
458457
None => self_ty.span.lo,
@@ -633,7 +632,7 @@ fn format_struct_struct(context: &RewriteContext,
633632
let header_str = format_header(item_name, ident, vis);
634633
result.push_str(&header_str);
635634

636-
let body_lo = span_after(span, "{", context.codemap);
635+
let body_lo = context.codemap.span_after(span, "{");
637636

638637
let generics_str = match generics {
639638
Some(g) => {
@@ -680,7 +679,7 @@ fn format_struct_struct(context: &RewriteContext,
680679
},
681680
|field| field.node.ty.span.hi,
682681
|field| field.rewrite(context, item_budget, item_indent),
683-
span_after(span, "{", context.codemap),
682+
context.codemap.span_after(span, "{"),
684683
span.hi);
685684
// 1 = ,
686685
let budget = context.config.max_width - offset.width() + context.config.tab_spaces - 1;
@@ -762,7 +761,7 @@ fn format_tuple_struct(context: &RewriteContext,
762761
},
763762
|field| field.node.ty.span.hi,
764763
|field| field.rewrite(context, item_budget, item_indent),
765-
span_after(span, "(", context.codemap),
764+
context.codemap.span_after(span, "("),
766765
span.hi);
767766
let body = try_opt!(format_item_list(items, item_budget, item_indent, context.config));
768767
result.push_str(&body);
@@ -798,7 +797,7 @@ pub fn rewrite_type_alias(context: &RewriteContext,
798797
result.push_str(&ident.to_string());
799798

800799
let generics_indent = indent + result.len();
801-
let generics_span = mk_sp(span_after(span, "type", context.codemap), ty.span.lo);
800+
let generics_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo);
802801
let generics_width = context.config.max_width - " =".len();
803802
let generics_str = try_opt!(rewrite_generics(context,
804803
generics,
@@ -1152,7 +1151,7 @@ fn rewrite_fn_base(context: &RewriteContext,
11521151
let args_start = generics.ty_params
11531152
.last()
11541153
.map_or(span.lo, |tp| end_typaram(tp));
1155-
let args_span = mk_sp(span_after(mk_sp(args_start, span.hi), "(", context.codemap),
1154+
let args_span = mk_sp(context.codemap.span_after(mk_sp(args_start, span.hi), "("),
11561155
span_for_return(&fd.output).lo);
11571156
let arg_str = try_opt!(rewrite_args(context,
11581157
&fd.inputs,
@@ -1304,7 +1303,7 @@ fn rewrite_args(context: &RewriteContext,
13041303
if args.len() >= min_args || variadic {
13051304
let comment_span_start = if min_args == 2 {
13061305
let reduced_span = mk_sp(span.lo, args[1].ty.span.lo);
1307-
span_after_last(reduced_span, ",", context.codemap)
1306+
context.codemap.span_after_last(reduced_span, ",")
13081307
} else {
13091308
span.lo
13101309
};
@@ -1316,7 +1315,7 @@ fn rewrite_args(context: &RewriteContext,
13161315

13171316
let variadic_arg = if variadic {
13181317
let variadic_span = mk_sp(args.last().unwrap().ty.span.hi, span.hi);
1319-
let variadic_start = span_after(variadic_span, "...", context.codemap) - BytePos(3);
1318+
let variadic_start = context.codemap.span_after(variadic_span, "...") - BytePos(3);
13201319
Some(ArgumentKind::Variadic(variadic_start))
13211320
} else {
13221321
None
@@ -1476,7 +1475,7 @@ fn rewrite_generics(context: &RewriteContext,
14761475
|&(sp, _)| sp.hi,
14771476
// FIXME: don't clone
14781477
|&(_, ref str)| str.clone(),
1479-
span_after(span, "<", context.codemap),
1478+
context.codemap.span_after(span, "<"),
14801479
span.hi);
14811480
let list_str = try_opt!(format_item_list(items, h_budget, offset, context.config));
14821481

src/macros.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use Indent;
2828
use rewrite::RewriteContext;
2929
use expr::{rewrite_call, rewrite_array};
3030
use comment::FindUncommented;
31-
use utils::{wrap_str, span_after};
31+
use utils::{CodeMapSpanUtils, wrap_str};
3232

3333
const FORCED_BRACKET_MACROS: &'static [&'static str] = &["vec!"];
3434

@@ -104,9 +104,8 @@ pub fn rewrite_macro(mac: &ast::Mac,
104104
// Format macro invocation as array literal.
105105
let extra_offset = macro_name.len();
106106
let rewrite = try_opt!(rewrite_array(expr_vec.iter().map(|x| &**x),
107-
mk_sp(span_after(mac.span,
108-
original_style.opener(),
109-
context.codemap),
107+
mk_sp(context.codemap.span_after(mac.span,
108+
original_style.opener()),
110109
mac.span.hi - BytePos(1)),
111110
context,
112111
try_opt!(width.checked_sub(extra_offset)),

src/patterns.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use Indent;
1212
use rewrite::{Rewrite, RewriteContext};
13-
use utils::{wrap_str, format_mutability, span_after};
13+
use utils::{CodeMapSpanUtils, wrap_str, format_mutability};
1414
use lists::{format_item_list, itemize_list};
1515
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
1616
use types::rewrite_path;
@@ -84,7 +84,7 @@ impl Rewrite for Pat {
8484
|item| item.span.lo,
8585
|item| item.span.hi,
8686
|item| item.rewrite(context, width, offset),
87-
span_after(self.span, "(", context.codemap),
87+
context.codemap.span_after(self.span, "("),
8888
self.span.hi);
8989
Some(format!("{}({})",
9090
path_str,
@@ -141,7 +141,7 @@ impl Rewrite for Pat {
141141
|f| f.span.lo,
142142
|f| f.span.hi,
143143
|f| f.node.rewrite(context, budget, offset),
144-
span_after(self.span, "{", context.codemap),
144+
context.codemap.span_after(self.span, "{"),
145145
self.span.hi);
146146
let mut field_string = try_opt!(format_item_list(items,
147147
budget,

src/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::abi;
1919
use {Indent, Spanned};
2020
use lists::{format_item_list, itemize_list, format_fn_args};
2121
use rewrite::{Rewrite, RewriteContext};
22-
use utils::{extra_offset, span_after, format_mutability, wrap_str};
22+
use utils::{CodeMapSpanUtils, extra_offset, format_mutability, wrap_str};
2323
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
2424
use config::TypeDensity;
2525

@@ -183,7 +183,7 @@ fn rewrite_segment(expr_context: bool,
183183
.collect::<Vec<_>>();
184184

185185
let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1);
186-
let list_lo = span_after(codemap::mk_sp(*span_lo, span_hi), "<", context.codemap);
186+
let list_lo = context.codemap.span_after(codemap::mk_sp(*span_lo, span_hi), "<");
187187
let separator = if expr_context {
188188
"::"
189189
} else {
@@ -246,7 +246,7 @@ fn format_function_type<'a, I>(inputs: I,
246246
let budget = try_opt!(width.checked_sub(2));
247247
// 1 for (
248248
let offset = offset + 1;
249-
let list_lo = span_after(span, "(", context.codemap);
249+
let list_lo = context.codemap.span_after(span, "(");
250250
let items = itemize_list(context.codemap,
251251
inputs,
252252
")",

src/utils.rs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,50 @@ use rewrite::{Rewrite, RewriteContext};
2020

2121
use SKIP_ANNOTATION;
2222

23-
// Computes the length of a string's last line, minus offset.
24-
#[inline]
25-
pub fn extra_offset(text: &str, offset: Indent) -> usize {
26-
match text.rfind('\n') {
27-
// 1 for newline character
28-
Some(idx) => text.len() - idx - 1 - offset.width(),
29-
None => text.len(),
30-
}
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;
3127
}
3228

33-
#[inline]
34-
pub fn span_after(original: Span, needle: &str, codemap: &CodeMap) -> BytePos {
35-
let snippet = codemap.span_to_snippet(original).unwrap();
36-
let offset = snippet.find_uncommented(needle).unwrap() + needle.len();
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();
3734

38-
original.lo + BytePos(offset as u32)
39-
}
35+
original.lo + BytePos(offset as u32)
36+
}
4037

41-
#[inline]
42-
pub fn span_before(original: Span, needle: &str, codemap: &CodeMap) -> BytePos {
43-
let snippet = codemap.span_to_snippet(original).unwrap();
44-
let offset = snippet.find_uncommented(needle).unwrap();
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;
4542

46-
original.lo + BytePos(offset as u32)
47-
}
43+
while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
44+
offset += additional_offset + needle.len();
45+
}
4846

49-
#[inline]
50-
pub fn span_after_last(original: Span, needle: &str, codemap: &CodeMap) -> BytePos {
51-
let snippet = codemap.span_to_snippet(original).unwrap();
52-
let mut offset = 0;
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();
5354

54-
while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
55-
offset += additional_offset + needle.len();
55+
original.lo + BytePos(offset as u32)
5656
}
57+
}
5758

58-
original.lo + BytePos(offset as u32)
59+
// Computes the length of a string's last line, minus offset.
60+
#[inline]
61+
pub fn extra_offset(text: &str, offset: Indent) -> usize {
62+
match text.rfind('\n') {
63+
// 1 for newline character
64+
Some(idx) => text.len() - idx - 1 - offset.width(),
65+
None => text.len(),
66+
}
5967
}
6068

6169
#[inline]

src/visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use syntax::visit;
1616
use strings::string_buffer::StringBuffer;
1717

1818
use Indent;
19-
use utils;
19+
use utils::{self, CodeMapSpanUtils};
2020
use config::Config;
2121
use rewrite::{Rewrite, RewriteContext};
2222
use comment::rewrite_comment;
@@ -450,7 +450,7 @@ impl<'a> FmtVisitor<'a> {
450450
if is_internal {
451451
self.buffer.push_str(" {");
452452
// Hackery to account for the closing }.
453-
let mod_lo = ::utils::span_after(s, "{", self.codemap);
453+
let mod_lo = self.codemap.span_after(s, "{");
454454
let body_snippet = self.snippet(codemap::mk_sp(mod_lo, m.inner.hi - BytePos(1)));
455455
let body_snippet = body_snippet.trim();
456456
if body_snippet.is_empty() {

0 commit comments

Comments
 (0)