手把手教你开发IDEA插件
手把手教你开发IDEA插件 环境准备IntelliJ IDEA并安装Plugin DevKit插件JDK8基于Gradle创建plugin项目File->New->ProjectsNextNextNext && Finish修改build.gradle文件
打开build.gradle文件可以查看到当前idea版本是2018.3.6。且可以看到idea推荐的Gradle版本为4.10此时项目在自动构建时会失败,因为gradle-intellij-plugin版本为1.3.1和Gradle版本不匹配,需要修改合适的gradle-intellij-plugin版本。根据gradle-intellij-plugin查看到4.10的Gradle可以使用0.7.3的gradle-intellij-pluginbuild.gradle修改后项目构建成功至此,我们项目插件项目创建完成了。
翻译插件创建Action
查看创建结果
编写翻译逻辑public void actionPerformed(AnActionEvent e) { Editor editor = e.getDataContext().getData(CommonDataKeys.EDITOR); String q = editor.getSelectionModel().getSelectedText(); String appId = “20190717000318901”; String appKey = “R8OPnhJn7TP2m2n4g7za”; String salt = RandomUtil.randomNumbers(3); MD5 md5 = MD5.create(); String sign = md5.digestHex(appId + q + salt + appKey); String transUrl = “https://fanyi-api.baidu.com/api/trans/vip/translate”; HttpGet httpGet = new HttpGet(transUrl + “?q=” + q + “&from=en&to=zh” + “&appid=” + appId + “&salt=” + salt + “&sign=” + sign); try (CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = httpclient.execute(httpGet)) { HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); String text = new BufferedReader(new InputStreamReader(content, StandardCharsets.UTF_8)) .lines().collect(Collectors.joining(“\n”)); ResultSet resultSet = JSONUtil.toBean(text, ResultSet.class); if (CollUtil.isNotEmpty(resultSet.getTrans_result())) { Notifications.Bus.notify(buildNotification(resultSet.getTrans_result().get(0).getDst(), NotificationType.INFORMATION)); } } catch (Exception ex) { // }}private Notification buildNotification(String content, NotificationType type) { String groupDisplayId = “translation”; Notification notification = new Notification(groupDisplayId, null, type); notification.setContent(content); return notification;}runIde & 测试
插件安装buildPlugin
install
选择生成的zip文件后,重启idea即可。
注意!此信息未认证,请谨慎判断信息的真实性!
提示