Skip to content
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

New config option to support lower width limit on code blocks in doc comments #5359

Closed
calebcartwright opened this issue Jun 1, 2022 · 0 comments · Fixed by #5372
Closed
Labels
feature-request good first issue Issues up for grabs, also good candidates for new rustfmt contributors help wanted

Comments

@calebcartwright
Copy link
Member

The motivation for this new option is outlined in #5345, and the option proposed here is a potential solution to that scenario.

Specifically, we need to introduce a new config option named something like doc_comment_code_block_width (but feel free to come up with a better name) that's used to set the width threshold used when rustfmt is reformating code blocks residing within doc comments.

This should be a fairly straightforward option to add, and a great way to start getting familiar with rustfmt, for anyone that's interested!

Implementation pointers:

  • Add an entry for the new option in the Configurations.md file, and the configuration mod, default value should also be 100
  • Update the cloned config value that's used when formatting the code block here:

    rustfmt/src/comment.rs

    Lines 726 to 742 in 79515f1

    let code_block = match self.code_block_attr.as_ref().unwrap() {
    CodeBlockAttribute::Rust
    if self.fmt.config.format_code_in_doc_comments()
    && !self.code_block_buffer.is_empty() =>
    {
    let mut config = self.fmt.config.clone();
    config.set().wrap_comments(false);
    if let Some(s) =
    crate::format_code_block(&self.code_block_buffer, &config, false)
    {
    trim_custom_comment_prefix(&s.snippet)
    } else {
    trim_custom_comment_prefix(&self.code_block_buffer)
    }
    }
    _ => trim_custom_comment_prefix(&self.code_block_buffer),
    };

I suspect what you'll want to do is ensure the max_width value on the cloned config is overridden to the lowest value between the new doc_comment_code_block_width or max_width (since the overarching max_width needs to be the ceiling anyway)

#5228 may also serve as a helpful reference for adding new options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request good first issue Issues up for grabs, also good candidates for new rustfmt contributors help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant