IDEA插件开发.02之“异味”代码收集插件
前言
许久没更新IDEA插件开发系列了。最近刚好在汇总日常开发中常见的代码“异味”,共享文档复制黏贴略显麻烦,所以想着是否可以搞一个IDEA插件来帮忙收集常见代码,毕竟IDEA作为后端程序员必备的开发工具,显然会方便很多。
于是,说干就干......
源码地址已开放:https://github.com/Shamee99/bugs-collectidea-plugins/tree/master。欢迎指导。
制定需求
功能需求大概包含如下:
选择“异味”代码,鼠标右键点击添加该代码片段;
添加代码片段,弹出信息添加弹窗,基本信息包含:代码标题简述,修改建议;
将添加的代码片段进行数据集中管理(存储);
添加消息推送功能;
开撸
新建工程bugs-collect
![](https://img-blog.csdnimg.cn/img_convert/d068194d22a44603bffc0ffedc7225b3.png)
配置plugin.xml
配置中提前配置了插件详细信息,以及插件对应的行为。
下面的配置信息可以用devkit插件生成,描述了该插件功能添加的位置,菜单等等。
<action id="BugsCollectPluginId" class="com.shamee.plugins.bugscollect.action.EditorBugsPopupAction"
text="添加Bug代码" description="右击添加该选中代码记录bug系统">
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
<keyboard-shortcut keymap="$default" first-keystroke="alt B"/>
</action>
下面为完整配置:
<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
<idea-plugin>
<id>com.shamee.ide.plugins.bug-collect</id>
<name>BugsCollect</name>
<vendor email="xxxxx" url="https://xxxxxx">shamee</vendor>
<description>
<![CDATA[
Common odor codes can be added to the bug collection system with the right mouse button
]]>
</description>
<depends>com.intellij.modules.platform</depends>
<extensions defaultExtensionNs="com.intellij">
</extensions>
<actions>
<!-- Add your actions here -->
<action id="BugsCollectPluginId" class="com.shamee.plugins.bugscollect.action.EditorBugsPopupAction"
text="添加Bug代码" description="右击添加该选中代码记录bug系统">
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
<keyboard-shortcut keymap="$default" first-keystroke="alt B"/>
</action>
</actions>
</idea-plugin>
设置获取选中代码片段行为类
EditorBugsPopupAction继承了AnAction,并重写actionPerformed方法。作用是获取idea编辑界面选中的文本内容,并且打开信息填写对话框。
public class EditorBugsPopupAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent e) {
// 获取到idea编辑界面实例
Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
// 获取编辑实例选择模式
SelectionModel selectionModel = editor.getSelectionModel();
// 获取选中文本信息
String selectedText = selectionModel.getSelectedText();
// 设置数据中心数据
DataCenter.SELECT_CODE = selectedText;
// 开启弹窗
new BugCollectDialog().show();
}
}
新建BugCollectDialog,用于填写异味代码详细信息
BugCollectDialog为代码信息填写的对话框。该对话框绘制了标题填写栏editorTextFieldTitle,建议填写栏editorTextFieldSuggest,以及按钮组件addButton。使用jpanel绘制弹窗布局。
public class BugCollectDialog extends DialogWrapper {
private static final Logger logger = LoggerFactory.getLogger(BugCollectDialog.class);
private EditorTextField editorTextFieldTitle;
private EditorTextField editorTextFieldSuggest;
public BugCollectDialog() {
super(true);
init();
setTitle("添加Bug代码片段信息");
}
@Override
protected @Nullable
JComponent createCenterPanel() {
JPanel panel = new JPanel(new BorderLayout());
editorTextFieldTitle = new EditorTextField("异味代码描述");
editorTextFieldSuggest = new EditorTextField("异味代码修改建议");
editorTextFieldSuggest.setPreferredSize(new Dimension(300, 200));
panel.add(editorTextFieldTitle, BorderLayout.NORTH);
panel.add(editorTextFieldSuggest, BorderLayout.CENTER);
return panel;
}
@Override
protected JComponent createSouthPanel() {
JPanel panel = new JPanel(new FlowLayout());
JButton addButton = new JButton("添加到异味代码列表");
// 按钮点击事件
addButton.addActionListener(e -> {
});
panel.add(addButton);
return panel;
}
添加按钮监听
BugCollectDialog底部按钮添加事件监听。当填写完代码信息后点击该按钮,将详细信息,代码片段统一入库。
// 按钮点击事件
addButton.addActionListener(e -> {
// 获取标题
String title = editorTextFieldTitle.getText();
// 获取建议
String suggest = editorTextFieldSuggest.getText();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
String sql = "INSERT INTO `bugs-collect`.`bugs-collect-info` (title, suggest, code, create_date) VALUES('" + title + "', '" + suggest + "', '" + DataCenter.SELECT_CODE + "', '" + date + "');";
Connection connection = null;
Statement statement = null;
try {
connection = JdbcQuery.getConnection();
statement = JdbcQuery.getStatement(connection);
statement.executeUpdate(sql);
} catch (SQLException ex) {
logger.error(ex.getMessage(), ex);
throw new RuntimeException(ex);
} finally {
JdbcQuery.relase(connection, statement, null);
}
MessageDialogBuilder.yesNo("操作结果", "添加成功").show();
BugCollectDialog.this.dispose();
});
数据库操作工具类
public class JdbcQuery {
static {
try {
Class.forName(JdbcConstants.JDBC_DRIVER);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取链接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(JdbcConstants.JDBC_URL, JdbcConstants.JDBC_USERNAME, JdbcConstants.JDBC_PASSWORD);
}
public static Statement getStatement(Connection con) throws SQLException {
return con.createStatement();
}
//释放连接资源
public static void relase(Connection co, Statement st, ResultSet rs){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (co != null) {
try {
co.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
调试看效果
选中代码右键,可以看到“添加Bug代码”功能已添加:
![](https://img-blog.csdnimg.cn/img_convert/36abc5bb4da44bba813f4802a06dc6ba.png)
点击添加Bug代码:
![](https://img-blog.csdnimg.cn/img_convert/080304a88c6f4b1eaf1f9127a49fdbef.png)
点击添加到异味代码列表,提示操作成功:
![](https://img-blog.csdnimg.cn/img_convert/d90bc4788b9b475284b41b2d4fb1e3de.png)
看一眼数据库数据:
![](https://img-blog.csdnimg.cn/img_convert/1d91cf13553547828cdf9a207f522471.png)
待处理列表
数据统一收集后,便可以进行数据的分发推送操作处理。
代码推送管理;
每次添加都直接入库,看不到添加的效果;
......
Gradle打包
点击Tasks -> intellij -> buildPlugin。
![](https://img-blog.csdnimg.cn/img_convert/a6a58d9f26b2421988ecbf5f86d1bacb.png)
控制台提示打包完成后,查看工程build-> distributions下,已经生成了该插件压缩包。
![](https://img-blog.csdnimg.cn/img_convert/50fb670f919f4f73a1cdf7ae5a6bc4d8.png)
安装试用
File -> Settings -> Plugins。
![](https://img-blog.csdnimg.cn/img_convert/b711cadec88746008c9f8fd4617a0b84.png)
选择刚打包好的zip就可以直接安装使用啦。
Nice...
相关文章
- IDEA在同一窗口导入多个项目
- 【Idea】idea中编译后无法提示错误信息的解决方案
- IntelliJ IDEA 热部署插件 JRebel 安装激活及使用
- IDEA突然无法运行
- idea插件actiBPM源码
- IntelliJ IDEA插件系列
- intellij idea(2019.2):安装阿里编码规约(代码规范)插件及使用
- IDEA创建Maven Project图解
- IDEA的GsonFormat插件--将json字符串直接实例化成类
- maven工程中添加scala-library并非idea scala插件
- Atitit git 使用法v2 目录 1. Git客户端工具1 1.1. Tortoisegit git gui 图形化工具。。1 1.2. Ide中的git插件(eclipse idea
- idea 热部署
- intellij idea、webstorm或者vscode怎样显示每行代码的git版本信息?是谁修改的?什么时候修改的?查看代码行git/svn提交记录git blame好用的ide插件推荐
- 【异常】IDEA中Maven插件提示异常Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin
- Java微服务(二)【idea中文插件安装】(手把手编写,超级详细)
- IDEA Translation插件安装及设置鼠标悬浮提示的自动翻译
- 【人工智能AI代码:AIXCodeCompletionHelper】一个集成了 Open AI 人工智能写代码的 Jetbrains IDE 插件(支持 Golang,IDEA等)
- 005-mac下Java开发工具安装,idea,maven,git,node
- 001-Eclipse、idea集成javap查看字节码、javap说明
- intellij idea 插件 ideaVim 用法
- intellij idea、webstorm或者vscode怎样显示每行代码的git版本信息?是谁修改的?什么时候修改的?查看代码行git/svn提交记录git blame好用的ide插件推荐
- idea中插件安装
- 【异常】IDEA中Maven插件提示异常Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin
- 【项目实战】看不懂同事的SpringBoot配置文件?试试Convert YAML and Properties File这款IDEA插件
- Eolink神技之四、IDEA工具插件Eolink ApiKit
- Spark高效数据分析01、idea开发环境搭建