Skip to content

Commit 3d5d433

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 a224f19 commit 3d5d433

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,
@@ -189,6 +189,9 @@ impl Configuration {
189189
magic_trailing_comma: format
190190
.magic_trailing_comma
191191
.unwrap_or(format_defaults.magic_trailing_comma),
192+
format_code_in_docstrings: format
193+
.format_code_in_docstrings
194+
.unwrap_or(format_defaults.format_code_in_docstrings),
192195
};
193196

194197
let lint = self.lint;
@@ -1020,6 +1023,7 @@ pub struct FormatConfiguration {
10201023
pub quote_style: Option<QuoteStyle>,
10211024
pub magic_trailing_comma: Option<MagicTrailingComma>,
10221025
pub line_ending: Option<LineEnding>,
1026+
pub format_code_in_docstrings: Option<DocstringCode>,
10231027
}
10241028

10251029
impl FormatConfiguration {
@@ -1046,6 +1050,13 @@ impl FormatConfiguration {
10461050
}
10471051
}),
10481052
line_ending: options.line_ending,
1053+
format_code_in_docstrings: options.format_code_in_docstrings.map(|yes| {
1054+
if yes {
1055+
DocstringCode::Enabled
1056+
} else {
1057+
DocstringCode::Disabled
1058+
}
1059+
}),
10491060
})
10501061
}
10511062

@@ -1059,6 +1070,9 @@ impl FormatConfiguration {
10591070
quote_style: self.quote_style.or(other.quote_style),
10601071
magic_trailing_comma: self.magic_trailing_comma.or(other.magic_trailing_comma),
10611072
line_ending: self.line_ending.or(other.line_ending),
1073+
format_code_in_docstrings: self
1074+
.format_code_in_docstrings
1075+
.or(other.format_code_in_docstrings),
10621076
}
10631077
}
10641078
}

crates/ruff_workspace/src/options.rs

+43
Original file line numberDiff line numberDiff line change
@@ -2894,6 +2894,49 @@ pub struct FormatOptions {
28942894
"#
28952895
)]
28962896
pub line_ending: Option<LineEnding>,
2897+
2898+
/// Whether to format code snippets in docstrings.
2899+
///
2900+
/// When this is enabled, Python code in the format of doctests
2901+
/// within docstrings is automatically reformatted.
2902+
///
2903+
/// For example, when this is enabled, the following code:
2904+
///
2905+
/// ```python
2906+
/// def f(x):
2907+
/// """
2908+
/// Something about `f`. And an example:
2909+
///
2910+
/// >>> f( x )
2911+
/// """
2912+
/// pass
2913+
/// ```
2914+
///
2915+
/// will be reformatted (assuming the rest of the options are set
2916+
/// to their defaults) as:
2917+
///
2918+
/// ```python
2919+
/// def f(x):
2920+
/// """
2921+
/// Something about `f`. And an example:
2922+
///
2923+
/// >>> f(x)
2924+
/// """
2925+
/// pass
2926+
/// ```
2927+
///
2928+
/// If a doctest contains invalid Python code or if the formatter would
2929+
/// otherwise write invalid Python code, then the doctest is ignored by
2930+
/// the formatter and kept as-is.
2931+
#[option(
2932+
default = "false",
2933+
value_type = "bool",
2934+
example = r#"
2935+
# Enable reformatting of code snippets in docstrings.
2936+
format-code-in-docstrings = true
2937+
"#
2938+
)]
2939+
pub format_code_in_docstrings: Option<bool>,
28972940
}
28982941

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