Skip to content

Commit da65090

Browse files
committed
Merge pull request #857 from kamalmarhubi/codemap-ext
utils: Add CodeMapExt extension trait for span_* methods
2 parents 17856e8 + d82d9fc commit da65090

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::ExprKind::Vec(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)
@@ -335,7 +335,7 @@ fn rewrite_closure(capture: ast::CaptureBy,
335335
|arg| span_lo_for_arg(arg),
336336
|arg| span_hi_for_arg(arg),
337337
|arg| arg.rewrite(context, budget, argument_offset),
338-
span_after(span, "|", context.codemap),
338+
context.codemap.span_after(span, "|"),
339339
body.span.lo);
340340
let item_vec = arg_items.collect::<Vec<_>>();
341341
let tactic = definitive_tactic(&item_vec, ListTactic::HorizontalVertical, horizontal_budget);
@@ -662,9 +662,9 @@ fn rewrite_if_else(context: &RewriteContext,
662662

663663
let if_block_string = try_opt!(if_block.rewrite(context, width, offset));
664664

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

669669
let between_if_cond_comment = extract_comment(between_if_cond, &context, offset, width);
670670

@@ -709,17 +709,17 @@ fn rewrite_if_else(context: &RewriteContext,
709709
};
710710

711711
let between_if_else_block = mk_sp(if_block.span.hi,
712-
span_before(mk_sp(if_block.span.hi, else_block.span.lo),
713-
"else",
714-
context.codemap));
712+
context.codemap.span_before(mk_sp(if_block.span.hi,
713+
else_block.span.lo),
714+
"else"));
715715
let between_if_else_block_comment = extract_comment(between_if_else_block,
716716
&context,
717717
offset,
718718
width);
719719

720-
let after_else = mk_sp(span_after(mk_sp(if_block.span.hi, else_block.span.lo),
721-
"else",
722-
context.codemap),
720+
let after_else = mk_sp(context.codemap
721+
.span_after(mk_sp(if_block.span.hi, else_block.span.lo),
722+
"else"),
723723
else_block.span.lo);
724724
let after_else_comment = extract_comment(after_else, &context, offset, width);
725725

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

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

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

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

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

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

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

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};
@@ -451,7 +450,7 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
451450
result.push_str(format_unsafety(unsafety));
452451
result.push_str("impl");
453452

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

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

637636
let generics_str = match generics {
638637
Some(g) => {
@@ -679,7 +678,7 @@ fn format_struct_struct(context: &RewriteContext,
679678
},
680679
|field| field.node.ty.span.hi,
681680
|field| field.rewrite(context, item_budget, item_indent),
682-
span_after(span, "{", context.codemap),
681+
context.codemap.span_after(span, "{"),
683682
span.hi);
684683
// 1 = ,
685684
let budget = context.config.max_width - offset.width() + context.config.tab_spaces - 1;
@@ -761,7 +760,7 @@ fn format_tuple_struct(context: &RewriteContext,
761760
},
762761
|field| field.node.ty.span.hi,
763762
|field| field.rewrite(context, item_budget, item_indent),
764-
span_after(span, "(", context.codemap),
763+
context.codemap.span_after(span, "("),
765764
span.hi);
766765
let body = try_opt!(format_item_list(items, item_budget, item_indent, context.config));
767766
result.push_str(&body);
@@ -797,7 +796,7 @@ pub fn rewrite_type_alias(context: &RewriteContext,
797796
result.push_str(&ident.to_string());
798797

799798
let generics_indent = indent + result.len();
800-
let generics_span = mk_sp(span_after(span, "type", context.codemap), ty.span.lo);
799+
let generics_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo);
801800
let generics_width = context.config.max_width - " =".len();
802801
let generics_str = try_opt!(rewrite_generics(context,
803802
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;
@@ -85,7 +85,7 @@ impl Rewrite for Pat {
8585
|item| item.span.lo,
8686
|item| item.span.hi,
8787
|item| item.rewrite(context, width, offset),
88-
span_after(self.span, "(", context.codemap),
88+
context.codemap.span_after(self.span, "("),
8989
self.span.hi);
9090
Some(format!("{}({})",
9191
path_str,
@@ -142,7 +142,7 @@ impl Rewrite for Pat {
142142
|f| f.span.lo,
143143
|f| f.span.hi,
144144
|f| f.node.rewrite(context, budget, offset),
145-
span_after(self.span, "{", context.codemap),
145+
context.codemap.span_after(self.span, "{"),
146146
self.span.hi);
147147
let mut field_string = try_opt!(format_item_list(items,
148148
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)