ruff_python_formatter: implement "dynamic" line width mode for docstring code formatting#9098
Merged
BurntSushi merged 7 commits intomainfrom Dec 12, 2023
Merged
ruff_python_formatter: implement "dynamic" line width mode for docstring code formatting#9098BurntSushi merged 7 commits intomainfrom
BurntSushi merged 7 commits intomainfrom
Conversation
770a101 to
a3ad4a6
Compare
Contributor
|
..._python_formatter/resources/test/fixtures/ruff/docstring_code_examples_dynamic_line_width.py
Show resolved
Hide resolved
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.
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.
3953bef to
e92a477
Compare
14 tasks
This file contains hidden or 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
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-widthsetting to additionally accept a string valuedynamic. Whendynamicis 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
dynamicmode.