Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private extension VSCodeColorSource {
}

internal extension ThemeSlot {
// A list that stops short leaves the slot to `fallback`, which derives the stripe and the bools the way VS Code paints them.
// A list that stops short leaves the slot to `fallback`, which derives the stripe and the values the way VS Code paints them.
var vscodeSources: [VSCodeColorSource] {
switch self {
case .windowBackdrop:
Expand Down Expand Up @@ -104,6 +104,13 @@ internal extension ThemeSlot {
return [.sqlToken("constant.language.boolean.true.sql")]
case .gridBoolFalse:
return [.sqlToken("constant.language.boolean.false.sql")]
case .gridString:
return [.sqlToken("string.quoted.single.sql")]
case .gridNumber:
return [.sqlToken("constant.numeric.sql")]
// The SQL grammar scopes no date, binary, JSON, UUID or enum literal.
case .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum:
return []
case .gridRowNumber:
return [.color("editorLineNumber.foreground")]
case .gridModified:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Foundation

public enum ThemeSchema {
public static let current = 3
public static let current = 4
// Every release before format 4 reads and writes format 3 only.
public static let oldestReadable = 3
}

public enum ThemeIdentifier {
Expand Down Expand Up @@ -45,12 +47,12 @@ public enum ThemeFileFormat {

guard let schema = root[MetadataKey.schema.rawValue]?.intValue else {
if let legacy = root["version"]?.intValue {
throw ThemeLoadError.schemaTooOld(found: legacy, supported: ThemeSchema.current)
throw ThemeLoadError.schemaTooOld(found: legacy, supported: ThemeSchema.oldestReadable)
}
throw ThemeLoadError.missingSchema
}
guard schema >= ThemeSchema.current else {
throw ThemeLoadError.schemaTooOld(found: schema, supported: ThemeSchema.current)
guard schema >= ThemeSchema.oldestReadable else {
throw ThemeLoadError.schemaTooOld(found: schema, supported: ThemeSchema.oldestReadable)
}
guard schema <= ThemeSchema.current else {
throw ThemeLoadError.schemaTooNew(found: schema, supported: ThemeSchema.current)
Expand Down Expand Up @@ -80,7 +82,7 @@ public enum ThemeFileFormat {
name: name,
author: author,
appearance: appearance,
overrides: try colors(in: root)
overrides: try colors(in: root, schema: schema)
)
}

Expand All @@ -89,7 +91,7 @@ public enum ThemeFileFormat {
let name = try validatedName(theme.name, policy: .strict)

var root: [String: Any] = [
MetadataKey.schema.rawValue: ThemeSchema.current,
MetadataKey.schema.rawValue: schema(of: theme),
MetadataKey.id.rawValue: theme.id,
MetadataKey.name.rawValue: name,
MetadataKey.author.rawValue: try validatedAuthor(theme.author, policy: .strict),
Expand All @@ -108,6 +110,12 @@ public enum ThemeFileFormat {
)
}

// The oldest format holding every key the theme sets, so an older TablePro still reads a theme
// that uses none of the newer keys.
private static func schema(of theme: ThemeDefinition) -> Int {
theme.overrides.keys.reduce(ThemeSchema.oldestReadable) { max($0, $1.introducedInSchema) }
}

// MARK: - Reading

// `JSONDecoder` over `JSONSerialization`: its error names the character and where it is.
Expand All @@ -122,27 +130,36 @@ public enum ThemeFileFormat {
}
}

private static func colors(in root: [String: ThemeJSONNode]) throws -> [ThemeSlot: ThemeColor] {
private static func colors(in root: [String: ThemeJSONNode], schema: Int) throws -> [ThemeSlot: ThemeColor] {
let leaves = root
.filter { MetadataKey(rawValue: $0.key) == nil }
.flatMap { key, node in node.leaves(prefix: key) }
.sorted { $0.path < $1.path }

var parsed: [ThemeSlot: ThemeColor] = [:]
var unknown: [String] = []
var newer: [ThemeSlot] = []

for leaf in leaves {
guard let slot = ThemeSlot(rawValue: leaf.path) else {
unknown.append(leaf.path)
continue
}
// Every build that reads the file's format has to read all of it.
guard slot.introducedInSchema <= schema else {
newer.append(slot)
continue
}
guard parsed[slot] == nil else { throw ThemeLoadError.duplicateKey(leaf.path) }
guard let raw = leaf.value else { throw ThemeLoadError.invalidColor(leaf.path) }
let color = try ThemeColor(validating: raw)
parsed[slot] = slot.requiresOpaque ? color.opaque : color
}

guard unknown.isEmpty else { throw ThemeLoadError.unknownKeys(unknown) }
if let needed = newer.map(\.introducedInSchema).max() {
throw ThemeLoadError.keysNeedNewerFormat(newer.map(\.rawValue), format: needed)
}
return parsed
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum ThemeLoadError: LocalizedError, Equatable, Sendable {
case invalidAuthor
case invalidColor(String)
case unknownKeys([String])
case keysNeedNewerFormat([String], format: Int)
case duplicateKey(String)
case reservedIdentifier(String)
case invalidIdentifier(String)
Expand Down Expand Up @@ -42,13 +43,13 @@ public enum ThemeLoadError: LocalizedError, Equatable, Sendable {
return String(localized: "The file does not declare a theme format version.")
case let .schemaTooOld(found, supported):
return String(
format: String(localized: "Theme format %1$d is no longer supported. This version of TablePro reads format %2$d."),
format: String(localized: "Theme format %1$d is no longer supported. This version of TablePro reads format %2$d and later."),
found,
supported
)
case let .schemaTooNew(found, supported):
return String(
format: String(localized: "Theme format %1$d was made for a newer TablePro. This version reads format %2$d."),
format: String(localized: "Theme format %1$d was made for a newer TablePro. This version reads format %2$d and earlier."),
found,
supported
)
Expand All @@ -69,6 +70,9 @@ public enum ThemeLoadError: LocalizedError, Equatable, Sendable {
case let .unknownKeys(names):
let shown = ListFormatter.localizedString(byJoining: names.prefix(3).map { "\"\($0)\"" })
return String(format: String(localized: "TablePro does not use these theme keys: %@."), shown)
case let .keysNeedNewerFormat(names, format):
let shown = ListFormatter.localizedString(byJoining: names.prefix(3).map { "\"\($0)\"" })
return String(format: String(localized: "These theme keys need theme format %1$d: %2$@."), format, shown)
case let .duplicateKey(path):
return String(format: String(localized: "The theme sets \"%@\" more than once."), path)
case let .reservedIdentifier(identifier):
Expand Down
53 changes: 48 additions & 5 deletions apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public enum ThemeSlot: String, CaseIterable, Sendable {
case gridNullValue = "dataGrid.nullValue"
case gridBoolTrue = "dataGrid.boolTrue"
case gridBoolFalse = "dataGrid.boolFalse"
case gridString = "dataGrid.string"
case gridNumber = "dataGrid.number"
case gridDateTime = "dataGrid.dateTime"
case gridBinary = "dataGrid.binary"
case gridJSON = "dataGrid.json"
case gridUUID = "dataGrid.uuid"
case gridEnum = "dataGrid.enum"
case gridRowNumber = "dataGrid.rowNumber"
case gridModified = "dataGrid.modified"
case gridInserted = "dataGrid.inserted"
Expand All @@ -52,8 +59,9 @@ public enum ThemeSlot: String, CaseIterable, Sendable {
return .syntax
case .gridBackground, .gridText, .gridAlternateRow, .gridHeaderBackground, .gridHeaderText,
.gridLine, .gridSelection, .gridSelectionText, .gridInactiveSelection, .gridFocusBorder,
.gridNullValue, .gridBoolTrue, .gridBoolFalse, .gridRowNumber,
.gridModified, .gridInserted, .gridDeleted, .gridDeletedText:
.gridNullValue, .gridBoolTrue, .gridBoolFalse,
.gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum,
.gridRowNumber, .gridModified, .gridInserted, .gridDeleted, .gridDeletedText:
return .dataGrid
}
}
Expand All @@ -69,8 +77,11 @@ public enum ThemeSlot: String, CaseIterable, Sendable {
.syntaxNull, .syntaxOperator, .syntaxFunction, .syntaxType:
return .syntax
case .gridBackground, .gridText, .gridAlternateRow, .gridHeaderBackground, .gridHeaderText,
.gridLine, .gridNullValue, .gridBoolTrue, .gridBoolFalse, .gridRowNumber:
.gridLine, .gridRowNumber:
return .dataGrid
case .gridNullValue, .gridBoolTrue, .gridBoolFalse,
.gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum:
return .values
case .gridSelection, .gridSelectionText, .gridInactiveSelection, .gridFocusBorder:
return .selection
case .gridModified, .gridInserted, .gridDeleted, .gridDeletedText:
Expand All @@ -94,6 +105,7 @@ public enum ThemeSlot: String, CaseIterable, Sendable {
.syntaxNull, .syntaxOperator, .syntaxFunction, .syntaxType,
.gridText, .gridAlternateRow, .gridHeaderText, .gridLine, .gridSelection, .gridSelectionText,
.gridInactiveSelection, .gridFocusBorder, .gridNullValue, .gridBoolTrue, .gridBoolFalse,
.gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum,
.gridRowNumber, .gridModified, .gridInserted, .gridDeleted, .gridDeletedText:
return false
}
Expand All @@ -110,6 +122,7 @@ public enum ThemeSlot: String, CaseIterable, Sendable {
.syntaxNull, .syntaxOperator, .syntaxFunction, .syntaxType,
.gridBackground, .gridText, .gridHeaderBackground, .gridHeaderText, .gridLine,
.gridSelectionText, .gridFocusBorder, .gridNullValue, .gridBoolTrue, .gridBoolFalse,
.gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum,
.gridRowNumber:
return false
}
Expand All @@ -126,8 +139,8 @@ public enum ThemeSlot: String, CaseIterable, Sendable {
case .editorLineNumber: return String(localized: "Line Number")
case .editorInvisibles: return String(localized: "Invisibles")
case .syntaxKeyword: return String(localized: "Keyword")
case .syntaxString: return String(localized: "String")
case .syntaxNumber: return String(localized: "Number")
case .syntaxString, .gridString: return String(localized: "String")
case .syntaxNumber, .gridNumber: return String(localized: "Number")
case .syntaxComment: return String(localized: "Comment")
case .syntaxNull: return String(localized: "NULL")
case .syntaxOperator: return String(localized: "Operator")
Expand All @@ -143,6 +156,11 @@ public enum ThemeSlot: String, CaseIterable, Sendable {
case .gridNullValue: return String(localized: "NULL Value")
case .gridBoolTrue: return String(localized: "Bool True")
case .gridBoolFalse: return String(localized: "Bool False")
case .gridDateTime: return String(localized: "Date & Time")
case .gridBinary: return String(localized: "Binary")
case .gridJSON: return String(localized: "JSON")
case .gridUUID: return String(localized: "UUID")
case .gridEnum: return String(localized: "Enum")
case .gridRowNumber: return String(localized: "Row Number")
case .gridModified: return String(localized: "Modified")
case .gridInserted: return String(localized: "Inserted")
Expand Down Expand Up @@ -184,11 +202,36 @@ public enum ThemeSlot: String, CaseIterable, Sendable {
case .gridNullValue: return String(localized: "Data Grid NULL Value")
case .gridBoolTrue: return String(localized: "Data Grid Bool True")
case .gridBoolFalse: return String(localized: "Data Grid Bool False")
case .gridString: return String(localized: "Data Grid String")
case .gridNumber: return String(localized: "Data Grid Number")
case .gridDateTime: return String(localized: "Data Grid Date & Time")
case .gridBinary: return String(localized: "Data Grid Binary")
case .gridJSON: return String(localized: "Data Grid JSON")
case .gridUUID: return String(localized: "Data Grid UUID")
case .gridEnum: return String(localized: "Data Grid Enum")
case .gridRowNumber: return String(localized: "Data Grid Row Number")
case .gridModified: return String(localized: "Data Grid Modified")
case .gridInserted: return String(localized: "Data Grid Inserted")
case .gridDeleted: return String(localized: "Data Grid Deleted")
case .gridDeletedText: return String(localized: "Data Grid Deleted Text")
}
}

// A file declaring an older format cannot hold the key.
public var introducedInSchema: Int {
switch self {
case .windowBackdrop,
.editorBackground, .editorText, .editorCursor, .editorSelection,
.editorCurrentLine, .editorCurrentStatement, .editorLineNumber, .editorInvisibles,
.syntaxKeyword, .syntaxString, .syntaxNumber, .syntaxComment,
.syntaxNull, .syntaxOperator, .syntaxFunction, .syntaxType,
.gridBackground, .gridText, .gridAlternateRow, .gridHeaderBackground, .gridHeaderText,
.gridLine, .gridSelection, .gridSelectionText, .gridInactiveSelection, .gridFocusBorder,
.gridNullValue, .gridBoolTrue, .gridBoolFalse, .gridRowNumber,
.gridModified, .gridInserted, .gridDeleted, .gridDeletedText:
return 3
case .gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum:
return 4
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public extension ThemeSlot {
return .defaultSyntax
case .syntaxOperator:
return .slot(.editorText)
// A bool in labelColor next to themed cell text reads as a different table.
case .gridBoolTrue, .gridBoolFalse:
// A value in labelColor next to themed cell text reads as a different table.
case .gridBoolTrue, .gridBoolFalse,
.gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum:
return .slot(.gridText)
case .gridAlternateRow:
return .derived(base: .gridBackground, rule: .stripe(toward: .gridText))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Foundation

// The theme editor's grouping: selection colors and row change colors, each chosen as a set, get sections of their own.
// The theme editor's grouping: value colors, selection colors and row change colors, each chosen as a set, get sections of their own.
public enum ThemeSlotSection: String, CaseIterable, Sendable {
case window
case editor
case syntax
case dataGrid
case values
case selection
case changes

Expand All @@ -15,6 +16,7 @@ public enum ThemeSlotSection: String, CaseIterable, Sendable {
case .editor: return String(localized: "Editor")
case .syntax: return String(localized: "Syntax")
case .dataGrid: return String(localized: "Data Grid")
case .values: return String(localized: "Values")
case .selection: return String(localized: "Selection")
case .changes: return String(localized: "Changes")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,38 @@ struct VSCodeThemeImportTests {
{"colors": {
"editor.background": "#202020",
"editor.foreground": "#E0E0E0"
}, "tokenColors": [{"scope": "constant.language.boolean", "settings": {"foreground": "#E0E0E0"}}]}
}, "tokenColors": [
{"scope": "constant.language.boolean", "settings": {"foreground": "#E0E0E0"}},
{"scope": "string", "settings": {"foreground": "#E0E0E0"}}
]}
"""
)

#expect(theme.hex(.gridBoolTrue) == nil)
#expect(theme.hex(.gridBoolFalse) == nil)
#expect(theme.hex(.gridString) == nil)
#expect(theme.value(for: .gridBoolTrue) == .themed(ThemeColor(red: 0xE0, green: 0xE0, blue: 0xE0)))
#expect(theme.value(for: .gridString) == .themed(ThemeColor(red: 0xE0, green: 0xE0, blue: 0xE0)))
}

@Test("String and number rules color the grid's strings and numbers, and the other value types follow the text")
func stringAndNumberRulesColorGridValues() throws {
let theme = try Self.theme(
"""
{"colors": {"editor.background": "#1E1E1E", "editor.foreground": "#D4D4D4"}, "tokenColors": [
{"scope": "string", "settings": {"foreground": "#CE9178"}},
{"scope": "constant.numeric", "settings": {"foreground": "#B5CEA8"}}
]}
"""
)
let text = ThemeColor(red: 0xD4, green: 0xD4, blue: 0xD4)

#expect(theme.hex(.gridString) == "#CE9178")
#expect(theme.hex(.gridNumber) == "#B5CEA8")
for slot in [ThemeSlot.gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum] {
#expect(theme.hex(slot) == nil, "\(slot)")
#expect(theme.value(for: slot) == .themed(text), "\(slot)")
}
}

@Test("A VS Code theme's table header, list selection and focus colors import as set, even where they match the grid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ struct VSCodeThemeMappingTests {
case .gridNullValue: return "#707070"
case .gridBoolTrue: return "#C00009"
case .gridBoolFalse: return "#C0000A"
case .gridString: return "#C00002"
case .gridNumber: return "#C00003"
case .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum: return nil
case .gridRowNumber: return "#808080"
// 0xFF scaled by 0.3 and by 0.5, rounded.
case .gridModified: return "#E0C0004D"
Expand Down Expand Up @@ -143,6 +146,9 @@ struct VSCodeThemeMappingTests {
case .gridNullValue: return "#40404080"
case .gridBoolTrue: return nil
case .gridBoolFalse: return nil
case .gridString: return nil
case .gridNumber: return nil
case .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum: return nil
case .gridRowNumber: return nil
case .gridModified: return nil
case .gridInserted: return "#00AA0033"
Expand Down
Loading
Loading