Skip to content

[New rule] spacingGuards#1804

Merged
calda merged 2 commits intonicklockwood:developfrom
NikKovIos:ruleSpacingGuards
Aug 1, 2024
Merged

[New rule] spacingGuards#1804
calda merged 2 commits intonicklockwood:developfrom
NikKovIos:ruleSpacingGuards

Conversation

@NikKovIos
Copy link
Copy Markdown
Contributor

This is a fixed version of #1801

Also added readme for #1777

@NikKovIos
Copy link
Copy Markdown
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
Copy Markdown
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

Comment thread Sources/Rules/SpacingGuards.swift
Comment thread Sources/Formatter.swift

/// Formatting linebreaks
/// Setting `linebreaksCount` linebreaks in `indexes`
func leaveOrSetLinebreaksInIndexes(_ indexes: Set<Int>, linebreaksCount: Int) {
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread README.md
Copy link
Copy Markdown
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!

Comment thread Sources/Rules/SpacingGuards.swift Outdated
}

let isGuard = nextNonSpaceAndNonLinebreakToken == .keyword("guard")
let indexesBetween = Set(endOfScopeOfGuard + 1 ... nextNonSpaceAndNonLinebreakIndex - 1)
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

@calda done

@codecov
Copy link
Copy Markdown

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
Copy Markdown
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
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 3, 2024
nicklockwood pushed a commit that referenced this pull request Nov 3, 2024
nicklockwood pushed a commit that referenced this pull request Nov 3, 2024
nicklockwood pushed a commit that referenced this pull request Nov 3, 2024
nicklockwood pushed a commit that referenced this pull request Nov 9, 2024
nicklockwood pushed a commit that referenced this pull request Nov 16, 2024
nicklockwood pushed a commit that referenced this pull request Nov 16, 2024
nicklockwood pushed a commit that referenced this pull request Nov 16, 2024
nicklockwood pushed a commit that referenced this pull request Nov 16, 2024
nicklockwood pushed a commit that referenced this pull request Nov 16, 2024
nicklockwood pushed a commit that referenced this pull request Nov 19, 2024
nicklockwood pushed a commit that referenced this pull request Nov 20, 2024
nicklockwood pushed a commit that referenced this pull request Nov 21, 2024
nicklockwood pushed a commit that referenced this pull request Nov 23, 2024
nicklockwood pushed a commit that referenced this pull request Nov 23, 2024
nicklockwood pushed a commit that referenced this pull request Nov 23, 2024
nicklockwood pushed a commit that referenced this pull request Nov 23, 2024
nicklockwood pushed a commit that referenced this pull request Nov 23, 2024
nicklockwood pushed a commit that referenced this pull request Nov 24, 2024
nicklockwood pushed a commit that referenced this pull request Nov 24, 2024
nicklockwood pushed a commit that referenced this pull request Nov 24, 2024
@NikKovIos
Copy link
Copy Markdown
Contributor Author

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

@calda
Copy link
Copy Markdown
Collaborator

calda commented May 5, 2025

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

@NikKovIos
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

@calda Can I use them via cocoapods?

@calda
Copy link
Copy Markdown
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
Copy Markdown
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