utils: Move codemap related utilities to a dedicated module
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
This commit is contained in:
parent
80c56a01ff
commit
bd10af127e
39
src/codemap.rs
Normal file
39
src/codemap.rs
Normal file
@ -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)
|
||||
}
|
||||
}
|
@ -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};
|
||||
|
@ -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;
|
||||
|
@ -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};
|
||||
|
@ -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;
|
||||
|
@ -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!"];
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
|
39
src/utils.rs
39
src/utils.rs
@ -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 {
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user