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

Docstring formatting: Preserve tab indentation when using indent-style=tabs #9915

Merged
merged 4 commits into from
Feb 12, 2024

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Feb 9, 2024

Summary

This PR ensures that the formatter preserves tabs inside of docstring when using indent-style=tab.

Today, the formatter converts all docstring-indent to spaces, regardless of the configured indent-style.
This is unexpected for users and creates with E101 (mixed-spaces-and-tabs). For example:

def tab_argument(arg1: str) -> None:
	"""
	Arguments:
		arg1: super duper arg with 2 tabs in front
	"""

Gets reformatted to

def tab_argument(arg1: str) -> None:
	"""
	Arguments:
	        arg1: super duper arg with 2 tabs in front
	"""

Even though the user expliclty wants tab indentations.

The formatter won't automatically convert multiples of indent-width spaces to tabs because it can break
ASCII arts (and other formatting that use spaces for alignmen).

The downside of this change is tha the formatter won't enforce a consistent indentation inside of docstrings when using indent-style=tab.

Fixes #8430

This PR does not yet address that our formatter always expands tab to 8 spaces (instead of the configured indent-width)

It's not necessary to gate this change behind preview because it doesn't change already formatted code.

Test Plan

added tests, cargo test

@MichaReiser MichaReiser added the formatter Related to the formatter label Feb 9, 2024
Copy link
Contributor

github-actions bot commented Feb 9, 2024

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser MichaReiser force-pushed the preserve-docstring-tabs branch from 25071ca to 6d2417a Compare February 9, 2024 20:42
@MichaReiser MichaReiser force-pushed the preserve-docstring-tabs branch from 3ad02d3 to 974e485 Compare February 10, 2024 06:39
@MichaReiser MichaReiser marked this pull request as ready for review February 10, 2024 06:40
Copy link
Member

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

This looks great to me! I think it might help to polish up the Indent type a touch, but I love the approach. :)

crates/ruff_python_formatter/src/string/docstring.rs Outdated Show resolved Hide resolved
crates/ruff_python_formatter/src/string/docstring.rs Outdated Show resolved Hide resolved
crates/ruff_python_formatter/src/string/docstring.rs Outdated Show resolved Hide resolved
crates/ruff_python_formatter/src/string/docstring.rs Outdated Show resolved Hide resolved
@MichaReiser MichaReiser merged commit 8657a39 into main Feb 12, 2024
17 checks passed
@MichaReiser MichaReiser deleted the preserve-docstring-tabs branch February 12, 2024 15:09
nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
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.

Formatter: Uses space indentation in docstrings when using indent-style = "tab"
2 participants