Skip to content

Commit 6445020

Browse files
committed
Added better formatting functions
1 parent c9ca266 commit 6445020

File tree

5 files changed

+81
-28
lines changed

5 files changed

+81
-28
lines changed

SwiftyJSONAccelerator.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
9361FD141BD18558001ED533 /* LineNumberRulerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9361FD131BD18558001ED533 /* LineNumberRulerView.swift */; settings = {ASSET_TAGS = (); }; };
1111
9362F8661BD07DBA0001372E /* JSONHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9362F8651BD07DBA0001372E /* JSONHelperTests.swift */; settings = {ASSET_TAGS = (); }; };
12+
93CDE7721BD1A00B00B5B234 /* SJTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93CDE7711BD1A00B00B5B234 /* SJTextView.swift */; settings = {ASSET_TAGS = (); }; };
1213
93F174431BD0707D007E7DFC /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93F174421BD0707D007E7DFC /* AppDelegate.swift */; };
1314
93F174451BD0707D007E7DFC /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93F174441BD0707D007E7DFC /* ViewController.swift */; };
1415
93F174471BD0707D007E7DFC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 93F174461BD0707D007E7DFC /* Assets.xcassets */; };
@@ -30,6 +31,7 @@
3031
/* Begin PBXFileReference section */
3132
9361FD131BD18558001ED533 /* LineNumberRulerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LineNumberRulerView.swift; sourceTree = "<group>"; };
3233
9362F8651BD07DBA0001372E /* JSONHelperTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONHelperTests.swift; sourceTree = "<group>"; };
34+
93CDE7711BD1A00B00B5B234 /* SJTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SJTextView.swift; sourceTree = "<group>"; };
3335
93F1743F1BD0707D007E7DFC /* SwiftyJSONAccelerator.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftyJSONAccelerator.app; sourceTree = BUILT_PRODUCTS_DIR; };
3436
93F174421BD0707D007E7DFC /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3537
93F174441BD0707D007E7DFC /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@@ -88,6 +90,7 @@
8890
93F174481BD0707D007E7DFC /* Main.storyboard */,
8991
93F1744B1BD0707D007E7DFC /* Info.plist */,
9092
93F1745F1BD070AA007E7DFC /* JSONHelper.swift */,
93+
93CDE7711BD1A00B00B5B234 /* SJTextView.swift */,
9194
);
9295
path = SwiftyJSONAccelerator;
9396
sourceTree = "<group>";
@@ -201,6 +204,7 @@
201204
isa = PBXSourcesBuildPhase;
202205
buildActionMask = 2147483647;
203206
files = (
207+
93CDE7721BD1A00B00B5B234 /* SJTextView.swift in Sources */,
204208
93F174601BD070AA007E7DFC /* JSONHelper.swift in Sources */,
205209
93F174451BD0707D007E7DFC /* ViewController.swift in Sources */,
206210
9361FD141BD18558001ED533 /* LineNumberRulerView.swift in Sources */,

SwiftyJSONAccelerator/Base.lproj/Main.storyboard

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,8 @@
651651
<scene sceneID="R2V-B0-nI4">
652652
<objects>
653653
<windowController id="B8D-0N-5wS" sceneMemberID="viewController">
654-
<window key="window" title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" animationBehavior="default" id="IQv-IB-iLA">
655-
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
654+
<window key="window" title="SwiftyJSONAccelerator" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="IQv-IB-iLA">
655+
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES" unifiedTitleAndToolbar="YES"/>
656656
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
657657
<rect key="contentRect" x="196" y="240" width="480" height="270"/>
658658
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
@@ -673,13 +673,24 @@
673673
<rect key="frame" x="0.0" y="0.0" width="593" height="460"/>
674674
<autoresizingMask key="autoresizingMask"/>
675675
<subviews>
676+
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="AwX-MF-DMD">
677+
<rect key="frame" x="14" y="3" width="565" height="32"/>
678+
<animations/>
679+
<buttonCell key="cell" type="push" title="Format" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="lfq-UD-8WD">
680+
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
681+
<font key="font" metaFont="system"/>
682+
</buttonCell>
683+
<connections>
684+
<action selector="format:" target="XfG-lQ-9wD" id="3wu-2c-YDh"/>
685+
</connections>
686+
</button>
676687
<scrollView horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Gmg-Tp-PPn">
677688
<rect key="frame" x="0.0" y="39" width="593" height="421"/>
678689
<clipView key="contentView" id="V7d-R1-cDN">
679690
<rect key="frame" x="1" y="1" width="591" height="419"/>
680691
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
681692
<subviews>
682-
<textView importsGraphics="NO" findStyle="panel" continuousSpellChecking="YES" allowsUndo="YES" usesRuler="YES" usesFontPanel="YES" verticallyResizable="YES" allowsNonContiguousLayout="YES" quoteSubstitution="YES" dashSubstitution="YES" spellingCorrection="YES" smartInsertDelete="YES" id="fWl-7Q-Cq6">
693+
<textView importsGraphics="NO" findStyle="panel" continuousSpellChecking="YES" allowsUndo="YES" usesRuler="YES" usesFontPanel="YES" verticallyResizable="YES" allowsNonContiguousLayout="YES" quoteSubstitution="YES" dashSubstitution="YES" spellingCorrection="YES" smartInsertDelete="YES" id="fWl-7Q-Cq6" customClass="SJTextView" customModule="SwiftyJSONAccelerator" customModuleProvider="target">
683694
<rect key="frame" x="0.0" y="0.0" width="591" height="419"/>
684695
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
685696
<animations/>
@@ -706,17 +717,6 @@
706717
<animations/>
707718
</scroller>
708719
</scrollView>
709-
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="AwX-MF-DMD">
710-
<rect key="frame" x="14" y="3" width="565" height="32"/>
711-
<animations/>
712-
<buttonCell key="cell" type="push" title="Format" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="lfq-UD-8WD">
713-
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
714-
<font key="font" metaFont="system"/>
715-
</buttonCell>
716-
<connections>
717-
<action selector="format:" target="XfG-lQ-9wD" id="3wu-2c-YDh"/>
718-
</connections>
719-
</button>
720720
</subviews>
721721
<constraints>
722722
<constraint firstItem="AwX-MF-DMD" firstAttribute="leading" secondItem="m2S-Jp-Qdl" secondAttribute="leading" constant="20" id="76H-O2-W7l"/>

SwiftyJSONAccelerator/LineNumberRulerView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension NSTextView {
4141

4242
func lnv_setUpLineNumberView() {
4343
if font == nil {
44-
font = NSFont.systemFontOfSize(16)
44+
font = NSFont.systemFontOfSize(12)
4545
}
4646

4747
if let scrollView = enclosingScrollView {
@@ -67,6 +67,7 @@ extension NSTextView {
6767

6868
lineNumberView.needsDisplay = true
6969
}
70+
7071
}
7172

7273
class LineNumberRulerView: NSRulerView {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// SJTextView.swift
3+
// SwiftyJSONAccelerator
4+
//
5+
// Created by Karthik on 16/10/2015.
6+
// Copyright © 2015 Karthikeya Udupa K M. All rights reserved.
7+
//
8+
9+
import Cocoa
10+
11+
class SJTextView: NSTextView {
12+
13+
override var readablePasteboardTypes: [String] {
14+
get {
15+
return [NSPasteboardTypeString]
16+
}
17+
}
18+
19+
internal func updateFormat() {
20+
textStorage?.font = NSFont(name: "Menlo", size: 12)
21+
}
22+
23+
override func paste(sender: AnyObject?) {
24+
super.paste(sender)
25+
updateFormat()
26+
}
27+
}

SwiftyJSONAccelerator/ViewController.swift

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ import Cocoa
1010

1111
class ViewController: NSViewController, NSTextViewDelegate {
1212

13-
@IBOutlet var textView: NSTextView?
13+
@IBOutlet var textView: SJTextView?
1414

1515
override func viewDidLoad() {
1616
super.viewDidLoad()
17-
JSONHelper.isStringValidJSON("")
18-
JSONHelper.prettyJSON(["value":20,"zen":"budhism"])
19-
textView!.lnv_setUpLineNumberView()
17+
2018
textView!.delegate = self
21-
22-
// Do any additional setup after loading the view.
19+
textView!.updateFormat()
20+
textView!.lnv_setUpLineNumberView()
2321
}
2422

2523
override var representedObject: AnyObject? {
@@ -29,24 +27,47 @@ class ViewController: NSViewController, NSTextViewDelegate {
2927
}
3028

3129
@IBAction func format(sender: AnyObject?) {
30+
31+
if textView?.string?.characters.count == 0 {
32+
return
33+
}
34+
3235
if let (valid, error): (Bool, NSError?) = JSONHelper.isStringValidJSON(textView?.string) {
3336
if(valid){
3437
textView?.string = JSONHelper.prettyJSON(textView?.string)!
3538
} else if error != nil {
36-
error.debugDescription
3739
let alert: NSAlert = NSAlert.init(error: error!)
40+
if let _ = error!.userInfo["debugDescription"] as? String? {
41+
alert.messageText = error!.userInfo["NSDebugDescription"]! as! String
42+
}
3843
alert.runModal()
3944
}
4045
}
46+
47+
textView!.updateFormat()
48+
textView!.lnv_textDidChange(NSNotification.init(name: NSTextDidChangeNotification, object: nil))
4149
}
4250

43-
func textView(textView: NSTextView, doCommandBySelector commandSelector: Selector) -> Bool {
44-
45-
if commandSelector == Selector("paste:") {
46-
format(nil)
51+
func characterRowAndLineAt(string: String,position: Int) -> (character: String, line: Int, column:Int) {
52+
var lineNumber = 0
53+
var characterPosition = 0
54+
for line in string.componentsSeparatedByString("\n") {
55+
lineNumber++
56+
var columnNumber = 0
57+
for column in line.characters {
58+
characterPosition++
59+
columnNumber++
60+
if characterPosition == position {
61+
return (String(column), lineNumber, columnNumber )
62+
}
63+
}
64+
characterPosition++
65+
if characterPosition == position {
66+
return ("\n", lineNumber, columnNumber+1 )
67+
}
4768
}
48-
49-
return true
69+
return ("", 0,0)
5070
}
71+
5172
}
5273

0 commit comments

Comments
 (0)