介绍
Demo说明
本文基于maven项目开发,idea版本为2022.3以上,jdk为1.8
本文在Tools插件之上进行开发
本次demo将使用idea的一些组件优化
Tools插件说明
Tools插件是一个Idea插件,此插件提供统一Spi规范,极大的降低了idea插件的开发难度,并提供开发者模块,可以极大的为开发者开发此插件提供便利
Tools插件安装需要idea2022.3以上版本
- 插件下载连接:
https://download.csdn.net/download/qq_42413011/89702325
- sdk下载连接:
https://download.csdn.net/download/qq_42413011/89702330
- pojo-serializer插件:
https://gitee.com/myprofile/pojo-serializer
正文
打开上一篇文章项目
修改pom文件,增加idea的依赖
shadow插件排除这两个依赖,这两个idea开发工具中已经存在了,不需要打包进jar
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lhstack</groupId><artifactId>tools-plugin-example</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><fastjson.version>2.0.52</fastjson.version></properties><dependencies><dependency><groupId>sdk</groupId><artifactId>sdk</artifactId><version>0.0.1</version><scope>system</scope><systemPath>${project.basedir}/lib/sdk-1.0.0.jar</systemPath></dependency><dependency><groupId>app</groupId><artifactId>app</artifactId><version>0.0.1</version><scope>system</scope><systemPath>F:\\Repo\\Gradle\\caches\\modules-2\\files-2.1\\com.jetbrains.intellij.idea\\ideaIC\\2022.3\\4d343cadac04a0a31d70f6f96facfaa7f949df01\\ideaIC-2022.3\\lib\\app.jar</systemPath></dependency><dependency><groupId>util</groupId><artifactId>util</artifactId><version>0.0.1</version><scope>system</scope><systemPath>F:\\Repo\\Gradle\\caches\\modules-2\\files-2.1\\com.jetbrains.intellij.idea\\ideaIC\\2022.3\\4d343cadac04a0a31d70f6f96facfaa7f949df01\\ideaIC-2022.3\\lib\\util.jar</systemPath></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.2.4</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><artifactSet><excludes><exclude>sdk:sdk</exclude><exclude>app:app</exclude><exclude>util:util</exclude></excludes></artifactSet></configuration></execution></executions></plugin></plugins></build></project>
修改代码如下
新增部分icon,可以在iconfont上下载即可
JsonView
package com.lhstack.aaa;import com.alibaba.fastjson.JSON;
import com.intellij.codeInsight.actions.ReformatCodeProcessor;
import com.intellij.icons.AllIcons;
import com.intellij.json.json5.Json5Language;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.ex.FoldingModelEx;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.ui.LanguageTextField;
import org.apache.commons.lang.StringUtils;import javax.swing.*;
import java.awt.*;public class JsonView extends JPanel implements Runnable {private final Project project;private LanguageTextField languageTextField;public JsonView(Project project) {this.project = project;this.init();}private void init() {setLayout(new BorderLayout());languageTextField = new LanguageTextField(Json5Language.INSTANCE, project, "", false){@Overrideprotected EditorEx createEditor() {EditorEx editor = super.createEditor();EditorSettings settings = editor.getSettings();settings.setRightMargin(-1);settings.setLineMarkerAreaShown(true);settings.setLineNumbersShown(true);settings.setLineCursorWidth(2);settings.setTabSize(4);settings.setUseTabCharacter(true);settings.setCaretInsideTabs(true);settings.setFoldingOutlineShown(true);super.resetKeyboardActions();return editor;}};this.add(languageTextField, BorderLayout.CENTER);this.initActions();}private void initActions() {DefaultActionGroup group = new DefaultActionGroup();group.add(new AnAction(() -> "全部展开", AllIcons.Actions.Expandall) {@Overridepublic void actionPerformed(AnActionEvent event) {Editor editor = languageTextField.getEditor();if(editor instanceof EditorEx editorEx){editorEx.getFoldingModel().runBatchFoldingOperation(() -> {for (FoldRegion foldRegion : editorEx.getFoldingModel().getAllFoldRegions()) {foldRegion.setExpanded(true);}});}}@Overridepublic ActionUpdateThread getActionUpdateThread() {return ActionUpdateThread.BGT;}});group.add(new AnAction(() -> "全部收起", AllIcons.Actions.Collapseall) {@Overridepublic void actionPerformed(AnActionEvent event) {Editor editor = languageTextField.getEditor();if(editor instanceof EditorEx editorEx){editorEx.getFoldingModel().runBatchFoldingOperation(() -> {for (FoldRegion foldRegion : editorEx.getFoldingModel().getAllFoldRegions()) {foldRegion.setExpanded(false);}});}}@Overridepublic ActionUpdateThread getActionUpdateThread() {return ActionUpdateThread.BGT;}});group.add(new AnAction(() -> "格式化", IconLoader.findIcon("icons/format.svg",JsonView.class)) {@Overridepublic void actionPerformed(AnActionEvent event) {String text = languageTextField.getText();if(StringUtils.isNotBlank(text)){PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(languageTextField.getDocument());if(psiFile != null){new ReformatCodeProcessor(psiFile,false).run();}}}@Overridepublic ActionUpdateThread getActionUpdateThread() {return ActionUpdateThread.BGT;}});group.add(new AnAction(() -> "压缩", IconLoader.findIcon("icons/compress.svg",JsonView.class)) {@Overridepublic void actionPerformed(AnActionEvent event) {String text = languageTextField.getText();if(StringUtils.isNotBlank(text)){try{Object object = JSON.parse(text);languageTextField.setText(JSON.toJSONString(object));}catch (Throwable e){Notifications.Bus.notify(new Notification("","格式化失败",e.toString(), NotificationType.ERROR),project);}}}@Overridepublic ActionUpdateThread getActionUpdateThread() {return ActionUpdateThread.BGT;}});ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar("ToolsPlugin@JsonView", group, true);actionToolbar.setTargetComponent(this);JComponent component = actionToolbar.getComponent();JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));panel.add(component);this.add(panel, BorderLayout.NORTH);}@Overridepublic void run() {Editor editor = languageTextField.getEditor();if(editor != null) {EditorFactory.getInstance().releaseEditor(editor);}}
}
PluginImpl
package com.lhstack.aaa;import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.lhstack.tools.plugins.IPlugin;import javax.swing.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;public class PluginImpl implements IPlugin {private final Map<String, Runnable> disposables = new HashMap<>();@Overridepublic JComponent createPanel(Project project) {return (JComponent) disposables.computeIfAbsent(project.getLocationHash(), key -> {return new JsonView(project);});}@Overridepublic void closeProject(String projectHash) {//关闭项目,移除项目对应打开的组件Optional.ofNullable(disposables.remove(projectHash)).ifPresent(Runnable::run);}@Overridepublic void unInstall() {//清除缓存disposables.values().forEach(Runnable::run);}@Overridepublic Icon pluginIcon() {return IconLoader.findIcon("logo.svg", PluginImpl.class);}@Overridepublic Icon pluginTabIcon() {return IconLoader.findIcon("logo_tab.svg", PluginImpl.class);}@Overridepublic String pluginName() {return "Json工具";}@Overridepublic String pluginDesc() {return "这是一个Json工具";}@Overridepublic String pluginVersion() {return "0.0.1";}
}
开发模块中运行
点击格式化
点击压缩
点击格式化,点击全部收起
点击全部展开
CTRL + F搜索
打包拖拽安装