Skip to content

Commit 0071e7d

Browse files
committed
config: add new 'docstring-code' knob
This commit does the plumbing to make a new formatting option, 'docstring-code', 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.
1 parent f585e3e commit 0071e7d

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

crates/ruff_workspace/src/configuration.rs

+13-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+
docstring_code: format
184+
.docstring_code
185+
.unwrap_or(format_defaults.docstring_code),
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 docstring_code: Option<DocstringCode>,
10141018
}
10151019

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

@@ -1050,6 +1061,7 @@ 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+
docstring_code: self.docstring_code.or(other.docstring_code),
10531065
}
10541066
}
10551067
}

crates/ruff_workspace/src/options.rs

+43
Original file line numberDiff line numberDiff line change
@@ -2871,6 +2871,49 @@ pub struct FormatOptions {
28712871
"#
28722872
)]
28732873
pub line_ending: Option<LineEnding>,
2874+
2875+
/// Whether to format code snippets in docstrings.
2876+
///
2877+
/// When this is enabled, Python code in the format of doctests
2878+
/// within docstrings is automatically reformatted.
2879+
///
2880+
/// For example, when this is enabled, the following code:
2881+
///
2882+
/// ```python
2883+
/// def f(x):
2884+
/// """
2885+
/// Something about `f`. And an example:
2886+
///
2887+
/// >>> f( x )
2888+
/// """
2889+
/// pass
2890+
/// ```
2891+
///
2892+
/// will be reformatted (assuming the rest of the options are set
2893+
/// to their defaults) as:
2894+
///
2895+
/// ```python
2896+
/// def f(x):
2897+
/// """
2898+
/// Something about `f`. And an example:
2899+
///
2900+
/// >>> f(x)
2901+
/// """
2902+
/// pass
2903+
/// ```
2904+
///
2905+
/// If a doctest contains invalid Python code or if the formatter would
2906+
/// otherwise write invalid Python code, then the doctest is ignored by
2907+
/// the formatter and kept as-is.
2908+
#[option(
2909+
default = "false",
2910+
value_type = "bool",
2911+
example = r#"
2912+
# Enable reformatting of code snippets in docstrings.
2913+
docstring-code = true
2914+
"#
2915+
)]
2916+
pub docstring_code: Option<bool>,
28742917
}
28752918

28762919
#[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 docstring_code: 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.docstring_code)
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+
docstring_code: 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)