@@ -27,7 +27,7 @@ use ruff_linter::settings::types::{
27
27
} ;
28
28
use ruff_linter:: { warn_user_once, RuleSelector } ;
29
29
use ruff_macros:: { CombineOptions , OptionsMetadata } ;
30
- use ruff_python_formatter:: QuoteStyle ;
30
+ use ruff_python_formatter:: { DocstringCodeLineWidth , QuoteStyle } ;
31
31
32
32
use crate :: settings:: LineEnding ;
33
33
@@ -2896,6 +2896,153 @@ pub struct FormatOptions {
2896
2896
"#
2897
2897
) ]
2898
2898
pub line_ending : Option < LineEnding > ,
2899
+
2900
+ /// Whether to format code snippets in docstrings.
2901
+ ///
2902
+ /// When this is enabled, Python code examples within docstrings is
2903
+ /// automatically reformatted.
2904
+ ///
2905
+ /// For example, when this is enabled, the following code:
2906
+ ///
2907
+ /// ```python
2908
+ /// def f(x):
2909
+ /// """
2910
+ /// Something about `f`. And an example in doctest format:
2911
+ ///
2912
+ /// >>> f( x )
2913
+ ///
2914
+ /// Markdown is also supported:
2915
+ ///
2916
+ /// ```py
2917
+ /// f( x )
2918
+ /// ```
2919
+ ///
2920
+ /// As are reStructuredText literal blocks::
2921
+ ///
2922
+ /// f( x )
2923
+ ///
2924
+ ///
2925
+ /// And reStructuredText code blocks:
2926
+ ///
2927
+ /// .. code-block:: python
2928
+ ///
2929
+ /// f( x )
2930
+ /// """
2931
+ /// pass
2932
+ /// ```
2933
+ ///
2934
+ /// ... will be reformatted (assuming the rest of the options are set to
2935
+ /// their defaults) as:
2936
+ ///
2937
+ /// ```python
2938
+ /// def f(x):
2939
+ /// """
2940
+ /// Something about `f`. And an example in doctest format:
2941
+ ///
2942
+ /// >>> f(x)
2943
+ ///
2944
+ /// Markdown is also supported:
2945
+ ///
2946
+ /// ```py
2947
+ /// f(x)
2948
+ /// ```
2949
+ ///
2950
+ /// As are reStructuredText literal blocks::
2951
+ ///
2952
+ /// f(x)
2953
+ ///
2954
+ ///
2955
+ /// And reStructuredText code blocks:
2956
+ ///
2957
+ /// .. code-block:: python
2958
+ ///
2959
+ /// f(x)
2960
+ /// """
2961
+ /// pass
2962
+ /// ```
2963
+ ///
2964
+ /// If a code snippt in a docstring contains invalid Python code or if the
2965
+ /// formatter would otherwise write invalid Python code, then the code
2966
+ /// example is ignored by the formatter and kept as-is.
2967
+ ///
2968
+ /// Currently, doctest, Markdown, reStructuredText literal blocks, and
2969
+ /// reStructuredText code blocks are all supported and automatically
2970
+ /// recognized.
2971
+ #[ option(
2972
+ default = "false" ,
2973
+ value_type = "bool" ,
2974
+ example = r#"
2975
+ # Enable reformatting of code snippets in docstrings.
2976
+ docstring-code-format = true
2977
+ "#
2978
+ ) ]
2979
+ pub docstring_code_format : Option < bool > ,
2980
+
2981
+ /// Set the line length used when formatting code snippets in docstrings.
2982
+ ///
2983
+ /// This only has an effect when the `docstring-code-format` setting is
2984
+ /// enabled.
2985
+ ///
2986
+ /// The default value for this setting is `"dynamic"`, which has the effect
2987
+ /// of ensuring that any reformatted code examples in docstrings adhere to
2988
+ /// the global line length configuration that is used for the surrounding
2989
+ /// Python code. The point of this setting is that it takes the indentation
2990
+ /// of the docstring into account when reformatting code examples.
2991
+ ///
2992
+ /// Alternatively, this can be set to a fixed integer, which will result
2993
+ /// in the same line length limit being applied to all reformatted code
2994
+ /// examples in docstrings. When set to a fixed integer, the indent of the
2995
+ /// docstring is not taken into account. That is, this may result in lines
2996
+ /// in the reformatted code example that exceed the globally configured
2997
+ /// line length limit.
2998
+ ///
2999
+ /// For example, when this is set to `20` and `docstring-code-format` is
3000
+ /// enabled, then this code:
3001
+ ///
3002
+ /// ```python
3003
+ /// def f(x):
3004
+ /// '''
3005
+ /// Something about `f`. And an example:
3006
+ ///
3007
+ /// .. code-block:: python
3008
+ ///
3009
+ /// foo, bar, quux = this_is_a_long_line(lion, hippo, lemur, bear)
3010
+ /// '''
3011
+ /// pass
3012
+ /// ```
3013
+ ///
3014
+ /// ... will be reformatted (assuming the rest of the options are set
3015
+ /// to their defaults) as:
3016
+ ///
3017
+ /// ```python
3018
+ /// def f(x):
3019
+ /// """
3020
+ /// Something about `f`. And an example:
3021
+ ///
3022
+ /// .. code-block:: python
3023
+ ///
3024
+ /// (
3025
+ /// foo,
3026
+ /// bar,
3027
+ /// quux,
3028
+ /// ) = this_is_a_long_line(
3029
+ /// lion,
3030
+ /// hippo,
3031
+ /// lemur,
3032
+ /// bear,
3033
+ /// )
3034
+ /// """
3035
+ /// pass
3036
+ /// ```
3037
+ #[ option(
3038
+ default = r#""dynamic""# ,
3039
+ value_type = r#"int | "dynamic""# ,
3040
+ example = r#"
3041
+ # Format all docstring code snippets with a line length of 60.
3042
+ docstring-code-line-length = 60
3043
+ "#
3044
+ ) ]
3045
+ pub docstring_code_line_length : Option < DocstringCodeLineWidth > ,
2899
3046
}
2900
3047
2901
3048
#[ cfg( test) ]
0 commit comments