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

Introduce Token element #7048

Merged
merged 4 commits into from
Sep 2, 2023
Merged

Introduce Token element #7048

merged 4 commits into from
Sep 2, 2023

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Sep 1, 2023

Summary

This PR replaces StaticText with a stricter Token element

  • ASCII only
  • No newlines
  • No tab characters

This proves to be sufficient for the text usages in Ruff's formatter today (and probably all code formatters) and has the advantage that the formatter can take shortcuts for these strings.

I thought about introducing a new Token element instead of replacing Text. But we don't really have any use case for it other than doctests, which doesn't justify adding a new format element.

Feedback welcome on

Should we rename dynamic_text to text?

Test Plan

cargo test

Performance

This improves performance between 2-3%

@MichaReiser
Copy link
Member Author

MichaReiser commented Sep 1, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@MichaReiser MichaReiser added the formatter Related to the formatter label Sep 1, 2023
/// indent(&format_args![
/// hard_line_break(),
/// text("indent level 1"),
/// token("indent level 1"),
Copy link
Member Author

Choose a reason for hiding this comment

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

This is arguably worse because these aren't really tokens but having a text builder just for the examples feels overkill (and the examples don't violate any constraint)

@zanieb
Copy link
Member

zanieb commented Sep 1, 2023

Cool! I like the rename of dynamic_text to text as well

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.

I support changing DynamicText to text.

crates/ruff_formatter/src/builders.rs Outdated Show resolved Hide resolved
@MichaReiser MichaReiser mentioned this pull request Sep 1, 2023
@MichaReiser MichaReiser merged commit c05e462 into main Sep 2, 2023
17 checks passed
@MichaReiser MichaReiser deleted the token-element branch September 2, 2023 08:05
fmt.debug_tuple("DynamicText").field(text).finish()
}
FormatElement::Token { text } => fmt.debug_tuple("Token").field(text).finish(),
FormatElement::Text { text, .. } => fmt.debug_tuple("DynamicText").field(text).finish(),

Choose a reason for hiding this comment

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

I guess that DynamicText -> Text.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter performance Potential performance improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants