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