File tree Expand file tree Collapse file tree 5 files changed +216
-0
lines changed
src/com/zuicoding/platform/plugins Expand file tree Collapse file tree 5 files changed +216
-0
lines changed Original file line number Diff line number Diff line change 1+ <idea-plugin >
2+ <id >com.your.company.unique.plugin.id</id >
3+ <name >Plugin display name here</name >
4+ <version >1.0</version >
5+ <vendor email =" stephen.linicoding@gmail.com" url =" http://www.zuicoding.com" >醉●coding</vendor >
6+
7+ <description ><![CDATA[
8+ Enter short description for your plugin here.<br>
9+ <em>most HTML tags may be used</em>
10+ ]]> </description >
11+
12+ <change-notes ><![CDATA[
13+ Add change notes here.<br>
14+ <em>most HTML tags may be used</em>
15+ ]]>
16+ </change-notes >
17+
18+ <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
19+ <idea-version since-build =" 145.0" />
20+
21+ <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
22+ on how to target different products -->
23+ <!-- uncomment to enable plugin in all products
24+ <depends>com.intellij.modules.lang</depends>
25+ -->
26+
27+ <extensions defaultExtensionNs =" com.intellij" >
28+ <!-- Add your extensions here -->
29+ </extensions >
30+
31+ <actions >
32+ <!-- Add your actions here -->
33+ <action id =" JSONFormater" class =" com.zuicoding.platform.plugins.jsonformater.JSONFormater" text =" JSON 格式化"
34+ description =" JSON 格式化" >
35+ <add-to-group group-id =" ToolsMenu" anchor =" first" />
36+ </action >
37+ </actions >
38+ </idea-plugin >
Original file line number Diff line number Diff line change 1+ package com .zuicoding .platform .plugins .jsonformater ;
2+
3+ import com .intellij .openapi .actionSystem .AnAction ;
4+ import com .intellij .openapi .actionSystem .AnActionEvent ;
5+ import com .intellij .openapi .ui .DialogWrapper ;
6+ import com .zuicoding .platform .plugins .ui .JSONViewer ;
7+
8+ /**
9+ * Created by <a href="mailto:stephen.linicoding@gmail.com">Stephen.lin</a> on 2017/12/4
10+ * <p>
11+ * <p></p>
12+ */
13+ public class JSONFormater extends AnAction {
14+
15+ @ Override
16+ public void actionPerformed (AnActionEvent e ) {
17+
18+ DialogWrapper dialog = new JSONViewer ();
19+ dialog .show ();
20+ dialog .setSize (800 ,600 );
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package com .zuicoding .platform .plugins .jsonformater ;
2+
3+ /**
4+ * Created by <a href="mailto:stephen.linicoding@gmail.com">Stephen.lin</a> on 2017/12/7
5+ * <p>
6+ * <p></p>
7+ */
8+ public class JsonUtils {
9+
10+ private JsonUtils (){}
11+ private static void addIndentBlank (StringBuilder sb , int indent ) {
12+ for (int i = 0 ; i < indent ; i ++) {
13+ sb .append ('\t' );
14+ }
15+ }
16+ public static String formatJson (String json ){
17+ if (json == null || json .trim ().isEmpty ())return "" ;
18+ StringBuilder sb = new StringBuilder ();
19+ char last = '\0' ;
20+ char current = '\0' ;
21+ int indent = 0 ;
22+ boolean isInQuotationMarks = false ;
23+ for (int i = 0 ; i < json .length (); i ++) {
24+ last = current ;
25+ current = json .charAt (i );
26+ switch (current ) {
27+ case '"' :
28+ if (last != '\\' ){
29+ isInQuotationMarks = !isInQuotationMarks ;
30+ }
31+ sb .append (current );
32+ break ;
33+ case '{' :
34+ case '[' :
35+ sb .append (current );
36+ if (!isInQuotationMarks ) {
37+ sb .append ('\n' );
38+ indent ++;
39+ addIndentBlank (sb , indent );
40+ }
41+ break ;
42+ case '}' :
43+ case ']' :
44+ if (!isInQuotationMarks ) {
45+ sb .append ('\n' );
46+ indent --;
47+ addIndentBlank (sb , indent );
48+ }
49+ sb .append (current );
50+ break ;
51+ case ',' :
52+ sb .append (current );
53+ if (last != '\\' && !isInQuotationMarks ) {
54+ sb .append ('\n' );
55+ addIndentBlank (sb , indent );
56+ }
57+ break ;
58+ default :
59+ sb .append (current );
60+ }
61+ }
62+
63+ return sb .toString ();
64+ }
65+
66+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <form xmlns =" http://www.intellij.com/uidesigner/form/" version =" 1" bind-to-class =" com.zuicoding.platform.plugins.ui.JSONViewer" >
3+ <grid id =" 27dc6" binding =" container" layout-manager =" GridLayoutManager" row-count =" 1" column-count =" 1" same-size-horizontally =" false" same-size-vertically =" false" hgap =" -1" vgap =" -1" >
4+ <margin top =" 0" left =" 0" bottom =" 0" right =" 0" />
5+ <constraints >
6+ <xy x =" 20" y =" 20" width =" 800" height =" 600" />
7+ </constraints >
8+ <properties >
9+ <minimumSize width =" 800" height =" 600" />
10+ </properties >
11+ <border type =" none" />
12+ <children >
13+ <scrollpane id =" dbe2d" >
14+ <constraints >
15+ <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" />
16+ </constraints >
17+ <properties />
18+ <border type =" none" />
19+ <children >
20+ <component id =" ad571" class =" javax.swing.JTextArea" binding =" jsonText" >
21+ <constraints />
22+ <properties />
23+ </component >
24+ </children >
25+ </scrollpane >
26+ </children >
27+ </grid >
28+ <inspectionSuppressions >
29+ <suppress inspection =" NoLabelFor" id =" ad571" />
30+ </inspectionSuppressions >
31+ </form >
Original file line number Diff line number Diff line change 1+ package com .zuicoding .platform .plugins .ui ;
2+
3+ import com .intellij .openapi .ui .DialogWrapper ;
4+ import com .zuicoding .platform .plugins .jsonformater .JsonUtils ;
5+ import org .jetbrains .annotations .NotNull ;
6+ import org .jetbrains .annotations .Nullable ;
7+
8+ import javax .swing .*;
9+ import java .awt .event .ActionEvent ;
10+
11+ /**
12+ * Created by <a href="mailto:stephen.linicoding@gmail.com">Stephen.lin</a> on 2017/12/7
13+ * <p>
14+ * <p></p>
15+ */
16+ public class JSONViewer extends DialogWrapper {
17+ private JPanel container ;
18+ private JTextArea jsonText ;
19+ public JSONViewer () {
20+ super (false );
21+ init ();
22+ setTitle ("JSON 格式化" );
23+ setOKButtonText ("格式化" );
24+
25+ }
26+
27+ public JPanel getContainer () {
28+ return container ;
29+ }
30+
31+ @ NotNull
32+ @ Override
33+ protected Action [] createActions () {
34+ Action [] actions = super .createActions ();
35+ actions [0 ] = new DialogWrapperAction ("格式化" ) {
36+
37+ @ Override
38+ protected void doAction (ActionEvent actionEvent ) {
39+ String text = jsonText .getText ();
40+ if (text == null || text .trim ().isEmpty ()) return ;
41+ System .err .println (text );
42+ jsonText .setText (JsonUtils .formatJson (text ));
43+ }
44+ };
45+
46+ return actions ;
47+ }
48+
49+
50+
51+
52+ @ Nullable
53+ @ Override
54+ protected JComponent createCenterPanel () {
55+ return container ;
56+ }
57+
58+
59+ }
You can’t perform that action at this time.
0 commit comments