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

Separate StringNormalizer from StringPart #9954

Merged
merged 1 commit into from
Feb 13, 2024
Merged

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Feb 12, 2024

Summary

This PR is a small refactor to extract out the logic for normalizing string in the formatter from the StringPart struct. It also separates the quote selection into a separate method on the new StringNormalizer. Both of these will help in the f-string formatting to use StringPart and choose_quotes irrespective of normalization.

The reason for having separate quote selection and normalization step is so that the f-string formatting can perform quote selection on its own.

Unlike string and byte literals, the f-string formatting would require that the normalization happens only for the literal elements of it i.e., the "foo" and "bar" in f"foo {x + y} bar". This will automatically be handled by the already separate normalize_string function.

Another use-case in the f-string formatting is to extract out the relevant information from the StringPart like quotes and prefix which is to be passed as context while formatting each element of an f-string.

Test Plan

Ensure that clippy is happy and all tests pass.

@dhruvmanila dhruvmanila added the internal An internal refactor or improvement label Feb 12, 2024
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Could you add an explanation to your PR explaining what motivates extracting the normalisation. E.g., what's difficult with the current design?I read that you want to use StringPart without the normalization. I understand this is possible today, for as long as you don't call part.normalze?

crates/ruff_python_formatter/src/string/mod.rs Outdated Show resolved Hide resolved
Base automatically changed from dhruv/fix-debug-text to main February 12, 2024 17:30
@dhruvmanila dhruvmanila force-pushed the dhruv/split-string-part branch from 55195fd to 8627f40 Compare February 12, 2024 18:50
Copy link
Contributor

github-actions bot commented Feb 12, 2024

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@dhruvmanila dhruvmanila force-pushed the dhruv/split-string-part branch 2 times, most recently from d3402d7 to a431f6a Compare February 13, 2024 12:10
@dhruvmanila dhruvmanila force-pushed the dhruv/split-string-part branch from a431f6a to 334616d Compare February 13, 2024 12:28
@dhruvmanila dhruvmanila merged commit 6f9c128 into main Feb 13, 2024
17 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/split-string-part branch February 13, 2024 12:44
nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
## Summary

This PR is a small refactor to extract out the logic for normalizing
string in the formatter from the `StringPart` struct. It also separates
the quote selection into a separate method on the new
`StringNormalizer`. Both of these will help in the f-string formatting
to use `StringPart` and `choose_quotes` irrespective of normalization.

The reason for having separate quote selection and normalization step is
so that the f-string formatting can perform quote selection on its own.

Unlike string and byte literals, the f-string formatting would require
that the normalization happens only for the literal elements of it i.e.,
the "foo" and "bar" in `f"foo {x + y} bar"`. This will automatically be
handled by the already separate `normalize_string` function.

Another use-case in the f-string formatting is to extract out the
relevant information from the `StringPart` like quotes and prefix which
is to be passed as context while formatting each element of an f-string.

## Test Plan

Ensure that clippy is happy and all tests pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants