Skip to content

Commit

Permalink
ColonRule allows setting flexible_right_spacing.
Browse files Browse the repository at this point in the history
Resolves issue realm#730.
  • Loading branch information
freak4pc committed Jul 26, 2016
1 parent df1f1a8 commit 3a5eedf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,10 @@

##### Enhancements

* Allow setting `flexible_right_spacing` configuration for the `colon` rule.
[Shai Mishali](https://github.com/freak4pc)
[#730](https://github.com/realm/SwiftLint/issues/730)

* Add Junit reporter.
[Matthew Ellis](https://github.com/matthewellis)

Expand Down
41 changes: 28 additions & 13 deletions Source/SwiftLintFramework/Rules/ColonRule.swift
Expand Up @@ -15,6 +15,16 @@ public struct ColonRule: CorrectableRule, ConfigurationProviderRule {

public init() {}

public var flexibleRightSpacing = false

public init(configuration: AnyObject) throws {
flexibleRightSpacing = configuration["flexible_right_spacing"] as? Int == 1
}

public var configurationDescription: String {
return "flexible_right_spacing: \(flexibleRightSpacing)"
}

public static let description = RuleDescription(
identifier: "colon",
name: "Colon",
Expand Down Expand Up @@ -110,19 +120,24 @@ public struct ColonRule: CorrectableRule, ConfigurationProviderRule {

// MARK: - Private

private let pattern =
"(\\w)" + // Capture an identifier
"(?:" + // start group
"\\s+" + // followed by whitespace
":" + // to the left of a colon
"\\s*" + // followed by any amount of whitespace.
"|" + // or
":" + // immediately followed by a colon
"(?:\\s{0}|\\s{2,})" + // followed by 0 or 2+ whitespace characters.
")" + // end group
"(" + // Capture a type identifier
"[\\[|\\(]*" + // which may begin with a series of nested parenthesis or brackets
"\\S)" // lazily to the first non-whitespace character.
private var pattern: String {
// If flexible_right_spacing is true, match only 0 whitespaces.
// If flexible_right_spacing is false or omitted, match 0 or 2+ whitespaces.
let spacingRegex = flexibleRightSpacing ? "(?:\\s{0})" : "(?:\\s{0}|\\s{2,})"

return "(\\w)" + // Capture an identifier
"(?:" + // start group
"\\s+" + // followed by whitespace
":" + // to the left of a colon
"\\s*" + // followed by any amount of whitespace.
"|" + // or
":" + // immediately followed by a colon
spacingRegex + // followed by right spacing regex
")" + // end group
"(" + // Capture a type identifier
"[\\[|\\(]*" + // which may begin with a series of nested parenthesis or brackets
"\\S)" // lazily to the first non-whitespace character.
}

private func violationRangesInFile(file: File, withPattern pattern: String) -> [NSRange] {
let nsstring = file.contents as NSString
Expand Down

0 comments on commit 3a5eedf

Please sign in to comment.