Skip to content

Commit

Permalink
utils: Move codemap related utilities to a dedicated module
Browse files Browse the repository at this point in the history
This commit adds a `codemap` module, and moves the `CodemapSpanUtils`
added in #857 to it. This is preparation for adding more `Codemap`
specific utilities.

Refs #434
  • Loading branch information
kamalmarhubi committed May 30, 2016
1 parent 80c56a0 commit bd10af1
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 47 deletions.
39 changes: 39 additions & 0 deletions src/codemap.rs
@@ -0,0 +1,39 @@
use syntax::codemap::{BytePos, CodeMap, Span};

use comment::FindUncommented;

pub trait SpanUtils {
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 SpanUtils 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)
}
}
5 changes: 3 additions & 2 deletions src/expr.rs
Expand Up @@ -16,12 +16,13 @@ use std::iter::ExactSizeIterator;
use std::fmt::Write;

use {Indent, Spanned};
use codemap::SpanUtils;
use rewrite::{Rewrite, RewriteContext};
use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic,
DefinitiveListTactic, definitive_tactic, ListItem, format_item_list};
use string::{StringFormat, rewrite_string};
use utils::{CodeMapSpanUtils, extra_offset, last_line_width, wrap_str, binary_search,
first_line_width, semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr};
use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr};
use visitor::FmtVisitor;
use config::{Config, StructLitStyle, MultilineStyle, ElseIfBraceStyle, ControlBraceStyle};
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
Expand Down
2 changes: 1 addition & 1 deletion src/imports.rs
Expand Up @@ -9,9 +9,9 @@
// except according to those terms.

use Indent;
use codemap::SpanUtils;
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, definitive_tactic};
use types::rewrite_path;
use utils::CodeMapSpanUtils;
use rewrite::{Rewrite, RewriteContext};

use syntax::ast;
Expand Down
5 changes: 3 additions & 2 deletions src/items.rs
Expand Up @@ -11,8 +11,9 @@
// Formatting top-level items - functions, structs, enums, traits, impls.

use Indent;
use utils::{CodeMapSpanUtils, format_mutability, format_visibility, contains_skip, end_typaram,
wrap_str, last_line_width, semicolon_for_expr, format_unsafety, trim_newlines};
use codemap::SpanUtils;
use utils::{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, ListTactic, definitive_tactic, format_item_list};
use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs};
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -52,6 +52,7 @@ pub use self::summary::Summary;
#[macro_use]
mod utils;
pub mod config;
pub mod codemap;
pub mod filemap;
pub mod visitor;
mod checkstyle;
Expand Down
3 changes: 2 additions & 1 deletion src/macros.rs
Expand Up @@ -25,10 +25,11 @@ use syntax::parse::tts_to_parser;
use syntax::codemap::{mk_sp, BytePos};

use Indent;
use codemap::SpanUtils;
use rewrite::{Rewrite, RewriteContext};
use expr::{rewrite_call, rewrite_array};
use comment::{FindUncommented, contains_comment};
use utils::{CodeMapSpanUtils, wrap_str};
use utils::wrap_str;

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

Expand Down
3 changes: 2 additions & 1 deletion src/patterns.rs
Expand Up @@ -9,8 +9,9 @@
// except according to those terms.

use Indent;
use codemap::SpanUtils;
use rewrite::{Rewrite, RewriteContext};
use utils::{CodeMapSpanUtils, wrap_str, format_mutability};
use utils::{wrap_str, format_mutability};
use lists::{format_item_list, itemize_list};
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
use types::rewrite_path;
Expand Down
3 changes: 2 additions & 1 deletion src/types.rs
Expand Up @@ -17,9 +17,10 @@ use syntax::codemap::{self, Span, BytePos};
use syntax::abi;

use {Indent, Spanned};
use codemap::SpanUtils;
use lists::{format_item_list, itemize_list, format_fn_args};
use rewrite::{Rewrite, RewriteContext};
use utils::{CodeMapSpanUtils, extra_offset, format_mutability, wrap_str};
use utils::{extra_offset, format_mutability, wrap_str};
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
use config::TypeDensity;

Expand Down
39 changes: 1 addition & 38 deletions src/utils.rs
Expand Up @@ -14,51 +14,14 @@ use std::cmp::Ordering;
use itertools::Itertools;

use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItemKind, Path};
use syntax::codemap::{CodeMap, Span, BytePos};
use syntax::codemap::BytePos;
use syntax::abi;

use Indent;
use comment::FindUncommented;
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 {
Expand Down
3 changes: 2 additions & 1 deletion src/visitor.rs
Expand Up @@ -15,7 +15,8 @@ use syntax::parse::ParseSess;
use strings::string_buffer::StringBuffer;

use Indent;
use utils::{self, CodeMapSpanUtils};
use utils;
use codemap::SpanUtils;
use config::Config;
use rewrite::{Rewrite, RewriteContext};
use comment::rewrite_comment;
Expand Down

0 comments on commit bd10af1

Please sign in to comment.