Syntax highlighter color updates

Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
This commit is contained in:
Eric Kuck 2019-01-08 12:35:06 +02:00 committed by Roopesh Chander
parent 64fe415879
commit 96fa6d3ba6
2 changed files with 30 additions and 45 deletions

View File

@ -3,22 +3,24 @@
import Cocoa
private let fontSize: CGFloat = 15
class ConfTextStorage: NSTextStorage {
struct TextColorTheme {
let black: NSColor
let red: NSColor
let green: NSColor
let yellow: NSColor
let blue: NSColor
let magenta: NSColor
let cyan: NSColor
let white: NSColor
let `default`: NSColor
let plainText: NSColor
let sections: NSColor
let key: NSColor
let url: NSColor
let urlAttribute: NSColor
let comments: NSColor
let number: NSColor
let error: NSColor
}
let defaultFont = NSFont.systemFont(ofSize: 16)
private let boldFont = NSFont.boldSystemFont(ofSize: 16)
let defaultFont = NSFontManager.shared.convertWeight(true, of: NSFont.systemFont(ofSize: fontSize))
private let boldFont = NSFont.boldSystemFont(ofSize: fontSize)
private lazy var italicFont = NSFontManager.shared.convert(defaultFont, toHaveTrait: .italicFontMask)
private var defaultAttributes: [NSAttributedString.Key: Any]! //swiftlint:disable:this implicitly_unwrapped_optional
private var highlightAttributes: [UInt32: [NSAttributedString.Key: Any]]! //swiftlint:disable:this implicitly_unwrapped_optional
@ -39,76 +41,59 @@ class ConfTextStorage: NSTextStorage {
fatalError("init(pasteboardPropertyList:ofType:) has not been implemented")
}
//swiftlint:disable:next function_body_length
func updateAttributes(for theme: TextColorTheme) {
self.defaultAttributes = [
.foregroundColor: theme.default,
.foregroundColor: theme.plainText,
.font: defaultFont
]
self.highlightAttributes = [
HighlightSection.rawValue: [
.foregroundColor: theme.black,
.foregroundColor: theme.sections,
.font: boldFont
],
HighlightKeytype.rawValue: [
.foregroundColor: theme.blue,
.foregroundColor: theme.key,
.font: boldFont
],
HighlightKey.rawValue: [
.foregroundColor: theme.yellow,
.font: boldFont
],
HighlightCmd.rawValue: [
.foregroundColor: theme.white,
.foregroundColor: theme.plainText,
.font: defaultFont
],
HighlightIP.rawValue: [
.foregroundColor: theme.green,
.foregroundColor: theme.url,
.font: defaultFont
],
HighlightCidr.rawValue: [
.foregroundColor: theme.yellow,
.foregroundColor: theme.urlAttribute,
.font: defaultFont
],
HighlightHost.rawValue: [
.foregroundColor: theme.green,
.font: boldFont
.foregroundColor: theme.url,
.font: defaultFont
],
HighlightPort.rawValue: [
.foregroundColor: theme.magenta,
.font: defaultFont
],
HighlightTable.rawValue: [
.foregroundColor: theme.blue,
.font: defaultFont
],
HighlightFwMark.rawValue: [
.foregroundColor: theme.blue,
.foregroundColor: theme.urlAttribute,
.font: defaultFont
],
HighlightMTU.rawValue: [
.foregroundColor: theme.blue,
.font: defaultFont
],
HighlightSaveConfig.rawValue: [
.foregroundColor: theme.blue,
.foregroundColor: theme.number,
.font: defaultFont
],
HighlightKeepalive.rawValue: [
.foregroundColor: theme.blue,
.foregroundColor: theme.number,
.font: defaultFont
],
HighlightComment.rawValue: [
.foregroundColor: theme.cyan,
.font: defaultFont
.foregroundColor: theme.comments,
.font: italicFont
],
HighlightDelimiter.rawValue: [
.foregroundColor: theme.cyan,
.foregroundColor: theme.plainText,
.font: defaultFont
],
HighlightError.rawValue: [
.foregroundColor: theme.red,
.foregroundColor: theme.error,
.font: defaultFont,
.underlineStyle: 1
]

View File

@ -38,9 +38,9 @@ class ConfTextView: NSTextView {
let theme: ConfTextStorage.TextColorTheme
switch effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) ?? .aqua {
case .darkAqua:
theme = ConfTextStorage.TextColorTheme(black: NSColor(hex: "#c7c7c7"), red: NSColor(hex: "#dc322f"), green: NSColor(hex: "#859900"), yellow: NSColor(hex: "#c7c400"), blue: NSColor(hex: "#268bd2"), magenta: NSColor(hex: "#d33682"), cyan: NSColor(hex: "#2aa198"), white: NSColor(hex: "#383838"), default: NSColor(hex: "#c7c7c7"))
theme = ConfTextStorage.TextColorTheme(plainText: NSColor(hex: "#FFFFFF"), sections: NSColor(hex: "#91D462"), key: NSColor(hex: "#FC5FA3"), url: NSColor(hex: "#53A5FB"), urlAttribute: NSColor(hex: "#75B492"), comments: NSColor(hex: "#6C7986"), number: NSColor(hex: "#9686F5"), error: NSColor(hex: "#FC6A5D"))
default:
theme = ConfTextStorage.TextColorTheme(black: NSColor(hex: "#000000"), red: NSColor(hex: "#c91b00"), green: NSColor(hex: "#00c200"), yellow: NSColor(hex: "#c7c400"), blue: NSColor(hex: "#0225c7"), magenta: NSColor(hex: "#c930c7"), cyan: NSColor(hex: "#00c5c7"), white: NSColor(hex: "#c7c7c7"), default: NSColor(hex: "#000000"))
theme = ConfTextStorage.TextColorTheme(plainText: NSColor(hex: "#000000"), sections: NSColor(hex: "#326D74"), key: NSColor(hex: "#9B2393"), url: NSColor(hex: "#0E0EFF"), urlAttribute: NSColor(hex: "#815F03"), comments: NSColor(hex: "#536579"), number: NSColor(hex: "#1C00CF"), error: NSColor(hex: "#C41A16"))
}
confTextStorage.updateAttributes(for: theme)
}