Skip to content

Commit

Permalink
Add SwiftLint (#49)
Browse files Browse the repository at this point in the history
This change adds SwiftLint to the project. For now, the rules are tweaked
to match the current state of the code base — but will over time be changed
back to their defaults in several cases.

Some smaller changes (mostly related to code style) were applied to the
project to remove all warnings.
  • Loading branch information
JohnSundell authored Mar 11, 2019
1 parent 892d6ae commit 8bc46cb
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 72 deletions.
7 changes: 7 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
disabled_rules:
- nesting
line_length: 200
function_body_length: 100
type_body_length: 600
file_length: 800
cyclomatic_complexity: 15
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import PackageDescription
let package = Package(
name: "Splash",
products: [
.library(name: "Splash", targets: ["Splash"])
.library(name: "Splash", targets: ["Splash"])
],
targets: [
.target(name: "Splash"),
Expand Down
2 changes: 1 addition & 1 deletion Sources/Splash/Output/AttributedStringOutputFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private extension NSMutableAttributedString {
.foregroundColor: color,
.font: font
])

append(attributedString)
}
}
Expand Down
108 changes: 54 additions & 54 deletions Sources/Splash/Theming/Theme+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public extension Theme {
blue: 0.74
),
tokenColors: [
.keyword : Color(red: 0.91, green: 0.2, blue: 0.54),
.string : Color(red: 0.98, green: 0.39, blue: 0.12),
.type : Color(red: 0.51, green: 0.51, blue: 0.79),
.call : Color(red: 0.2, green: 0.56, blue: 0.9),
.number : Color(red: 0.86, green: 0.44, blue: 0.34),
.comment : Color(red: 0.42, green: 0.54, blue: 0.58),
.property : Color(red: 0.13, green: 0.67, blue: 0.62),
.dotAccess : Color(red: 0.57, green: 0.7, blue: 0),
.preprocessing : Color(red: 0.71, green: 0.54, blue: 0)
.keyword: Color(red: 0.91, green: 0.2, blue: 0.54),
.string: Color(red: 0.98, green: 0.39, blue: 0.12),
.type: Color(red: 0.51, green: 0.51, blue: 0.79),
.call: Color(red: 0.2, green: 0.56, blue: 0.9),
.number: Color(red: 0.86, green: 0.44, blue: 0.34),
.comment: Color(red: 0.42, green: 0.54, blue: 0.58),
.property: Color(red: 0.13, green: 0.67, blue: 0.62),
.dotAccess: Color(red: 0.57, green: 0.7, blue: 0),
.preprocessing: Color(red: 0.71, green: 0.54, blue: 0)
],
backgroundColor: Color(
red: 0.098,
Expand All @@ -47,15 +47,15 @@ public extension Theme {
blue: 1
),
tokenColors: [
.keyword : Color(red: 0.828, green: 0.095, blue: 0.583),
.string : Color(red: 1.0, green: 0.171, blue: 0.219),
.type : Color(red: 0.137, green: 1.0, blue: 0.512),
.call : Color(red: 0.137, green: 1.0, blue: 0.512),
.number : Color(red: 0.469, green: 0.426, blue: 1.00),
.comment : Color(red: 0.255, green: 0.801, blue: 0.27),
.property : Color(red: 0.431, green: 0.714, blue: 0.533),
.dotAccess : Color(red: 0.431, green: 0.714, blue: 0.533),
.preprocessing : Color(red: 0.896, green: 0.488, blue: 0.284)
.keyword: Color(red: 0.828, green: 0.095, blue: 0.583),
.string: Color(red: 1.0, green: 0.171, blue: 0.219),
.type: Color(red: 0.137, green: 1.0, blue: 0.512),
.call: Color(red: 0.137, green: 1.0, blue: 0.512),
.number: Color(red: 0.469, green: 0.426, blue: 1.00),
.comment: Color(red: 0.255, green: 0.801, blue: 0.27),
.property: Color(red: 0.431, green: 0.714, blue: 0.533),
.dotAccess: Color(red: 0.431, green: 0.714, blue: 0.533),
.preprocessing: Color(red: 0.896, green: 0.488, blue: 0.284)
],
backgroundColor: Color(
red: 0,
Expand All @@ -75,15 +75,15 @@ public extension Theme {
blue: 0.84
),
tokenColors: [
.keyword : Color(red: 0.992, green: 0.791, blue: 0.45),
.string : Color(red: 0.966, green: 0.517, blue: 0.29),
.type : Color(red: 0.431, green: 0.714, blue: 0.533),
.call : Color(red: 0.431, green: 0.714, blue: 0.533),
.number : Color(red: 0.559, green: 0.504, blue: 0.745),
.comment : Color(red: 0.484, green: 0.483, blue: 0.504),
.property : Color(red: 0.431, green: 0.714, blue: 0.533),
.dotAccess : Color(red: 0.431, green: 0.714, blue: 0.533),
.preprocessing : Color(red: 0.992, green: 0.791, blue: 0.45)
.keyword: Color(red: 0.992, green: 0.791, blue: 0.45),
.string: Color(red: 0.966, green: 0.517, blue: 0.29),
.type: Color(red: 0.431, green: 0.714, blue: 0.533),
.call: Color(red: 0.431, green: 0.714, blue: 0.533),
.number: Color(red: 0.559, green: 0.504, blue: 0.745),
.comment: Color(red: 0.484, green: 0.483, blue: 0.504),
.property: Color(red: 0.431, green: 0.714, blue: 0.533),
.dotAccess: Color(red: 0.431, green: 0.714, blue: 0.533),
.preprocessing: Color(red: 0.992, green: 0.791, blue: 0.45)
],
backgroundColor: Color(
red: 0.18,
Expand All @@ -103,15 +103,15 @@ public extension Theme {
blue: 1
),
tokenColors: [
.keyword : Color(red: 0.948, green: 0.140, blue: 0.547),
.string : Color(red: 0.988, green: 0.273, blue: 0.317),
.type : Color(red: 0.584, green: 0.898, blue: 0.361),
.call : Color(red: 0.584, green: 0.898, blue: 0.361),
.number : Color(red: 0.587, green: 0.517, blue: 0.974),
.comment : Color(red: 0.424, green: 0.475, blue: 0.529),
.property : Color(red: 0.584, green: 0.898, blue: 0.361),
.dotAccess : Color(red: 0.584, green: 0.898, blue: 0.361),
.preprocessing : Color(red: 0.952, green: 0.526, blue: 0.229)
.keyword: Color(red: 0.948, green: 0.140, blue: 0.547),
.string: Color(red: 0.988, green: 0.273, blue: 0.317),
.type: Color(red: 0.584, green: 0.898, blue: 0.361),
.call: Color(red: 0.584, green: 0.898, blue: 0.361),
.number: Color(red: 0.587, green: 0.517, blue: 0.974),
.comment: Color(red: 0.424, green: 0.475, blue: 0.529),
.property: Color(red: 0.584, green: 0.898, blue: 0.361),
.dotAccess: Color(red: 0.584, green: 0.898, blue: 0.361),
.preprocessing: Color(red: 0.952, green: 0.526, blue: 0.229)
],
backgroundColor: Color(
red: 0.163,
Expand All @@ -131,15 +131,15 @@ public extension Theme {
blue: 0
),
tokenColors: [
.keyword : Color(red: 0.161, green: 0.259, blue: 0.467),
.string : Color(red: 0.875, green: 0.027, blue: 0.0),
.type : Color(red: 0.706, green: 0.27, blue: 0.0),
.call : Color(red: 0.278, green: 0.415, blue: 0.593),
.number : Color(red: 0.161, green: 0.259, blue: 0.467),
.comment : Color(red: 0.765, green: 0.455, blue: 0.11),
.property : Color(red: 0.278, green: 0.415, blue: 0.593),
.dotAccess : Color(red: 0.278, green: 0.415, blue: 0.593),
.preprocessing : Color(red: 0.392, green: 0.391, blue: 0.52)
.keyword: Color(red: 0.161, green: 0.259, blue: 0.467),
.string: Color(red: 0.875, green: 0.027, blue: 0.0),
.type: Color(red: 0.706, green: 0.27, blue: 0.0),
.call: Color(red: 0.278, green: 0.415, blue: 0.593),
.number: Color(red: 0.161, green: 0.259, blue: 0.467),
.comment: Color(red: 0.765, green: 0.455, blue: 0.11),
.property: Color(red: 0.278, green: 0.415, blue: 0.593),
.dotAccess: Color(red: 0.278, green: 0.415, blue: 0.593),
.preprocessing: Color(red: 0.392, green: 0.391, blue: 0.52)
],
backgroundColor: Color(
red: 1,
Expand All @@ -159,15 +159,15 @@ public extension Theme {
blue: 0
),
tokenColors: [
.keyword : Color(red: 0.706, green: 0.0, blue: 0.384),
.string : Color(red: 0.729, green: 0.0, blue: 0.067),
.type : Color(red: 0.267, green: 0.537, blue: 0.576),
.call : Color(red: 0.267, green: 0.537, blue: 0.576),
.number : Color(red: 0.0, green: 0.043, blue: 1.0),
.comment : Color(red: 0.336, green: 0.376, blue: 0.42),
.property : Color(red: 0.267, green: 0.537, blue: 0.576),
.dotAccess : Color(red: 0.267, green: 0.537, blue: 0.576),
.preprocessing : Color(red: 0.431, green: 0.125, blue: 0.051)
.keyword: Color(red: 0.706, green: 0.0, blue: 0.384),
.string: Color(red: 0.729, green: 0.0, blue: 0.067),
.type: Color(red: 0.267, green: 0.537, blue: 0.576),
.call: Color(red: 0.267, green: 0.537, blue: 0.576),
.number: Color(red: 0.0, green: 0.043, blue: 1.0),
.comment: Color(red: 0.336, green: 0.376, blue: 0.42),
.property: Color(red: 0.267, green: 0.537, blue: 0.576),
.dotAccess: Color(red: 0.267, green: 0.537, blue: 0.576),
.preprocessing: Color(red: 0.431, green: 0.125, blue: 0.051)
],
backgroundColor: Color(
red: 1,
Expand Down
7 changes: 5 additions & 2 deletions Sources/Splash/Theming/Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ public struct Theme {
/// What color to use for the background
public var backgroundColor: Color
/// What color to use for the text's highlighted tokens
public var tokenColors: [TokenType : Color]
public var tokenColors: [TokenType: Color]

public init(font: Font, plainTextColor: Color, tokenColors: [TokenType : Color], backgroundColor: Color = Color(white: 0.12, alpha: 1)) {
public init(font: Font,
plainTextColor: Color,
tokenColors: [TokenType: Color],
backgroundColor: Color = Color(white: 0.12, alpha: 1)) {
self.font = font
self.plainTextColor = plainTextColor
self.tokenColors = tokenColors
Expand Down
2 changes: 1 addition & 1 deletion Sources/Splash/Tokenizing/Segment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public extension Segment {
/// All tokens that have been found so far (excluding the current one)
public var all: [String]
/// The number of times a given token has been found up until this point
public var counts: [String : Int]
public var counts: [String: Int]
/// The tokens that were previously found on the same line as the current one
public var onSameLine: [String]
/// The token that was previously found (may be on a different line)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Splash/Tokenizing/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private extension Tokenizer {
private let code: String
private let delimiters: CharacterSet
private var index: String.Index?
private var tokenCounts = [String : Int]()
private var tokenCounts = [String: Int]()
private var allTokens = [String]()
private var lineTokens = [String]()
private var segments: (current: Segment?, previous: Segment?)
Expand Down
8 changes: 4 additions & 4 deletions Tests/SplashTests/Tests/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,10 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
.plainText("}")
])
}

func testDeferDeclaration() {
let components = highlighter.highlight("func hello() { defer {} }")

XCTAssertEqual(components, [
.token("func", .keyword),
.whitespace(" "),
Expand All @@ -616,10 +616,10 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
.plainText("}")
])
}

func testFunctionDeclarationWithInOutParameter() {
let components = highlighter.highlight("func swapValues(value1: inout Int, value2: inout Int) { }")

XCTAssertEqual(components, [
.token("func", .keyword),
.whitespace(" "),
Expand Down
14 changes: 7 additions & 7 deletions Tests/SplashTests/Tests/StatementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ final class StatementTests: SyntaxHighlighterTestCase {
.plainText("}")
])
}

func testSwitchStatementWithFallthrough() {
let components = highlighter.highlight("""
switch variable {
Expand All @@ -143,7 +143,7 @@ final class StatementTests: SyntaxHighlighterTestCase {
callB()
}
""")

XCTAssertEqual(components, [
.token("switch", .keyword),
.whitespace(" "),
Expand Down Expand Up @@ -260,10 +260,10 @@ final class StatementTests: SyntaxHighlighterTestCase {
.plainText("}")
])
}

func testForStatementWithContinue() {
let components = highlighter.highlight("for value in Enum.allCases { continue }")

XCTAssertEqual(components, [
.token("for", .keyword),
.whitespace(" "),
Expand All @@ -277,12 +277,12 @@ final class StatementTests: SyntaxHighlighterTestCase {
.whitespace(" "),
.plainText("{"),
.whitespace(" "),
.token("continue",.keyword),
.token("continue", .keyword),
.whitespace(" "),
.plainText("}")
])
}

func testRepeatWhileStatement() {
let components = highlighter.highlight("""
var x = 5
Expand All @@ -291,7 +291,7 @@ final class StatementTests: SyntaxHighlighterTestCase {
x = x - 1
} while x > 1
""")

XCTAssertEqual(components, [
.token("var", .keyword),
.whitespace(" "),
Expand Down
1 change: 0 additions & 1 deletion Tests/SplashTests/Tests/TokenTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ extension TokenTypeTests {
]
}
}

0 comments on commit 8bc46cb

Please sign in to comment.