Skip to content

Commit 05ffc0b

Browse files
committed
Add doc_comment_code_block_small_heuristics
This allows overriding `use_small_heuristics` in doc code. This is particularly useful for code that wants to use `use_small_heuristics = "Max"` internally but present doctests and examples using `use_small_heuristics = "Default"`.
1 parent 0332da0 commit 05ffc0b

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

Configurations.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,14 @@ Max width for code snippets included in doc comments. Only used if [`format_code
10511051
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
10521052
- **Stable**: No (tracking issue: [#5359](https://github.com/rust-lang/rustfmt/issues/5359))
10531053

1054+
## `doc_comment_code_block_small_heuristics`
1055+
1056+
Value for [`use_small_heuristics`](#use_small_heuristics) for use in code blocks in doc comments. Only used if [`format_code_in_doc_comments`](#format_code_in_doc_comments) is true.
1057+
1058+
- **Default value**: `"Default"`
1059+
- **Possible values**: `"Default"`, `"Off"`, `"Max"`
1060+
- **Stable**: No (tracking issue: [#5359](https://github.com/rust-lang/rustfmt/issues/5359))
1061+
10541062
## `format_generated_files`
10551063

10561064
Format generated files. A file is considered generated if any of the first several lines contain a `@generated` comment marker. The number of lines to check is configured by `generated_marker_line_search_limit`.

src/comment.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,14 @@ impl<'a> CommentRewrite<'a> {
764764
.doc_comment_code_block_width()
765765
.min(config.max_width());
766766
config.set().max_width(comment_max_width);
767+
if let Some(comment_use_small_heuristics) = config
768+
.doc_comment_code_block_small_heuristics()
769+
.to_heuristics()
770+
{
771+
config
772+
.set()
773+
.use_small_heuristics(comment_use_small_heuristics);
774+
}
767775
if let Some(s) =
768776
crate::format_code_block(&self.code_block_buffer, &config, false)
769777
{

src/config/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ create_config! {
6565
doc comments.";
6666
doc_comment_code_block_width: DocCommentCodeBlockWidth, false, "Maximum width for code \
6767
snippets in doc comments. No effect unless format_code_in_doc_comments = true";
68+
doc_comment_code_block_small_heuristics: DocUseSmallHeuristics, false,
69+
"Value for use_small_heuristics for code blocks in doc comments. \
70+
No effect unless format_code_in_doc_comments = true";
6871
comment_width: CommentWidth, false,
6972
"Maximum length of comments. No effect unless wrap_comments = true";
7073
normalize_comments: NormalizeComments, false, "Convert /* */ comments to // comments where \
@@ -772,6 +775,7 @@ single_line_let_else_max_width = 50
772775
wrap_comments = false
773776
format_code_in_doc_comments = false
774777
doc_comment_code_block_width = 100
778+
doc_comment_code_block_small_heuristics = "Inherit"
775779
comment_width = 80
776780
normalize_comments = false
777781
normalize_doc_attributes = false
@@ -864,6 +868,7 @@ single_line_let_else_max_width = 50
864868
wrap_comments = false
865869
format_code_in_doc_comments = false
866870
doc_comment_code_block_width = 100
871+
doc_comment_code_block_small_heuristics = "Inherit"
867872
comment_width = 80
868873
normalize_comments = false
869874
normalize_doc_attributes = false

src/config/options.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,31 @@ pub enum Heuristics {
9595
Default,
9696
}
9797

98+
#[config_type]
99+
/// Heuristic settings for doc comments. Same as `Heuristics`, but `Inherit` will inherit the value
100+
/// from the top-level configuration.
101+
pub enum DocCodeHeuristics {
102+
/// Inherit from the top-level configuration
103+
Inherit,
104+
/// Turn off any heuristics
105+
Off,
106+
/// Turn on max heuristics
107+
Max,
108+
/// Use scaled values based on the value of `max_width`
109+
Default,
110+
}
111+
112+
impl DocCodeHeuristics {
113+
pub fn to_heuristics(self) -> Option<Heuristics> {
114+
match self {
115+
DocCodeHeuristics::Inherit => None,
116+
DocCodeHeuristics::Off => Some(Heuristics::Off),
117+
DocCodeHeuristics::Max => Some(Heuristics::Max),
118+
DocCodeHeuristics::Default => Some(Heuristics::Default),
119+
}
120+
}
121+
}
122+
98123
impl Density {
99124
pub fn to_list_tactic(self, len: usize) -> ListTactic {
100125
match self {
@@ -620,6 +645,7 @@ config_option_with_style_edition_default!(
620645
WrapComments, bool, _ => false;
621646
FormatCodeInDocComments, bool, _ => false;
622647
DocCommentCodeBlockWidth, usize, _ => 100;
648+
DocUseSmallHeuristics, DocCodeHeuristics, _ => DocCodeHeuristics::Inherit;
623649
CommentWidth, usize, _ => 80;
624650
NormalizeComments, bool, _ => false;
625651
NormalizeDocAttributes, bool, _ => false;

0 commit comments

Comments
 (0)