Compare commits

..

1 Commits

Author SHA1 Message Date
Shadowfacts 56efc109d7
Add rudimentary JavaScript highlighting to query text view 2020-04-02 23:06:45 -04:00
1 changed files with 22 additions and 15 deletions

View File

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