From 5e7c8500f9c40b7a8b08e8340adb2fc334c09fc6 Mon Sep 17 00:00:00 2001 From: iosdevzone Date: Sat, 31 Mar 2018 01:51:53 -0700 Subject: [PATCH 1/3] Update parameter lists. (i.e. Insert _) --- SyntaxKit/ResultSet.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SyntaxKit/ResultSet.swift b/SyntaxKit/ResultSet.swift index 839d6d0..99fcd44 100644 --- a/SyntaxKit/ResultSet.swift +++ b/SyntaxKit/ResultSet.swift @@ -26,7 +26,7 @@ struct ResultSet { // MARK: - Adding - mutating func addResult(result: Result) { + mutating func addResult(_ result: Result) { _results.append(result) guard let range = range else { @@ -37,7 +37,7 @@ struct ResultSet { self.range = NSUnionRange(range, result.range) } - mutating func addResults(resultSet: ResultSet) { + mutating func addResults(_ resultSet: ResultSet) { for result in resultSet.results { addResult(result) } From f386679d69eb2b87a5ed4c2066d84d30b03bd134 Mon Sep 17 00:00:00 2001 From: iosdevzone Date: Sat, 31 Mar 2018 16:33:14 -0700 Subject: [PATCH 2/3] Update to Swift 4.1. All tests passing. --- Cartfile.resolved | 2 +- SyntaxKit.xcodeproj/project.pbxproj | 25 ++++++--- .../xcschemes/SyntaxKit-tvOS.xcscheme | 10 ++-- SyntaxKit/AttributedParser.swift | 53 ++++++++++--------- SyntaxKit/Capture.swift | 2 +- SyntaxKit/CaptureCollection.swift | 10 ++-- SyntaxKit/Language.swift | 30 +++++------ SyntaxKit/Parser.swift | 37 ++++++------- SyntaxKit/Pattern.swift | 10 ++-- SyntaxKit/Tests/AttributedParserTests.swift | 10 ++-- SyntaxKit/Tests/LanguageTests.swift | 16 +++--- SyntaxKit/Tests/ParserTests.swift | 10 ++-- SyntaxKit/Tests/TestHelper.swift | 46 ++++++++++------ SyntaxKit/Tests/ThemeTests.swift | 11 ++-- SyntaxKit/Theme.swift | 30 ++++++----- 15 files changed, 168 insertions(+), 134 deletions(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index f9795d5..61fc6f5 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1 +1 @@ -github "soffes/X" "v0.3.1" +github "soffes/X" "v0.4.0" diff --git a/SyntaxKit.xcodeproj/project.pbxproj b/SyntaxKit.xcodeproj/project.pbxproj index eb2ab12..233a89e 100644 --- a/SyntaxKit.xcodeproj/project.pbxproj +++ b/SyntaxKit.xcodeproj/project.pbxproj @@ -84,7 +84,6 @@ 2198CEDA1B36D5DE00BD463F /* ResultSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 211989941B2EC38B00F0D786 /* ResultSet.swift */; }; 2198CEDB1B36D5DE00BD463F /* Theme.swift in Sources */ = {isa = PBXBuildFile; fileRef = 211989961B2EC38B00F0D786 /* Theme.swift */; }; 2198CEDC1B36D5E100BD463F /* SyntaxKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 211989951B2EC38B00F0D786 /* SyntaxKit.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 21F28DDB1B7AC7F8009DD1E9 /* X.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 21F28DDA1B7AC7F8009DD1E9 /* X.framework */; }; 21F28DDD1B7AC802009DD1E9 /* X.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 21F28DDC1B7AC802009DD1E9 /* X.framework */; }; 21F28DDF1B7AC80B009DD1E9 /* X.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 21F28DDE1B7AC80B009DD1E9 /* X.framework */; }; 92EC79D41CC9060600E058ED /* JavaScript.tmLanguage in Resources */ = {isa = PBXBuildFile; fileRef = 92EC79D31CC9060600E058ED /* JavaScript.tmLanguage */; }; @@ -212,7 +211,6 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 21F28DDB1B7AC7F8009DD1E9 /* X.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -566,9 +564,11 @@ }; 211989A61B2EC3B600F0D786 = { CreatedOnToolsVersion = 7.0; + DevelopmentTeam = GEA92R6RK9; }; 211989AF1B2EC3B600F0D786 = { CreatedOnToolsVersion = 7.0; + DevelopmentTeam = GEA92R6RK9; }; 2122A6DD1B22B9320006409B = { CreatedOnToolsVersion = 6.3.2; @@ -883,8 +883,10 @@ 211989B91B2EC3B600F0D786 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; DEFINES_MODULE = YES; + DEVELOPMENT_TEAM = GEA92R6RK9; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; @@ -894,7 +896,7 @@ ); INFOPLIST_FILE = "$(SRCROOT)/SyntaxKit/Resources/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.samsoffes.syntaxkit; PRODUCT_NAME = SyntaxKit; @@ -909,6 +911,7 @@ buildSettings = { "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; DEFINES_MODULE = YES; + DEVELOPMENT_TEAM = GEA92R6RK9; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; @@ -918,7 +921,7 @@ ); INFOPLIST_FILE = "$(SRCROOT)/SyntaxKit/Resources/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.samsoffes.syntaxkit; PRODUCT_NAME = SyntaxKit; @@ -934,12 +937,13 @@ buildSettings = { APPLICATION_EXTENSION_API_ONLY = NO; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + DEVELOPMENT_TEAM = GEA92R6RK9; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Carthage/Build/iOS", ); INFOPLIST_FILE = SyntaxKit/Tests/Resources/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.samsoffes.syntaxkit-ios.tests"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -952,12 +956,13 @@ buildSettings = { APPLICATION_EXTENSION_API_ONLY = NO; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + DEVELOPMENT_TEAM = GEA92R6RK9; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Carthage/Build/iOS", ); INFOPLIST_FILE = SyntaxKit/Tests/Resources/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.samsoffes.syntaxkit-ios.tests"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -1004,12 +1009,13 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; MACOSX_DEPLOYMENT_TARGET = 10.9; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -1046,10 +1052,11 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; MACOSX_DEPLOYMENT_TARGET = 10.9; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; + SWIFT_VERSION = 4.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -1201,6 +1208,7 @@ 211826ED1D257A71003F2BF2 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 211826EE1D257A71003F2BF2 /* Build configuration list for PBXNativeTarget "SyntaxKitTests-tvOS" */ = { isa = XCConfigurationList; @@ -1209,6 +1217,7 @@ 211826F01D257A71003F2BF2 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 211989B81B2EC3B600F0D786 /* Build configuration list for PBXNativeTarget "SyntaxKit-iOS" */ = { isa = XCConfigurationList; diff --git a/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-tvOS.xcscheme b/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-tvOS.xcscheme index 566e79a..765a3a1 100644 --- a/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-tvOS.xcscheme +++ b/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-tvOS.xcscheme @@ -15,7 +15,7 @@ @@ -33,7 +33,7 @@ @@ -43,7 +43,7 @@ @@ -65,7 +65,7 @@ @@ -83,7 +83,7 @@ diff --git a/SyntaxKit/AttributedParser.swift b/SyntaxKit/AttributedParser.swift index 0785482..edd88c9 100644 --- a/SyntaxKit/AttributedParser.swift +++ b/SyntaxKit/AttributedParser.swift @@ -6,68 +6,69 @@ // Copyright © 2014-2015 Sam Soffes. All rights reserved. // -public class AttributedParser: Parser { +public class AttributedParser: Parser { + // MARK: - Types - - public typealias AttributedCallback = (scope: String, range: NSRange, attributes: Attributes?) -> Void - - + + public typealias AttributedCallback = (/* scope: */ String, /* range: */ NSRange, /* attributes: */ Attributes?) -> Void + + // MARK: - Properties - + public let theme: Theme - - + + // MARK: - Initializers - + public required init(language: Language, theme: Theme) { self.theme = theme super.init(language: language) } - - + + // MARK: - Parsing - - public func parse(string: String, match callback: AttributedCallback) { + + public func parse(_ string: String, match callback: AttributedCallback) { parse(string) { scope, range in - callback(scope: scope, range: range, attributes: self.attributesForScope(scope)) + callback(scope, range, self.attributesForScope(scope)) } } - + public func attributedStringForString(string: String, baseAttributes: Attributes? = nil) -> NSAttributedString { let output = NSMutableAttributedString(string: string, attributes: baseAttributes) parse(string) { _, range, attributes in - if let attributes = attributes { + if let attributes = attributes{ output.addAttributes(attributes, range: range) } } return output } - - + + // MARK: - Private - - private func attributesForScope(scope: String) -> Attributes? { - let components = scope.componentsSeparatedByString(".") as NSArray + + private func attributesForScope(_ scope: String) -> Attributes? { + let components = scope.components(separatedBy: ".")// as NSArray let count = components.count if count == 0 { return nil } - + var attributes = Attributes() - for i in 0.. Void + public typealias Callback = (/* scope: */ String, /* range: */ NSRange) -> Void // MARK: - Properties @@ -29,16 +29,16 @@ public class Parser { // MARK: - Parsing - public func parse(string: String, match callback: Callback) { + public func parse(_ string: String, match callback: Callback) { // Loop through paragraphs - let s: NSString = string + let s = string as NSString let length = s.length var paragraphEnd = 0 while paragraphEnd < length { var paragraphStart = 0 var contentsEnd = 0 - s.getParagraphStart(¶graphStart, end: ¶graphEnd, contentsEnd: &contentsEnd, forRange: NSMakeRange(paragraphEnd, 0)) + s.getParagraphStart(¶graphStart, end: ¶graphEnd, contentsEnd: &contentsEnd, for: NSMakeRange(paragraphEnd, 0)) let paragraphRange = NSMakeRange(paragraphStart, contentsEnd - paragraphStart) let limit = NSMaxRange(paragraphRange) @@ -46,7 +46,7 @@ public class Parser { // Loop through the line until we reach the end while range.length > 0 && range.location < limit { - let location = parse(string, inRange: range, callback: callback) + let location = parse(string, inRange: range, callback: callback) range.location = Int(location) range.length = max(0, range.length - paragraphRange.location - range.location) } @@ -57,7 +57,8 @@ public class Parser { // MARK: - Private /// Returns new location - private func parse(string: String, inRange bounds: NSRange, callback: Callback) -> UInt { + + private func parse(_ string: String, inRange bounds: NSRange, callback: Callback) -> UInt { for pattern in language.patterns { // Single pattern if let match = pattern.match { @@ -69,15 +70,15 @@ public class Parser { } // Begin & end - if let begin = pattern.begin, end = pattern.end { + if let begin = pattern.begin, let end = pattern.end { guard let beginResults = parse(string, inRange: bounds, expression: begin, captures: pattern.beginCaptures), - beginRange = beginResults.range else { continue } + let beginRange = beginResults.range else { continue } let location = NSMaxRange(beginRange) let endBounds = NSMakeRange(location, NSMaxRange(bounds) - location) guard let endResults = parse(string, inRange: endBounds, expression: end, captures: pattern.endCaptures), - endRange = endResults.range else { /* TODO: Rewind? */ continue } + let endRange = endResults.range else { /* TODO: Rewind? */ continue } // Add whole scope before start and end var results = ResultSet() @@ -96,11 +97,11 @@ public class Parser { } /// Parse an expression with captures - private func parse(string: String, inRange bounds: NSRange, scope: String? = nil, expression expressionString: String, captures: CaptureCollection?) -> ResultSet? { + private func parse(_ string: String, inRange bounds: NSRange, scope: String? = nil, expression expressionString: String, captures: CaptureCollection?) -> ResultSet? { let matches: [NSTextCheckingResult] do { - let expression = try NSRegularExpression(pattern: expressionString, options: [.CaseInsensitive]) - matches = expression.matchesInString(string, options: [], range: bounds) + let expression = try NSRegularExpression(pattern: expressionString, options: [.caseInsensitive]) + matches = expression.matches(in: string, options: [], range: bounds) } catch { return nil } @@ -108,19 +109,19 @@ public class Parser { guard let result = matches.first else { return nil } var resultSet = ResultSet() - if let scope = scope where result.range.location != NSNotFound { - resultSet.addResult(Result(scope: scope, range: result.range)) + if let scope = scope, result.range.location != NSNotFound { + resultSet.addResult(Result(scope: scope, range: result.range)) } if let captures = captures { for index in captures.captureIndexes { - let range = result.rangeAtIndex(Int(index)) + let range = result.range(at: Int(index)) if range.location == NSNotFound { continue } if let scope = captures[index]?.name { - resultSet.addResult(Result(scope: scope, range: range)) + resultSet.addResult(Result(scope: scope, range: range)) } } } @@ -132,10 +133,10 @@ public class Parser { return nil } - private func applyResults(resultSet: ResultSet, callback: Callback) -> UInt { + private func applyResults(_ resultSet: ResultSet, callback: Callback) -> UInt { var i = 0 for result in resultSet.results { - callback(scope: result.scope, range: result.range) + callback(result.scope, result.range) i = max(NSMaxRange(result.range), i) } return UInt(i) diff --git a/SyntaxKit/Pattern.swift b/SyntaxKit/Pattern.swift index fa2453f..fe4c8f0 100644 --- a/SyntaxKit/Pattern.swift +++ b/SyntaxKit/Pattern.swift @@ -32,33 +32,33 @@ final class Pattern { // MARK: - Initializers - init?(dictionary: [NSObject: AnyObject], parent: Pattern? = nil) { + init?(dictionary: CaptureDictionary, parent: Pattern? = nil) { self.parent = parent self.name = dictionary["name"] as? String self.match = dictionary["match"] as? String self.begin = dictionary["begin"] as? String self.end = dictionary["end"] as? String - if let dictionary = dictionary["beginCaptures"] as? [NSObject: AnyObject] { + if let dictionary = dictionary["beginCaptures"] as? CaptureDictionary { self.beginCaptures = CaptureCollection(dictionary: dictionary) } else { self.beginCaptures = nil } - if let dictionary = dictionary["captures"] as? [NSObject: AnyObject] { + if let dictionary = dictionary["captures"] as? CaptureDictionary { self.captures = CaptureCollection(dictionary: dictionary) } else { self.captures = nil } - if let dictionary = dictionary["endCaptures"] as? [NSObject: AnyObject] { + if let dictionary = dictionary["endCaptures"] as? CaptureDictionary { self.endCaptures = CaptureCollection(dictionary: dictionary) } else { self.endCaptures = nil } var patterns = [Pattern]() - if let array = dictionary["patterns"] as? [[NSObject: AnyObject]] { + if let array = dictionary["patterns"] as? [[String: AnyObject]] { for value in array { if let pattern = Pattern(dictionary: value, parent: parent) { patterns.append(pattern) diff --git a/SyntaxKit/Tests/AttributedParserTests.swift b/SyntaxKit/Tests/AttributedParserTests.swift index 605d2d2..64fb625 100644 --- a/SyntaxKit/Tests/AttributedParserTests.swift +++ b/SyntaxKit/Tests/AttributedParserTests.swift @@ -19,11 +19,11 @@ class AttributedParserTests: XCTestCase { // MARK: - Tests func testParsing() { - let string = parser.attributedStringForString("title: Hello World\ncount: 42\n") + let string = parser.attributedStringForString(string: "title: Hello World\ncount: 42\n") - XCTAssertEqual(["color": "blue"] as NSDictionary, string.attributesAtIndex(0, effectiveRange: nil) as NSDictionary) - XCTAssertEqual(["color": "red"] as NSDictionary, string.attributesAtIndex(7, effectiveRange: nil) as NSDictionary) - XCTAssertEqual(["color": "blue"] as NSDictionary, string.attributesAtIndex(19, effectiveRange: nil) as NSDictionary) - XCTAssertEqual(["color": "purple"] as NSDictionary, string.attributesAtIndex(25, effectiveRange: nil) as NSDictionary) + XCTAssertEqual(["color": "blue"] as NSDictionary, string.attributes(at: 0, effectiveRange: nil) as NSDictionary) + XCTAssertEqual(["color": "red"] as NSDictionary, string.attributes(at: 7, effectiveRange: nil) as NSDictionary) + XCTAssertEqual(["color": "blue"] as NSDictionary, string.attributes(at: 19, effectiveRange: nil) as NSDictionary) + XCTAssertEqual(["color": "purple"] as NSDictionary, string.attributes(at: 25, effectiveRange: nil) as NSDictionary) } } diff --git a/SyntaxKit/Tests/LanguageTests.swift b/SyntaxKit/Tests/LanguageTests.swift index 4eb0662..f8cab28 100644 --- a/SyntaxKit/Tests/LanguageTests.swift +++ b/SyntaxKit/Tests/LanguageTests.swift @@ -13,21 +13,21 @@ class LanguageTests: XCTestCase { // MARK: - Properties - let yaml = language("YAML") + let yaml = language(name: "YAML") // MARK: - Tests func testLoading() { - XCTAssertEqual("B0C44228-4F1F-11DA-AFF2-000A95AF0064", yaml.UUID) - XCTAssertEqual("YAML", yaml.name) - XCTAssertEqual("source.yaml", yaml.scopeName) + XCTAssertEqual("B0C44228-4F1F-11DA-AFF2-000A95AF0064", yaml!.UUID) + XCTAssertEqual("YAML", yaml!.name) + XCTAssertEqual("source.yaml", yaml!.scopeName) - XCTAssertEqual("meta.embedded.line.ruby", yaml.patterns[0].name!) - XCTAssertEqual("string.unquoted.block.yaml", yaml.patterns[1].name!) - XCTAssertEqual("constant.numeric.yaml", yaml.patterns[2].name!) + XCTAssertEqual("meta.embedded.line.ruby", yaml!.patterns[0].name!) + XCTAssertEqual("string.unquoted.block.yaml", yaml!.patterns[1].name!) + XCTAssertEqual("constant.numeric.yaml", yaml!.patterns[2].name!) - let pattern = yaml.patterns[3] + let pattern = yaml!.patterns[3] XCTAssertEqual("string.unquoted.yaml", pattern.name!) XCTAssertEqual("punctuation.definition.entry.yaml", pattern.captures![1]!.name) } diff --git a/SyntaxKit/Tests/ParserTests.swift b/SyntaxKit/Tests/ParserTests.swift index 49c0483..d693acb 100644 --- a/SyntaxKit/Tests/ParserTests.swift +++ b/SyntaxKit/Tests/ParserTests.swift @@ -13,7 +13,7 @@ class ParserTests: XCTestCase { // MARK: - Properties - let parser = Parser(language: language("YAML")) + let parser = Parser(language: language(name: "YAML")) // MARK: - Tests @@ -23,7 +23,7 @@ class ParserTests: XCTestCase { var punctuationBegin: NSRange? var punctuationEnd: NSRange? - parser.parse("title: \"Hello World\"\n") { scope, range in + parser.parse("title: \"Hello World\"\n") { scope, range in if stringQuoted == nil && scope.hasPrefix("string.quoted.double") { stringQuoted = range } @@ -45,7 +45,7 @@ class ParserTests: XCTestCase { func testParsingBeginEndCrap() { var stringQuoted: NSRange? - parser.parse("title: Hello World\ncomments: 24\nposts: \"12\"zz\n") { scope, range in + parser.parse("title: Hello World\ncomments: 24\nposts: \"12\"zz\n") { scope, range in if stringQuoted == nil && scope.hasPrefix("string.quoted.double") { stringQuoted = range } @@ -57,12 +57,12 @@ class ParserTests: XCTestCase { func testRuby() { let parser = Parser(language: language("Ruby")) let input = fixture("test.rb", "txt") - parser.parse(input, match: { _, _ in return }) + parser.parse(input!, match: { _, _ in return }) } func testJavaScript() { let parser = Parser(language: language("JavaScript")) let input = fixture("test.js", "txt") - parser.parse(input, match: { _, _ in return }) + parser.parse(input!, match: { _, _ in return }) } } diff --git a/SyntaxKit/Tests/TestHelper.swift b/SyntaxKit/Tests/TestHelper.swift index cf93e8e..c6e8937 100644 --- a/SyntaxKit/Tests/TestHelper.swift +++ b/SyntaxKit/Tests/TestHelper.swift @@ -11,27 +11,37 @@ import XCTest import X @testable import SyntaxKit +func fixture(_ name: String, _ type: String) -> String! { + let path = Bundle(for: LanguageTests.self).path(forResource: name, ofType: type)! + return try! String(contentsOfFile: path) +} + func fixture(name: String, _ type: String) -> String! { - let path = NSBundle(forClass: LanguageTests.self).pathForResource(name, ofType: type)! + let path = Bundle(for: LanguageTests.self).path(forResource: name, ofType: type)! return try! String(contentsOfFile: path) } +/// Loads a tmLanguage file from the [Class] Bundle. +func language(_ name: String) -> Language { + return language(name:name) +} + func language(name: String) -> Language! { - let path = NSBundle(forClass: LanguageTests.self).pathForResource(name, ofType: "tmLanguage")! - let plist = NSDictionary(contentsOfFile: path)! as [NSObject: AnyObject] + let path = Bundle(for: LanguageTests.self).path(forResource: name, ofType: "tmLanguage")! + guard let plist = NSDictionary(contentsOfFile: path)! as? [String: AnyObject] else { fatalError() } return Language(dictionary: plist)! } func theme(name: String) -> Theme! { - let path = NSBundle(forClass: LanguageTests.self).pathForResource(name, ofType: "tmTheme")! + let path = Bundle(for: LanguageTests.self).path(forResource: name, ofType: "tmTheme")! let plist = NSDictionary(contentsOfFile: path)! as [NSObject: AnyObject] - return Theme(dictionary: plist)! + return Theme(dictionary: plist as! [String : AnyObject])! } func simpleTheme() -> Theme! { return Theme(dictionary: [ - "uuid": "7", - "name": "Simple", + "uuid": "7" , + "name": "Simple" , "settings": [ [ "scope": "entity.name", @@ -52,18 +62,20 @@ func simpleTheme() -> Theme! { ] ] ] - ]) + ] as [String: AnyObject]) } func assertEqualColors(color1: Color, _ color2: Color, accuracy: CGFloat = 0.005) { - XCTAssertEqualWithAccuracy(color1.redComponent, color2.redComponent, accuracy: accuracy) - XCTAssertEqualWithAccuracy(color1.greenComponent, color2.greenComponent, accuracy: accuracy) - XCTAssertEqualWithAccuracy(color1.blueComponent, color2.blueComponent, accuracy: accuracy) - XCTAssertEqualWithAccuracy(color1.alphaComponent, color2.alphaComponent, accuracy: accuracy) + // This was originally XCTAssertEqualWithAccuracy, which is deprecated + // switch to suggested XCTAssertEqual, but may need to revisit if issues arise. + XCTAssertEqual(color1.redComponent, color2.redComponent) + XCTAssertEqual(color1.greenComponent, color2.greenComponent) + XCTAssertEqual(color1.blueComponent, color2.blueComponent) + XCTAssertEqual(color1.alphaComponent, color2.alphaComponent) } -extension NSRange: Equatable { } - -public func ==(lhs: NSRange, rhs: NSRange) -> Bool { - return lhs.location == rhs.location && lhs.length == rhs.length -} +//extension NSRange: Equatable { } +// +//public func ==(lhs: NSRange, rhs: NSRange) -> Bool { +// return lhs.location == rhs.location && lhs.length == rhs.length +//} diff --git a/SyntaxKit/Tests/ThemeTests.swift b/SyntaxKit/Tests/ThemeTests.swift index fa8471d..bfe61b2 100644 --- a/SyntaxKit/Tests/ThemeTests.swift +++ b/SyntaxKit/Tests/ThemeTests.swift @@ -14,14 +14,17 @@ class ThemeTests: XCTestCase { // MARK: - Properties - let tomorrow = theme("Tomorrow") + let tomorrow = theme(name: "Tomorrow") // MARK: - Tests func testLoading() { - XCTAssertEqual("82CCD69C-F1B1-4529-B39E-780F91F07604", tomorrow.UUID) - XCTAssertEqual("Tomorrow", tomorrow.name) - assertEqualColors(Color(hex: "#666969")!, tomorrow.attributes["constant.other"]![NSForegroundColorAttributeName] as! Color) + XCTAssertEqual("82CCD69C-F1B1-4529-B39E-780F91F07604", tomorrow!.UUID) + XCTAssertEqual("Tomorrow", tomorrow!.name) + let attrs = tomorrow!.attributes["constant.other"]! + assertEqualColors(color1: Color(hex: "#666969")!, + attrs[NSAttributedStringKey.foregroundColor] as! Color) + } } diff --git a/SyntaxKit/Theme.swift b/SyntaxKit/Theme.swift index a665939..c317a76 100644 --- a/SyntaxKit/Theme.swift +++ b/SyntaxKit/Theme.swift @@ -13,7 +13,7 @@ import X import UIKit #endif -public typealias Attributes = [String: AnyObject] +public typealias Attributes = [NSAttributedStringKey: AnyObject] public struct Theme { @@ -26,10 +26,10 @@ public struct Theme { // MARK: - Initializers - public init?(dictionary: [NSObject: AnyObject]) { + public init?(dictionary: [String: AnyObject]) { guard let UUID = dictionary["uuid"] as? String, - name = dictionary["name"] as? String, - rawSettings = dictionary["settings"] as? [[String: AnyObject]] + let name = dictionary["name"] as? String, + let rawSettings = dictionary["settings"] as? [[String: AnyObject]] else { return nil } self.UUID = UUID @@ -40,19 +40,25 @@ public struct Theme { guard let scopes = raw["scope"] as? String else { continue } guard var setting = raw["settings"] as? [String: AnyObject] else { continue } - if let value = setting.removeValueForKey("foreground") as? String { - setting[NSForegroundColorAttributeName] = Color(hex: value) + + if let value = setting.removeValue(forKey: "foreground") as? String { + setting[NSAttributedStringKey.foregroundColor.rawValue] = Color(hex: value) } - if let value = setting.removeValueForKey("background") as? String { - setting[NSBackgroundColorAttributeName] = Color(hex: value) + if let value = setting.removeValue(forKey: "background") as? String { + + setting[NSAttributedStringKey.backgroundColor.rawValue] = Color(hex: value) } // TODO: caret, invisibles, lightHighlight, selection, font style - - for scope in scopes.componentsSeparatedByString(",") { - let key = scope.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) - attributes[key] = setting + var attrs = Attributes() + for (k,v) in setting { + attrs[NSAttributedStringKey(k)] = v + } + + for scope in scopes.components(separatedBy: ",") { + let key = scope.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) + attributes[key] = attrs } } self.attributes = attributes From d75cd955a02c9d1ee9a513d2e091954c8ae78638 Mon Sep 17 00:00:00 2001 From: iosdevzone Date: Sat, 31 Mar 2018 16:33:59 -0700 Subject: [PATCH 3/3] Updated to recommended project settings. --- SyntaxKit.xcodeproj/project.pbxproj | 34 ++++++++++++++++--- .../xcschemes/SyntaxKit-iOS.xcscheme | 6 ++-- .../xcschemes/SyntaxKit-macOS.xcscheme | 6 ++-- .../xcschemes/SyntaxKit-tvOS.xcscheme | 2 +- .../xcschemes/SyntaxKit-watchOS.xcscheme | 2 +- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/SyntaxKit.xcodeproj/project.pbxproj b/SyntaxKit.xcodeproj/project.pbxproj index 233a89e..1159c48 100644 --- a/SyntaxKit.xcodeproj/project.pbxproj +++ b/SyntaxKit.xcodeproj/project.pbxproj @@ -553,7 +553,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0730; - LastUpgradeCheck = 0700; + LastUpgradeCheck = 0930; ORGANIZATIONNAME = "Sam Soffes"; TargetAttributes = { 211826D91D257A71003F2BF2 = { @@ -798,6 +798,7 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ANALYZER_NONNULL = YES; + CODE_SIGN_IDENTITY = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -822,6 +823,7 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ANALYZER_NONNULL = YES; + CODE_SIGN_IDENTITY = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -883,7 +885,7 @@ 211989B91B2EC3B600F0D786 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - CODE_SIGN_IDENTITY = "iPhone Developer"; + CODE_SIGN_IDENTITY = ""; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = GEA92R6RK9; @@ -909,6 +911,7 @@ 211989BA1B2EC3B600F0D786 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + CODE_SIGN_IDENTITY = ""; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = GEA92R6RK9; @@ -980,13 +983,23 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 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_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; @@ -1030,13 +1043,23 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 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_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; @@ -1056,6 +1079,7 @@ MACOSX_DEPLOYMENT_TARGET = 10.9; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; SWIFT_VERSION = 4.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -1065,13 +1089,13 @@ 2122A6F51B22B9320006409B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; CLANG_ENABLE_MODULES = YES; COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; - EMBEDDED_CONTENT_CONTAINS_SWIFT = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Carthage/Build/Mac", @@ -1090,13 +1114,13 @@ 2122A6F61B22B9320006409B /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; CLANG_ENABLE_MODULES = YES; COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; - EMBEDDED_CONTENT_CONTAINS_SWIFT = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Carthage/Build/Mac", @@ -1152,6 +1176,7 @@ 2198CECF1B36D5D700BD463F /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + CODE_SIGN_IDENTITY = ""; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; @@ -1176,6 +1201,7 @@ 2198CED01B36D5D700BD463F /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + CODE_SIGN_IDENTITY = ""; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; diff --git a/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-iOS.xcscheme b/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-iOS.xcscheme index e630ca1..c8cc0f2 100644 --- a/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-iOS.xcscheme +++ b/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-iOS.xcscheme @@ -1,6 +1,6 @@ + codeCoverageEnabled = "YES" + shouldUseLaunchSchemeArgsEnv = "YES"> diff --git a/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-macOS.xcscheme b/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-macOS.xcscheme index 3490ea1..79b9ae3 100644 --- a/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-macOS.xcscheme +++ b/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-macOS.xcscheme @@ -1,6 +1,6 @@ + codeCoverageEnabled = "YES" + shouldUseLaunchSchemeArgsEnv = "YES"> diff --git a/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-tvOS.xcscheme b/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-tvOS.xcscheme index 765a3a1..38b7a13 100644 --- a/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-tvOS.xcscheme +++ b/SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-tvOS.xcscheme @@ -1,6 +1,6 @@