aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--SharedSupport/Default Bundles/Prettify JSON.spBundle/command.plist48
-rw-r--r--SharedSupport/Default Bundles/Serialize JSON.spBundle/command.plist54
-rw-r--r--SharedSupport/Default Bundles/Toggle JSON Format.spBundle/command.plist68
-rw-r--r--Source/SPTextViewAdditions.m5
4 files changed, 71 insertions, 104 deletions
diff --git a/SharedSupport/Default Bundles/Prettify JSON.spBundle/command.plist b/SharedSupport/Default Bundles/Prettify JSON.spBundle/command.plist
deleted file mode 100644
index 508394a9..00000000
--- a/SharedSupport/Default Bundles/Prettify JSON.spBundle/command.plist
+++ /dev/null
@@ -1,48 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
-<plist version="1.0">
-<dict>
- <key>author</key>
- <string>Hans-Jörg Bibiko</string>
- <key>category</key>
- <string>Format</string>
- <key>command</key>
- <string>cat | python -mjson.tool
-
-# if there's a need to preserve Unicode characters use this line of code:
-# cat | python -mjson.tool | perl -Xpe 'binmode STDIN,":utf8";binmode STDOUT,":utf8";s/\\u([0-9A-F]{4})/chr(hex($1))/ieg'</string>
- <key>contact</key>
- <string>znvy@ovovxb.qr</string>
- <key>description</key>
- <string>Take the selection or the entire content of the current text input field, interpret it as JSON, and replace the JSON data human-readable. Unicode characters will be converted to \u.... sequences. If you want to preserve these Unicode characters you can uncomment the last code line.
-
-Version 1.0</string>
- <key>input</key>
- <string>selectedtext</string>
- <key>input_fallback</key>
- <string>entirecontent</string>
- <key>internalKeyEquivalent</key>
- <dict>
- <key>characters</key>
- <string>J</string>
- <key>keyCode</key>
- <integer>38</integer>
- <key>modifierFlags</key>
- <integer>1572864</integer>
- </dict>
- <key>keyEquivalent</key>
- <string>~@j</string>
- <key>name</key>
- <string>Prettify JSON</string>
- <key>output</key>
- <string>replaceselection</string>
- <key>scope</key>
- <string>inputfield</string>
- <key>tooltip</key>
- <string>Pretty print JSON data</string>
- <key>uuid</key>
- <string>40BC811D-4BAB-41EA-A096-9A202B646619</string>
- <key>isDefaultBundle</key>
- <true/>
-</dict>
-</plist>
diff --git a/SharedSupport/Default Bundles/Serialize JSON.spBundle/command.plist b/SharedSupport/Default Bundles/Serialize JSON.spBundle/command.plist
deleted file mode 100644
index a47bf148..00000000
--- a/SharedSupport/Default Bundles/Serialize JSON.spBundle/command.plist
+++ /dev/null
@@ -1,54 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
-<plist version="1.0">
-<dict>
- <key>author</key>
- <string>Hans-Jörg Bibiko</string>
- <key>category</key>
- <string>Format</string>
- <key>command</key>
- <string>php -r '
-$jsonData = "";
-$inputStream = fopen("php://stdin", "r");
-while($d = fgets($inputStream)) { $jsonData .= $d; }
-print json_encode(json_decode($jsonData));
-'
-
-# if there's a need to preserve Unicode characters pipe the output
-# of the the PHP script to the following Perl code:
-# perl -Xpe 'binmode STDIN,":utf8";binmode STDOUT,":utf8";s/\\u([0-9A-F]{4})/chr(hex($1))/ieg'</string>
- <key>contact</key>
- <string>znvy@ovovxb.qr</string>
- <key>description</key>
- <string>Take the selection or the entire content of the current text input field and serialize the JSON data. Unicode characters will be converted to \u.... sequences. If you want to preserve these Unicode characters you can uncomment the last code line.
-
-Version 1.0</string>
- <key>input</key>
- <string>selectedtext</string>
- <key>input_fallback</key>
- <string>entirecontent</string>
- <key>internalKeyEquivalent</key>
- <dict>
- <key>characters</key>
- <string>J</string>
- <key>keyCode</key>
- <integer>38</integer>
- <key>modifierFlags</key>
- <integer>1572864</integer>
- </dict>
- <key>keyEquivalent</key>
- <string>~@j</string>
- <key>name</key>
- <string>Serialize JSON</string>
- <key>output</key>
- <string>replaceselection</string>
- <key>scope</key>
- <string>inputfield</string>
- <key>tooltip</key>
- <string>Serialize JSON data</string>
- <key>uuid</key>
- <string>D07AF7FA-CFD1-41A9-8786-FFF50FE8737A</string>
- <key>isDefaultBundle</key>
- <true/>
-</dict>
-</plist>
diff --git a/SharedSupport/Default Bundles/Toggle JSON Format.spBundle/command.plist b/SharedSupport/Default Bundles/Toggle JSON Format.spBundle/command.plist
new file mode 100644
index 00000000..6476d86b
--- /dev/null
+++ b/SharedSupport/Default Bundles/Toggle JSON Format.spBundle/command.plist
@@ -0,0 +1,68 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>author</key>
+ <string>Hans-Jörg Bibiko</string>
+ <key>category</key>
+ <string>Format</string>
+ <key>command</key>
+ <string>DATA=$(cat)
+
+FORMAT=$(echo "$DATA" | head -n 1 | perl -e '$l=&lt;&gt;;print "1" if($l=~m/^\s*\{\s*$/)')
+
+# if FORMAT == 1 then serialize JSON data otherwise pretty print them
+if [ "$FORMAT" == 1 ]; then
+
+ DATA=$(echo "$DATA" | php -r '
+ $jsonData = "";
+ $inputStream = fopen("php://stdin", "r");
+ while($d = fgets($inputStream)) { $jsonData .= $d; }
+ print json_encode(json_decode($jsonData));
+ ')
+
+else
+
+ DATA=$(echo "$DATA" | python -mjson.tool)
+
+fi
+
+# if there's a need to preserve Unicode characters remove the first to characters of the following line
+# DATA=$(echo "$DATA" | perl -Xpe 'binmode STDIN,":utf8";binmode STDOUT,":utf8";s/\\u([0-9A-F]{4})/chr(hex($1))/ieg')
+
+echo "$DATA"</string>
+ <key>contact</key>
+ <string>znvy@ovovxb.qr</string>
+ <key>description</key>
+ <string>If the first line of the selection or the entire content of the current text input field only contains a "{" then serialize the JSON data otherwise the JSON data will be pretty printed. If there's a need to preserve the Unicode characters you can uncomment the line 22.
+
+Version 1.0</string>
+ <key>input</key>
+ <string>selectedtext</string>
+ <key>input_fallback</key>
+ <string>entirecontent</string>
+ <key>internalKeyEquivalent</key>
+ <dict>
+ <key>characters</key>
+ <string>J</string>
+ <key>keyCode</key>
+ <integer>38</integer>
+ <key>modifierFlags</key>
+ <integer>1572864</integer>
+ </dict>
+ <key>keyEquivalent</key>
+ <string>~@j</string>
+ <key>name</key>
+ <string>Toggle JSON Format</string>
+ <key>output</key>
+ <string>replaceselection</string>
+ <key>scope</key>
+ <string>inputfield</string>
+ <key>tooltip</key>
+ <string>Serialize or pretty print JSON data</string>
+ <key>uuid</key>
+ <string>87FD8A4F-90AA-4020-9E0B-8CDD05764D08</string>
+ <key>isDefaultBundle</key>
+ <true/>
+</dict>
+</plist>
diff --git a/Source/SPTextViewAdditions.m b/Source/SPTextViewAdditions.m
index 47263311..750527f1 100644
--- a/Source/SPTextViewAdditions.m
+++ b/Source/SPTextViewAdditions.m
@@ -742,8 +742,9 @@
}
else if([action isEqualToString:SPBundleOutputActionReplaceSelection]) {
- [self shouldChangeTextInRange:replaceRange replacementString:output];
- [self replaceCharactersInRange:replaceRange withString:output];
+ NSRange safeRange = NSIntersectionRange(replaceRange, NSMakeRange(0, [[self string] length]));
+ [self shouldChangeTextInRange:safeRange replacementString:output];
+ [self replaceCharactersInRange:safeRange withString:output];
}
} else {