Skip to content

Commit

Permalink
rename matchAndTokensPattern to rangesAndTokensMatching
Browse files Browse the repository at this point in the history
also align regex pattern comments & fix indentation
  • Loading branch information
jpsim committed Feb 10, 2016
1 parent 83c0085 commit 7d0afb1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
14 changes: 7 additions & 7 deletions Source/SwiftLintFramework/Extensions/File+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,22 @@ extension File {
}.map { $0.0 }
}

internal func matchAndTokensPattern(pattern: String) -> [(NSRange, [SyntaxToken])] {
return matchAndTokensPattern(regex(pattern))
internal func rangesAndTokensMatching(pattern: String) -> [(NSRange, [SyntaxToken])] {
return rangesAndTokensMatching(regex(pattern))
}

internal func matchAndTokensPattern(regex: NSRegularExpression) -> [(NSRange, [SyntaxToken])] {
internal func rangesAndTokensMatching(regex: NSRegularExpression) ->
[(NSRange, [SyntaxToken])] {
let contents = self.contents as NSString
let range = NSRange(location: 0, length: contents.length)
let syntax = syntaxMap
let matches = regex.matchesInString(self.contents, options: [], range: range)
return matches.map { match in
return regex.matchesInString(self.contents, options: [], range: range).map { match in
let matchByteRange = contents.NSRangeToByteRange(start: match.range.location,
length: match.range.length) ?? match.range
let tokensInRange = syntax.tokens.filter { token in
let tokenByteRange = NSRange(location: token.offset, length: token.length)
return NSIntersectionRange(matchByteRange, tokenByteRange).length > 0
}.map({ $0 })
}.map({ $0 })
return (match.range, tokensInRange)
}
}
Expand All @@ -98,7 +98,7 @@ extension File {
}

public func matchPattern(regex: NSRegularExpression) -> [(NSRange, [SyntaxKind])] {
return matchAndTokensPattern(regex).map { range, tokens in
return rangesAndTokensMatching(regex).map { range, tokens in
(range, tokens.map({ $0.type }).flatMap(SyntaxKind.init))
}
}
Expand Down
24 changes: 12 additions & 12 deletions Source/SwiftLintFramework/Rules/ColonRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,23 @@ public struct ColonRule: CorrectableRule, ConfigProviderRule {
// 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
"(\\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.
")" + // 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
let commentAndStringKindsSet = Set(SyntaxKind.commentAndStringKinds())
return file.matchAndTokensPattern(pattern).filter { range, syntaxTokens in
return file.rangesAndTokensMatching(pattern).filter { range, syntaxTokens in
let syntaxKinds = syntaxTokens.map({ $0.type }).flatMap(SyntaxKind.init)
if !syntaxKinds.startsWith([.Identifier, .Typeidentifier]) {
return false
Expand Down

0 comments on commit 7d0afb1

Please sign in to comment.