diff --git a/Sources/ThemePark/Style.swift b/Sources/ThemePark/Style.swift index 479f9bd..2f4876d 100644 --- a/Sources/ThemePark/Style.swift +++ b/Sources/ThemePark/Style.swift @@ -9,6 +9,25 @@ public typealias PlatformColor = NSColor public typealias PlatformFont = NSFont #endif +extension PlatformColor { + /// Makes a darker color lighter and a ligher color darker + public func emphasize(by ratio: CGFloat) -> PlatformColor { + let positive = ratio > 0 + let dark = relativeLuminance < 0.5 + + return switch (positive, dark) { + case (true, true): + lightening(by: ratio) + case (true, false): + darkening(by: ratio) + case (false, true): + darkening(by: ratio) + case (false, false): + lightening(by: ratio) + } + } +} + public struct Style: Hashable { public let color: PlatformColor public let font: PlatformFont? diff --git a/Sources/ThemePark/SyntaxSpecifier.swift b/Sources/ThemePark/SyntaxSpecifier.swift index 7719cd7..6a242be 100644 --- a/Sources/ThemePark/SyntaxSpecifier.swift +++ b/Sources/ThemePark/SyntaxSpecifier.swift @@ -71,6 +71,7 @@ public enum SyntaxSpecifier: Hashable, Sendable { case context public init?(highlightsQueryCapture name: String) { + print(name) guard let specififer = SyntaxSpecifier.treeSitterQueryCaptureMap[name] else { return nil } @@ -83,12 +84,15 @@ extension SyntaxSpecifier { private static let treeSitterQueryCaptureMap: [String: SyntaxSpecifier] = [ "boolean": .literal(.boolean), "conditional": .keyword(.conditional), + "constant": .identifier(.constant), + "constant.builtin": .identifier(.constant), "constructor": .keyword(.definition(.constructor)), "comment": .comment(nil), "float": .literal(.number(.float)), "function": .keyword(.definition(.function)), "function.call": .keyword(.operator(.call(.function))), "function.macro": .keyword(.operator(.call(.macro))), + "function.method": .keyword(.definition(.method)), "include": .keyword(.import), "keyword": .keyword(nil), "keyword.function": .keyword(.definition(.function)), diff --git a/Sources/ThemePark/TextMateTheme.swift b/Sources/ThemePark/TextMateTheme.swift index 5e0352d..305485f 100644 --- a/Sources/ThemePark/TextMateTheme.swift +++ b/Sources/ThemePark/TextMateTheme.swift @@ -7,6 +7,14 @@ public struct TextMateTheme: Codable, Hashable, Sendable { public let name: String? public let scope: String? public let settings: [String: String] + + var scopeComponents: Set { + let scopeString = (scope ?? "").trimmingCharacters(in: .whitespacesAndNewlines) + + let components = scopeString.components(separatedBy: ",") + + return Set(components) + } } public let author: String @@ -60,7 +68,7 @@ extension TextMateTheme: Styling { } private func resolveScope(_ scope: String) -> Style { - let colorHex = settings.first(where: { $0.scope == scope })?.settings["foreground"] + let colorHex = settings.first(where: { $0.scopeComponents.contains(scope) })?.settings["foreground"] let color = colorHex.flatMap { PlatformColor(hex: $0) } ?? fallbackForegroundColor @@ -69,17 +77,38 @@ extension TextMateTheme: Styling { public func style(for query: Query) -> Style { switch query.key { - case .editor(.background): + case .editor(.background), .gutter(.background): let colorHex = settings.first?.settings["background"] let color = PlatformColor(hex: colorHex!)! return Style(color: color, font: nil) - case .syntax(.text): + case .syntax(.text), .gutter(.label): let color = fallbackForegroundColor return Style(color: color, font: nil) case .syntax(.comment(_)): return resolveScope("comment") + case .syntax(.keyword(_)): + return resolveScope("keyword") + case .syntax(.literal(.string(_))): + return resolveScope("string") + case .syntax(.literal(_)): + return resolveScope("constant") + case .syntax(.identifier(.variable)): + return resolveScope("variable") + case .syntax(.identifier(.parameter)): + return resolveScope("variable") + case .syntax(.identifier(.type)): + return resolveScope("entity") + case .editor(.accessoryForeground): + let color = fallbackForegroundColor + + return Style(color: color, font: nil) + case .editor(.accessoryBackground): + let colorHex = settings.first?.settings["background"] + let color = PlatformColor(hex: colorHex!)!.emphasize(by: 0.4) + + return Style(color: color) default: return Style(color: .red, font: nil) } diff --git a/Sources/ThemePark/XcodeTheme.swift b/Sources/ThemePark/XcodeTheme.swift index 39a347b..d6377b7 100644 --- a/Sources/ThemePark/XcodeTheme.swift +++ b/Sources/ThemePark/XcodeTheme.swift @@ -3,6 +3,7 @@ import Foundation public struct XcodeTheme: Codable, Hashable, Sendable { public let version: Int public let sourceTextBackground: String + public let selection: String public let markupTextNormal: String public let insertionPoint: String public let invisibles: String @@ -13,6 +14,7 @@ public struct XcodeTheme: Codable, Hashable, Sendable { case markupTextNormal = "DVTMarkupTextNormalColor" case syntaxColors = "DVTSourceTextSyntaxColors" case sourceTextBackground = "DVTSourceTextBackground" + case selection = "DVTSourceTextSelectionColor" case insertionPoint = "DVTSourceTextInsertionPointColor" case invisibles = "DVTSourceTextInvisiblesColor" } @@ -149,6 +151,12 @@ extension XcodeTheme: Styling { let color = PlatformColor(componentsString: insertionPoint) ?? fallbackForegroundColor return Style(color: color, font: nil) + case .editor(.accessoryForeground): + return syntaxStyle(for: "xcode.syntax.plain") + case .editor(.accessoryBackground): + let color = fallbackBackgroundColor.emphasize(by: 0.4) + + return Style(color: color) case .syntax(.comment(_)): return syntaxStyle(for: "xcode.syntax.comment") case .syntax(.literal(.string(_))): @@ -169,8 +177,6 @@ extension XcodeTheme: Styling { return Style(color: color, font: nil) case .syntax(_): return syntaxStyle(for: "xcode.syntax.plain") - default: - return Style(color: .red, font: nil) } }