1 // A generic trait to abstract the rewriting of an element (of the AST).
3 use std::cell::{Cell, RefCell};
9 use crate::config::{Config, IndentStyle};
10 use crate::parse::session::ParseSess;
11 use crate::shape::Shape;
12 use crate::skip::SkipContext;
13 use crate::visitor::SnippetProvider;
14 use crate::FormatReport;
15 use rustc_data_structures::stable_map::FxHashMap;
17 pub(crate) trait Rewrite {
18 /// Rewrite self into shape.
19 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>;
22 impl<T: Rewrite> Rewrite for ptr::P<T> {
23 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
24 (**self).rewrite(context, shape)
28 #[derive(Clone, PartialEq, Eq, Hash)]
29 pub(crate) struct QueryId {
30 pub(crate) shape: Shape,
31 pub(crate) span: Span,
34 // We use Option<HashMap> instead of HashMap, because in case of `None`
35 // the function clean the memoize map, but it doesn't clean when
36 // there is `Some(empty)`, so they are different.
37 pub(crate) type Memoize = Rc<Cell<Option<FxHashMap<QueryId, Option<String>>>>>;
40 pub(crate) struct RewriteContext<'a> {
41 pub(crate) parse_sess: &'a ParseSess,
42 pub(crate) config: &'a Config,
43 pub(crate) memoize: Memoize,
44 pub(crate) inside_macro: Rc<Cell<bool>>,
45 // Force block indent style even if we are using visual indent style.
46 pub(crate) use_block: Cell<bool>,
47 // When `is_if_else_block` is true, unindent the comment on top
48 // of the `else` or `else if`.
49 pub(crate) is_if_else_block: Cell<bool>,
50 // When rewriting chain, veto going multi line except the last element
51 pub(crate) force_one_line_chain: Cell<bool>,
52 pub(crate) snippet_provider: &'a SnippetProvider,
53 // Used for `format_snippet`
54 pub(crate) macro_rewrite_failure: Cell<bool>,
55 pub(crate) is_macro_def: bool,
56 pub(crate) report: FormatReport,
57 pub(crate) skip_context: SkipContext,
58 pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
61 pub(crate) struct InsideMacroGuard {
62 is_nested_macro_context: bool,
63 inside_macro_ref: Rc<Cell<bool>>,
66 impl InsideMacroGuard {
67 pub(crate) fn is_nested(&self) -> bool {
68 self.is_nested_macro_context
72 impl Drop for InsideMacroGuard {
74 self.inside_macro_ref.replace(self.is_nested_macro_context);
78 impl<'a> RewriteContext<'a> {
79 pub(crate) fn snippet(&self, span: Span) -> &str {
80 self.snippet_provider.span_to_snippet(span).unwrap()
83 /// Returns `true` if we should use block indent style for rewriting function call.
84 pub(crate) fn use_block_indent(&self) -> bool {
85 self.config.indent_style() == IndentStyle::Block || self.use_block.get()
88 pub(crate) fn budget(&self, used_width: usize) -> usize {
89 self.config.max_width().saturating_sub(used_width)
92 pub(crate) fn inside_macro(&self) -> bool {
93 self.inside_macro.get()
96 pub(crate) fn enter_macro(&self) -> InsideMacroGuard {
97 let is_nested_macro_context = self.inside_macro.replace(true);
99 is_nested_macro_context,
100 inside_macro_ref: self.inside_macro.clone(),
104 pub(crate) fn leave_macro(&self) {
105 self.inside_macro.replace(false);
108 pub(crate) fn is_if_else_block(&self) -> bool {
109 self.is_if_else_block.get()