mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
Move structured text into its own file
This commit is contained in:
@@ -11,42 +11,6 @@ import Commandant
|
||||
import LlamaKit
|
||||
import SwiftLintFramework
|
||||
|
||||
typealias StructuredInlineText = String
|
||||
|
||||
extension Array {
|
||||
func dropWhile(x: Element -> Bool) -> [Element] {
|
||||
var count = 0
|
||||
while x(self[count]) { count++ }
|
||||
var copy = self
|
||||
copy.removeRange(0..<count)
|
||||
return copy
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension String {
|
||||
var chomped: String {
|
||||
return String(reverse(self).dropWhile { $0 == "\n" }.reverse())
|
||||
}
|
||||
}
|
||||
|
||||
enum StructuredText {
|
||||
case Header(level: Int, text: StructuredInlineText)
|
||||
case Paragraph(StructuredInlineText)
|
||||
case List(items: [StructuredText])
|
||||
case Joined([StructuredText])
|
||||
|
||||
var markdown: String {
|
||||
switch self {
|
||||
|
||||
case let .Header(level, t): return join("", Array(count: level, repeatedValue: "#")) + " " + t
|
||||
case .Paragraph(let t): return t.chomped
|
||||
case .List(let items): return "\n".join(items.map { "* " + $0.markdown })
|
||||
case .Joined(let items): return "\n\n".join(items.map { $0.markdown } )
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func describeExample(example: RuleExample) -> StructuredText {
|
||||
return .Joined([
|
||||
.Header(level: 1, text: example.ruleName),
|
||||
@@ -68,7 +32,7 @@ struct ExplainCommand: CommandType {
|
||||
switch mode {
|
||||
case let .Arguments:
|
||||
for example: RuleExample in Linter.explainableRules {
|
||||
println(describeExample(example).markdown)
|
||||
println(describeExample(example).ansi)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// StructuredText.swift
|
||||
// SwiftLint
|
||||
//
|
||||
// Created by Chris Eidhof on 20/05/15.
|
||||
// Copyright (c) 2015 Realm. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
typealias StructuredInlineText = String
|
||||
|
||||
private extension Array {
|
||||
func dropWhile(x: Element -> Bool) -> [Element] {
|
||||
var count = 0
|
||||
while x(self[count]) { count++ }
|
||||
var copy = self
|
||||
copy.removeRange(0..<count)
|
||||
return copy
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension String {
|
||||
var chomped: String {
|
||||
return String(reverse(self).dropWhile { $0 == "\n" }.reverse())
|
||||
}
|
||||
}
|
||||
|
||||
enum AnsiCode: Int {
|
||||
case Reset = 0
|
||||
case Bold = 1
|
||||
case RedForeground = 31
|
||||
case GreenForeground = 32
|
||||
|
||||
|
||||
|
||||
var string: String {
|
||||
return "\u{001B}[0;\(rawValue)m"
|
||||
}
|
||||
|
||||
func wrap(text: String) -> String {
|
||||
return self.string + text + AnsiCode.Reset.string
|
||||
}
|
||||
|
||||
static func red(string: String) -> String {
|
||||
return AnsiCode.RedForeground.wrap(string)
|
||||
}
|
||||
|
||||
static func green(string: String) -> String {
|
||||
return AnsiCode.GreenForeground.wrap(string)
|
||||
}
|
||||
}
|
||||
|
||||
enum StructuredText {
|
||||
case Header(level: Int, text: StructuredInlineText)
|
||||
case Paragraph(StructuredInlineText)
|
||||
case List(items: [StructuredText])
|
||||
case Joined([StructuredText])
|
||||
|
||||
var markdown: String {
|
||||
switch self {
|
||||
case let .Header(level, t): return join("", Array(count: level, repeatedValue: "#")) + " " + t
|
||||
case .Paragraph(let t): return t.chomped
|
||||
case .List(let items): return "\n".join(items.map { "* " + $0.markdown })
|
||||
case .Joined(let items): return "\n\n".join(items.map { $0.markdown } )
|
||||
}
|
||||
}
|
||||
|
||||
var ansi: String {
|
||||
switch self {
|
||||
case .Header(1, let t): return t.uppercaseString
|
||||
case let .Header(other, t): return t
|
||||
case .Paragraph(let t): return t.chomped
|
||||
case .List(let items): return "\n".join(items.map { "* " + $0.ansi })
|
||||
case .Joined(let items): return "\n\n".join(items.map { $0.ansi } )
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
83894F221B0C928A006214E1 /* ExplainCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83894F211B0C928A006214E1 /* ExplainCommand.swift */; };
|
||||
83894F241B0CAD41006214E1 /* StructuredText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83894F231B0CAD41006214E1 /* StructuredText.swift */; };
|
||||
D0AAAB5019FB0960007B24B3 /* SwiftLintFramework.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SwiftLintFramework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
|
||||
D0D1217819E87B05005E4BAA /* SwiftLintFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SwiftLintFramework.framework */; };
|
||||
D0E7B65319E9C6AD00EDBA4D /* SwiftLintFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SwiftLintFramework.framework */; };
|
||||
@@ -102,6 +103,7 @@
|
||||
5499CA961A2394B700783309 /* Components.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Components.plist; sourceTree = "<group>"; };
|
||||
5499CA971A2394B700783309 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
83894F211B0C928A006214E1 /* ExplainCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExplainCommand.swift; sourceTree = "<group>"; };
|
||||
83894F231B0CAD41006214E1 /* StructuredText.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StructuredText.swift; sourceTree = "<group>"; };
|
||||
D0D1211B19E87861005E4BAA /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; usesTabs = 0; };
|
||||
D0D1212419E878CC005E4BAA /* Common.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Common.xcconfig; sourceTree = "<group>"; };
|
||||
D0D1212619E878CC005E4BAA /* Debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Debug.xcconfig; sourceTree = "<group>"; };
|
||||
@@ -235,6 +237,7 @@
|
||||
E83A0B341A5D382B0041A60A /* Version.swift */,
|
||||
5499CA981A2394BD00783309 /* Supporting Files */,
|
||||
83894F211B0C928A006214E1 /* ExplainCommand.swift */,
|
||||
83894F231B0CAD41006214E1 /* StructuredText.swift */,
|
||||
);
|
||||
name = swiftlint;
|
||||
path = Source/swiftlint;
|
||||
@@ -582,6 +585,7 @@
|
||||
E83A0B351A5D382B0041A60A /* Version.swift in Sources */,
|
||||
E861519B1B0573B900C54AC0 /* Lint.swift in Sources */,
|
||||
D0E7B65619E9C76900EDBA4D /* main.swift in Sources */,
|
||||
83894F241B0CAD41006214E1 /* StructuredText.swift in Sources */,
|
||||
83894F221B0C928A006214E1 /* ExplainCommand.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
||||
Reference in New Issue
Block a user