about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorKamal Marhubi <kamal@marhubi.com>2016-05-25 20:41:26 +0200
committerKamal Marhubi <kamal@marhubi.com>2016-05-31 00:49:26 +0200
commitbd10af127ee4acbc6cd9e3221f78620d4f1e83be (patch)
tree6733df0129c8fee24e065dd50990f7d2a77f9317 /src
parent80c56a01ff9722ab0e52a8bd73b50603bc5a9a96 (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/codemap.rs39
-rw-r--r--src/expr.rs5
-rw-r--r--src/imports.rs2
-rw-r--r--src/items.rs5
-rw-r--r--src/lib.rs1
-rw-r--r--src/macros.rs3
-rw-r--r--src/patterns.rs3
-rw-r--r--src/types.rs3
-rw-r--r--src/utils.rs39
-rw-r--r--src/visitor.rs3
10 files changed, 56 insertions, 47 deletions
diff --git a/src/codemap.rs b/src/codemap.rs
new file mode 100644
index 00000000000..c70decd8422
--- /dev/null
+++ b/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)
+    }
+}
diff --git a/src/expr.rs b/src/expr.rs
index fc7f9cc6ca3..af0492b2913 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -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};
diff --git a/src/imports.rs b/src/imports.rs
index cb644485199..89030392774 100644
--- a/src/imports.rs
+++ b/src/imports.rs
@@ -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;
diff --git a/src/items.rs b/src/items.rs
index defd8e1a742..d83ad3c88ee 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -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};
diff --git a/src/lib.rs b/src/lib.rs
index 9b6d69e2f16..8df2c069bcc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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;
diff --git a/src/macros.rs b/src/macros.rs
index e9992fb0eb0..9cb0f8619c0 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -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!"];
 
diff --git a/src/patterns.rs b/src/patterns.rs
index 08d9db85201..a0b15a0f8c9 100644
--- a/src/patterns.rs
+++ b/src/patterns.rs
@@ -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;
diff --git a/src/types.rs b/src/types.rs
index 656701b6c78..b1b9b749dd8 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -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;
 
diff --git a/src/utils.rs b/src/utils.rs
index 188174e6021..82fbb8d5925 100644
--- a/src/utils.rs
+++ b/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 {
diff --git a/src/visitor.rs b/src/visitor.rs
index 45c72e2fdf8..c2ebcd17119 100644
--- a/src/visitor.rs
+++ b/src/visitor.rs
@@ -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;