Skip to content

Commit f671ce1

Browse files
committed
config: add new 'format-code-in-docstrings' knob
This commit does the plumbing to make a new formatting option, 'format-code-in-docstrings', available in the configuration for end users. It is disabled by default (opt-in). It is opt-in at least initially to reflect a conservative posture. The intent is to make it opt-out at some point in the future. The name was inspired by the name of a similar configuration in rustfmt[1]. [1]: https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#format_code_in_doc_comments
1 parent c48ba69 commit f671ce1

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

crates/ruff_workspace/src/configuration.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use ruff_linter::settings::{
3434
use ruff_linter::{
3535
fs, warn_user, warn_user_once, warn_user_once_by_id, RuleSelector, RUFF_PKG_VERSION,
3636
};
37-
use ruff_python_formatter::{MagicTrailingComma, QuoteStyle};
37+
use ruff_python_formatter::{DocstringCode, MagicTrailingComma, QuoteStyle};
3838

3939
use crate::options::{
4040
Flake8AnnotationsOptions, Flake8BanditOptions, Flake8BugbearOptions, Flake8BuiltinsOptions,
@@ -180,6 +180,9 @@ impl Configuration {
180180
magic_trailing_comma: format
181181
.magic_trailing_comma
182182
.unwrap_or(format_defaults.magic_trailing_comma),
183+
format_code_in_docstrings: format
184+
.format_code_in_docstrings
185+
.unwrap_or(format_defaults.format_code_in_docstrings),
183186
};
184187

185188
let lint = self.lint;
@@ -1011,6 +1014,7 @@ pub struct FormatConfiguration {
10111014
pub quote_style: Option<QuoteStyle>,
10121015
pub magic_trailing_comma: Option<MagicTrailingComma>,
10131016
pub line_ending: Option<LineEnding>,
1017+
pub format_code_in_docstrings: Option<DocstringCode>,
10141018
}
10151019

10161020
impl FormatConfiguration {
@@ -1037,6 +1041,13 @@ impl FormatConfiguration {
10371041
}
10381042
}),
10391043
line_ending: options.line_ending,
1044+
format_code_in_docstrings: options.format_code_in_docstrings.map(|yes| {
1045+
if yes {
1046+
DocstringCode::Enabled
1047+
} else {
1048+
DocstringCode::Disabled
1049+
}
1050+
}),
10401051
})
10411052
}
10421053

@@ -1050,6 +1061,9 @@ impl FormatConfiguration {
10501061
quote_style: self.quote_style.or(other.quote_style),
10511062
magic_trailing_comma: self.magic_trailing_comma.or(other.magic_trailing_comma),
10521063
line_ending: self.line_ending.or(other.line_ending),
1064+
format_code_in_docstrings: self
1065+
.format_code_in_docstrings
1066+
.or(other.format_code_in_docstrings),
10531067
}
10541068
}
10551069
}

crates/ruff_workspace/src/options.rs

+43
Original file line numberDiff line numberDiff line change
@@ -2882,6 +2882,49 @@ pub struct FormatOptions {
28822882
"#
28832883
)]
28842884
pub line_ending: Option<LineEnding>,
2885+
2886+
/// Whether to format code snippets in docstrings.
2887+
///
2888+
/// When this is enabled, Python code in the format of doctests
2889+
/// within docstrings is automatically reformatted.
2890+
///
2891+
/// For example, when this is enabled, the following code:
2892+
///
2893+
/// ```python
2894+
/// def f(x):
2895+
/// """
2896+
/// Something about `f`. And an example:
2897+
///
2898+
/// >>> f( x )
2899+
/// """
2900+
/// pass
2901+
/// ```
2902+
///
2903+
/// will be reformatted (assuming the rest of the options are set
2904+
/// to their defaults) as:
2905+
///
2906+
/// ```python
2907+
/// def f(x):
2908+
/// """
2909+
/// Something about `f`. And an example:
2910+
///
2911+
/// >>> f(x)
2912+
/// """
2913+
/// pass
2914+
/// ```
2915+
///
2916+
/// If a doctest contains invalid Python code or if the formatter would
2917+
/// otherwise write invalid Python code, then the doctest is ignored by
2918+
/// the formatter and kept as-is.
2919+
#[option(
2920+
default = "false",
2921+
value_type = "bool",
2922+
example = r#"
2923+
# Enable reformatting of code snippets in docstrings.
2924+
format-code-in-docstrings = true
2925+
"#
2926+
)]
2927+
pub format_code_in_docstrings: Option<bool>,
28852928
}
28862929

28872930
#[cfg(test)]

crates/ruff_workspace/src/settings.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use ruff_linter::settings::types::{FilePattern, FilePatternSet, SerializationFor
55
use ruff_linter::settings::LinterSettings;
66
use ruff_macros::CacheKey;
77
use ruff_python_ast::PySourceType;
8-
use ruff_python_formatter::{MagicTrailingComma, PreviewMode, PyFormatOptions, QuoteStyle};
8+
use ruff_python_formatter::{
9+
DocstringCode, MagicTrailingComma, PreviewMode, PyFormatOptions, QuoteStyle,
10+
};
911
use ruff_source_file::find_newline;
1012
use std::path::{Path, PathBuf};
1113

@@ -124,6 +126,8 @@ pub struct FormatterSettings {
124126
pub magic_trailing_comma: MagicTrailingComma,
125127

126128
pub line_ending: LineEnding,
129+
130+
pub format_code_in_docstrings: DocstringCode,
127131
}
128132

129133
impl FormatterSettings {
@@ -157,6 +161,7 @@ impl FormatterSettings {
157161
.with_preview(self.preview)
158162
.with_line_ending(line_ending)
159163
.with_line_width(self.line_width)
164+
.with_docstring_code(self.format_code_in_docstrings)
160165
}
161166
}
162167

@@ -173,6 +178,7 @@ impl Default for FormatterSettings {
173178
indent_width: default_options.indent_width(),
174179
quote_style: default_options.quote_style(),
175180
magic_trailing_comma: default_options.magic_trailing_comma(),
181+
format_code_in_docstrings: default_options.docstring_code(),
176182
}
177183
}
178184
}

ruff.schema.json

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)