Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix formatting issue with IfConfigDecl/ImportDecl interaction.#860

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
shawnhyam wants to merge2 commits intoswiftlang:main
base:main
Choose a base branch
Loading
fromshawnhyam:if-config-decl-import-problem

Conversation

@shawnhyam
Copy link
Contributor

The ImportDecl syntax node disables all line breaks (other thanhard). This creates code that fails to compile when#if/#endif are involved (see example), removing all the line breaks after the#if <clause> as well as the#endif. Switching to hard line breaks would certainly work but feels a bit heavy handed and could end up with extra line breaks in other scenarios.

@shawnhyam
Copy link
ContributorAuthor

@allevato I'm wondering if you have any thoughts on this; I want to make sure I'm understanding everything correctly and that there aren't any other logic bugs. We insert aprinterControl token on an ImportDecl node that disables breaking, which seems to disableall breaking other than.hard (is this correct?). Since we can have an#if clause at the beginning of an ImportDecl statement, that would imply we need a.hard line break at the end of each condition, as well as the#endif. However, I believe a hard line break may give us too many newlines in other contexts where breaking is enabled. But the newlines are absolutely required, so maybe this is the best approach anyway? Or maybe the pretty print features aren't interacting the way they're intended to...

@allevato
Copy link
Member

I think we want to keep theoverall stylistic idea that "imports look better all on one line", and so we only need to fix that logic specifically for imports that have#if-surrounded attributes. (There's value to having one-line imports in the general case; other than just having them look better when they're a block at the top of the file, naïve regex-based tools can scan imports without having to do a full parse or require swift-syntax.)

So, I think we can address this a different way. What if, inTokenStreamCreator.visit(ImportDeclSyntax), we just check to see if the decl'sattributes contain anyIfConfigDecls? If it does, move the printer control token to precede theimportKeyword token instead of the attribute list. Then, it will allow the attributes to span multiple lines (fixing the#if case) without causing unwanted wrapping for simpler imports.

Sure, that breaks the promise we're making to simpler tools that might want to quickly scan for those imports, but they were never going to handle something that complicated anyway without a full parse (and#if config evaluation).

@shawnhyam
Copy link
ContributorAuthor

That sounds like a more promising approach than what I'd considered so far, so I'll give that a try. Thanks for your thoughts!

@shawnhyamshawnhyamforce-pushed theif-config-decl-import-problem branch froma1fd498 to2065608CompareOctober 22, 2024 15:37
The ImportDeclSyntax attributes might include IfConfigDecls, whichabsolutely require line breaks in certain positions. The disablebreaking logic of ImportDecl was preventing these from being emitted.
@shawnhyamshawnhyamforce-pushed theif-config-decl-import-problem branch from2065608 to4f4be5dCompareOctober 22, 2024 16:05
@shawnhyamshawnhyam marked this pull request as ready for reviewOctober 22, 2024 16:31
.iOSSpecificModifier()
#endif
.commonModifier()
.commonModifier()
Copy link
Member

Choose a reason for hiding this comment

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

Can these changes be avoided? The idea is that the indentation of these would be the same if there weren't an intervening#if-config; i.e., we wouldn't have this:

SomeFunction(  foo,  bar)  .commonModifier()

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@allevatoallevatoallevato left review comments

@ahoppenahoppenAwaiting requested review from ahoppen

@bnbarhambnbarhamAwaiting requested review from bnbarhambnbarham is a code owner

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@shawnhyam@allevato

[8]ページ先頭

©2009-2025 Movatter.jp