Skip to content

Commit 2571a26

Browse files
authored
Add doc_comment_code_block_small_heuristics, to override use_small_heuristics in doc code
* 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"`. * Add test for `doc_comment_code_block_small_heuristics` This test is based on the `use_small_heuristics` tests. It tests that the code *outside* the doc comment gets formatted using `Max`, while the code inside doc comments gets formatted using `Default`. * Add another test for `doc_comment_code_block_small_heuristics` This test goes from Default to Max heuristics. * Add tracking issue for `doc_comment_use_small_heuristics`
1 parent d5f07e1 commit 2571a26

8 files changed

Lines changed: 330 additions & 0 deletions

File tree

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 nonnegative 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: [#6942](https://github.com/rust-lang/rustfmt/issues/6942))
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;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
// rustfmt-use_small_heuristics: Default
3+
// rustfmt-doc_comment_code_block_small_heuristics: Max
4+
5+
/// Start of a doc comment.
6+
///
7+
/// ```
8+
/// enum Lorem {
9+
/// Ipsum,
10+
/// Dolor(bool),
11+
/// Sit { amet: Consectetur, adipiscing: Elit },
12+
/// }
13+
///
14+
/// fn main() {
15+
/// lorem(
16+
/// "lorem",
17+
/// "ipsum",
18+
/// "dolor",
19+
/// "sit",
20+
/// "amet",
21+
/// "consectetur",
22+
/// "adipiscing",
23+
/// );
24+
///
25+
/// let lorem = Lorem {
26+
/// ipsum: dolor,
27+
/// sit: amet,
28+
/// };
29+
///
30+
/// let lorem = if ipsum { dolor } else { sit };
31+
/// }
32+
///
33+
/// fn format_let_else() {
34+
/// let Some(a) = opt else {};
35+
///
36+
/// let Some(b) = opt else { return };
37+
///
38+
/// let Some(c) = opt else { return };
39+
///
40+
/// let Some(d) = some_very_very_very_very_long_name else {
41+
/// return;
42+
/// };
43+
/// }
44+
/// ```
45+
///
46+
/// End of a doc comment.
47+
struct S;
48+
49+
enum Lorem {
50+
Ipsum,
51+
Dolor(bool),
52+
Sit { amet: Consectetur, adipiscing: Elit },
53+
}
54+
55+
fn main() {
56+
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
57+
58+
let lorem = Lorem { ipsum: dolor, sit: amet };
59+
60+
let lorem = if ipsum { dolor } else { sit };
61+
}
62+
63+
fn format_let_else() {
64+
let Some(a) = opt else {};
65+
66+
let Some(b) = opt else { return };
67+
68+
let Some(c) = opt else { return };
69+
70+
let Some(d) = some_very_very_very_very_long_name else { return };
71+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
// rustfmt-use_small_heuristics: Max
3+
// rustfmt-doc_comment_code_block_small_heuristics: Default
4+
5+
/// Start of a doc comment.
6+
///
7+
/// ```
8+
/// enum Lorem {
9+
/// Ipsum,
10+
/// Dolor(bool),
11+
/// Sit { amet: Consectetur, adipiscing: Elit },
12+
/// }
13+
///
14+
/// fn main() {
15+
/// lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
16+
///
17+
/// let lorem = Lorem { ipsum: dolor, sit: amet };
18+
///
19+
/// let lorem = if ipsum { dolor } else { sit };
20+
/// }
21+
///
22+
/// fn format_let_else() {
23+
/// let Some(a) = opt else {};
24+
///
25+
/// let Some(b) = opt else { return };
26+
///
27+
/// let Some(c) = opt else { return };
28+
///
29+
/// let Some(d) = some_very_very_very_very_long_name else { return };
30+
/// }
31+
/// ```
32+
///
33+
/// End of a doc comment.
34+
struct S;
35+
36+
enum Lorem {
37+
Ipsum,
38+
Dolor(bool),
39+
Sit {
40+
amet: Consectetur,
41+
adipiscing: Elit,
42+
},
43+
}
44+
45+
fn main() {
46+
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
47+
48+
let lorem = Lorem {
49+
ipsum: dolor,
50+
sit: amet,
51+
};
52+
53+
let lorem = if ipsum {
54+
dolor
55+
} else {
56+
sit
57+
};
58+
}
59+
60+
fn format_let_else() {
61+
let Some(a) = opt else {};
62+
63+
let Some(b) = opt else { return };
64+
65+
let Some(c) = opt else { return };
66+
67+
let Some(d) = some_very_very_very_very_long_name else { return };
68+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
// rustfmt-use_small_heuristics: Default
3+
// rustfmt-doc_comment_code_block_small_heuristics: Max
4+
5+
/// Start of a doc comment.
6+
///
7+
/// ```
8+
/// enum Lorem {
9+
/// Ipsum,
10+
/// Dolor(bool),
11+
/// Sit { amet: Consectetur, adipiscing: Elit },
12+
/// }
13+
///
14+
/// fn main() {
15+
/// lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
16+
///
17+
/// let lorem = Lorem { ipsum: dolor, sit: amet };
18+
///
19+
/// let lorem = if ipsum { dolor } else { sit };
20+
/// }
21+
///
22+
/// fn format_let_else() {
23+
/// let Some(a) = opt else {};
24+
///
25+
/// let Some(b) = opt else { return };
26+
///
27+
/// let Some(c) = opt else { return };
28+
///
29+
/// let Some(d) = some_very_very_very_very_long_name else {
30+
/// return;
31+
/// };
32+
/// }
33+
/// ```
34+
///
35+
/// End of a doc comment.
36+
struct S;
37+
38+
enum Lorem {
39+
Ipsum,
40+
Dolor(bool),
41+
Sit { amet: Consectetur, adipiscing: Elit },
42+
}
43+
44+
fn main() {
45+
lorem(
46+
"lorem",
47+
"ipsum",
48+
"dolor",
49+
"sit",
50+
"amet",
51+
"consectetur",
52+
"adipiscing",
53+
);
54+
55+
let lorem = Lorem {
56+
ipsum: dolor,
57+
sit: amet,
58+
};
59+
60+
let lorem = if ipsum { dolor } else { sit };
61+
}
62+
63+
fn format_let_else() {
64+
let Some(a) = opt else {};
65+
66+
let Some(b) = opt else { return };
67+
68+
let Some(c) = opt else { return };
69+
70+
let Some(d) = some_very_very_very_very_long_name else {
71+
return;
72+
};
73+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
// rustfmt-use_small_heuristics: Max
3+
// rustfmt-doc_comment_code_block_small_heuristics: Default
4+
5+
/// Start of a doc comment.
6+
///
7+
/// ```
8+
/// enum Lorem {
9+
/// Ipsum,
10+
/// Dolor(bool),
11+
/// Sit { amet: Consectetur, adipiscing: Elit },
12+
/// }
13+
///
14+
/// fn main() {
15+
/// lorem(
16+
/// "lorem",
17+
/// "ipsum",
18+
/// "dolor",
19+
/// "sit",
20+
/// "amet",
21+
/// "consectetur",
22+
/// "adipiscing",
23+
/// );
24+
///
25+
/// let lorem = Lorem {
26+
/// ipsum: dolor,
27+
/// sit: amet,
28+
/// };
29+
///
30+
/// let lorem = if ipsum { dolor } else { sit };
31+
/// }
32+
///
33+
/// fn format_let_else() {
34+
/// let Some(a) = opt else {};
35+
///
36+
/// let Some(b) = opt else { return };
37+
///
38+
/// let Some(c) = opt else { return };
39+
///
40+
/// let Some(d) = some_very_very_very_very_long_name else {
41+
/// return;
42+
/// };
43+
/// }
44+
/// ```
45+
///
46+
/// End of a doc comment.
47+
struct S;
48+
49+
enum Lorem {
50+
Ipsum,
51+
Dolor(bool),
52+
Sit { amet: Consectetur, adipiscing: Elit },
53+
}
54+
55+
fn main() {
56+
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
57+
58+
let lorem = Lorem { ipsum: dolor, sit: amet };
59+
60+
let lorem = if ipsum { dolor } else { sit };
61+
}
62+
63+
fn format_let_else() {
64+
let Some(a) = opt else {};
65+
66+
let Some(b) = opt else { return };
67+
68+
let Some(c) = opt else { return };
69+
70+
let Some(d) = some_very_very_very_very_long_name else { return };
71+
}

0 commit comments

Comments
 (0)