Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use syntax::parse::classify;

use {Indent, Shape, Spanned};
use chains::rewrite_chain;
use codemap::SpanUtils;
use codemap::{LineRangeUtils, SpanUtils};
use comment::{contains_comment, recover_comment_removed, rewrite_comment, FindUncommented};
use config::{Config, ControlBraceStyle, IndentStyle, MultilineStyle, Style};
use items::{span_hi_for_arg, span_lo_for_arg};
Expand Down Expand Up @@ -111,9 +111,12 @@ pub fn format_expr(
context: &RewriteContext,
shape: Shape,
) -> Option<String> {
skip_out_of_file_lines_range!(context, expr.span);

if contains_skip(&*expr.attrs) {
return Some(context.snippet(expr.span()));
}

let expr_rw = match expr.node {
ast::ExprKind::Array(ref expr_vec) => rewrite_array(
expr_vec.iter().map(|e| &**e),
Expand Down Expand Up @@ -898,6 +901,8 @@ impl Rewrite for ast::Block {

impl Rewrite for ast::Stmt {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
skip_out_of_file_lines_range!(context, self.span());

let result = match self.node {
ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
Expand Down
5 changes: 4 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use syntax::ast::ImplItem;
use syntax::codemap::{BytePos, Span};

use {Indent, Shape, Spanned};
use codemap::SpanUtils;
use codemap::{LineRangeUtils, SpanUtils};
use comment::{contains_comment, recover_comment_removed, rewrite_comment, FindUncommented};
use config::{BraceStyle, Config, Density, IndentStyle, ReturnIndent, Style};
use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
Expand Down Expand Up @@ -50,6 +50,9 @@ impl Rewrite for ast::Local {
shape.width,
shape.indent
);

skip_out_of_file_lines_range!(context, self.span);

let mut result = "let ".to_owned();

// 4 = "let ".len()
Expand Down
25 changes: 25 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,31 @@ pub fn mk_sp(lo: BytePos, hi: BytePos) -> Span {
}
}

// Return true if the given span does not intersect with file lines.
macro_rules! out_of_file_lines_range {
($self:ident, $span:expr) => {
!$self.config
.file_lines()
.intersects(&$self.codemap.lookup_line_range($span))
}
}

macro_rules! skip_out_of_file_lines_range {
($self:ident, $span:expr) => {
if out_of_file_lines_range!($self, $span) {
return None;
}
}
}

macro_rules! skip_out_of_file_lines_range_visitor {
($self:ident, $span:expr) => {
if out_of_file_lines_range!($self, $span) {
return;
}
}
}

// Wraps string-like values in an Option. Returns Some when the string adheres
// to the Rewrite constraints defined for the Rewrite trait and else otherwise.
pub fn wrap_str<S: AsRef<str>>(s: S, max_width: usize, shape: Shape) -> Option<S> {
Expand Down
16 changes: 8 additions & 8 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ impl<'a> FmtVisitor<'a> {
self.codemap.lookup_char_pos(stmt.span.hi)
);

// FIXME(#434): Move this check to somewhere more central, eg Rewrite.
if !self.config
.file_lines()
.intersects(&self.codemap.lookup_line_range(stmt.span))
{
return;
}

match stmt.node {
ast::StmtKind::Item(ref item) => {
self.visit_item(item);
Expand Down Expand Up @@ -264,6 +256,8 @@ impl<'a> FmtVisitor<'a> {
}

pub fn visit_item(&mut self, item: &ast::Item) {
skip_out_of_file_lines_range_visitor!(self, item.span);

// This is where we bail out if there is a skip attribute. This is only
// complex in the module case. It is complex because the module could be
// in a separate file and there might be attributes in both files, but
Expand Down Expand Up @@ -465,6 +459,8 @@ impl<'a> FmtVisitor<'a> {
}

pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
skip_out_of_file_lines_range_visitor!(self, ti.span);

if self.visit_attrs(&ti.attrs, ast::AttrStyle::Outer) {
self.push_rewrite(ti.span, None);
return;
Expand Down Expand Up @@ -517,6 +513,8 @@ impl<'a> FmtVisitor<'a> {
}

pub fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
skip_out_of_file_lines_range_visitor!(self, ii.span);

if self.visit_attrs(&ii.attrs, ast::AttrStyle::Outer) {
self.push_rewrite(ii.span, None);
return;
Expand Down Expand Up @@ -565,6 +563,8 @@ impl<'a> FmtVisitor<'a> {
}

fn visit_mac(&mut self, mac: &ast::Mac, ident: Option<ast::Ident>, pos: MacroPosition) {
skip_out_of_file_lines_range_visitor!(self, mac.span);

// 1 = ;
let shape = Shape::indented(self.block_indent, self.config)
.sub_width(1)
Expand Down
12 changes: 12 additions & 0 deletions tests/source/file-lines-item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-file_lines: [{"file":"tests/source/file-lines-item.rs","range":[5,7]}]

use foo::{c, b, a};

fn foo() {
bar ( ) ;
}

impl Drop for Context {
fn drop(&mut self) {
}
}
12 changes: 12 additions & 0 deletions tests/target/file-lines-item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-file_lines: [{"file":"tests/source/file-lines-item.rs","range":[5,7]}]

use foo::{c, b, a};

fn foo() {
bar();
}

impl Drop for Context {
fn drop(&mut self) {
}
}