Skip to content

Add doc_comment_code_block_small_heuristics, to override use_small_heuristics in doc code #6616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,14 @@ Max width for code snippets included in doc comments. Only used if [`format_code
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
- **Stable**: No (tracking issue: [#5359](https://github.com/rust-lang/rustfmt/issues/5359))

## `doc_comment_code_block_small_heuristics`

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.

- **Default value**: `"Default"`
- **Possible values**: `"Default"`, `"Off"`, `"Max"`
- **Stable**: No (tracking issue: [#FIXME](https://github.com/rust-lang/rustfmt/issues/FIXME))

## `format_generated_files`

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`.
Expand Down
8 changes: 8 additions & 0 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,14 @@ impl<'a> CommentRewrite<'a> {
.doc_comment_code_block_width()
.min(config.max_width());
config.set().max_width(comment_max_width);
if let Some(comment_use_small_heuristics) = config
.doc_comment_code_block_small_heuristics()
.to_heuristics()
{
config
.set()
.use_small_heuristics(comment_use_small_heuristics);
}
if let Some(s) =
crate::format_code_block(&self.code_block_buffer, &config, false)
{
Expand Down
5 changes: 5 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ create_config! {
doc comments.";
doc_comment_code_block_width: DocCommentCodeBlockWidth, false, "Maximum width for code \
snippets in doc comments. No effect unless format_code_in_doc_comments = true";
doc_comment_code_block_small_heuristics: DocUseSmallHeuristics, false,
"Value for use_small_heuristics for code blocks in doc comments. \
No effect unless format_code_in_doc_comments = true";
comment_width: CommentWidth, false,
"Maximum length of comments. No effect unless wrap_comments = true";
normalize_comments: NormalizeComments, false, "Convert /* */ comments to // comments where \
Expand Down Expand Up @@ -772,6 +775,7 @@ single_line_let_else_max_width = 50
wrap_comments = false
format_code_in_doc_comments = false
doc_comment_code_block_width = 100
doc_comment_code_block_small_heuristics = "Inherit"
comment_width = 80
normalize_comments = false
normalize_doc_attributes = false
Expand Down Expand Up @@ -864,6 +868,7 @@ single_line_let_else_max_width = 50
wrap_comments = false
format_code_in_doc_comments = false
doc_comment_code_block_width = 100
doc_comment_code_block_small_heuristics = "Inherit"
comment_width = 80
normalize_comments = false
normalize_doc_attributes = false
Expand Down
26 changes: 26 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ pub enum Heuristics {
Default,
}

#[config_type]
/// Heuristic settings for doc comments. Same as `Heuristics`, but `Inherit` will inherit the value
/// from the top-level configuration.
pub enum DocCodeHeuristics {
/// Inherit from the top-level configuration
Inherit,
/// Turn off any heuristics
Off,
/// Turn on max heuristics
Max,
/// Use scaled values based on the value of `max_width`
Default,
}

impl DocCodeHeuristics {
pub fn to_heuristics(self) -> Option<Heuristics> {
match self {
DocCodeHeuristics::Inherit => None,
DocCodeHeuristics::Off => Some(Heuristics::Off),
DocCodeHeuristics::Max => Some(Heuristics::Max),
DocCodeHeuristics::Default => Some(Heuristics::Default),
}
}
}

impl Density {
pub fn to_list_tactic(self, len: usize) -> ListTactic {
match self {
Expand Down Expand Up @@ -620,6 +645,7 @@ config_option_with_style_edition_default!(
WrapComments, bool, _ => false;
FormatCodeInDocComments, bool, _ => false;
DocCommentCodeBlockWidth, usize, _ => 100;
DocUseSmallHeuristics, DocCodeHeuristics, _ => DocCodeHeuristics::Inherit;
CommentWidth, usize, _ => 80;
NormalizeComments, bool, _ => false;
NormalizeDocAttributes, bool, _ => false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// rustfmt-format_code_in_doc_comments: true
// rustfmt-use_small_heuristics: Max
// rustfmt-doc_comment_code_block_small_heuristics: Default

/// Start of a doc comment.
///
/// ```
/// enum Lorem {
/// Ipsum,
/// Dolor(bool),
/// Sit { amet: Consectetur, adipiscing: Elit },
/// }
///
/// fn main() {
/// lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
///
/// let lorem = Lorem { ipsum: dolor, sit: amet };
///
/// let lorem = if ipsum { dolor } else { sit };
/// }
///
/// fn format_let_else() {
/// let Some(a) = opt else {};
///
/// let Some(b) = opt else { return };
///
/// let Some(c) = opt else { return };
///
/// let Some(d) = some_very_very_very_very_long_name else { return };
/// }
/// ```
///
/// End of a doc comment.
struct S;

enum Lorem {
Ipsum,
Dolor(bool),
Sit {
amet: Consectetur,
adipiscing: Elit,
},
}

fn main() {
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");

let lorem = Lorem {
ipsum: dolor,
sit: amet,
};

let lorem = if ipsum {
dolor
} else {
sit
};
}

fn format_let_else() {
let Some(a) = opt else {};

let Some(b) = opt else { return };

let Some(c) = opt else { return };

let Some(d) = some_very_very_very_very_long_name else { return };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// rustfmt-format_code_in_doc_comments: true
// rustfmt-use_small_heuristics: Max
// rustfmt-doc_comment_code_block_small_heuristics: Default

/// Start of a doc comment.
///
/// ```
/// enum Lorem {
/// Ipsum,
/// Dolor(bool),
/// Sit { amet: Consectetur, adipiscing: Elit },
/// }
///
/// fn main() {
/// lorem(
/// "lorem",
/// "ipsum",
/// "dolor",
/// "sit",
/// "amet",
/// "consectetur",
/// "adipiscing",
/// );
///
/// let lorem = Lorem {
/// ipsum: dolor,
/// sit: amet,
/// };
///
/// let lorem = if ipsum { dolor } else { sit };
/// }
///
/// fn format_let_else() {
/// let Some(a) = opt else {};
///
/// let Some(b) = opt else { return };
///
/// let Some(c) = opt else { return };
///
/// let Some(d) = some_very_very_very_very_long_name else {
/// return;
/// };
/// }
/// ```
///
/// End of a doc comment.
struct S;

enum Lorem {
Ipsum,
Dolor(bool),
Sit { amet: Consectetur, adipiscing: Elit },
}

fn main() {
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");

let lorem = Lorem { ipsum: dolor, sit: amet };

let lorem = if ipsum { dolor } else { sit };
}

fn format_let_else() {
let Some(a) = opt else {};

let Some(b) = opt else { return };

let Some(c) = opt else { return };

let Some(d) = some_very_very_very_very_long_name else { return };
}
Loading