macOS: Reset attributes for each syntax highlight cycle

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2019-01-22 01:19:16 +05:30
parent 15517c4c3d
commit 1bc2177883
2 changed files with 22 additions and 3 deletions

View File

@ -4,10 +4,16 @@
import Cocoa import Cocoa
protocol ConfTextColorTheme { protocol ConfTextColorTheme {
var defaultColor: NSColor { get }
func color(for: highlight_type) -> NSColor func color(for: highlight_type) -> NSColor
} }
struct ConfTextAquaColorTheme: ConfTextColorTheme { struct ConfTextAquaColorTheme: ConfTextColorTheme {
var defaultColor: NSColor {
return NSColor(hex: "#000000") // Plain text in Xcode
}
func color(for highlightType: highlight_type) -> NSColor { func color(for highlightType: highlight_type) -> NSColor {
switch highlightType.rawValue { switch highlightType.rawValue {
case HighlightSection.rawValue: case HighlightSection.rawValue:
@ -27,12 +33,16 @@ struct ConfTextAquaColorTheme: ConfTextColorTheme {
case HighlightError.rawValue: case HighlightError.rawValue:
return NSColor(hex: "#C41A16") // Strings in Xcode return NSColor(hex: "#C41A16") // Strings in Xcode
default: default:
return NSColor(hex: "#000000") // Plain text in Xcode return defaultColor
} }
} }
} }
struct ConfTextDarkAquaColorTheme: ConfTextColorTheme { struct ConfTextDarkAquaColorTheme: ConfTextColorTheme {
var defaultColor: NSColor {
return NSColor(hex: "#FFFFFF") // Plain text in Xcode
}
func color(for highlightType: highlight_type) -> NSColor { func color(for highlightType: highlight_type) -> NSColor {
switch highlightType.rawValue { switch highlightType.rawValue {
case HighlightSection.rawValue: case HighlightSection.rawValue:
@ -52,7 +62,7 @@ struct ConfTextDarkAquaColorTheme: ConfTextColorTheme {
case HighlightError.rawValue: case HighlightError.rawValue:
return NSColor(hex: "#FF4C4C") // Strings in Xcode return NSColor(hex: "#FF4C4C") // Strings in Xcode
default: default:
return NSColor(hex: "#FFFFFF") // Plain text in Xcode return defaultColor
} }
} }
} }

View File

@ -85,7 +85,16 @@ class ConfTextStorage: NSTextStorage {
hasError = false hasError = false
privateKeyString = nil privateKeyString = nil
let fullTextRange = NSRange(location: 0, length: (backingStore.string as NSString).length)
backingStore.beginEditing() backingStore.beginEditing()
if let textColorTheme = textColorTheme {
let defaultAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: textColorTheme.defaultColor,
.font: defaultFont
]
backingStore.setAttributes(defaultAttributes, range: fullTextRange)
}
var spans = highlight_config(backingStore.string.cString(using: String.Encoding.utf8))! var spans = highlight_config(backingStore.string.cString(using: String.Encoding.utf8))!
while spans.pointee.type != HighlightEnd { while spans.pointee.type != HighlightEnd {
@ -111,7 +120,7 @@ class ConfTextStorage: NSTextStorage {
backingStore.endEditing() backingStore.endEditing()
beginEditing() beginEditing()
edited(.editedAttributes, range: NSRange(location: 0, length: (backingStore.string as NSString).length), changeInLength: 0) edited(.editedAttributes, range: fullTextRange, changeInLength: 0)
endEditing() endEditing()
} }