-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BurntSushi
force-pushed
the
ag/fmt/dynamic
branch
from
December 11, 2023 18:59
770a101
to
a3ad4a6
Compare
|
..._python_formatter/resources/test/fixtures/ruff/docstring_code_examples_dynamic_line_width.py
Show resolved
Hide resolved
charliermarsh
approved these changes
Dec 11, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
MichaReiser
approved these changes
Dec 12, 2023
…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.
BurntSushi
force-pushed
the
ag/fmt/dynamic
branch
from
December 12, 2023 14:12
a3ad4a6
to
3953bef
Compare
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
force-pushed
the
ag/fmt/dynamic
branch
from
December 12, 2023 14:25
3953bef
to
e92a477
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR changes the internal
docstring-code-line-width
setting to additionally accept a string valuedynamic
. Whendynamic
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.