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

Fix formatting issue with IfConfigDecl/ImportDecl interaction. #860

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

shawnhyam
Copy link
Contributor

The ImportDecl syntax node disables all line breaks (other than hard). 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
Contributor Author

@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 a printerControl token on an ImportDecl node that disables breaking, which seems to disable all 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 the overall 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, in TokenStreamCreator.visit(ImportDeclSyntax), we just check to see if the decl's attributes contain any IfConfigDecls? If it does, move the printer control token to precede the importKeyword 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
Contributor Author

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!

@shawnhyam shawnhyam force-pushed the if-config-decl-import-problem branch from a1fd498 to 2065608 Compare October 22, 2024 15:37
The ImportDeclSyntax attributes might include IfConfigDecls, which
absolutely require line breaks in certain positions. The disable
breaking logic of ImportDecl was preventing these from being emitted.
@shawnhyam shawnhyam force-pushed the if-config-decl-import-problem branch from 2065608 to 4f4be5d Compare October 22, 2024 16:05
@shawnhyam shawnhyam marked this pull request as ready for review October 22, 2024 16:31
@@ -510,7 +510,7 @@ final class IfConfigTests: PrettyPrintTestCase {
#if os(iOS)
.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 free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants