Skip to content

Commit

Permalink
Add SwiftFormat to Build Phases (#9)
Browse files Browse the repository at this point in the history
* Add SwiftFormat to Build Phases

* Update MarkupTokenizer.swift

Update comment
  • Loading branch information
gonzalezreal authored Dec 22, 2018
1 parent 69c2f57 commit d4a9862
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 50 deletions.
79 changes: 79 additions & 0 deletions Markup.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 52D6D9901BEFF229002C0205 /* Build configuration list for PBXNativeTarget "Markup-iOS" */;
buildPhases = (
991DA25A21CED6C100C5DBBC /* Run SwiftFormat */,
52D6D9771BEFF229002C0205 /* Sources */,
52D6D9781BEFF229002C0205 /* Frameworks */,
52D6D9791BEFF229002C0205 /* Headers */,
Expand Down Expand Up @@ -282,6 +283,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 52D6D9E71BEFFF6E002C0205 /* Build configuration list for PBXNativeTarget "Markup-watchOS" */;
buildPhases = (
991DA25C21CED7C800C5DBBC /* Run SwiftFormat */,
52D6D9DD1BEFFF6E002C0205 /* Sources */,
52D6D9DE1BEFFF6E002C0205 /* Frameworks */,
52D6D9DF1BEFFF6E002C0205 /* Headers */,
Expand All @@ -300,6 +302,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 52D6DA011BEFFFBE002C0205 /* Build configuration list for PBXNativeTarget "Markup-tvOS" */;
buildPhases = (
991DA25D21CED7FA00C5DBBC /* Run SwiftFormat */,
52D6D9EB1BEFFFBE002C0205 /* Sources */,
52D6D9EC1BEFFFBE002C0205 /* Frameworks */,
52D6D9ED1BEFFFBE002C0205 /* Headers */,
Expand All @@ -318,6 +321,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 52D6DA201BF000BD002C0205 /* Build configuration list for PBXNativeTarget "Markup-macOS" */;
buildPhases = (
991DA25B21CED78D00C5DBBC /* Run SwiftFormat */,
52D6DA0A1BF000BD002C0205 /* Sources */,
52D6DA0B1BF000BD002C0205 /* Frameworks */,
52D6DA0C1BF000BD002C0205 /* Headers */,
Expand Down Expand Up @@ -483,6 +487,81 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
991DA25A21CED6C100C5DBBC /* Run SwiftFormat */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = "Run SwiftFormat";
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Scripts/run-swiftformat.sh\"\n";
};
991DA25B21CED78D00C5DBBC /* Run SwiftFormat */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = "Run SwiftFormat";
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Scripts/run-swiftformat.sh\"\n";
};
991DA25C21CED7C800C5DBBC /* Run SwiftFormat */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = "Run SwiftFormat";
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Scripts/run-swiftformat.sh\"\n";
};
991DA25D21CED7FA00C5DBBC /* Run SwiftFormat */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = "Run SwiftFormat";
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Scripts/run-swiftformat.sh\"\n";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
52D6D9771BEFF229002C0205 /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
4 changes: 0 additions & 4 deletions MarkupExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
Expand All @@ -38,7 +37,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

6 changes: 3 additions & 3 deletions MarkupExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
// See LICENSE file for license
//

import UIKit
import Markup
import UIKit

class ViewController: UIViewController {
@IBOutlet weak var plainTextLabel: UILabel!
@IBOutlet weak var richTextLabel: UILabel!
@IBOutlet var plainTextLabel: UILabel!
@IBOutlet var richTextLabel: UILabel!

private let renderer = MarkupRenderer(baseFont: .systemFont(ofSize: 16))
private let sampleText = "~Annihilate?~ No. No violence. I won't stand for it. _Not now, not ever, do you understand me?!_ *I'm the Doctor, the Oncoming Storm - and you basically meant beat them in a football match, didn't you?* I'm nobody's taxi service; I'm not gonna be there to catch you every time you feel like _*jumping out of a spaceship*_."
Expand Down
4 changes: 4 additions & 0 deletions Scripts/run-swiftformat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
set -e

"${PROJECT_DIR}/Tools/swiftformat" --indent tabs --stripunusedargs closure-only --decimalgrouping 3,4 --commas inline --disable hoistPatternLet "${SRCROOT}/MarkupExample/" "${SRCROOT}/Sources/" "${SRCROOT}/Tests/"
1 change: 1 addition & 0 deletions Sources/MarkupRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ extension Array where Element: NSAttributedString {
return UIFont(descriptor: descriptor, size: 0)
}
}

#elseif os(OSX)
extension NSFont {
func boldFont() -> NSFont? {
Expand Down
2 changes: 1 addition & 1 deletion Sources/MarkupToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum MarkupToken {
}

extension MarkupToken: Equatable {
static func ==(lhs: MarkupToken, rhs: MarkupToken) -> Bool {
static func == (lhs: MarkupToken, rhs: MarkupToken) -> Bool {
switch (lhs, rhs) {
case let (.text(lvalue), .text(rvalue)):
return lvalue == rvalue
Expand Down
38 changes: 18 additions & 20 deletions Sources/MarkupTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@ private extension UnicodeScalar {
static let space: UnicodeScalar = " "
}

/**
Breaks a string into markup tokens.

How to use it:

var tokenizer = MarkupTokenizer(string: "_Hello *world*_")
while let token = tokenizer.nextToken() {
switch token {
case let .text(value):
print("text: \(value)"
case let .leftDelimiter(value):
print("left delimiter: \(value)"
case let .rightDelimiter(value):
print("right delimiter: \(value)"
}
}
*/
/// Breaks a string into markup tokens.
///
/// How to use it:
///
/// var tokenizer = MarkupTokenizer(string: "_Hello *world*_")
/// while let token = tokenizer.nextToken() {
/// switch token {
/// case let .text(value):
/// print("text: \(value)"
/// case let .leftDelimiter(value):
/// print("left delimiter: \(value)"
/// case let .rightDelimiter(value):
/// print("right delimiter: \(value)"
/// }
/// }
struct MarkupTokenizer {
/// The input string
private let input: String.UnicodeScalarView
Expand Down Expand Up @@ -117,7 +115,7 @@ struct MarkupTokenizer {
guard CharacterSet.whitespaceAndPunctuation.contains(p) &&
!CharacterSet.whitespacesAndNewlines.contains(n) &&
!leftDelimiters.contains(delimiter) else {
return nil
return nil
}

leftDelimiters.append(delimiter)
Expand All @@ -138,7 +136,7 @@ struct MarkupTokenizer {
guard !CharacterSet.whitespacesAndNewlines.contains(p) &&
CharacterSet.whitespaceAndPunctuation.contains(n) &&
leftDelimiters.contains(delimiter) else {
return nil
return nil
}

while !leftDelimiters.isEmpty {
Expand All @@ -159,7 +157,7 @@ struct MarkupTokenizer {
return nil
}

return .text(String(input[startIndex..<currentIndex]))
return .text(String(input[startIndex ..< currentIndex]))
}

private mutating func scanUntil(_ predicate: (UnicodeScalar) -> Bool) {
Expand Down
2 changes: 1 addition & 1 deletion Tests/MarkupTests/MarkupParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// See LICENSE file for license
//

import XCTest
import Markup
import XCTest

private func equalDump<T>(_ lhs: T, _ rhs: T) -> Bool {
var (ldump, rdump) = ("", "")
Expand Down
42 changes: 21 additions & 21 deletions Tests/MarkupTests/MarkupRendererTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,41 @@
// See LICENSE file for license
//

import XCTest
@testable import Markup
import XCTest

class MarkupRendererTests: XCTestCase {
let baseFont = Font.systemFont(ofSize: 16)
var sut: MarkupRenderer!

override func setUp() {
super.setUp()
sut = MarkupRenderer(baseFont: baseFont)
}

func testPlainText_render_rendersPlainText() {
// given
let input = "hello there"
let expected = NSAttributedString(string: "hello there",
attributes: [NSAttributedString.Key.font: baseFont])
attributes: [NSAttributedString.Key.font: baseFont])

// when
let result = sut.render(text: input)

// then
XCTAssertEqual(result, expected)
}

func testStrongText_render_rendersBoldText() {
// given
let input = "hello *there*"
let boldFont = baseFont.boldFont()!
let expected = [
NSAttributedString(string: "hello ",
attributes: [NSAttributedString.Key.font: baseFont]),
attributes: [NSAttributedString.Key.font: baseFont]),
NSAttributedString(string: "there",
attributes: [NSAttributedString.Key.font: boldFont])
].joined()
attributes: [NSAttributedString.Key.font: boldFont])
].joined()

// when
let result = sut.render(text: input)
Expand All @@ -53,10 +53,10 @@ class MarkupRendererTests: XCTestCase {
let italicFont = baseFont.italicFont()!
let expected = [
NSAttributedString(string: "hello ",
attributes: [NSAttributedString.Key.font: baseFont]),
attributes: [NSAttributedString.Key.font: baseFont]),
NSAttributedString(string: "there",
attributes: [NSAttributedString.Key.font: italicFont])
].joined()
attributes: [NSAttributedString.Key.font: italicFont])
].joined()

// when
let result = sut.render(text: input)
Expand All @@ -75,10 +75,10 @@ class MarkupRendererTests: XCTestCase {
]
let expected = [
NSAttributedString(string: "hello ",
attributes: [NSAttributedString.Key.font: baseFont]),
attributes: [NSAttributedString.Key.font: baseFont]),
NSAttributedString(string: "there",
attributes: strikethroughAttributes)
].joined()
attributes: strikethroughAttributes)
].joined()

// when
let result = sut.render(text: input)
Expand All @@ -93,14 +93,14 @@ class MarkupRendererTests: XCTestCase {
let boldItalicFont = baseFont.boldFont()!.italicFont()!
let expected = [
NSAttributedString(string: "hello ",
attributes: [NSAttributedString.Key.font: baseFont]),
attributes: [NSAttributedString.Key.font: baseFont]),
NSAttributedString(string: "there",
attributes: [NSAttributedString.Key.font: boldItalicFont]),
attributes: [NSAttributedString.Key.font: boldItalicFont]),
NSAttributedString(string: " ",
attributes: [NSAttributedString.Key.font: baseFont]),
attributes: [NSAttributedString.Key.font: baseFont]),
NSAttributedString(string: "there",
attributes: [NSAttributedString.Key.font: boldItalicFont])
].joined()
attributes: [NSAttributedString.Key.font: boldItalicFont])
].joined()

// when
let result = sut.render(text: input)
Expand Down
Binary file added Tools/swiftformat
Binary file not shown.

0 comments on commit d4a9862

Please sign in to comment.