From d82d9fc808bfa3f99844ed40f622c8a53e2d267e Mon Sep 17 00:00:00 2001 From: Kamal Marhubi Date: Mon, 7 Mar 2016 13:41:32 -0500 Subject: [PATCH] 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. --- src/expr.rs | 33 +++++++++++++------------ src/imports.rs | 4 ++-- src/items.rs | 23 +++++++++--------- src/macros.rs | 7 +++--- src/patterns.rs | 6 ++--- src/types.rs | 6 ++--- src/utils.rs | 64 +++++++++++++++++++++++++++---------------------- src/visitor.rs | 4 ++-- 8 files changed, 76 insertions(+), 71 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 34a30e94899..10d8330f51a 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -20,7 +20,7 @@ use rewrite::{Rewrite, RewriteContext}; use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic, DefinitiveListTactic, definitive_tactic, ListItem, format_fn_args}; use string::{StringFormat, rewrite_string}; -use utils::{span_after, span_before, extra_offset, last_line_width, wrap_str, binary_search, +use utils::{CodeMapSpanUtils, extra_offset, last_line_width, wrap_str, binary_search, first_line_width, semicolon_for_stmt}; use visitor::FmtVisitor; use config::{Config, StructLitStyle, MultilineStyle}; @@ -39,7 +39,7 @@ impl Rewrite for ast::Expr { let result = match self.node { ast::Expr_::ExprVec(ref expr_vec) => { rewrite_array(expr_vec.iter().map(|e| &**e), - mk_sp(span_after(self.span, "[", context.codemap), self.span.hi), + mk_sp(context.codemap.span_after(self.span, "["), self.span.hi), context, width, offset) @@ -332,7 +332,7 @@ fn rewrite_closure(capture: ast::CaptureClause, |arg| span_lo_for_arg(arg), |arg| span_hi_for_arg(arg), |arg| arg.rewrite(context, budget, argument_offset), - span_after(span, "|", context.codemap), + context.codemap.span_after(span, "|"), body.span.lo); let item_vec = arg_items.collect::>(); let tactic = definitive_tactic(&item_vec, ListTactic::HorizontalVertical, horizontal_budget); @@ -660,9 +660,9 @@ fn rewrite_if_else(context: &RewriteContext, let if_block_string = try_opt!(if_block.rewrite(context, width, offset)); - let between_if_cond = mk_sp(span_after(span, "if", context.codemap), + let between_if_cond = mk_sp(context.codemap.span_after(span, "if"), pat.map_or(cond.span.lo, - |_| span_before(span, "let", context.codemap))); + |_| context.codemap.span_before(span, "let"))); let between_if_cond_comment = extract_comment(between_if_cond, &context, offset, width); @@ -707,17 +707,17 @@ fn rewrite_if_else(context: &RewriteContext, }; let between_if_else_block = mk_sp(if_block.span.hi, - span_before(mk_sp(if_block.span.hi, else_block.span.lo), - "else", - context.codemap)); + context.codemap.span_before(mk_sp(if_block.span.hi, + else_block.span.lo), + "else")); let between_if_else_block_comment = extract_comment(between_if_else_block, &context, offset, width); - let after_else = mk_sp(span_after(mk_sp(if_block.span.hi, else_block.span.lo), - "else", - context.codemap), + let after_else = mk_sp(context.codemap + .span_after(mk_sp(if_block.span.hi, else_block.span.lo), + "else"), else_block.span.lo); let after_else_comment = extract_comment(after_else, &context, offset, width); @@ -863,9 +863,8 @@ fn rewrite_match(context: &RewriteContext, let arm_indent = nested_context.block_indent; let arm_indent_str = arm_indent.to_string(context.config); - let open_brace_pos = span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])), - "{", - context.codemap); + let open_brace_pos = context.codemap + .span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])), "{"); for (i, arm) in arms.iter().enumerate() { // Make sure we get the stuff between arms. @@ -1275,7 +1274,7 @@ fn rewrite_call_inner(context: &RewriteContext, None => return Err(Ordering::Greater), }; - let span_lo = span_after(span, "(", context.codemap); + let span_lo = context.codemap.span_after(span, "("); let span = mk_sp(span_lo, span.hi); let extra_offset = extra_offset(&callee_str, offset); @@ -1461,7 +1460,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, } } }, - span_after(span, "{", context.codemap), + context.codemap.span_after(span, "{"), span.hi); let item_vec = items.collect::>(); @@ -1569,7 +1568,7 @@ pub fn rewrite_tuple<'a, I>(context: &RewriteContext, return items.next().unwrap().rewrite(context, budget, indent).map(|s| format!("({},)", s)); } - let list_lo = span_after(span, "(", context.codemap); + let list_lo = context.codemap.span_after(span, "("); let items = itemize_list(context.codemap, items, ")", diff --git a/src/imports.rs b/src/imports.rs index 70351ca8d79..8fa0347329c 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -11,7 +11,7 @@ use Indent; use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, definitive_tactic}; use types::rewrite_path; -use utils::span_after; +use utils::CodeMapSpanUtils; use rewrite::{Rewrite, RewriteContext}; use syntax::ast; @@ -130,7 +130,7 @@ pub fn rewrite_use_list(width: usize, |vpi| vpi.span.lo, |vpi| vpi.span.hi, rewrite_path_item, - span_after(span, "{", context.codemap), + context.codemap.span_after(span, "{"), span.hi); items.extend(iter); items diff --git a/src/items.rs b/src/items.rs index d7c16c4561c..4ba0897ef62 100644 --- a/src/items.rs +++ b/src/items.rs @@ -11,9 +11,8 @@ // Formatting top-level items - functions, structs, enums, traits, impls. use Indent; -use utils::{format_mutability, format_visibility, contains_skip, span_after, end_typaram, - wrap_str, last_line_width, semicolon_for_expr, format_unsafety, trim_newlines, - span_after_last}; +use utils::{CodeMapSpanUtils, format_mutability, format_visibility, contains_skip, end_typaram, + wrap_str, last_line_width, semicolon_for_expr, format_unsafety, trim_newlines}; use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, DefinitiveListTactic, definitive_tactic, format_item_list}; 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) - result.push_str(format_unsafety(unsafety)); result.push_str("impl"); - let lo = span_after(item.span, "impl", context.codemap); + let lo = context.codemap.span_after(item.span, "impl"); let hi = match *trait_ref { Some(ref tr) => tr.path.span.lo, None => self_ty.span.lo, @@ -633,7 +632,7 @@ fn format_struct_struct(context: &RewriteContext, let header_str = format_header(item_name, ident, vis); result.push_str(&header_str); - let body_lo = span_after(span, "{", context.codemap); + let body_lo = context.codemap.span_after(span, "{"); let generics_str = match generics { Some(g) => { @@ -680,7 +679,7 @@ fn format_struct_struct(context: &RewriteContext, }, |field| field.node.ty.span.hi, |field| field.rewrite(context, item_budget, item_indent), - span_after(span, "{", context.codemap), + context.codemap.span_after(span, "{"), span.hi); // 1 = , let budget = context.config.max_width - offset.width() + context.config.tab_spaces - 1; @@ -762,7 +761,7 @@ fn format_tuple_struct(context: &RewriteContext, }, |field| field.node.ty.span.hi, |field| field.rewrite(context, item_budget, item_indent), - span_after(span, "(", context.codemap), + context.codemap.span_after(span, "("), span.hi); let body = try_opt!(format_item_list(items, item_budget, item_indent, context.config)); result.push_str(&body); @@ -798,7 +797,7 @@ pub fn rewrite_type_alias(context: &RewriteContext, result.push_str(&ident.to_string()); let generics_indent = indent + result.len(); - let generics_span = mk_sp(span_after(span, "type", context.codemap), ty.span.lo); + let generics_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo); let generics_width = context.config.max_width - " =".len(); let generics_str = try_opt!(rewrite_generics(context, generics, @@ -1152,7 +1151,7 @@ fn rewrite_fn_base(context: &RewriteContext, let args_start = generics.ty_params .last() .map_or(span.lo, |tp| end_typaram(tp)); - let args_span = mk_sp(span_after(mk_sp(args_start, span.hi), "(", context.codemap), + let args_span = mk_sp(context.codemap.span_after(mk_sp(args_start, span.hi), "("), span_for_return(&fd.output).lo); let arg_str = try_opt!(rewrite_args(context, &fd.inputs, @@ -1304,7 +1303,7 @@ fn rewrite_args(context: &RewriteContext, if args.len() >= min_args || variadic { let comment_span_start = if min_args == 2 { let reduced_span = mk_sp(span.lo, args[1].ty.span.lo); - span_after_last(reduced_span, ",", context.codemap) + context.codemap.span_after_last(reduced_span, ",") } else { span.lo }; @@ -1316,7 +1315,7 @@ fn rewrite_args(context: &RewriteContext, let variadic_arg = if variadic { let variadic_span = mk_sp(args.last().unwrap().ty.span.hi, span.hi); - let variadic_start = span_after(variadic_span, "...", context.codemap) - BytePos(3); + let variadic_start = context.codemap.span_after(variadic_span, "...") - BytePos(3); Some(ArgumentKind::Variadic(variadic_start)) } else { None @@ -1476,7 +1475,7 @@ fn rewrite_generics(context: &RewriteContext, |&(sp, _)| sp.hi, // FIXME: don't clone |&(_, ref str)| str.clone(), - span_after(span, "<", context.codemap), + context.codemap.span_after(span, "<"), span.hi); let list_str = try_opt!(format_item_list(items, h_budget, offset, context.config)); diff --git a/src/macros.rs b/src/macros.rs index a8da45c6408..324c68d750a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -28,7 +28,7 @@ use Indent; use rewrite::RewriteContext; use expr::{rewrite_call, rewrite_array}; use comment::FindUncommented; -use utils::{wrap_str, span_after}; +use utils::{CodeMapSpanUtils, wrap_str}; const FORCED_BRACKET_MACROS: &'static [&'static str] = &["vec!"]; @@ -104,9 +104,8 @@ pub fn rewrite_macro(mac: &ast::Mac, // Format macro invocation as array literal. let extra_offset = macro_name.len(); let rewrite = try_opt!(rewrite_array(expr_vec.iter().map(|x| &**x), - mk_sp(span_after(mac.span, - original_style.opener(), - context.codemap), + mk_sp(context.codemap.span_after(mac.span, + original_style.opener()), mac.span.hi - BytePos(1)), context, try_opt!(width.checked_sub(extra_offset)), diff --git a/src/patterns.rs b/src/patterns.rs index a5f09823212..b176327ab15 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -10,7 +10,7 @@ use Indent; use rewrite::{Rewrite, RewriteContext}; -use utils::{wrap_str, format_mutability, span_after}; +use utils::{CodeMapSpanUtils, wrap_str, format_mutability}; use lists::{format_item_list, itemize_list}; use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple}; use types::rewrite_path; @@ -84,7 +84,7 @@ impl Rewrite for Pat { |item| item.span.lo, |item| item.span.hi, |item| item.rewrite(context, width, offset), - span_after(self.span, "(", context.codemap), + context.codemap.span_after(self.span, "("), self.span.hi); Some(format!("{}({})", path_str, @@ -141,7 +141,7 @@ impl Rewrite for Pat { |f| f.span.lo, |f| f.span.hi, |f| f.node.rewrite(context, budget, offset), - span_after(self.span, "{", context.codemap), + context.codemap.span_after(self.span, "{"), self.span.hi); let mut field_string = try_opt!(format_item_list(items, budget, diff --git a/src/types.rs b/src/types.rs index 4f930f22cad..917a3839d54 100644 --- a/src/types.rs +++ b/src/types.rs @@ -19,7 +19,7 @@ use syntax::abi; use {Indent, Spanned}; use lists::{format_item_list, itemize_list, format_fn_args}; use rewrite::{Rewrite, RewriteContext}; -use utils::{extra_offset, span_after, format_mutability, wrap_str}; +use utils::{CodeMapSpanUtils, extra_offset, format_mutability, wrap_str}; use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple}; use config::TypeDensity; @@ -183,7 +183,7 @@ fn rewrite_segment(expr_context: bool, .collect::>(); let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1); - let list_lo = span_after(codemap::mk_sp(*span_lo, span_hi), "<", context.codemap); + let list_lo = context.codemap.span_after(codemap::mk_sp(*span_lo, span_hi), "<"); let separator = if expr_context { "::" } else { @@ -246,7 +246,7 @@ fn format_function_type<'a, I>(inputs: I, let budget = try_opt!(width.checked_sub(2)); // 1 for ( let offset = offset + 1; - let list_lo = span_after(span, "(", context.codemap); + let list_lo = context.codemap.span_after(span, "("); let items = itemize_list(context.codemap, inputs, ")", diff --git a/src/utils.rs b/src/utils.rs index 3b03ede2be3..5c2bca73432 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -20,6 +20,42 @@ use rewrite::{Rewrite, RewriteContext}; use SKIP_ANNOTATION; +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; +} + +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) + } + + #[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) + } +} + // Computes the length of a string's last line, minus offset. #[inline] pub fn extra_offset(text: &str, offset: Indent) -> usize { @@ -30,34 +66,6 @@ pub fn extra_offset(text: &str, offset: Indent) -> usize { } } -#[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(); - - 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(); - - original.lo + BytePos(offset as u32) -} - -#[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; - - while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) { - offset += additional_offset + needle.len(); - } - - original.lo + BytePos(offset as u32) -} - #[inline] pub fn format_visibility(vis: Visibility) -> &'static str { match vis { diff --git a/src/visitor.rs b/src/visitor.rs index f53ed2246d5..76cdf543946 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -16,7 +16,7 @@ use syntax::visit; use strings::string_buffer::StringBuffer; use Indent; -use utils; +use utils::{self, CodeMapSpanUtils}; use config::Config; use rewrite::{Rewrite, RewriteContext}; use comment::rewrite_comment; @@ -450,7 +450,7 @@ impl<'a> FmtVisitor<'a> { if is_internal { self.buffer.push_str(" {"); // Hackery to account for the closing }. - let mod_lo = ::utils::span_after(s, "{", self.codemap); + let mod_lo = self.codemap.span_after(s, "{"); let body_snippet = self.snippet(codemap::mk_sp(mod_lo, m.inner.hi - BytePos(1))); let body_snippet = body_snippet.trim(); if body_snippet.is_empty() {