Skip to content

Commit 6f31a95

Browse files
committed
feat: support sql statement syntax highlighting and resolving some bugs
1 parent b000e3d commit 6f31a95

File tree

9 files changed

+175
-233
lines changed

9 files changed

+175
-233
lines changed

build.gradle

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'org.linyimin'
7-
version '1.0.3'
7+
version '1.0.4'
88

99
repositories {
1010
maven { url 'https://maven.aliyun.com/repository/public/' }
@@ -16,8 +16,9 @@ dependencies {
1616
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
1717
implementation 'com.alibaba:fastjson:2.0.19'
1818
implementation 'mysql:mysql-connector-java:8.0.30'
19-
implementation 'com.github.vertical-blank:sql-formatter:2.0.3'
2019
implementation group: 'ognl', name: 'ognl', version: '3.0.4'
20+
implementation group: 'com.alibaba', name: 'druid', version: '1.2.15'
21+
implementation group: 'com.fifesoft', name: 'rsyntaxtextarea', version: '3.1.1'
2122
}
2223

2324
// See https://github.com/JetBrains/gradle-intellij-plugin/
@@ -28,6 +29,13 @@ intellij {
2829
}
2930
patchPluginXml {
3031
changeNotes = """
32+
<h4>1.0.4</h4>
33+
<ul>
34+
<li>SQL statement syntax highlighting and resolving some bugs</li>
35+
</ul>
36+
<ul>
37+
<li>SQL语句语法高亮和解决一些小bug</li>
38+
</ul>
3139
<h4>1.0.3</h4>
3240
<ul>
3341
<li>Refactoring, performance optimization, to avoid CPU occupancy</li>

src/main/java/io/github/linyimin/plugin/mybatis/scripting/tags/DynamicSqlSource.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package io.github.linyimin.plugin.mybatis.scripting.tags;
22

3-
import com.github.vertical_blank.sqlformatter.SqlFormatter;
4-
import com.github.vertical_blank.sqlformatter.core.FormatConfig;
5-
import com.github.vertical_blank.sqlformatter.languages.Dialect;
3+
import com.alibaba.druid.sql.SQLUtils;
4+
import com.alibaba.druid.util.JdbcConstants;
5+
import com.alibaba.fastjson.JSONArray;
6+
import com.alibaba.fastjson.JSONObject;
67
import io.github.linyimin.plugin.mybatis.mapping.SqlSource;
78

89
import java.util.ArrayList;
@@ -33,9 +34,7 @@ public String getSql(Object parameterObject) {
3334

3435
String sql = parameterize(context.getSql(), context);
3536

36-
FormatConfig formatConfig = FormatConfig.builder().uppercase(true).maxColumnLength(100).build();
37-
38-
return SqlFormatter.of(Dialect.MySql).format(sql, formatConfig);
37+
return SQLUtils.format(sql, JdbcConstants.MYSQL);
3938
}
4039

4140
private String parameterize(String preparedStatement, DynamicContext context) {
@@ -70,7 +69,9 @@ private String setParameter(String parameterizeSql, String param, DynamicContext
7069
return parameterizeSql;
7170
}
7271

73-
if (value.getClass() == String.class || value.getClass() == Character.class || value.getClass() == Date.class) {
72+
Class<?> clazz = value.getClass();
73+
74+
if (clazz == String.class || clazz == Character.class || clazz == Date.class || clazz == JSONObject.class || clazz == JSONArray.class) {
7475
value = "'" + value +"'";
7576
}
7677

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.github.linyimin.plugin.ui;
2+
3+
import com.intellij.ide.ui.UISettings;
4+
import com.intellij.openapi.ui.Messages;
5+
import com.intellij.ui.JBColor;
6+
import io.github.linyimin.plugin.constant.Constant;
7+
import org.fife.ui.rsyntaxtextarea.*;
8+
9+
import javax.swing.text.AttributeSet;
10+
import javax.swing.text.BadLocationException;
11+
import java.awt.*;
12+
13+
/**
14+
* @author banzhe
15+
* @date 2022/11/22 16:17
16+
**/
17+
public class CustomTextField {
18+
19+
public static RSyntaxTextArea createArea(String type) {
20+
21+
RSyntaxTextArea area = new RSyntaxTextArea();
22+
area.setDocument(new MaxLengthDocument(5000000));
23+
24+
if ("JSON".equalsIgnoreCase(type)) {
25+
area.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON);
26+
} else if ("SQL".equalsIgnoreCase(type)) {
27+
area.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
28+
}
29+
30+
SyntaxScheme scheme = area.getSyntaxScheme();
31+
32+
scheme.getStyle(Token.LITERAL_BOOLEAN).foreground = new JBColor(new Color(167, 29, 93), new Color(167, 29, 93));
33+
34+
scheme.getStyle(Token.LITERAL_NUMBER_DECIMAL_INT).foreground = new JBColor(new Color(0, 134, 179), new Color(0, 134, 179));
35+
36+
scheme.getStyle(Token.LITERAL_STRING_DOUBLE_QUOTE).foreground = new JBColor(new Color(24, 54, 145), new Color(24, 54, 145));
37+
scheme.getStyle(Token.VARIABLE).foreground = new JBColor(new Color(0, 134, 179), new Color(0, 134, 179));
38+
scheme.getStyle(Token.LITERAL_CHAR).foreground = new JBColor(new Color(99, 163, 92), new Color(99, 163, 92));
39+
40+
scheme.getStyle(Token.SEPARATOR).foreground = new JBColor(new Color(99, 163, 92), new Color(99, 163, 92));
41+
scheme.getStyle(Token.OPERATOR).foreground = new JBColor(new Color(99, 163, 92), new Color(99, 163, 92));
42+
43+
scheme.getStyle(Token.RESERVED_WORD).foreground = new JBColor(new Color(167, 29, 93), new Color(167, 29, 93));
44+
scheme.getStyle(Token.RESERVED_WORD_2).foreground = new JBColor(new Color(167, 29, 93), new Color(167, 29, 93));
45+
46+
area.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 13));
47+
48+
return area;
49+
}
50+
51+
public static class MaxLengthDocument extends RSyntaxDocument {
52+
int maxChars;
53+
54+
public MaxLengthDocument(int max) {
55+
super(SYNTAX_STYLE_NONE);
56+
maxChars = max;
57+
}
58+
59+
@Override
60+
public void insertString(int offset, String s, AttributeSet a) throws BadLocationException {
61+
try {
62+
if (getLength() + s.length() > maxChars) {
63+
Toolkit.getDefaultToolkit().beep();
64+
tipDia("内容过长,最大" + maxChars + "个字符!");
65+
return;
66+
}
67+
super.insertString(offset, s, a);
68+
} catch (Exception e) {
69+
System.out.println(e.getMessage());
70+
}
71+
}
72+
}
73+
74+
private static void tipDia(String msg) {
75+
Messages.showInfoMessage(msg, Constant.APPLICATION_NAME);
76+
}
77+
78+
}

src/main/java/io/github/linyimin/plugin/view/MybatisSqlViewerToolWindow.form renamed to src/main/java/io/github/linyimin/plugin/ui/MybatisSqlViewerToolWindow.form

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="io.github.linyimin.plugin.view.MybatisSqlViewerToolWindow">
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="io.github.linyimin.plugin.ui.MybatisSqlViewerToolWindow">
33
<grid id="27dc6" binding="root" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
@@ -17,42 +17,24 @@
1717
<title-color color="-16777216"/>
1818
</border>
1919
<children>
20-
<scrollpane id="935d5" binding="paramsScroll">
20+
<grid id="2f712" binding="paramsPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
21+
<margin top="0" left="0" bottom="0" right="0"/>
2122
<constraints>
2223
<tabbedpane title="params"/>
2324
</constraints>
2425
<properties/>
2526
<border type="none"/>
26-
<children>
27-
<grid id="453d" binding="params" custom-create="true" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
28-
<margin top="0" left="0" bottom="0" right="0"/>
29-
<constraints/>
30-
<properties/>
31-
<border type="none"/>
32-
<children/>
33-
</grid>
34-
</children>
35-
</scrollpane>
36-
<scrollpane id="84ccf" binding="sqlScroll">
27+
<children/>
28+
</grid>
29+
<grid id="36cf5" binding="sqlPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
30+
<margin top="0" left="0" bottom="0" right="0"/>
3731
<constraints>
3832
<tabbedpane title="sql"/>
3933
</constraints>
40-
<properties>
41-
<verifyInputWhenFocusTarget value="false"/>
42-
<verticalScrollBarPolicy value="20"/>
43-
<wheelScrollingEnabled value="true"/>
44-
</properties>
34+
<properties/>
4535
<border type="none"/>
46-
<children>
47-
<grid id="d9725" binding="sql" custom-create="true" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
48-
<margin top="0" left="0" bottom="0" right="0"/>
49-
<constraints/>
50-
<properties/>
51-
<border type="none"/>
52-
<children/>
53-
</grid>
54-
</children>
55-
</scrollpane>
36+
<children/>
37+
</grid>
5638
<scrollpane id="1709" binding="resultScroll">
5739
<constraints>
5840
<tabbedpane title="result"/>

src/main/java/io/github/linyimin/plugin/view/MybatisSqlViewerToolWindow.java renamed to src/main/java/io/github/linyimin/plugin/ui/MybatisSqlViewerToolWindow.java

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
package io.github.linyimin.plugin.view;
1+
package io.github.linyimin.plugin.ui;
22

3-
import com.intellij.json.JsonFileType;
4-
import com.intellij.json.JsonLanguage;
5-
import com.intellij.openapi.fileTypes.PlainTextFileType;
6-
import com.intellij.openapi.fileTypes.PlainTextLanguage;
73
import com.intellij.openapi.project.Project;
84
import com.intellij.openapi.ui.Messages;
95
import com.intellij.openapi.ui.SimpleToolWindowPanel;
106
import com.intellij.openapi.wm.ToolWindow;
7+
import com.intellij.util.ui.JBUI;
118
import io.github.linyimin.plugin.constant.Constant;
129
import io.github.linyimin.plugin.service.MybatisSqlStateComponent;
1310
import io.github.linyimin.plugin.service.SqlParamGenerateService;
1411
import io.github.linyimin.plugin.service.model.MybatisSqlConfiguration;
1512
import io.github.linyimin.plugin.utils.MybatisSqlUtils;
16-
import org.jetbrains.annotations.NotNull;
13+
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
14+
import org.fife.ui.rtextarea.RTextScrollPane;
1715

1816
import javax.swing.*;
17+
import javax.swing.border.EmptyBorder;
1918
import javax.swing.event.DocumentEvent;
2019
import javax.swing.event.DocumentListener;
20+
import java.awt.*;
2121
import java.sql.SQLException;
2222

2323

@@ -26,6 +26,7 @@
2626
* @date 2022/02/01 12:31 下午
2727
**/
2828
public class MybatisSqlViewerToolWindow extends SimpleToolWindowPanel {
29+
2930
private JTextField methodName;
3031
private JTabbedPane tabbedPane;
3132
private JTextField host;
@@ -37,11 +38,17 @@ public class MybatisSqlViewerToolWindow extends SimpleToolWindowPanel {
3738
private JButton connectionTestButton;
3839
private JPanel root;
3940
private JTextArea connectionInfoTextArea;
40-
private JPanel params;
41+
4142
private JTextArea result;
42-
private JPanel sql;
43-
private JScrollPane sqlScroll;
44-
private JScrollPane paramsScroll;
43+
44+
private final RSyntaxTextArea sqlText;
45+
private JPanel sqlPanel;
46+
private final RTextScrollPane sqlScroll;
47+
48+
private JPanel paramsPanel;
49+
private final RTextScrollPane paramsScroll;
50+
private final RSyntaxTextArea paramsText;
51+
4552
private JScrollPane resultScroll;
4653

4754
private final Project myProject;
@@ -61,9 +68,29 @@ public JPanel getContent() {
6168
}
6269

6370
public MybatisSqlViewerToolWindow(ToolWindow toolWindow, Project project) {
71+
6472
super(true, false);
6573
this.myProject = project;
74+
75+
paramsText = CustomTextField.createArea("json");
76+
sqlText = CustomTextField.createArea("sql");
77+
78+
sqlPanel.setLayout(new BorderLayout());
79+
80+
sqlScroll = new RTextScrollPane(sqlText);
81+
sqlScroll.setBorder(new EmptyBorder(JBUI.emptyInsets()));
82+
sqlPanel.add(sqlScroll);
83+
84+
paramsPanel.setLayout(new BorderLayout());
85+
86+
paramsScroll = new RTextScrollPane(paramsText);
87+
paramsScroll.setBorder(new EmptyBorder(JBUI.emptyInsets()));
88+
paramsPanel.add(paramsScroll);
89+
90+
setScrollUnitIncrement();
91+
6692
addComponentListener();
93+
6794
}
6895

6996

@@ -75,34 +102,43 @@ public void refresh(Project project) {
75102
assert config != null;
76103

77104
methodName.setText(config.getMethod());
78-
result.setText(config.getResult());
79-
((MyTextField) params).setText(config.getParams());
80105

81-
((MyTextField) sql).setText(config.getSql());
106+
paramsText.setText(config.getParams());
82107

83-
// 默认每次打开,都展示第一个tab
84-
tabbedPane.setSelectedIndex(0);
108+
sqlText.setText(config.getSql());
85109

86-
setScrollUnitIncrement();
110+
result.setText(config.getResult());
87111

88-
}
112+
// 默认每次打开,都展示第一个tab
113+
tabbedPane.setSelectedIndex(0);
89114

90-
private void createUIComponents() {
91-
params = new MyTextField(this.myProject, JsonLanguage.INSTANCE, JsonFileType.INSTANCE);
92-
sql = new MyTextField(this.myProject, PlainTextLanguage.INSTANCE, PlainTextFileType.INSTANCE);
93115
}
94116

95117
private void addComponentListener() {
96118
host.getDocument().addDocumentListener(new DatasourceChangeListener());
97119
port.getDocument().addDocumentListener(new DatasourceChangeListener());
98120
database.getDocument().addDocumentListener(new DatasourceChangeListener());
99121

100-
((MyTextField) params).addDocumentListener(new com.intellij.openapi.editor.event.DocumentListener() {
122+
paramsText.getDocument().addDocumentListener(new DocumentListener() {
123+
@Override
124+
public void insertUpdate(DocumentEvent e) {
125+
updateParams();
126+
}
127+
101128
@Override
102-
public void documentChanged(com.intellij.openapi.editor.event.@NotNull DocumentEvent event) {
129+
public void removeUpdate(DocumentEvent e) {
130+
updateParams();
131+
}
132+
133+
@Override
134+
public void changedUpdate(DocumentEvent e) {
135+
updateParams();
136+
}
137+
138+
private void updateParams() {
103139
MybatisSqlConfiguration config = myProject.getService(MybatisSqlStateComponent.class).getState();
104140
assert config != null;
105-
config.setParams(((MyTextField) params).getText());
141+
config.setParams(paramsText.getText());
106142
}
107143
});
108144

@@ -128,7 +164,7 @@ public void documentChanged(com.intellij.openapi.editor.event.@NotNull DocumentE
128164

129165
// 点击sql tab时生成sql
130166
if (selectedIndex == TabbedComponentType.sql.index) {
131-
((MyTextField) sql).setText("Loading...");
167+
sqlText.setText("Loading...");
132168
generateSql();
133169
}
134170

@@ -151,7 +187,7 @@ private void generateSql() {
151187
String sqlStr = generateService.generateSql(myProject, sqlConfig.getMethod(), sqlConfig.getParams());
152188
sqlConfig.setSql(sqlStr);
153189

154-
((MyTextField) sql).setText(sqlStr);
190+
sqlText.setText(sqlStr);
155191
} catch (Throwable e) {
156192
Messages.showInfoMessage("generate sql error. err: " + e.getMessage(), Constant.APPLICATION_NAME);
157193
}
@@ -163,7 +199,7 @@ private void executeSql() {
163199
String passwordText = String.valueOf(password.getPassword());
164200
String resultText;
165201
try {
166-
resultText = MybatisSqlUtils.executeSql(urlText, user.getText(), passwordText, ((MyTextField) sql).getText());
202+
resultText = MybatisSqlUtils.executeSql(urlText, user.getText(), passwordText, sqlText.getText());
167203
} catch (SQLException e) {
168204
resultText = "Execute Sql Failed. err: " + e.getMessage();
169205
}
@@ -202,6 +238,10 @@ private void updateUrlTextField() {
202238
}
203239
}
204240

241+
private void scrollPanelConfig() {
242+
243+
}
244+
205245
private void setScrollUnitIncrement() {
206246
int unit = 16;
207247
this.sqlScroll.getVerticalScrollBar().setUnitIncrement(unit);

0 commit comments

Comments
 (0)