Skip to content

Commit

Permalink
Merge branch 'feature/xml-format' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhihang committed Aug 21, 2019
2 parents 1db6a99 + e4ad982 commit a81e40e
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 73 deletions.
19 changes: 10 additions & 9 deletions README.md
Expand Up @@ -9,28 +9,28 @@ Toolkit

一个小工具包, 暂时还有很多功能需要扩展.

Feature
-------
特征
----
- Mybatis: 通过侧栏箭头在 MyBatis XML文件和 Mapper文件之间相互跳转
- Json
- JavaBean复制为Json字符串
- Json字符串格式化


Install
-------
安装
----
- **在线安装:**
- `File` -> `Setting` -> `Plugins` -> 搜索 `Toolkit`

- **手动安装:**
- [下载插件](https://github.com/liuzhihangs/toolkit/releases) -> `File` -> `Setting` -> `Plugins` -> `Install Plugin from Disk...`

Usage
-----
使用
----
- 右键菜单选择 `Tookit`

Update
------
更新
----
## [v1.0.1](https://github.com/liuzhihangs/toolkit/releases/tag/v1.0.1)(2019-5-06)

- 新增在JavaBean中复制为Json字符串的功能
Expand All @@ -40,7 +40,8 @@ Update

[查看更多历史更新记录](./doc/ChangeNotes.md)

## Thanks
感谢
----

##### MyBatis:
 mybatis support: [https://github.com/zhaoqin102/mybatis-support](https://github.com/zhaoqin102/mybatis-support)
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.liuzhihang.toolkit'
version '1.0.2'
version '1.0.3'

sourceCompatibility = 1.8

Expand All @@ -17,6 +17,7 @@ repositories {

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.dom4j', name: 'dom4j', version: '2.1.1'
// compile group: 'com.alibaba', name: 'fastjson', version: '1.2.58'

}
Expand Down
12 changes: 12 additions & 0 deletions doc/ChangeNotes.md
@@ -1,5 +1,17 @@
# ChangeNotes


## [v1.0.3](https://github.com/liuzhihangs/toolkit/releases/tag/v1.0.3) (2019-08-21)

- 修改问中文描述
- 移除JavaBean中复制为Json的注释字段(@comment)
- 使用dom4j, 新增xml格式化, 但是会导致包插件包过大

## [v1.0.2](https://github.com/liuzhihangs/toolkit/releases/tag/v1.0.2) (2019-05-09)

- 新增在JavaBean中复制为Json功能


## [v1.0.1](https://github.com/liuzhihangs/toolkit/releases/tag/v1.0.1) (2019-05-06)

- 新增在JavaBean中复制为Json字符串的功能
Expand Down
8 changes: 4 additions & 4 deletions parts/changeNotes.html
@@ -1,9 +1,9 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<ul>
<li>1.0.2
<li>1.0.3
<ol>
<li>JSON: Added "Json Format" function</li>
<li>JSON: Modify shortcut for "Copy As Json"</li>
<li>JSON: Optimize indentation format</li>
<li>XML: 新增XML格式化, 使用dom4j</li>
<li>支持创建多个窗口</li>
</ol>
</li>

Expand Down
30 changes: 13 additions & 17 deletions parts/pluginDescription.html
@@ -1,3 +1,4 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<h1>IntelliJ Lombok plugin</h1>
<br/>
<a href="https://github.com/liuzhihangs/toolkit">GitHub</a> |
Expand All @@ -13,24 +14,19 @@ <h1>IntelliJ Lombok plugin</h1>

<ul>
<li>MyBatis
<ol>
<li>Mapper file and XML file jump to each other by the sidebar icon</li>
</ol>
<ul>
<li>通过侧栏的icon实现 Mapper 和 Xml 文件之间跳转</li>
</ul>
</li>
<li>JSON
<ol>
<li>Copy As Json
<ul>
<li>Description: Right-click to copy json in JavaBean</li>
<li>Usage: In JavaBean -> Click the right mouse button -> "Toolkit" -> "Copy As Json"</li>
</ul>
</li>
<li>Json Format
<ul>
<li>Description: Json string formatting for easy reading</li>
<li>Usage: Click the right mouse button -> "Toolkit" -> "Json Format"</li>
</ul>
</li>
</ol>
<ul>
<li>将JavaBean复制为Json字符串</li>
<li>Json字符串格式化</li>
</ul>
</li>
<li>Xml
<ul>
<li>Xml格式化</li>
</ul>
</li>
</ul>
Expand Up @@ -96,16 +96,16 @@ public void actionPerformed(@NotNull AnActionEvent e) {
public static Map getFields(PsiClass psiClass) {

Map<String, Object> fieldMap = new LinkedHashMap<>();
Map<String, Object> commentFieldMap = new LinkedHashMap<>();
// Map<String, Object> commentFieldMap = new LinkedHashMap<>();

if (psiClass != null && psiClass.getClassKind() == JvmClassKind.CLASS) {
for (PsiField field : psiClass.getAllFields()) {
PsiType type = field.getType();
String name = field.getName();
if (field.getDocComment() != null && StringUtils.isNotBlank(field.getDocComment().getText())) {
String fieldComment = field.getDocComment().getText();
commentFieldMap.put(name, CommentUtils.removeSymbol(fieldComment));
}
// if (field.getDocComment() != null && StringUtils.isNotBlank(field.getDocComment().getText())) {
// String fieldComment = field.getDocComment().getText();
// commentFieldMap.put(name, CommentUtils.removeSymbol(fieldComment));
// }
// 判断注解 javax.annotation.Resource org.springframework.beans.factory.annotation.Autowired
PsiAnnotation[] annotations = field.getAnnotations();
if (annotations.length > 0 && containsAnnotation(annotations)) {
Expand Down Expand Up @@ -157,9 +157,10 @@ public static Map getFields(PsiClass psiClass) {
}
}
}
if (commentFieldMap.size() > 0) {
fieldMap.put("@comment", commentFieldMap);
}
// json 串中的注释字段 暂时不添加
// if (commentFieldMap.size() > 0) {
// fieldMap.put("@comment", commentFieldMap);
// }
}
return fieldMap;
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/liuzhihang/toolkit/action/XmlFormatAction.java
@@ -0,0 +1,30 @@
package com.liuzhihang.toolkit.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.psi.PsiFile;
import com.liuzhihang.toolkit.ui.XmlFormat;

/**
* @author liuzhihang
* @date 2019/5/9 18:07
*/
public class XmlFormatAction extends AnAction {

@Override
public void actionPerformed(AnActionEvent e) {

Project project = e.getData(PlatformDataKeys.PROJECT);
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
Editor editor = e.getData(CommonDataKeys.EDITOR);

DialogWrapper dialog = new XmlFormat(project);
dialog.show();

}
}
2 changes: 1 addition & 1 deletion src/main/java/com/liuzhihang/toolkit/ui/JsonFormat.form
Expand Up @@ -28,7 +28,7 @@
<properties/>
<border type="none"/>
<children>
<component id="e81b1" class="javax.swing.JTextPane" binding="textPanel">
<component id="e81b1" class="javax.swing.JTextPane" binding="textPane">
<constraints/>
<properties/>
</component>
Expand Down
96 changes: 63 additions & 33 deletions src/main/java/com/liuzhihang/toolkit/ui/JsonFormat.java
Expand Up @@ -20,14 +20,14 @@ public class JsonFormat extends DialogWrapper {

private JPanel rootJPanel;
private JButton formatButton;
private JTextPane textPanel;
private JTextPane textPane;
private JButton removeSpecialCharsButton;
private JLabel errorJLabel;
private JButton cancelButton;
private JButton nextButton;

public JsonFormat(@Nullable Project project) {
super(project);
super(project, true, IdeModalityType.MODELESS);
init();
setTitle("JsonFormat");
getRootPane().setDefaultButton(nextButton);
Expand All @@ -42,42 +42,72 @@ public JsonFormat(@Nullable Project project) {
private void startListener() {

// 监听formatButton按钮
formatButton.addActionListener(actionEvent -> {

try {
String text = textPanel.getText().trim();

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jsonParser = new JsonParser();
if (text.startsWith("{") && text.endsWith("}")) {

JsonObject jsonObject = jsonParser.parse(text).getAsJsonObject();
String writer = GsonFormatUtil.gsonFormat(gson, jsonObject);
textPanel.setText(writer);
errorJLabel.setText("");
} else if (text.startsWith("[") && text.endsWith("]")) {

JsonArray jsonArray = jsonParser.parse(text).getAsJsonArray();
String writer = GsonFormatUtil.gsonFormat(gson, jsonArray);
textPanel.setText(writer);
errorJLabel.setText("");
} else {
errorJLabel.setForeground(JBColor.RED);
errorJLabel.setText("Please enter the correct Json string!");
}
} catch (Exception e) {
errorJLabel.setForeground(JBColor.RED);
errorJLabel.setText("JsonFormat Failed!");
}

});
formatButton.addActionListener(actionEvent -> formatAction());
// 去除转义符号
removeSpecialCharsButton.addActionListener(actionEvent -> {
String text = textPanel.getText();
String text = textPane.getText();
String resultText = text.replace("\\", "");
textPanel.setText(resultText);
textPane.setText(resultText);
});

cancelButton.addActionListener(actionEvent -> dispose());

nextButton.addActionListener(actionEvent -> nextAction());
}

/**
* next 按钮相关操作
*/
private void nextAction() {
try {
String text = textPane.getText().trim();

JsonParser jsonParser = new JsonParser();
if (text.startsWith("{") && text.endsWith("}")) {
JsonObject jsonObject = jsonParser.parse(text).getAsJsonObject();


} else if (text.startsWith("[") && text.endsWith("]")) {
errorJLabel.setText("JsonArray is not supported");
} else {
errorJLabel.setForeground(JBColor.RED);
errorJLabel.setText("Please enter the correct Json string!");
}
} catch (Exception e) {
errorJLabel.setForeground(JBColor.RED);
errorJLabel.setText("JsonFormat Failed!");
}
}

/**
* json format 相关操作
*/
private void formatAction() {
try {
String text = textPane.getText().trim();

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jsonParser = new JsonParser();
if (text.startsWith("{") && text.endsWith("}")) {

JsonObject jsonObject = jsonParser.parse(text).getAsJsonObject();
String writer = GsonFormatUtil.gsonFormat(gson, jsonObject);
textPane.setText(writer);
errorJLabel.setText("");
} else if (text.startsWith("[") && text.endsWith("]")) {

JsonArray jsonArray = jsonParser.parse(text).getAsJsonArray();
String writer = GsonFormatUtil.gsonFormat(gson, jsonArray);
textPane.setText(writer);
errorJLabel.setText("");
} else {
errorJLabel.setForeground(JBColor.RED);
errorJLabel.setText("Please enter the correct Json string!");
}
} catch (Exception e) {
errorJLabel.setForeground(JBColor.RED);
errorJLabel.setText("JsonFormat Failed!");
}
}


Expand Down

0 comments on commit a81e40e

Please sign in to comment.