宝安-网站建设信科网络,网站续费公司,做网站需要购买网站空间吗,淘客网站推广怎么做前言许久没更新IDEA插件开发系列了。最近刚好在汇总日常开发中常见的代码“异味”#xff0c;共享文档复制黏贴略显麻烦#xff0c;所以想着是否可以搞一个IDEA插件来帮忙收集常见代码#xff0c;毕竟IDEA作为后端程序员必备的开发工具#xff0c;显然会方便很多。于是共享文档复制黏贴略显麻烦所以想着是否可以搞一个IDEA插件来帮忙收集常见代码毕竟IDEA作为后端程序员必备的开发工具显然会方便很多。于是说干就干......制定需求功能需求大概包含如下选择“异味”代码鼠标右键点击添加该代码片段添加代码片段弹出信息添加弹窗基本信息包含代码标题简述修改建议将添加的代码片段进行数据集中管理存储添加消息推送功能开撸新建工程bugs-collect配置plugin.xml配置中提前配置了插件详细信息以及插件对应的行为。下面的配置信息可以用devkit插件生成描述了该插件功能添加的位置菜单等等。action idBugsCollectPluginId classcom.shamee.plugins.bugscollect.action.EditorBugsPopupActiontext添加Bug代码 description右击添加该选中代码记录bug系统add-to-group group-idEditorPopupMenu anchorfirst/keyboard-shortcut keymap$default first-keystrokealt B//action下面为完整配置!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html --
idea-pluginidcom.shamee.ide.plugins.bug-collect/idnameBugsCollect/namevendor emailxxxxx urlhttps://xxxxxxshamee/vendordescription![CDATA[Common odor codes can be added to the bug collection system with the right mouse button]]/descriptiondependscom.intellij.modules.platform/dependsextensions defaultExtensionNscom.intellij/extensionsactions!-- Add your actions here --action idBugsCollectPluginId classcom.shamee.plugins.bugscollect.action.EditorBugsPopupActiontext添加Bug代码 description右击添加该选中代码记录bug系统add-to-group group-idEditorPopupMenu anchorfirst/keyboard-shortcut keymap$default first-keystrokealt B//action/actions
/idea-plugin设置获取选中代码片段行为类EditorBugsPopupAction继承了AnAction并重写actionPerformed方法。作用是获取idea编辑界面选中的文本内容并且打开信息填写对话框。public class EditorBugsPopupAction extends AnAction {Overridepublic 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代码片段信息);}Overrideprotected NullableJComponent 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;}Overrideprotected 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代码”功能已添加点击添加Bug代码点击添加到异味代码列表提示操作成功看一眼数据库数据待处理列表数据统一收集后便可以进行数据的分发推送操作处理。代码推送管理每次添加都直接入库看不到添加的效果......Gradle打包点击Tasks - intellij - buildPlugin。控制台提示打包完成后查看工程build- distributions下已经生成了该插件压缩包。安装试用File - Settings - Plugins。选择刚打包好的zip就可以直接安装使用啦。Nice...