Compare commits

..

1 Commits

Author SHA1 Message Date
Shadowfacts 93e60850f3
Add rudimentary JavaScript highlighting to query text view 2020-04-02 22:59:05 -04:00
1 changed files with 15 additions and 22 deletions

View File

@ -38,14 +38,7 @@ class JavaScriptHighlighter {
return NSRange(from..<to, in: text)
}
private func prevCharRange() -> NSRange {
return range(from: text.index(before: currentIndex), to: currentIndex)
}
private func peek() -> Unicode.Scalar? {
guard currentIndex < text.endIndex else {
return nil
}
private func peek() -> Unicode.Scalar {
return text.unicodeScalars[currentIndex]
}
@ -55,7 +48,7 @@ class JavaScriptHighlighter {
}
@discardableResult
private func consume() -> Unicode.Scalar? {
private func consume() -> Unicode.Scalar {
let c = peek()
currentIndex = text.index(after: currentIndex)
return c
@ -79,7 +72,7 @@ class JavaScriptHighlighter {
}
private func consumeExpression() {
guard let char = peek() else { return }
let char = peek()
if identifierStarts.contains(char) {
consumeIdentifier()
@ -106,7 +99,7 @@ class JavaScriptHighlighter {
private func consumeIdentifier() {
let identifierStart = currentIndex!
while let char = peek(), identifiers.contains(char) {
while currentIndex < text.endIndex && identifiers.contains(peek()) {
consume()
}
print("Identifier: '\(text[identifierStart..<currentIndex])'")
@ -114,12 +107,12 @@ class JavaScriptHighlighter {
private func consumeNumber() {
let numberStart = currentIndex!
while let char = peek(), CharacterSet.decimalDigits.contains(char) {
while currentIndex < text.endIndex && CharacterSet.decimalDigits.contains(peek()) {
consume()
}
if currentIndex < text.endIndex && peek() == "." {
consume()
while let char = peek(), CharacterSet.decimalDigits.contains(char) {
while currentIndex < text.endIndex && CharacterSet.decimalDigits.contains(peek()) {
consume()
}
}
@ -180,14 +173,14 @@ class JavaScriptHighlighter {
}
private func consumeOperator() {
print("Operator: \(peek()!)")
print("Operator: \(peek())")
consume()
}
private func consumeFunctionCallOrGrouping() {
consume() // (
print("Opening (")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
indent += " "
while currentIndex < text.endIndex && peek() != ")" {
consumeExpression()
@ -196,14 +189,14 @@ class JavaScriptHighlighter {
if currentIndex < text.endIndex {
consume() // )
print("Closing )")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
}
}
private func consumeObject() {
consume() // {
print("Opening {")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
indent += " "
object:
while currentIndex < text.endIndex && peek() != "}" {
@ -226,13 +219,13 @@ class JavaScriptHighlighter {
if currentIndex < text.endIndex {
consume() // }
print("Closing }")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
}
}
private func consumeObjectKey() {
let keyStart = currentIndex!
while let char = peek(), identifiers.contains(char) {
while currentIndex < text.endIndex && identifiers.contains(peek()) {
consume()
}
print("Object key: '\(text[keyStart..<currentIndex])'")
@ -241,13 +234,13 @@ class JavaScriptHighlighter {
private func consumeDotLookup() {
consume() // .
print("Dot lookup")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
}
private func consumeArray() {
consume() // [
print("Opening [")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
indent += " "
array:
while currentIndex < text.endIndex && peek() != "]" {
@ -269,7 +262,7 @@ class JavaScriptHighlighter {
if currentIndex < text.endIndex {
consume() // ]
print("Closing ]")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
}
}