rustfmt_nightly/
rewrite.rs

1// A generic trait to abstract the rewriting of an element (of the AST).
2
3use std::cell::{Cell, RefCell};
4use std::rc::Rc;
5
6use rustc_span::Span;
7use thiserror::Error;
8
9use crate::FormatReport;
10use crate::config::{Config, IndentStyle};
11use crate::parse::session::ParseSess;
12use crate::shape::Shape;
13use crate::skip::SkipContext;
14use crate::visitor::SnippetProvider;
15
16pub(crate) type RewriteResult = Result<String, RewriteError>;
17pub(crate) trait Rewrite {
18    /// Rewrite self into shape.
19    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>;
20
21    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
22        self.rewrite(context, shape).unknown_error()
23    }
24}
25
26impl<T: Rewrite> Rewrite for Box<T> {
27    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
28        (**self).rewrite(context, shape)
29    }
30}
31
32#[derive(Clone, Debug, PartialEq)]
33pub(crate) enum MacroErrorKind {
34    ParseFailure,
35    ReplaceMacroVariable,
36    Unknown,
37}
38
39impl std::fmt::Display for MacroErrorKind {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            MacroErrorKind::ParseFailure => write!(f, "(parse failure)"),
43            MacroErrorKind::ReplaceMacroVariable => write!(f, "(replacing macro variables with $)"),
44            MacroErrorKind::Unknown => write!(f, ""),
45        }
46    }
47}
48
49#[derive(Clone, Error, Debug)]
50pub(crate) enum RewriteError {
51    #[error("Formatting was skipped due to skip attribute or out of file range.")]
52    SkipFormatting,
53
54    #[error("It exceeds the required width of {configured_width} for the span: {span:?}")]
55    ExceedsMaxWidth { configured_width: usize, span: Span },
56
57    #[error("Failed to format given macro{} at: {span:?}", kind)]
58    MacroFailure { kind: MacroErrorKind, span: Span },
59
60    /// Format failure that does not fit to above categories.
61    #[error("An unknown error occurred during formatting.")]
62    Unknown,
63}
64
65/// Extension trait used to conveniently convert to RewriteError
66pub(crate) trait RewriteErrorExt<T> {
67    fn max_width_error(self, width: usize, span: Span) -> Result<T, RewriteError>;
68    fn macro_error(self, kind: MacroErrorKind, span: Span) -> Result<T, RewriteError>;
69    fn unknown_error(self) -> Result<T, RewriteError>;
70}
71
72impl<T> RewriteErrorExt<T> for Option<T> {
73    fn max_width_error(self, width: usize, span: Span) -> Result<T, RewriteError> {
74        self.ok_or_else(|| RewriteError::ExceedsMaxWidth {
75            configured_width: width,
76            span: span,
77        })
78    }
79
80    fn macro_error(self, kind: MacroErrorKind, span: Span) -> Result<T, RewriteError> {
81        self.ok_or_else(|| RewriteError::MacroFailure {
82            kind: kind,
83            span: span,
84        })
85    }
86
87    fn unknown_error(self) -> Result<T, RewriteError> {
88        self.ok_or_else(|| RewriteError::Unknown)
89    }
90}
91
92#[derive(Clone)]
93pub(crate) struct RewriteContext<'a> {
94    pub(crate) psess: &'a ParseSess,
95    pub(crate) config: &'a Config,
96    pub(crate) inside_macro: Rc<Cell<bool>>,
97    // Force block indent style even if we are using visual indent style.
98    pub(crate) use_block: Cell<bool>,
99    // When `is_if_else_block` is true, unindent the comment on top
100    // of the `else` or `else if`.
101    pub(crate) is_if_else_block: Cell<bool>,
102    // When rewriting chain, veto going multi line except the last element
103    pub(crate) force_one_line_chain: Cell<bool>,
104    pub(crate) snippet_provider: &'a SnippetProvider,
105    // Used for `format_snippet`
106    pub(crate) macro_rewrite_failure: Cell<bool>,
107    pub(crate) is_macro_def: bool,
108    pub(crate) report: FormatReport,
109    pub(crate) skip_context: SkipContext,
110    pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
111}
112
113pub(crate) struct InsideMacroGuard {
114    is_nested_macro_context: bool,
115    inside_macro_ref: Rc<Cell<bool>>,
116}
117
118impl InsideMacroGuard {
119    pub(crate) fn is_nested(&self) -> bool {
120        self.is_nested_macro_context
121    }
122}
123
124impl Drop for InsideMacroGuard {
125    fn drop(&mut self) {
126        self.inside_macro_ref.replace(self.is_nested_macro_context);
127    }
128}
129
130impl<'a> RewriteContext<'a> {
131    pub(crate) fn snippet(&self, span: Span) -> &str {
132        self.snippet_provider.span_to_snippet(span).unwrap()
133    }
134
135    /// Returns `true` if we should use block indent style for rewriting function call.
136    pub(crate) fn use_block_indent(&self) -> bool {
137        self.config.indent_style() == IndentStyle::Block || self.use_block.get()
138    }
139
140    pub(crate) fn budget(&self, used_width: usize) -> usize {
141        self.config.max_width().saturating_sub(used_width)
142    }
143
144    pub(crate) fn inside_macro(&self) -> bool {
145        self.inside_macro.get()
146    }
147
148    pub(crate) fn enter_macro(&self) -> InsideMacroGuard {
149        let is_nested_macro_context = self.inside_macro.replace(true);
150        InsideMacroGuard {
151            is_nested_macro_context,
152            inside_macro_ref: self.inside_macro.clone(),
153        }
154    }
155
156    pub(crate) fn leave_macro(&self) {
157        self.inside_macro.replace(false);
158    }
159
160    pub(crate) fn is_if_else_block(&self) -> bool {
161        self.is_if_else_block.get()
162    }
163}