Use RefCell for RewriteContext fields

This commit is contained in:
Seiichi Uchida 2018-03-07 15:37:44 +09:00
parent dc2f1429e7
commit 8943c376bc
3 changed files with 10 additions and 6 deletions

View file

@ -251,7 +251,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
Cow::from("")
} else {
// Use new lines.
if context.force_one_line_chain {
if *context.force_one_line_chain.borrow() {
return None;
}
nested_shape.indent.to_string_with_newline(context.config)

View file

@ -17,6 +17,8 @@ use config::{Config, IndentStyle};
use shape::Shape;
use visitor::SnippetProvider;
use std::cell::RefCell;
pub trait Rewrite {
/// Rewrite self into shape.
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String>;
@ -29,12 +31,12 @@ pub struct RewriteContext<'a> {
pub config: &'a Config,
pub inside_macro: bool,
// Force block indent style even if we are using visual indent style.
pub use_block: bool,
pub use_block: RefCell<bool>,
// When `format_if_else_cond_comment` is true, unindent the comment on top
// of the `else` or `else if`.
pub is_if_else_block: bool,
// When rewriting chain, veto going multi line except the last element
pub force_one_line_chain: bool,
pub force_one_line_chain: RefCell<bool>,
pub snippet_provider: &'a SnippetProvider<'a>,
}
@ -45,7 +47,7 @@ impl<'a> RewriteContext<'a> {
/// Return true if we should use block indent style for rewriting function call.
pub fn use_block_indent(&self) -> bool {
self.config.indent_style() == IndentStyle::Block || self.use_block
self.config.indent_style() == IndentStyle::Block || *self.use_block.borrow()
}
pub fn budget(&self, used_width: usize) -> usize {

View file

@ -26,6 +26,8 @@ use shape::{Indent, Shape};
use spanned::Spanned;
use utils::{self, contains_skip, count_newlines, inner_attributes, mk_sp, ptr_vec_to_ref_vec};
use std::cell::RefCell;
/// Creates a string slice corresponding to the specified span.
pub struct SnippetProvider<'a> {
/// A pointer to the content of the file we are formatting.
@ -691,9 +693,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
codemap: self.codemap,
config: self.config,
inside_macro: false,
use_block: false,
use_block: RefCell::new(false),
is_if_else_block: false,
force_one_line_chain: false,
force_one_line_chain: RefCell::new(false),
snippet_provider: self.snippet_provider,
}
}