Skip to content

[New rule] spacingGuards #1804

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

Merged
merged 2 commits into from
Aug 1, 2024
Merged

Conversation

NikKovIos
Copy link
Contributor

This is a fixed version of #1801

Also added readme for #1777

@NikKovIos
Copy link
Contributor Author

NikKovIos commented Aug 1, 2024

@calda Could you please explain why in my project when I try to get new rule with cocoapods:

pod 'SwiftFormat/CLI', :git => 'https://github.com/NikKovIos/SwiftFormatNikeKov.git', :branch => 'ruleSpacingGuards'

I have an error:

Running SwiftFormat...
Reading config file at /Users/<>/r/<>/.swiftformat
Reading swift-version file at /Users/<>/r/<>/.swift-version (version 5.10)
error: Unknown rule 'spacingGuards'. Did you mean 'specifiers'?

Before merging I'd like to test rule in my project and can't understand why it not see my new rule.

--enable anyObjectProtocol, \
spacingGuards

@calda
Copy link
Collaborator

calda commented Aug 1, 2024

Could you please explain why in my project when I try to get new rule with cocoapods

Not sure, I've never tried to use Cocoapods like this.

Here's a convenient way to run the CLI tool for a given commit, or WIP local code changes:

  1. Change the scheme to the SwiftFormat (Command Line Tool)
image
  1. In the scheme Arguments Passed On Launch value, specify the path to the code you want to format, and your config. (Whatever arguments you would normally use when calling $ swiftformat ...
image
  1. Run the command line tool from Xcode


/// Formatting linebreaks
/// Setting `linebreaksCount` linebreaks in `indexes`
func leaveOrSetLinebreaksInIndexes(_ indexes: Set<Int>, linebreaksCount: Int) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we put this in an extension in SpacingGuards.swift? Like:

extension Formatter {
    /// Formatting linebreaks
     /// Setting `linebreaksCount` linebreaks in `indexes`
     func leaveOrSetLinebreaksInIndexes(_ indexes: Set<Int>, linebreaksCount: Int) { ... }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can put it there as fileprivate func. However it could be useful for other rules. If so - better to leave it in common place public extension Formatter.

Copy link
Collaborator

@calda calda left a comment

Choose a reason for hiding this comment

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

Looking good!

I see there are still several failing tests that need to be updated. Let me know if you have any questions about how to fix them or run in to issues!

}

let isGuard = nextNonSpaceAndNonLinebreakToken == .keyword("guard")
let indexesBetween = Set(endOfScopeOfGuard + 1 ... nextNonSpaceAndNonLinebreakIndex - 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

When running the tests locally I hit a crash here from endOfScopeOfGuard + 1 and nextNonSpaceAndNonLinebreakIndex - 1 being the same value, which is not allowed in a ClosedRange.

You can fix this by using a Range instead:

Suggested change
let indexesBetween = Set(endOfScopeOfGuard + 1 ... nextNonSpaceAndNonLinebreakIndex - 1)
let indexesBetween = Set(endOfScopeOfGuard + 1 ..< nextNonSpaceAndNonLinebreakIndex)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice 👍

@NikKovIos
Copy link
Contributor Author

@calda done

Copy link

codecov bot commented Aug 1, 2024

Codecov Report

Attention: Patch coverage is 96.61017% with 2 lines in your changes missing coverage. Please review.

Project coverage is 95.16%. Comparing base (410de98) to head (441affb).

Files Patch % Lines
Sources/Formatter.swift 95.00% 1 Missing ⚠️
Sources/Rules/SpacingGuards.swift 96.15% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1804      +/-   ##
===========================================
- Coverage    95.17%   95.16%   -0.01%     
===========================================
  Files          132      133       +1     
  Lines        23723    23782      +59     
===========================================
+ Hits         22578    22632      +54     
- Misses        1145     1150       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@calda calda left a comment

Choose a reason for hiding this comment

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

Good stuff

@calda calda merged commit 7cea762 into nicklockwood:develop Aug 1, 2024
10 checks passed
nicklockwood pushed a commit that referenced this pull request Aug 24, 2024
nicklockwood pushed a commit that referenced this pull request Sep 1, 2024
nicklockwood pushed a commit that referenced this pull request Sep 1, 2024
nicklockwood pushed a commit that referenced this pull request Sep 1, 2024
nicklockwood pushed a commit that referenced this pull request Sep 4, 2024
nicklockwood pushed a commit that referenced this pull request Sep 5, 2024
nicklockwood pushed a commit that referenced this pull request Sep 5, 2024
nicklockwood pushed a commit that referenced this pull request Sep 5, 2024
nicklockwood pushed a commit that referenced this pull request Sep 5, 2024
nicklockwood pushed a commit that referenced this pull request Sep 6, 2024
nicklockwood pushed a commit that referenced this pull request Sep 6, 2024
nicklockwood pushed a commit that referenced this pull request Sep 7, 2024
nicklockwood pushed a commit that referenced this pull request Sep 7, 2024
nicklockwood pushed a commit that referenced this pull request Sep 8, 2024
nicklockwood pushed a commit that referenced this pull request Sep 8, 2024
nicklockwood pushed a commit that referenced this pull request Sep 9, 2024
nicklockwood pushed a commit that referenced this pull request Nov 24, 2024
nicklockwood pushed a commit that referenced this pull request Nov 26, 2024
nicklockwood pushed a commit that referenced this pull request Nov 26, 2024
nicklockwood pushed a commit that referenced this pull request Nov 27, 2024
nicklockwood pushed a commit that referenced this pull request Dec 10, 2024
nicklockwood pushed a commit that referenced this pull request Dec 16, 2024
nicklockwood pushed a commit that referenced this pull request Dec 22, 2024
nicklockwood pushed a commit that referenced this pull request Dec 22, 2024
nicklockwood pushed a commit that referenced this pull request Dec 22, 2024
nicklockwood pushed a commit that referenced this pull request Dec 22, 2024
nicklockwood pushed a commit that referenced this pull request Jan 19, 2025
nicklockwood pushed a commit that referenced this pull request Jan 19, 2025
nicklockwood pushed a commit that referenced this pull request Jan 19, 2025
nicklockwood pushed a commit that referenced this pull request Jan 19, 2025
nicklockwood pushed a commit that referenced this pull request Jan 20, 2025
nicklockwood pushed a commit that referenced this pull request Jan 21, 2025
calda pushed a commit to calda/SwiftFormat that referenced this pull request Apr 30, 2025
calda pushed a commit that referenced this pull request Apr 30, 2025
calda pushed a commit that referenced this pull request Apr 30, 2025
calda pushed a commit to calda/SwiftFormat that referenced this pull request May 2, 2025
@NikKovIos
Copy link
Contributor Author

@calda @nicklockwood Could we please add this rule to release?

@calda
Copy link
Collaborator

calda commented May 5, 2025

I plan on making a new release in the next few weeks

@NikKovIos
Copy link
Contributor Author

Would be appreciated) I have a couple of minds about new rules, but don't wont to spend time for them if the rule not releasing.)

@calda
Copy link
Collaborator

calda commented May 9, 2025

In the mean time, I set up a new nightly build pipeline. Using a nightly build would let you start using new rules / options right away in your projects before they're included in an official release: https://github.com/nicklockwood/SwiftFormat?tab=readme-ov-file#prerelease-builds

@NikKovIos
Copy link
Contributor Author

@calda Can I use them via cocoapods?

@calda
Copy link
Collaborator

calda commented May 9, 2025

We don't have it set up to support Cocoapods.

We also just updated Homebrew support so that brew install swiftformat --HEAD builds and installs the latest changes from the develop branch, if that's helpful as an alternative: https://github.com/nicklockwood/SwiftFormat?tab=readme-ov-file#prerelease-builds

@calda
Copy link
Collaborator

calda commented May 13, 2025

0.56.0 has been released and includes this change: https://github.com/nicklockwood/SwiftFormat/releases/tag/0.56.0

Thanks for your patience @NikKovIos, more rules are always welcome!

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