Fix formatting of import with multiple attributes (fixes #445) and ensure that imports never wrap #501
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#445 occurs because Swift Format simply just adds a space after the attribute list of import statements.
arrangeAttributeList
can be reused to correctly format the attributes of an import. However, when modifying the test for imports to include an import decl with multiple attributes, I found that attributes with arguments (e.g.@_spi(STP)
) get wrapped when the import decl exceeds the line length. The comment included with the existing tests says that import decls shouldn't wrap (and I take this to also mean that discretionary breaks should be removed if present). Thus, I have made additional changes to ensure that import statements never wrap in any situation, please correct me if this is not the desired behaviour.To achieve this behaviour (see snippet below), I had to modify the
disableBreaking
printer control to be able to configure whether discretionary breaks should be allowed when breaks are suppressed (currently they are, but for the case of import decls this needs to be overridden to ensure that imports never wrap).I can see people wanting to be able to write code that wraps between attributes (as is sometimes done with
@resultBuilder
on struct decls, or property wrappers). Should this be made configurable, or is it bad style?I also fixed the typo of
isBreakingSupressed
(toisBreakingSuppressed
) because it was only used in 3 places or so, which imo makes a merge conflict very unlikely. I'm happy to revert that if requested.