Skip to content
Open
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
11 changes: 11 additions & 0 deletions Notepad/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,18 @@ public class Storage: NSTextStorage {
for (style) in theme.styles {
style.regex.enumerateMatches(in: backingString, options: .withoutAnchoringBounds, range: range, using: { (match, flags, stop) in
guard let match = match else { return }

backingStore.addAttributes(style.attributes, range: match.range(at: 0))
if #available(iOS 11.0, *) {
if let groups = style.groups {
for group in groups {
if let groupStyle = theme.style(withName: group) {
let range = match.range(withName: group)
backingStore.addAttributes(groupStyle.attributes, range: range)
}
}
}
}
})
}
}
Expand Down
10 changes: 8 additions & 2 deletions Notepad/Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
import Foundation

public struct Style {
public var name: String?
var regex: NSRegularExpression!
public var attributes: [NSAttributedString.Key: Any] = [:]
var groups: [String]?

public init(element: Element, attributes: [NSAttributedString.Key: Any]) {
public init(element: Element, attributes: [NSAttributedString.Key: Any], groups: [String]? = nil, name: String? = nil) {
self.name = name
self.regex = element.toRegex()
self.attributes = attributes
self.groups = groups
}

public init(regex: NSRegularExpression, attributes: [NSAttributedString.Key: Any]) {
public init(regex: NSRegularExpression, attributes: [NSAttributedString.Key: Any], groups: [String]? = nil, name: String? = nil) {
self.name = name
self.regex = regex
self.attributes = attributes
self.groups = groups
}

public init() {
Expand Down
12 changes: 8 additions & 4 deletions Notepad/Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public struct Theme {
/// All of the other styles for the Notepad editor.
public var styles: [Style] = []

public func style(withName name: String) -> Style? {
styles.first(where: { $0.name == name })
}

/// Build a theme from a JSON theme file.
///
Expand Down Expand Up @@ -120,12 +123,13 @@ public struct Theme {
allStyles.removeValue(forKey: "body")
for (element, attributes) in allStyles {
if let parsedStyles = parse(attributes as! [String : AnyObject]) {
let groups = attributes["groups"] as? [String]

if let regexString = attributes["regex"] as? String {
let regex = regexString.toRegex()
styles.append(Style(regex: regex, attributes: parsedStyles))
}
else {
styles.append(Style(element: Element.unknown.from(string: element), attributes: parsedStyles))
styles.append(Style(regex: regex, attributes: parsedStyles, groups: groups, name: element))
} else {
styles.append(Style(element: Element.unknown.from(string: element), attributes: parsedStyles, groups: groups, name: element))
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion Notepad/themes/one-dark-custom.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
"size": 25
},
"h3": {
"regex": "^((?<hash>\\#{3})(.*))$",
"font": "Courier-Bold",
"color": "#D36770",
"groups": ["hash"],
"color": "#FFFFFF",
"size": 22
},
"hash": {
"color": "#D36770"
},
"body": {
"font": "Courier",
"size": 17,
Expand Down