Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports back references #5

Closed
wants to merge 11 commits into from
134 changes: 84 additions & 50 deletions SyntaxKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

24 changes: 10 additions & 14 deletions SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-iOS.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1030"
LastUpgradeVersion = "1240"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -27,6 +27,15 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "211989A61B2EC3B600F0D786"
BuildableName = "SyntaxKit.framework"
BlueprintName = "SyntaxKit-iOS"
ReferencedContainer = "container:SyntaxKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -39,17 +48,6 @@
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "211989A61B2EC3B600F0D786"
BuildableName = "SyntaxKit.framework"
BlueprintName = "SyntaxKit-iOS"
ReferencedContainer = "container:SyntaxKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -70,8 +68,6 @@
ReferencedContainer = "container:SyntaxKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
28 changes: 12 additions & 16 deletions SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-macOS.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1030"
LastUpgradeVersion = "1240"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,8 +26,17 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2122A6DD1B22B9320006409B"
BuildableName = "SyntaxKit.framework"
BlueprintName = "SyntaxKit-macOS"
ReferencedContainer = "container:SyntaxKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -40,17 +49,6 @@
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2122A6DD1B22B9320006409B"
BuildableName = "SyntaxKit.framework"
BlueprintName = "SyntaxKit-macOS"
ReferencedContainer = "container:SyntaxKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -71,8 +69,6 @@
ReferencedContainer = "container:SyntaxKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
24 changes: 10 additions & 14 deletions SyntaxKit.xcodeproj/xcshareddata/xcschemes/SyntaxKit-tvOS.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1030"
LastUpgradeVersion = "1240"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -27,6 +27,15 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "211826D91D257A71003F2BF2"
BuildableName = "SyntaxKit.framework"
BlueprintName = "SyntaxKit-tvOS"
ReferencedContainer = "container:SyntaxKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -39,17 +48,6 @@
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "211826D91D257A71003F2BF2"
BuildableName = "SyntaxKit.framework"
BlueprintName = "SyntaxKit-tvOS"
ReferencedContainer = "container:SyntaxKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -70,8 +68,6 @@
ReferencedContainer = "container:SyntaxKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "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 = "1030"
LastUpgradeVersion = "1240"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -29,8 +29,6 @@
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -51,8 +49,6 @@
ReferencedContainer = "container:SyntaxKit.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
2 changes: 2 additions & 0 deletions SyntaxKit/AttributedParsingOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// Copyright © 2016 Alexander Hedges. All rights reserved.
//

import Foundation

/// Represents one change (insertion or deletion) between two strings
internal struct Diff {

Expand Down
72 changes: 46 additions & 26 deletions SyntaxKit/BundleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// Copyright © 2016 Alexander Hedges. All rights reserved.
//

import Foundation

open class BundleManager {

public enum TextMateFileType {
Expand All @@ -37,10 +39,15 @@ open class BundleManager {
/// has to be done separately using clearLanguageCache.
open var languageCaching: Bool = true

/// You probably want to leave the themeCaching property set to true.
///
/// - note: Setting it to false will not invalidate or purge the cache. This
/// has to be done separately using clearThemeCache.
open var themeCaching: Bool = true

public static var defaultManager: BundleManager?

private var bundleCallback: BundleLocationCallback
private var dependencies: [Language] = []
private var cachedLanguages: [String: Language] = [:]
private var cachedThemes: [String: Theme] = [:]

Expand Down Expand Up @@ -70,16 +77,34 @@ open class BundleManager {
return language
}

self.dependencies = []
var language = self.loadRawLanguage(withIdentifier: identifier)
language?.validate(with: self.dependencies)
guard let newLanguage = includeLanguage(withIdentifier: identifier) else {
return nil
}

var languageSet = Set<Language>(arrayLiteral: newLanguage)
var languageDependencies = [Language]()

while let language = languageSet.popFirst() {
languageDependencies.append(language)
for childLanguageRef in language.referencedLanguageRefs {
if languageDependencies.map({ $0.scopeName }).contains(childLanguageRef) {
continue
}
guard let childLanguage = includeLanguage(withIdentifier: childLanguageRef) else {
continue
}
languageSet.insert(childLanguage)
}
}

// Now we finally got all helper languages
newLanguage.validate(with: languageDependencies)

if languageCaching && language != nil {
self.cachedLanguages[identifier] = language
if languageCaching {
self.cachedLanguages[identifier] = newLanguage
}

self.dependencies = []
return language
return newLanguage
}

open func theme(withIdentifier identifier: String) -> Theme? {
Expand All @@ -93,33 +118,28 @@ open class BundleManager {
return nil
}

cachedThemes[identifier] = newTheme
if themeCaching {
self.cachedThemes[identifier] = newTheme
}
return newTheme
}

/// Clears the language cache. Use if low on memory.
open func clearLanguageCache() {
self.cachedLanguages = [:]
/// Use if low on memory.
open func clearCaches() {
self.cachedLanguages.removeAll()
self.cachedThemes.removeAll()
}

// MARK: - Internal Interface

/// - parameter identifier: The identifier of the requested language.
/// - returns: The Language with unresolved extenal references, if found
func loadRawLanguage(withIdentifier identifier: String) -> Language? {
let indexOfStoredLanguage = self.dependencies.firstIndex { (lang: Language) in lang.scopeName == identifier }

if let index = indexOfStoredLanguage {
return self.dependencies[index]
} else {
guard let dictURL = self.bundleCallback(identifier, .language),
let plist = NSDictionary(contentsOf: dictURL) as? [String: Any],
let newLanguage = Language(dictionary: plist, manager: self) else {
return nil
}

self.dependencies.append(newLanguage)
return newLanguage
private func includeLanguage(withIdentifier identifier: String) -> Language? {
guard let dictURL = self.bundleCallback(identifier, .language),
let plist = NSDictionary(contentsOf: dictURL) as? [String: Any],
let newLanguage = Language(dictionary: plist, manager: self) else {
return nil
}
return newLanguage
}
}
2 changes: 2 additions & 0 deletions SyntaxKit/Capture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// Copyright © 2014-2015 Sam Soffes. All rights reserved.
//

import Foundation

internal struct Capture {

// MARK: - Properties
Expand Down
2 changes: 2 additions & 0 deletions SyntaxKit/CaptureCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// Copyright © 2014-2015 Sam Soffes. All rights reserved.
//

import Foundation

internal struct CaptureCollection {

// MARK: - Properties
Expand Down
2 changes: 1 addition & 1 deletion SyntaxKit/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2015 Sam Soffes. All rights reserved.
//

#if os(OSX)
#if os(macOS)
import AppKit.NSColor
public typealias ColorType = NSColor

Expand Down
18 changes: 16 additions & 2 deletions SyntaxKit/Language.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@
// Copyright © 2014-2015 Sam Soffes. All rights reserved.
//

public struct Language {
import Foundation

public struct Language: Hashable {

public static func == (lhs: Language, rhs: Language) -> Bool {
return lhs.uuid == rhs.uuid && lhs.name == rhs.name && lhs.scopeName == rhs.scopeName
}

public func hash(into hasher: inout Hasher) {
hasher.combine(uuid)
hasher.combine(name)
hasher.combine(scopeName)
}

// MARK: - Properties

Expand All @@ -21,6 +33,8 @@ public struct Language {
let referenceManager: ReferenceManager
let repository: Repository

var referencedLanguageRefs: [String] { referenceManager.includedLanguageRefs }

static let globalScope: String = "GLOBAL"

// MARK: - Initializers
Expand Down Expand Up @@ -49,7 +63,7 @@ public struct Language {
/// - parameter helperLanguages: The languages that the language has
/// references to resolve against. This should at least contain the
/// language itself.
mutating func validate(with helperLanguages: [Language]) {
func validate(with helperLanguages: [Language]) {
ReferenceManager.resolveExternalReferences(between: helperLanguages, basename: self.scopeName)
}
}
Loading