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