Skip to content

Commit 1a75fdd

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 d9845a2 commit 1a75fdd

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
@@ -2835,6 +2835,49 @@ pub struct FormatOptions {
28352835
"#
28362836
)]
28372837
pub line_ending: Option<LineEnding>,
2838+
2839+
/// Whether to format code snippets in docstrings.
2840+
///
2841+
/// When this is enabled, Python code in the format of doctests
2842+
/// within docstrings is automatically reformatted.
2843+
///
2844+
/// For example, when this is enabled, the following code:
2845+
///
2846+
/// ```python
2847+
/// def f(x):
2848+
/// """
2849+
/// Something about `f`. And an example:
2850+
///
2851+
/// >>> f( x )
2852+
/// """
2853+
/// pass
2854+
/// ```
2855+
///
2856+
/// will be reformatted (assuming the rest of the options are set
2857+
/// to their defaults) as:
2858+
///
2859+
/// ```python
2860+
/// def f(x):
2861+
/// """
2862+
/// Something about `f`. And an example:
2863+
///
2864+
/// >>> f(x)
2865+
/// """
2866+
/// pass
2867+
/// ```
2868+
///
2869+
/// If a doctest contains invalid Python code or if the formatter would
2870+
/// otherwise write invalid Python code, then the doctest is ignored by
2871+
/// the formatter and kept as-is.
2872+
#[option(
2873+
default = "false",
2874+
value_type = "bool",
2875+
example = r#"
2876+
# Enable reformatting of code snippets in docstrings.
2877+
docstring-code = true
2878+
"#
2879+
)]
2880+
pub docstring_code: Option<bool>,
28382881
}
28392882

28402883
#[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)