Skip to main content

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
65pub(crate) struct ExceedsMaxWidthError {
66    pub configured_width: usize,
67    pub span: Span,
68}
69
70impl From<ExceedsMaxWidthError> for RewriteError {
71    fn from(error: ExceedsMaxWidthError) -> Self {
72        RewriteError::ExceedsMaxWidth {
73            configured_width: error.configured_width,
74            span: error.span,
75        }
76    }
77}
78
79/// Extension trait used to conveniently convert to RewriteError
80pub(crate) trait RewriteErrorExt<T> {
81    fn max_width_error(self, width: usize, span: Span) -> Result<T, RewriteError>;
82    fn macro_error(self, kind: MacroErrorKind, span: Span) -> Result<T, RewriteError>;
83    fn unknown_error(self) -> Result<T, RewriteError>;
84}
85
86impl<T> RewriteErrorExt<T> for Option<T> {
87    fn max_width_error(self, width: usize, span: Span) -> Result<T, RewriteError> {
88        self.ok_or_else(|| RewriteError::ExceedsMaxWidth {
89            configured_width: width,
90            span: span,
91        })
92    }
93
94    fn macro_error(self, kind: MacroErrorKind, span: Span) -> Result<T, RewriteError> {
95        self.ok_or_else(|| RewriteError::MacroFailure {
96            kind: kind,
97            span: span,
98        })
99    }
100
101    fn unknown_error(self) -> Result<T, RewriteError> {
102        self.ok_or_else(|| RewriteError::Unknown)
103    }
104}
105
106#[derive(Clone)]
107pub(crate) struct RewriteContext<'a> {
108    pub(crate) psess: &'a ParseSess,
109    pub(crate) config: &'a Config,
110    pub(crate) inside_macro: Rc<Cell<bool>>,
111    // Force block indent style even if we are using visual indent style.
112    pub(crate) use_block: Cell<bool>,
113    // When `is_if_else_block` is true, unindent the comment on top
114    // of the `else` or `else if`.
115    pub(crate) is_if_else_block: Cell<bool>,
116    // When `is_loop_block` is true, we can more aggressively end the
117    // last statement of the block with a semicolon.
118    pub(crate) is_loop_block: Cell<bool>,
119    // When rewriting chain, veto going multi line except the last element
120    pub(crate) force_one_line_chain: Cell<bool>,
121    pub(crate) snippet_provider: &'a SnippetProvider,
122    // Used for `format_snippet`
123    pub(crate) macro_rewrite_failure: Cell<bool>,
124    pub(crate) is_macro_def: bool,
125    pub(crate) report: FormatReport,
126    pub(crate) skip_context: SkipContext,
127    pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
128}
129
130pub(crate) struct InsideMacroGuard {
131    is_nested_macro_context: bool,
132    inside_macro_ref: Rc<Cell<bool>>,
133}
134
135impl InsideMacroGuard {
136    pub(crate) fn is_nested(&self) -> bool {
137        self.is_nested_macro_context
138    }
139}
140
141impl Drop for InsideMacroGuard {
142    fn drop(&mut self) {
143        self.inside_macro_ref.replace(self.is_nested_macro_context);
144    }
145}
146
147impl<'a> RewriteContext<'a> {
148    pub(crate) fn snippet(&self, span: Span) -> &str {
149        self.snippet_provider.span_to_snippet(span).unwrap()
150    }
151
152    /// Returns `true` if we should use block indent style for rewriting function call.
153    pub(crate) fn use_block_indent(&self) -> bool {
154        self.config.indent_style() == IndentStyle::Block || self.use_block.get()
155    }
156
157    pub(crate) fn budget(&self, used_width: usize) -> usize {
158        self.config.max_width().saturating_sub(used_width)
159    }
160
161    pub(crate) fn inside_macro(&self) -> bool {
162        self.inside_macro.get()
163    }
164
165    pub(crate) fn enter_macro(&self) -> InsideMacroGuard {
166        let is_nested_macro_context = self.inside_macro.replace(true);
167        InsideMacroGuard {
168            is_nested_macro_context,
169            inside_macro_ref: self.inside_macro.clone(),
170        }
171    }
172
173    pub(crate) fn leave_macro(&self) {
174        self.inside_macro.replace(false);
175    }
176
177    pub(crate) fn is_if_else_block(&self) -> bool {
178        self.is_if_else_block.get()
179    }
180
181    pub(crate) fn is_loop_block(&self) -> bool {
182        self.is_loop_block.get()
183    }
184}