Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="PLUGIN_MODULE" version="4"> | ||
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/resources/META-INF/plugin.xml" /> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<idea-plugin version="2"> | ||
<id>com.your.company.unique.plugin.id</id> | ||
<name>JSON Viewer</name> | ||
<version>0.1</version> | ||
<vendor email="potterhsu0818@gmail.com" url="https://github.com/potterhsu">Potter Hsu</vendor> | ||
|
||
<description><![CDATA[ | ||
JetBrains IDE plugin for viewing formatted JSON text on tool window. | ||
]]></description> | ||
|
||
<change-notes><![CDATA[ | ||
Version 0.1 | ||
]]> | ||
</change-notes> | ||
|
||
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description --> | ||
<idea-version since-build="141.0"/> | ||
|
||
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html | ||
on how to target different products --> | ||
<!-- uncomment to enable plugin in all products | ||
<depends>com.intellij.modules.lang</depends> | ||
--> | ||
|
||
<extensions defaultExtensionNs="com.intellij"> | ||
<toolWindow id="JsonViewer" anchor="right" factoryClass="JsonViewerToolWindow" /> | ||
</extensions> | ||
|
||
<actions> | ||
<!-- Add your actions here --> | ||
</actions> | ||
|
||
</idea-plugin> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Created by Potter Hsu on 5/3/17. | ||
*/ | ||
public class JsonFormatter { | ||
|
||
private static final int INDENT_SPACE = 2; | ||
|
||
public String format(String text) { | ||
StringBuilder formattedText = new StringBuilder(); | ||
|
||
int indentCount = 0; | ||
|
||
for (char c : text.replaceAll("(\\s|\\n)", "").toCharArray()) { | ||
if (c == '[' || c == '{') { | ||
formattedText.append(c); | ||
formattedText.append('\n'); | ||
indentCount += 1; | ||
appendIndent(formattedText, indentCount); | ||
} | ||
else if (c == ',') { | ||
formattedText.append(c); | ||
formattedText.append('\n'); | ||
appendIndent(formattedText, indentCount); | ||
} | ||
else if (c == ']' || c == '}') { | ||
formattedText.append('\n'); | ||
indentCount -= 1; | ||
appendIndent(formattedText, indentCount); | ||
formattedText.append(c); | ||
} | ||
else { | ||
formattedText.append(c); | ||
} | ||
} | ||
|
||
return formattedText.toString(); | ||
} | ||
|
||
private void appendIndent(StringBuilder text, int indentCount) { | ||
for (int i = 0; i < indentCount * INDENT_SPACE; ++i) { | ||
text.append(' '); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Created by Potter Hsu on 5/3/17. | ||
*/ | ||
class JsonFormatterTest extends GroovyTestCase { | ||
|
||
private JsonFormatter jsonFormatter; | ||
|
||
void setUp() { | ||
super.setUp() | ||
jsonFormatter = new JsonFormatter(); | ||
} | ||
|
||
void tearDown() { | ||
|
||
} | ||
|
||
void testFormat() { | ||
String text = "{\"version\":\"1.0\",\"data\":{\"sampleArray\":[\"string value\",5,{\"name\":\"sub object\"}]}}"; | ||
|
||
String formattedText = jsonFormatter.format(text); | ||
println(formattedText); | ||
|
||
String formattedFormattedText = jsonFormatter.format(formattedText); | ||
println(formattedFormattedText); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="JsonViewerToolWindow"> | ||
<grid id="27dc6" binding="toolWindowContent" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="0" left="0" bottom="0" right="0"/> | ||
<constraints> | ||
<xy x="20" y="20" width="500" height="400"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<component id="e0562" class="javax.swing.JButton" binding="buttonFormat"> | ||
<constraints> | ||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties> | ||
<text value="Format"/> | ||
</properties> | ||
</component> | ||
<scrollpane id="4bec0"> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<component id="2e04" class="javax.swing.JTextArea" binding="textArea"> | ||
<constraints/> | ||
<properties/> | ||
</component> | ||
</children> | ||
</scrollpane> | ||
</children> | ||
</grid> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.wm.ToolWindow; | ||
import com.intellij.openapi.wm.ToolWindowFactory; | ||
import com.intellij.ui.content.Content; | ||
import com.intellij.ui.content.ContentFactory; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.swing.*; | ||
|
||
/** | ||
* Created by Potter Hsu on 5/3/17. | ||
*/ | ||
public class JsonViewerToolWindow implements ToolWindowFactory { | ||
|
||
private JPanel toolWindowContent; | ||
private JButton buttonFormat; | ||
private JTextArea textArea; | ||
|
||
private JsonFormatter jsonFormatter = new JsonFormatter(); | ||
|
||
public JsonViewerToolWindow() { | ||
buttonFormat.addActionListener(e -> { | ||
String text = textArea.getText(); | ||
String formattedText = jsonFormatter.format(text); | ||
textArea.setText(formattedText); | ||
}); | ||
} | ||
|
||
@Override | ||
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) { | ||
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance(); | ||
Content content = contentFactory.createContent(toolWindowContent, "", false); | ||
toolWindow.getContentManager().addContent(content); | ||
} | ||
} |