Skip to content

Commit

Permalink
Version 0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
potterhsu committed May 3, 2017
1 parent 70b5fcc commit b0a1db4
Show file tree
Hide file tree
Showing 13 changed files with 290 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions JsonViewer.iml
@@ -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>
33 changes: 33 additions & 0 deletions resources/META-INF/plugin.xml
@@ -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>
45 changes: 45 additions & 0 deletions src/JsonFormatter.java
@@ -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(' ');
}
}

}
27 changes: 27 additions & 0 deletions src/JsonFormatterTest.groovy
@@ -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);
}

}
34 changes: 34 additions & 0 deletions src/JsonViewerToolWindow.form
@@ -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>
35 changes: 35 additions & 0 deletions src/JsonViewerToolWindow.java
@@ -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);
}
}

0 comments on commit b0a1db4

Please sign in to comment.