-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Refactor swayfmt
to support arbitrary lexed trees
#6806
Conversation
CodSpeed Performance ReportMerging #6806 will not alter performanceComparing Summary
|
Nice one @ironcev great work with this. Being able to format in memory structures is a great addition. Just a couple of code nits with trailing |
@alfiedotwtf Thanks for such a detailed review of token definitions! To keep this PR focused, I propose to have these potential changes as a separate PR, and combine it with the redefiniton of the I've opened a separate issue for that: #6808. Is this ok for you? |
My comments may not have been correct, but creating a new issue so we don't derail this PR is a good idea :) |
I've got no other suggestions to the PR. With my |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No more commas!
Description
This PR refactors
swayfmt
to be able generate code from arbitrary lexed trees. Arbitrary means a lexed tree that can be fully or partially created in-memory by manipulating the tree structure in code. It does not need to necessarily be backed by source code. This is needed to be able to reuseswayfmt
for generating source code from tools that analyze and modify Sway code, as explained in #6779.This PR makes the
swayfmt
independent of spans backed by the source code, by doing the following changes:AS_STR
associated constants.Token
trait is extended with theAS_STR
associated constant, and tokens are rendered by using that constant.Ident
s are rendered by using theirsas_str()
methods. This method takes the textual implementation fromIdent::name_override_opt
if it is provided, and ignores the span in that case.Literal
s are rendered based on the literal value and not their spans. The exception are numeric literals that do not have empty spans. Those are considered to be backed by source code and are rendered as written in code, e.g.,1_000_000u64
.The PR also fixes some existing bugs in
swayfmt
:use path::{single_import,};
will be formatted asuse path::single_import;
use path::{a,b,};
will be formatted asuse path::{a, b};
swayfmt
#6802 by rendering annotations for final values.Punctuated
without trailing comma everywhere inswayfmt
#6805 by properly rendering final values, but still not using the expected field alignment.The PR also removes the
path
parameter from theparse_file
. It was used only to provide an unnecessary dummysource_id
which was always pointing to the package root file.Closes #6779.
Checklist
Breaking*
orNew Feature
labels where relevant.