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

ruff_python_formatter: implement "dynamic" line width mode for docstring code formatting #9098

Merged
merged 7 commits into from
Dec 12, 2023

Conversation

BurntSushi
Copy link
Member

@BurntSushi BurntSushi commented Dec 11, 2023

Summary

This PR changes the internal docstring-code-line-width setting to additionally accept a string value dynamic. When dynamic is set, the line width is dynamically adjusted when reformatting code snippets in docstrings based on the indent level of the docstring. The result is that the reformatted lines from the code snippet should not exceed the "global" line width configuration for the surrounding source.

This PR does not change the default behavior, although I suspect the default should probably be dynamic.

Closes #8855

Test Plan

I added a new configuration to the existing docstring code tests and also added a new set of tests dedicated to the new dynamic mode.

Copy link
Contributor

github-actions bot commented Dec 11, 2023

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@charliermarsh charliermarsh added the formatter Related to the formatter label Dec 11, 2023
Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

crates/ruff_python_formatter/src/context.rs Outdated Show resolved Hide resolved
crates/ruff_python_formatter/src/context.rs Outdated Show resolved Hide resolved
crates/ruff_python_formatter/src/statement/suite.rs Outdated Show resolved Hide resolved
…amic

This makes the internal docstring code line width setting have
a "mixed type." That is, it can either be a numeric value indicating
a fixed line width, or it can be the string "dynamic" indicating that
the line width should respect the overall line width of the surrounding
code.
This lets us track the indent level as state during formatting.

We'll use this to compute the line width for docstring code
reformatting when "dynamic" mode is enabled.
This computes the line width to use on the nested call to the
formatter based on the current indent level, indent width and
configured global setting.

Ref #8855
This just uses the standard default configuration. We'll add the snapshot
itself in the next commit and then update the test configuration to use
the new "dynamic" setting so that we can scrutinize the specific changes.
This updates the snapshot with the added configuration from the
previous commit. This should match the default test configuration,
so there shouldn't be anything "interesting" in this commit.
This tests the "dynamic" line width mode. As with the fixed line width
setting, not much changes here since most of our code examples use very
short lines.

In a subsequent commit, we'll add more tests specifically targeted at
line width.
This commit adds a few tests specifically for checking the 'dynamic'
line width mode for formatting docstring code examples.
@BurntSushi BurntSushi merged commit b972455 into main Dec 12, 2023
17 checks passed
@BurntSushi BurntSushi deleted the ag/fmt/dynamic branch December 12, 2023 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

docstring code formatter: figure out how to handle line width
3 participants