From 0b4a09433bf52bfe85964710417816b0ab0aab6b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 4 Apr 2020 11:03:52 -0400 Subject: [PATCH] Extract JavaScript highlighted text view to separate class --- MongoView.xcodeproj/project.pbxproj | 16 +++++++++-- .../JavaScriptHighlighter.swift | 14 ++++++---- .../QueryViewController.swift | 24 ++++++++-------- .../View Controllers/QueryViewController.xib | 6 ++-- MongoView/Views/JavaScriptEditorView.swift | 28 +++++++++++++++++++ .../{Windows => Views}/WindowStatusView.swift | 0 .../{Windows => Views}/WindowStatusView.xib | 0 7 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 MongoView/Views/JavaScriptEditorView.swift rename MongoView/{Windows => Views}/WindowStatusView.swift (100%) rename MongoView/{Windows => Views}/WindowStatusView.xib (100%) diff --git a/MongoView.xcodeproj/project.pbxproj b/MongoView.xcodeproj/project.pbxproj index e258f72..df778e5 100644 --- a/MongoView.xcodeproj/project.pbxproj +++ b/MongoView.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ D60C863A23CA2DD100C9DB8E /* ServerConnectWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */; }; D60C863F23CA2E2100C9DB8E /* ServerConnectViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D60C863D23CA2E2100C9DB8E /* ServerConnectViewController.swift */; }; 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 */; }; D63CDEC023C837DD0012D658 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D63CDEBF23C837DD0012D658 /* Assets.xcassets */; }; 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 = ""; }; D60C863D23CA2E2100C9DB8E /* ServerConnectViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerConnectViewController.swift; sourceTree = ""; }; D60C863E23CA2E2100C9DB8E /* ServerConnectViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ServerConnectViewController.xib; sourceTree = ""; }; + D62408C02438CF550020E09F /* JavaScriptEditorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JavaScriptEditorView.swift; sourceTree = ""; }; 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 = ""; }; D63CDEBF23C837DD0012D658 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -149,8 +151,6 @@ D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */, D63CDF3A23C838470012D658 /* DatabaseWindowController.swift */, D63CDF3B23C838470012D658 /* DatabaseWindowController.xib */, - D6A7D095243541A400B46857 /* WindowStatusView.swift */, - D6A7D099243546B500B46857 /* WindowStatusView.xib */, ); path = Windows; sourceTree = ""; @@ -168,6 +168,16 @@ path = "View Controllers"; sourceTree = ""; }; + D62408BF2438CF3C0020E09F /* Views */ = { + isa = PBXGroup; + children = ( + D6A7D095243541A400B46857 /* WindowStatusView.swift */, + D6A7D099243546B500B46857 /* WindowStatusView.xib */, + D62408C02438CF550020E09F /* JavaScriptEditorView.swift */, + ); + path = Views; + sourceTree = ""; + }; D63CDEB123C837DC0012D658 = { isa = PBXGroup; children = ( @@ -197,6 +207,7 @@ D6A7D0A22435880700B46857 /* Synax Highlighting */, D60C863B23CA2DD600C9DB8E /* Windows */, D60C863C23CA2DDD00C9DB8E /* View Controllers */, + D62408BF2438CF3C0020E09F /* Views */, D63CDEBF23C837DD0012D658 /* Assets.xcassets */, D63CDEC123C837DD0012D658 /* MainMenu.xib */, D63CDEC423C837DD0012D658 /* Info.plist */, @@ -345,6 +356,7 @@ D63CDF3C23C838470012D658 /* DatabaseWindowController.swift in Sources */, D6A7D096243541A400B46857 /* WindowStatusView.swift in Sources */, D6A7D0A42435885B00B46857 /* JavaScriptHighlighter.swift in Sources */, + D62408C12438CF550020E09F /* JavaScriptEditorView.swift in Sources */, D63CDF4023C839010012D658 /* QueryViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/MongoView/Synax Highlighting/JavaScriptHighlighter.swift b/MongoView/Synax Highlighting/JavaScriptHighlighter.swift index a8a4c91..41657b8 100644 --- a/MongoView/Synax Highlighting/JavaScriptHighlighter.swift +++ b/MongoView/Synax Highlighting/JavaScriptHighlighter.swift @@ -23,12 +23,18 @@ fileprivate let expressionEnds = CharacterSet(charactersIn: ",]});") class JavaScriptHighlighter { private let text: String - private var attributed: NSMutableAttributedString! + private var attributed: NSMutableAttributedString private var currentIndex: String.Index! private var indent = "" init(text: String) { 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) { @@ -62,9 +68,7 @@ class JavaScriptHighlighter { return c } - func highlight(mutableAttributed: NSMutableAttributedString? = nil) -> NSAttributedString { - attributed = mutableAttributed ?? NSMutableAttributedString(attributedString: NSAttributedString(string: text)) - + func highlight() { let fullRange = NSRange(location: 0, length: attributed.length) attributed.setAttributes([ .foregroundColor: NSColor.textColor, @@ -75,8 +79,6 @@ class JavaScriptHighlighter { while let char = peek(), !expressionEnds.contains(char) { consumeExpression() } - - return attributed } private func emit(token: TokenType, range: NSRange) { diff --git a/MongoView/View Controllers/QueryViewController.swift b/MongoView/View Controllers/QueryViewController.swift index 47462ab..69b62fd 100644 --- a/MongoView/View Controllers/QueryViewController.swift +++ b/MongoView/View Controllers/QueryViewController.swift @@ -12,7 +12,7 @@ import MongoSwift class QueryViewController: NSViewController { @IBOutlet weak var verticalSplitView: NSSplitView! - @IBOutlet var queryTextView: NSTextView! + @IBOutlet var queryTextView: JavaScriptEditorView! @IBOutlet weak var outlineView: NSOutlineView! @IBOutlet weak var documentCountLabel: NSTextField! @@ -53,8 +53,8 @@ class QueryViewController: NSViewController { queryTextView.font = .monospacedSystemFont(ofSize: 13, weight: .regular) queryTextView.isAutomaticQuoteSubstitutionEnabled = false queryTextView.string = defaultQuery - queryTextView.delegate = self - highlightQuery() +// queryTextView.delegate = self +// highlightQuery() outlineView.dataSource = self outlineView.delegate = self @@ -78,9 +78,9 @@ class QueryViewController: NSViewController { view.window!.makeFirstResponder(outlineView) } - func highlightQuery() { - _ = JavaScriptHighlighter(text: queryTextView.string).highlight(mutableAttributed: queryTextView.textStorage!) - } +// func highlightQuery() { +// JavaScriptHighlighter(mutableAttributed: queryTextView.textStorage!).highlight() +// } func refresh(reload: Bool = true) { if let query = mostRecentQuery { @@ -175,12 +175,12 @@ class QueryViewController: NSViewController { } } - -extension QueryViewController: NSTextViewDelegate { - func textDidChange(_ notification: Notification) { - highlightQuery() - } -} +// +//extension QueryViewController: NSTextViewDelegate { +// func textDidChange(_ notification: Notification) { +// highlightQuery() +// } +//} extension QueryViewController: NSMenuItemValidation { func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { diff --git a/MongoView/View Controllers/QueryViewController.xib b/MongoView/View Controllers/QueryViewController.xib index f53470d..a15357d 100644 --- a/MongoView/View Controllers/QueryViewController.xib +++ b/MongoView/View Controllers/QueryViewController.xib @@ -1,7 +1,7 @@ - + - + @@ -38,7 +38,7 @@ - + diff --git a/MongoView/Views/JavaScriptEditorView.swift b/MongoView/Views/JavaScriptEditorView.swift new file mode 100644 index 0000000..834cdf4 --- /dev/null +++ b/MongoView/Views/JavaScriptEditorView.swift @@ -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() + } +} diff --git a/MongoView/Windows/WindowStatusView.swift b/MongoView/Views/WindowStatusView.swift similarity index 100% rename from MongoView/Windows/WindowStatusView.swift rename to MongoView/Views/WindowStatusView.swift diff --git a/MongoView/Windows/WindowStatusView.xib b/MongoView/Views/WindowStatusView.xib similarity index 100% rename from MongoView/Windows/WindowStatusView.xib rename to MongoView/Views/WindowStatusView.xib