Skip to content

Comments

Introduce Token element#7048

Merged
MichaReiser merged 4 commits intomainfrom
token-element
Sep 2, 2023
Merged

Introduce Token element#7048
MichaReiser merged 4 commits intomainfrom
token-element

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)

@MichaReiser MichaReiser added the performance Potential performance improvement label Sep 1, 2023
@MichaReiser MichaReiser marked this pull request as ready for review September 1, 2023 17:01
@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.

@MichaReiser MichaReiser mentioned this pull request Sep 1, 2023
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