Skip to content

Commit

Permalink
Update to Swift 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pietbrauer committed Sep 20, 2016
1 parent 03d8707 commit 9cfdae4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
17 changes: 16 additions & 1 deletion JSDiff.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0730;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = "nerdish by nature";
TargetAttributes = {
2380771E1CB94D9600415155 = {
Expand All @@ -259,9 +259,11 @@
};
23A4A1531CB93D6500A3DEE3 = {
CreatedOnToolsVersion = 7.3;
LastSwiftMigration = 0800;
};
23A4A15D1CB93D6500A3DEE3 = {
CreatedOnToolsVersion = 7.3;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -407,6 +409,7 @@
238077311CB94D9700415155 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=appletvos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -423,6 +426,7 @@
238077321CB94D9700415155 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=appletvos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand Down Expand Up @@ -474,8 +478,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -506,6 +512,7 @@
PRODUCT_NAME = JSDiff;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand All @@ -526,8 +533,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -550,6 +559,8 @@
PRODUCT_BUNDLE_IDENTIFIER = com.nerdishbynature.JSDiff;
PRODUCT_NAME = JSDiff;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -561,6 +572,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -576,6 +588,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -595,6 +608,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.nerdishbynature.JSDiffTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -606,6 +620,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.nerdishbynature.JSDiffTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion JSDiff.xcodeproj/xcshareddata/xcschemes/JSDiff.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
22 changes: 11 additions & 11 deletions JSDiff/JSDiff.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import UIKit
import JavaScriptCore

@objc public class JSLineDiff: NSObject {
public let newLine: NSAttributedString
public let oldLine: NSAttributedString
@objc open class JSLineDiff: NSObject {
open let newLine: NSAttributedString
open let oldLine: NSAttributedString

init(oldLine: NSAttributedString, newLine: NSAttributedString) {
self.oldLine = oldLine
self.newLine = newLine
}
}

@objc public class JSDiff: NSObject {
@objc open class JSDiff: NSObject {
let deletedColor: UIColor
let deletedWordColor: UIColor
let addedColor: UIColor
let addedWordColor: UIColor
private lazy var context: JSContext = {
fileprivate lazy var context: JSContext = {
let context = JSContext()
if let path = NSBundle(forClass: self.dynamicType).pathForResource("bundle", ofType: "js"), source = try? String(contentsOfFile: path) {
context.evaluateScript(source)
if let path = Bundle(for: type(of: self)).path(forResource: "bundle", ofType: "js"), let source = try? String(contentsOfFile: path) {
_ = context?.evaluateScript(source)
}
return context
return context!
}()

public init(deletedColor: UIColor, deletedWordColor: UIColor, addedColor: UIColor, addedWordColor: UIColor) {
Expand All @@ -31,12 +31,12 @@ import JavaScriptCore
self.addedWordColor = addedWordColor
}

private func results(oldLine: String, newLine: String) -> [[String: AnyObject]] {
fileprivate func results(_ oldLine: String, newLine: String) -> [[String: AnyObject]] {
let diff = context.objectForKeyedSubscript("swift_diffStrings")
return diff.callWithArguments([oldLine, newLine]).toArray() as? [[String: AnyObject]] ?? []
return diff!.call(withArguments: [oldLine, newLine]).toArray() as? [[String: AnyObject]] ?? []
}

@objc public func diffWords(oldLine: String, newLine: String) -> JSLineDiff {
@objc open func diffWords(_ oldLine: String, newLine: String) -> JSLineDiff {
var oldLineIndex = 0
var newLineIndex = 0
let attributedOldLine = NSMutableAttributedString(string: oldLine)
Expand Down
10 changes: 5 additions & 5 deletions JSDiffTests/JSDiffTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class JSDiffTests: XCTestCase {
let jsDiff = JSDiff(deletedColor: UIColor.deletedColor(), deletedWordColor: UIColor.strongDeletedColor(), addedColor: UIColor.addedColor(), addedWordColor: UIColor.strongAddedColor())

let expectedOldLine = NSMutableAttributedString(string: oldLine, attributes: [NSBackgroundColorAttributeName: UIColor.deletedColor()])
expectedOldLine.addAttributes([NSBackgroundColorAttributeName: UIColor.strongDeletedColor()], range: (oldLine as NSString).rangeOfString("-you-always"))
expectedOldLine.addAttributes([NSBackgroundColorAttributeName: UIColor.strongDeletedColor()], range: (oldLine as NSString).rangeOfString("id963577401"))
expectedOldLine.addAttributes([NSBackgroundColorAttributeName: UIColor.strongDeletedColor()], range: (oldLine as NSString).range(of: "-you-always"))
expectedOldLine.addAttributes([NSBackgroundColorAttributeName: UIColor.strongDeletedColor()], range: (oldLine as NSString).range(of: "id963577401"))

let expectedNewLine = NSMutableAttributedString(string: newLine, attributes: [NSBackgroundColorAttributeName: UIColor.addedColor()])
expectedNewLine.addAttributes([NSBackgroundColorAttributeName: UIColor.strongAddedColor()], range: (newLine as NSString).rangeOfString("enterprise-"))
expectedNewLine.addAttributes([NSBackgroundColorAttributeName: UIColor.strongAddedColor()], range: (newLine as NSString).rangeOfString("id1079716387"))
expectedNewLine.addAttributes([NSBackgroundColorAttributeName: UIColor.strongAddedColor()], range: (newLine as NSString).rangeOfString("ls=1&"))
expectedNewLine.addAttributes([NSBackgroundColorAttributeName: UIColor.strongAddedColor()], range: (newLine as NSString).range(of: "enterprise-"))
expectedNewLine.addAttributes([NSBackgroundColorAttributeName: UIColor.strongAddedColor()], range: (newLine as NSString).range(of: "id1079716387"))
expectedNewLine.addAttributes([NSBackgroundColorAttributeName: UIColor.strongAddedColor()], range: (newLine as NSString).range(of: "ls=1&"))

let result = jsDiff.diffWords(oldLine, newLine: newLine)
XCTAssertEqual(result.oldLine, expectedOldLine)
Expand Down

0 comments on commit 9cfdae4

Please sign in to comment.