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
@@ -3,22 +3,24 @@
import Cocoa import Cocoa
private let fontSize: CGFloat = 15
class ConfTextStorage: NSTextStorage { class ConfTextStorage: NSTextStorage {
struct TextColorTheme { struct TextColorTheme {
let black: NSColor let plainText: NSColor
let red: NSColor let sections: NSColor
let green: NSColor let key: NSColor
let yellow: NSColor let url: NSColor
let blue: NSColor let urlAttribute: NSColor
let magenta: NSColor let comments: NSColor
let cyan: NSColor let number: NSColor
let white: NSColor let error: NSColor
let `default`: NSColor
} }
let defaultFont = NSFont.systemFont(ofSize: 16) let defaultFont = NSFontManager.shared.convertWeight(true, of: NSFont.systemFont(ofSize: fontSize))
private let boldFont = NSFont.boldSystemFont(ofSize: 16) 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 defaultAttributes: [NSAttributedString.Key: Any]! //swiftlint:disable:this implicitly_unwrapped_optional
private var highlightAttributes: [UInt32: [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") fatalError("init(pasteboardPropertyList:ofType:) has not been implemented")
} }
//swiftlint:disable:next function_body_length
func updateAttributes(for theme: TextColorTheme) { func updateAttributes(for theme: TextColorTheme) {
self.defaultAttributes = [ self.defaultAttributes = [
.foregroundColor: theme.default, .foregroundColor: theme.plainText,
.font: defaultFont .font: defaultFont
] ]
self.highlightAttributes = [ self.highlightAttributes = [
HighlightSection.rawValue: [ HighlightSection.rawValue: [
.foregroundColor: theme.black, .foregroundColor: theme.sections,
.font: boldFont .font: boldFont
], ],
HighlightKeytype.rawValue: [ HighlightKeytype.rawValue: [
.foregroundColor: theme.blue, .foregroundColor: theme.key,
.font: boldFont .font: boldFont
], ],
HighlightKey.rawValue: [ HighlightKey.rawValue: [
.foregroundColor: theme.yellow, .foregroundColor: theme.plainText,
.font: boldFont
],
HighlightCmd.rawValue: [
.foregroundColor: theme.white,
.font: defaultFont .font: defaultFont
], ],
HighlightIP.rawValue: [ HighlightIP.rawValue: [
.foregroundColor: theme.green, .foregroundColor: theme.url,
.font: defaultFont .font: defaultFont
], ],
HighlightCidr.rawValue: [ HighlightCidr.rawValue: [
.foregroundColor: theme.yellow, .foregroundColor: theme.urlAttribute,
.font: defaultFont .font: defaultFont
], ],
HighlightHost.rawValue: [ HighlightHost.rawValue: [
.foregroundColor: theme.green, .foregroundColor: theme.url,
.font: boldFont .font: defaultFont
], ],
HighlightPort.rawValue: [ HighlightPort.rawValue: [
.foregroundColor: theme.magenta, .foregroundColor: theme.urlAttribute,
.font: defaultFont
],
HighlightTable.rawValue: [
.foregroundColor: theme.blue,
.font: defaultFont
],
HighlightFwMark.rawValue: [
.foregroundColor: theme.blue,
.font: defaultFont .font: defaultFont
], ],
HighlightMTU.rawValue: [ HighlightMTU.rawValue: [
.foregroundColor: theme.blue, .foregroundColor: theme.number,
.font: defaultFont
],
HighlightSaveConfig.rawValue: [
.foregroundColor: theme.blue,
.font: defaultFont .font: defaultFont
], ],
HighlightKeepalive.rawValue: [ HighlightKeepalive.rawValue: [
.foregroundColor: theme.blue, .foregroundColor: theme.number,
.font: defaultFont .font: defaultFont
], ],
HighlightComment.rawValue: [ HighlightComment.rawValue: [
.foregroundColor: theme.cyan, .foregroundColor: theme.comments,
.font: defaultFont .font: italicFont
], ],
HighlightDelimiter.rawValue: [ HighlightDelimiter.rawValue: [
.foregroundColor: theme.cyan, .foregroundColor: theme.plainText,
.font: defaultFont .font: defaultFont
], ],
HighlightError.rawValue: [ HighlightError.rawValue: [
.foregroundColor: theme.red, .foregroundColor: theme.error,
.font: defaultFont, .font: defaultFont,
.underlineStyle: 1 .underlineStyle: 1
] ]
@@ -38,9 +38,9 @@ class ConfTextView: NSTextView {
let theme: ConfTextStorage.TextColorTheme let theme: ConfTextStorage.TextColorTheme
switch effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) ?? .aqua { switch effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) ?? .aqua {
case .darkAqua: 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: 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) confTextStorage.updateAttributes(for: theme)
} }