插件的开发
参考网址
(gradle例子,真的讲的好)
(gradle例子2)
(官网文档地址,gradle构建)
前提
1.idea需要安装这两个插件
2.电脑安装gradle(非必须)
如果电脑没有安装gradle,idea会自动安装,就是给你安装到c盘,然后给你占用几个G/(ㄒoㄒ)/~~
gradle安装的官网教程
官网安装在c盘的,我们这里随便安装在哪就行
idea创建项目
官方文档上面有这些说明,说的很清楚
文档
我需要提及的是,官网那个教程是默认给我们安装个gradle到c盘,下载插件过后大概有几个g,所以我们需要配置自己的gradle,在gradle安装目录创建**.gradle**文件夹,大部分gradle的东西都保存在这
等待下载插件完成
代码
关于实现,translate类继承一个类,重写方法,在xml中注册即可
官方文档关于继承action的说明
这三个文件需要修改
这是translate的代码,可以直接复制使用
package com.first.translate;
import com.alibaba.fastjson.JSONObject;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.JBColor;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.util.TextUtils;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
public class translate extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
//Gets the current project entity
Project project = e.getData(PlatformDataKeys.PROJECT);
//Gets the current mouse
final Editor mEditor = e.getData(PlatformDataKeys.EDITOR);
if (null == mEditor) {
return;
}
//Gets the selected mouse
SelectionModel model = mEditor.getSelectionModel();
//text
final String selectedText = model.getSelectedText();
if (TextUtils.isEmpty(selectedText)) {
return;
}
showPopupBalloon(mEditor,doGet("http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i="+selectedText));
//http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i=你好
}
//Display popup window
private void showPopupBalloon(final Editor editor, final String result) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
JBPopupFactory factory = JBPopupFactory.getInstance();
factory.createHtmlTextBalloonBuilder(result, null, new JBColor(new Color(186, 238, 186), new Color(73, 117, 73)), null)
.setFadeoutTime(5000)
.createBalloon()
.show(factory.guessBestPopupLocation(editor), Balloon.Position.below);
}
});
}
public static String doGet(String url) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String result = "";
try {
// Create an instance of httpClient with the address default configuration
httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
// httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
// Connection host service timeout
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)
// Request timeout
.setConnectionRequestTimeout(35000)
// Data read timeout
.setSocketTimeout(60000)
.build();
httpGet.setConfig(requestConfig);
response = httpClient.execute(httpGet);
// Gets the returned data by returning the object
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
List<List<JSONObject>> re = (List<List<JSONObject>>)JSONObject.parseObject(result, HashMap.class).get("translateResult");
result= ""+re.get(0).get(0).get("tgt");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}
plugin.xml中内容如下,修改下自己的
<idea-plugin>
<id>com.idea.plugins</id>
<!--这个是在idea已安装插件那里显示的名字-->
<name>hello</name>
<vendor email="1768923041@qq.com">qinjie</vendor>
<!--这个是在idea已安装插件那里显示的-->
<description><![CDATA[
test<br>
<em>most HTML tags may be used</em>
]]></description>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<!-- uncomment to enable plugin in all products
<depends>com.intellij.modules.lang</depends>
-->
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
</extensions>
<actions>
<group id="com.first.translate" text="translate" description="translate">
<action id="com.first.translate.translate" class="com.first.translate.translate" text="translate" description="translate" />
<!--添加到菜单栏尾部-->
<add-to-group group-id="MainMenu" anchor="last" />
</group>
</actions>
</idea-plugin>
build.gradle中的代码
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.4.16'
}
group 'com.idea'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
mavenLocal()
// mavenCentral()
}
dependencies {
compile group:'com.alibaba', name:'fastjson', version:'1.2.58'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
intellij {
version '2019.1.4'
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}
jar{
// 将所依赖的jar放进去
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }}
into('assets'){
from 'assets'
}
}
运行
点击运行,打开新的一个idea窗口,第一次运行会下载插件
给它设置快捷方式
使用快捷方式测试测试
发布到自己的idea
运行后如下目录有这个文件,idea手动安装插件即可。安装方式如下图
注意build.gradle中有下面代码,不然依赖的包不会被打包进入生成的jar
jar{
// 将所依赖的jar放进去
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }}
into('assets'){
from 'assets'
}
}
重启后,给它设置快捷键即可,运行测试
打包方式
直接运行也可以生成jar,或者如下图右边运行那些命令
中英文都是可以翻译的,用的我也不知道是哪个的翻译接口,网上找的,试了一下可以就用了。
总结
折腾了一天,最后搞定了,不算太难,应该说是不熟悉吧。看网上说gradle好像用的人也多,有些人从maven到gradle了。这样一个插件也算是对自己有用的,不用运行一个单独的翻译软件。