Extract JavaScript highlighted text view to separate class

This commit is contained in:
Shadowfacts 2020-04-04 11:03:52 -04:00
parent 1b0d261d54
commit 0b4a09433b
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
7 changed files with 65 additions and 23 deletions

View File

@ -11,6 +11,7 @@
D60C863A23CA2DD100C9DB8E /* ServerConnectWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */; }; D60C863A23CA2DD100C9DB8E /* ServerConnectWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */; };
D60C863F23CA2E2100C9DB8E /* ServerConnectViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D60C863D23CA2E2100C9DB8E /* ServerConnectViewController.swift */; }; D60C863F23CA2E2100C9DB8E /* ServerConnectViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D60C863D23CA2E2100C9DB8E /* ServerConnectViewController.swift */; };
D60C864023CA2E2100C9DB8E /* ServerConnectViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D60C863E23CA2E2100C9DB8E /* ServerConnectViewController.xib */; }; D60C864023CA2E2100C9DB8E /* ServerConnectViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D60C863E23CA2E2100C9DB8E /* ServerConnectViewController.xib */; };
D62408C12438CF550020E09F /* JavaScriptEditorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D62408C02438CF550020E09F /* JavaScriptEditorView.swift */; };
D63CDEBE23C837DC0012D658 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63CDEBD23C837DC0012D658 /* AppDelegate.swift */; }; D63CDEBE23C837DC0012D658 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63CDEBD23C837DC0012D658 /* AppDelegate.swift */; };
D63CDEC023C837DD0012D658 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D63CDEBF23C837DD0012D658 /* Assets.xcassets */; }; D63CDEC023C837DD0012D658 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D63CDEBF23C837DD0012D658 /* Assets.xcassets */; };
D63CDEC323C837DD0012D658 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = D63CDEC123C837DD0012D658 /* MainMenu.xib */; }; D63CDEC323C837DD0012D658 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = D63CDEC123C837DD0012D658 /* MainMenu.xib */; };
@ -84,6 +85,7 @@
D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ServerConnectWindowController.xib; sourceTree = "<group>"; }; D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ServerConnectWindowController.xib; sourceTree = "<group>"; };
D60C863D23CA2E2100C9DB8E /* ServerConnectViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerConnectViewController.swift; sourceTree = "<group>"; }; D60C863D23CA2E2100C9DB8E /* ServerConnectViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerConnectViewController.swift; sourceTree = "<group>"; };
D60C863E23CA2E2100C9DB8E /* ServerConnectViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ServerConnectViewController.xib; sourceTree = "<group>"; }; D60C863E23CA2E2100C9DB8E /* ServerConnectViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ServerConnectViewController.xib; sourceTree = "<group>"; };
D62408C02438CF550020E09F /* JavaScriptEditorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JavaScriptEditorView.swift; sourceTree = "<group>"; };
D63CDEBA23C837DC0012D658 /* MongoView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MongoView.app; sourceTree = BUILT_PRODUCTS_DIR; }; D63CDEBA23C837DC0012D658 /* MongoView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MongoView.app; sourceTree = BUILT_PRODUCTS_DIR; };
D63CDEBD23C837DC0012D658 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; }; D63CDEBD23C837DC0012D658 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
D63CDEBF23C837DD0012D658 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; }; D63CDEBF23C837DD0012D658 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@ -149,8 +151,6 @@
D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */, D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */,
D63CDF3A23C838470012D658 /* DatabaseWindowController.swift */, D63CDF3A23C838470012D658 /* DatabaseWindowController.swift */,
D63CDF3B23C838470012D658 /* DatabaseWindowController.xib */, D63CDF3B23C838470012D658 /* DatabaseWindowController.xib */,
D6A7D095243541A400B46857 /* WindowStatusView.swift */,
D6A7D099243546B500B46857 /* WindowStatusView.xib */,
); );
path = Windows; path = Windows;
sourceTree = "<group>"; sourceTree = "<group>";
@ -168,6 +168,16 @@
path = "View Controllers"; path = "View Controllers";
sourceTree = "<group>"; sourceTree = "<group>";
}; };
D62408BF2438CF3C0020E09F /* Views */ = {
isa = PBXGroup;
children = (
D6A7D095243541A400B46857 /* WindowStatusView.swift */,
D6A7D099243546B500B46857 /* WindowStatusView.xib */,
D62408C02438CF550020E09F /* JavaScriptEditorView.swift */,
);
path = Views;
sourceTree = "<group>";
};
D63CDEB123C837DC0012D658 = { D63CDEB123C837DC0012D658 = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@ -197,6 +207,7 @@
D6A7D0A22435880700B46857 /* Synax Highlighting */, D6A7D0A22435880700B46857 /* Synax Highlighting */,
D60C863B23CA2DD600C9DB8E /* Windows */, D60C863B23CA2DD600C9DB8E /* Windows */,
D60C863C23CA2DDD00C9DB8E /* View Controllers */, D60C863C23CA2DDD00C9DB8E /* View Controllers */,
D62408BF2438CF3C0020E09F /* Views */,
D63CDEBF23C837DD0012D658 /* Assets.xcassets */, D63CDEBF23C837DD0012D658 /* Assets.xcassets */,
D63CDEC123C837DD0012D658 /* MainMenu.xib */, D63CDEC123C837DD0012D658 /* MainMenu.xib */,
D63CDEC423C837DD0012D658 /* Info.plist */, D63CDEC423C837DD0012D658 /* Info.plist */,
@ -345,6 +356,7 @@
D63CDF3C23C838470012D658 /* DatabaseWindowController.swift in Sources */, D63CDF3C23C838470012D658 /* DatabaseWindowController.swift in Sources */,
D6A7D096243541A400B46857 /* WindowStatusView.swift in Sources */, D6A7D096243541A400B46857 /* WindowStatusView.swift in Sources */,
D6A7D0A42435885B00B46857 /* JavaScriptHighlighter.swift in Sources */, D6A7D0A42435885B00B46857 /* JavaScriptHighlighter.swift in Sources */,
D62408C12438CF550020E09F /* JavaScriptEditorView.swift in Sources */,
D63CDF4023C839010012D658 /* QueryViewController.swift in Sources */, D63CDF4023C839010012D658 /* QueryViewController.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;

View File

@ -23,12 +23,18 @@ fileprivate let expressionEnds = CharacterSet(charactersIn: ",]});")
class JavaScriptHighlighter { class JavaScriptHighlighter {
private let text: String private let text: String
private var attributed: NSMutableAttributedString! private var attributed: NSMutableAttributedString
private var currentIndex: String.Index! private var currentIndex: String.Index!
private var indent = "" private var indent = ""
init(text: String) { init(text: String) {
self.text = text self.text = text
self.attributed = NSMutableAttributedString(attributedString: NSAttributedString(string: text))
}
init(mutableAttributed: NSMutableAttributedString) {
self.text = mutableAttributed.string
self.attributed = mutableAttributed
} }
private func print(_ str: String) { private func print(_ str: String) {
@ -62,9 +68,7 @@ class JavaScriptHighlighter {
return c return c
} }
func highlight(mutableAttributed: NSMutableAttributedString? = nil) -> NSAttributedString { func highlight() {
attributed = mutableAttributed ?? NSMutableAttributedString(attributedString: NSAttributedString(string: text))
let fullRange = NSRange(location: 0, length: attributed.length) let fullRange = NSRange(location: 0, length: attributed.length)
attributed.setAttributes([ attributed.setAttributes([
.foregroundColor: NSColor.textColor, .foregroundColor: NSColor.textColor,
@ -75,8 +79,6 @@ class JavaScriptHighlighter {
while let char = peek(), !expressionEnds.contains(char) { while let char = peek(), !expressionEnds.contains(char) {
consumeExpression() consumeExpression()
} }
return attributed
} }
private func emit(token: TokenType, range: NSRange) { private func emit(token: TokenType, range: NSRange) {

View File

@ -12,7 +12,7 @@ import MongoSwift
class QueryViewController: NSViewController { class QueryViewController: NSViewController {
@IBOutlet weak var verticalSplitView: NSSplitView! @IBOutlet weak var verticalSplitView: NSSplitView!
@IBOutlet var queryTextView: NSTextView! @IBOutlet var queryTextView: JavaScriptEditorView!
@IBOutlet weak var outlineView: NSOutlineView! @IBOutlet weak var outlineView: NSOutlineView!
@IBOutlet weak var documentCountLabel: NSTextField! @IBOutlet weak var documentCountLabel: NSTextField!
@ -53,8 +53,8 @@ class QueryViewController: NSViewController {
queryTextView.font = .monospacedSystemFont(ofSize: 13, weight: .regular) queryTextView.font = .monospacedSystemFont(ofSize: 13, weight: .regular)
queryTextView.isAutomaticQuoteSubstitutionEnabled = false queryTextView.isAutomaticQuoteSubstitutionEnabled = false
queryTextView.string = defaultQuery queryTextView.string = defaultQuery
queryTextView.delegate = self // queryTextView.delegate = self
highlightQuery() // highlightQuery()
outlineView.dataSource = self outlineView.dataSource = self
outlineView.delegate = self outlineView.delegate = self
@ -78,9 +78,9 @@ class QueryViewController: NSViewController {
view.window!.makeFirstResponder(outlineView) view.window!.makeFirstResponder(outlineView)
} }
func highlightQuery() { // func highlightQuery() {
_ = JavaScriptHighlighter(text: queryTextView.string).highlight(mutableAttributed: queryTextView.textStorage!) // JavaScriptHighlighter(mutableAttributed: queryTextView.textStorage!).highlight()
} // }
func refresh(reload: Bool = true) { func refresh(reload: Bool = true) {
if let query = mostRecentQuery { if let query = mostRecentQuery {
@ -175,12 +175,12 @@ class QueryViewController: NSViewController {
} }
} }
//
extension QueryViewController: NSTextViewDelegate { //extension QueryViewController: NSTextViewDelegate {
func textDidChange(_ notification: Notification) { // func textDidChange(_ notification: Notification) {
highlightQuery() // highlightQuery()
} // }
} //}
extension QueryViewController: NSMenuItemValidation { extension QueryViewController: NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16085" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct"> <document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16096" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies> <dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16085"/> <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16096"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies> </dependencies>
<objects> <objects>
@ -38,7 +38,7 @@
<rect key="frame" x="0.0" y="0.0" width="726" height="86"/> <rect key="frame" x="0.0" y="0.0" width="726" height="86"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews> <subviews>
<textView importsGraphics="NO" richText="NO" verticallyResizable="YES" allowsCharacterPickerTouchBarItem="NO" textCompletion="NO" id="f8D-lV-IMK"> <textView importsGraphics="NO" richText="NO" verticallyResizable="YES" allowsCharacterPickerTouchBarItem="NO" textCompletion="NO" id="f8D-lV-IMK" customClass="JavaScriptEditorView" customModule="MongoView" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="726" height="86"/> <rect key="frame" x="0.0" y="0.0" width="726" height="86"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/> <color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>

View File

@ -0,0 +1,28 @@
//
// JavaScriptEditorView.swift
// MongoView
//
// Created by Shadowfacts on 4/4/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import AppKit
class JavaScriptEditorView: NSTextView {
override var string: String {
didSet {
rehighlight()
}
}
func rehighlight() {
JavaScriptHighlighter(mutableAttributed: self.textStorage!).highlight()
}
override func didChangeText() {
rehighlight()
super.didChangeText()
}
}