Skip to content

Commit

Permalink
Color emphasis, improve theme intrepretation
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Apr 24, 2024
1 parent c55c583 commit 12f55f4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
19 changes: 19 additions & 0 deletions Sources/ThemePark/Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
4 changes: 4 additions & 0 deletions Sources/ThemePark/SyntaxSpecifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)),
Expand Down
35 changes: 32 additions & 3 deletions Sources/ThemePark/TextMateTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
let scopeString = (scope ?? "").trimmingCharacters(in: .whitespacesAndNewlines)

let components = scopeString.components(separatedBy: ",")

return Set(components)
}
}

public let author: String
Expand Down Expand Up @@ -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

Expand All @@ -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)
}
Expand Down
10 changes: 8 additions & 2 deletions Sources/ThemePark/XcodeTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
Expand Down Expand Up @@ -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(_))):
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 12f55f4

Please sign in to comment.