Browse Source

Extract JavaScript highlighted text view to separate class

master
Shadowfacts 3 months ago
parent
commit
0b4a09433b
Signed by: Shadowfacts <me@shadowfacts.net> GPG Key ID: 94A5AB95422746E5

+ 14
- 2
MongoView.xcodeproj/project.pbxproj View File

@@ -11,6 +11,7 @@
11 11
 		D60C863A23CA2DD100C9DB8E /* ServerConnectWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */; };
12 12
 		D60C863F23CA2E2100C9DB8E /* ServerConnectViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D60C863D23CA2E2100C9DB8E /* ServerConnectViewController.swift */; };
13 13
 		D60C864023CA2E2100C9DB8E /* ServerConnectViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D60C863E23CA2E2100C9DB8E /* ServerConnectViewController.xib */; };
14
+		D62408C12438CF550020E09F /* JavaScriptEditorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D62408C02438CF550020E09F /* JavaScriptEditorView.swift */; };
14 15
 		D63CDEBE23C837DC0012D658 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63CDEBD23C837DC0012D658 /* AppDelegate.swift */; };
15 16
 		D63CDEC023C837DD0012D658 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D63CDEBF23C837DD0012D658 /* Assets.xcassets */; };
16 17
 		D63CDEC323C837DD0012D658 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = D63CDEC123C837DD0012D658 /* MainMenu.xib */; };
@@ -84,6 +85,7 @@
84 85
 		D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ServerConnectWindowController.xib; sourceTree = "<group>"; };
85 86
 		D60C863D23CA2E2100C9DB8E /* ServerConnectViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerConnectViewController.swift; sourceTree = "<group>"; };
86 87
 		D60C863E23CA2E2100C9DB8E /* ServerConnectViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ServerConnectViewController.xib; sourceTree = "<group>"; };
88
+		D62408C02438CF550020E09F /* JavaScriptEditorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JavaScriptEditorView.swift; sourceTree = "<group>"; };
87 89
 		D63CDEBA23C837DC0012D658 /* MongoView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MongoView.app; sourceTree = BUILT_PRODUCTS_DIR; };
88 90
 		D63CDEBD23C837DC0012D658 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
89 91
 		D63CDEBF23C837DD0012D658 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -149,8 +151,6 @@
149 151
 				D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */,
150 152
 				D63CDF3A23C838470012D658 /* DatabaseWindowController.swift */,
151 153
 				D63CDF3B23C838470012D658 /* DatabaseWindowController.xib */,
152
-				D6A7D095243541A400B46857 /* WindowStatusView.swift */,
153
-				D6A7D099243546B500B46857 /* WindowStatusView.xib */,
154 154
 			);
155 155
 			path = Windows;
156 156
 			sourceTree = "<group>";
@@ -168,6 +168,16 @@
168 168
 			path = "View Controllers";
169 169
 			sourceTree = "<group>";
170 170
 		};
171
+		D62408BF2438CF3C0020E09F /* Views */ = {
172
+			isa = PBXGroup;
173
+			children = (
174
+				D6A7D095243541A400B46857 /* WindowStatusView.swift */,
175
+				D6A7D099243546B500B46857 /* WindowStatusView.xib */,
176
+				D62408C02438CF550020E09F /* JavaScriptEditorView.swift */,
177
+			);
178
+			path = Views;
179
+			sourceTree = "<group>";
180
+		};
171 181
 		D63CDEB123C837DC0012D658 = {
172 182
 			isa = PBXGroup;
173 183
 			children = (
@@ -197,6 +207,7 @@
197 207
 				D6A7D0A22435880700B46857 /* Synax Highlighting */,
198 208
 				D60C863B23CA2DD600C9DB8E /* Windows */,
199 209
 				D60C863C23CA2DDD00C9DB8E /* View Controllers */,
210
+				D62408BF2438CF3C0020E09F /* Views */,
200 211
 				D63CDEBF23C837DD0012D658 /* Assets.xcassets */,
201 212
 				D63CDEC123C837DD0012D658 /* MainMenu.xib */,
202 213
 				D63CDEC423C837DD0012D658 /* Info.plist */,
@@ -345,6 +356,7 @@
345 356
 				D63CDF3C23C838470012D658 /* DatabaseWindowController.swift in Sources */,
346 357
 				D6A7D096243541A400B46857 /* WindowStatusView.swift in Sources */,
347 358
 				D6A7D0A42435885B00B46857 /* JavaScriptHighlighter.swift in Sources */,
359
+				D62408C12438CF550020E09F /* JavaScriptEditorView.swift in Sources */,
348 360
 				D63CDF4023C839010012D658 /* QueryViewController.swift in Sources */,
349 361
 			);
350 362
 			runOnlyForDeploymentPostprocessing = 0;

+ 8
- 6
MongoView/Synax Highlighting/JavaScriptHighlighter.swift View File

@@ -23,12 +23,18 @@ fileprivate let expressionEnds = CharacterSet(charactersIn: ",]});")
23 23
 
24 24
 class JavaScriptHighlighter {
25 25
     private let text: String
26
-    private var attributed: NSMutableAttributedString!
26
+    private var attributed: NSMutableAttributedString
27 27
     private var currentIndex: String.Index!
28 28
     private var indent = ""
29 29
     
30 30
     init(text: String) {
31 31
         self.text = text
32
+        self.attributed = NSMutableAttributedString(attributedString: NSAttributedString(string: text))
33
+    }
34
+    
35
+    init(mutableAttributed: NSMutableAttributedString) {
36
+        self.text = mutableAttributed.string
37
+        self.attributed = mutableAttributed
32 38
     }
33 39
     
34 40
     private func print(_ str: String) {
@@ -62,9 +68,7 @@ class JavaScriptHighlighter {
62 68
         return c
63 69
     }
64 70
     
65
-    func highlight(mutableAttributed: NSMutableAttributedString? = nil) -> NSAttributedString {
66
-        attributed = mutableAttributed ?? NSMutableAttributedString(attributedString: NSAttributedString(string: text))
67
-        
71
+    func highlight() {
68 72
         let fullRange = NSRange(location: 0, length: attributed.length)
69 73
         attributed.setAttributes([
70 74
             .foregroundColor: NSColor.textColor,
@@ -75,8 +79,6 @@ class JavaScriptHighlighter {
75 79
         while let char = peek(), !expressionEnds.contains(char) {
76 80
             consumeExpression()
77 81
         }
78
-
79
-        return attributed
80 82
     }
81 83
     
82 84
     private func emit(token: TokenType, range: NSRange) {

+ 12
- 12
MongoView/View Controllers/QueryViewController.swift View File

@@ -12,7 +12,7 @@ import MongoSwift
12 12
 class QueryViewController: NSViewController {
13 13
 
14 14
     @IBOutlet weak var verticalSplitView: NSSplitView!
15
-    @IBOutlet var queryTextView: NSTextView!
15
+    @IBOutlet var queryTextView: JavaScriptEditorView!
16 16
     @IBOutlet weak var outlineView: NSOutlineView!
17 17
     @IBOutlet weak var documentCountLabel: NSTextField!
18 18
     
@@ -53,8 +53,8 @@ class QueryViewController: NSViewController {
53 53
         queryTextView.font = .monospacedSystemFont(ofSize: 13, weight: .regular)
54 54
         queryTextView.isAutomaticQuoteSubstitutionEnabled = false
55 55
         queryTextView.string = defaultQuery
56
-        queryTextView.delegate = self
57
-        highlightQuery()
56
+//        queryTextView.delegate = self
57
+//        highlightQuery()
58 58
         
59 59
         outlineView.dataSource = self
60 60
         outlineView.delegate = self
@@ -78,9 +78,9 @@ class QueryViewController: NSViewController {
78 78
         view.window!.makeFirstResponder(outlineView)
79 79
     }
80 80
     
81
-    func highlightQuery() {
82
-        _ = JavaScriptHighlighter(text: queryTextView.string).highlight(mutableAttributed: queryTextView.textStorage!)
83
-    }
81
+//    func highlightQuery() {
82
+//        JavaScriptHighlighter(mutableAttributed: queryTextView.textStorage!).highlight()
83
+//    }
84 84
     
85 85
     func refresh(reload: Bool = true) {
86 86
         if let query = mostRecentQuery {
@@ -175,12 +175,12 @@ class QueryViewController: NSViewController {
175 175
     }
176 176
     
177 177
 }
178
-
179
-extension QueryViewController: NSTextViewDelegate {
180
-    func textDidChange(_ notification: Notification) {
181
-        highlightQuery()
182
-    }
183
-}
178
+//
179
+//extension QueryViewController: NSTextViewDelegate {
180
+//    func textDidChange(_ notification: Notification) {
181
+//        highlightQuery()
182
+//    }
183
+//}
184 184
 
185 185
 extension QueryViewController: NSMenuItemValidation {
186 186
     func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {

+ 3
- 3
MongoView/View Controllers/QueryViewController.xib View File

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

+ 28
- 0
MongoView/Views/JavaScriptEditorView.swift View File

@@ -0,0 +1,28 @@
1
+//
2
+//  JavaScriptEditorView.swift
3
+//  MongoView
4
+//
5
+//  Created by Shadowfacts on 4/4/20.
6
+//  Copyright © 2020 Shadowfacts. All rights reserved.
7
+//
8
+
9
+import AppKit
10
+
11
+class JavaScriptEditorView: NSTextView {
12
+    
13
+    override var string: String {
14
+        didSet {
15
+            rehighlight()
16
+        }
17
+    }
18
+
19
+    func rehighlight() {
20
+        JavaScriptHighlighter(mutableAttributed: self.textStorage!).highlight()
21
+    }
22
+    
23
+    override func didChangeText() {
24
+        rehighlight()
25
+        
26
+        super.didChangeText()
27
+    }
28
+}

MongoView/Windows/WindowStatusView.swift → MongoView/Views/WindowStatusView.swift View File


MongoView/Windows/WindowStatusView.xib → MongoView/Views/WindowStatusView.xib View File


Loading…
Cancel
Save