+// plugin PrettyPrint JSON
+// version 0.0
+// release April 2016
+// author Plugin Fan
+// license GPL v 3.0
+
+// Formats the current selection as prettified JSON :
+// - Alt-P - PrettyPrint JSON
+//
+// Not really related to the source code, more a helper to debug AJAX communication.
+
+
+import com.intellij.openapi.actionSystem.AnActionEvent
+import com.intellij.notification.NotificationType
+
+import groovy.json.*
+
+import static liveplugin.PluginUtil.*
+
+registerAction('PrettyPrint JSON', 'alt P') { AnActionEvent event ->
+ runDocumentWriteAction(event.project) {
+ currentEditorIn(event.project).with {
+ if (! selectionModel.hasSelection())
+ return
+
+ def text = selectionModel.getSelectedText().trim()
+ if (! text)
+ return
+
+ try {
+ text = JsonOutput.prettyPrint(text)
+ } catch (JsonException e) {
+ return show(e.message, '', NotificationType.ERROR)
+ }
+
+ document.replaceString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), text)
+ }
+ }
+}