Idea和Android Studio插件全局自动设置代码高亮级别为 Syntax
Posted 代码盗圣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Idea和Android Studio插件全局自动设置代码高亮级别为 Syntax相关的知识,希望对你有一定的参考价值。
IDEA默认的代码高亮级别是all problems,显示所有可能存在的问题。问题是这样虽然详细,但会把编辑器搞得很卡 —— 一方面,idea 在检查代码的时候会跑满cpu,有时移动鼠标都很卡;另一方面,这个宇宙最强ide之一竟然没有gpu加速功能,滚动条上的标记都是cpu绘制的。
网上已有一些全局设置的方法,比如inspect设置中,去掉整个java的勾选。或者筛选出weak warning后去掉这些较弱的提示。这些方法的缺点是修改了编辑器设置,如果需要重新开启,则需要重新设置,比较麻烦。
本文介绍一种使用插件完成类似目的的方法 —— 通过开发idea插件,将默认的 highlighting level 设为 Syntax,如需要重新 inspect all problem,则将鼠标移动至滚动条上方,等待弹出小型设置窗口,将 syntax 下拉菜单改成 all problem 即可(见视频中的操作)。
插件的关键代码如下(机器人辅助编写):
static // 将本段代码插入任意插件项目的启动调用代码即可。
System.out.println("TweakerAction !!!");
ProjectManagerListener listener = new ProjectManagerListener()
@Override
public void projectOpened(@NotNull Project project)
ProjectManagerListener.super.projectOpened(project);
System.out.println("initializedFileListener !!!");
//Project mProject = e.getData(PlatformDataKeys.PROJECT);
Project mProject = project;
FileEditorManager fileMan = FileEditorManager.getInstance(mProject);
fileMan.addFileEditorManagerListener(new FileEditorManagerListener()
@Override
public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file)
FileEditorManagerListener.super.fileOpened(source, file);
PsiFile psiFile = PsiManager.getInstance(mProject).findFile(file);
HighlightingSettingsPerFile highlighterEx = HighlightingSettingsPerFile.getInstance(mProject);
//System.out.println("set to syntax !!!");
// only work when the document is first opened. todo make this code work dynamically. ( need to notify the editor. )
highlighterEx.setHighlightingSettingForRoot(psiFile, FileHighlightingSetting.SKIP_INSPECTION);
);
;
Project[] projs = ProjectManager.getInstance().getOpenProjects();
for (int i = 0; i < projs.length; i++)
listener.projectOpened(projs[i]);
ProjectManager.getInstance().addProjectManagerListener(listener);
// 附 import 如下,如果没有com.intellij则需要新建在jdk设置页面新建plugin sdk,指定idea目录:
import com.intellij.codeInsight.daemon.impl.analysis.FileHighlightingSetting;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightingSettingsPerFile;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
这段代码虽说是机器人辅助编写的,不过在这个例子中,机器人的作用仅仅是更好的搜索引擎了,回答的代码都不能直接使用。
本代码通过设置 highlighterEx.setHighlightingSettingForRoot(psiFile, FileHighlightingSetting.SKIP_INSPECTION);
来设置高亮级别(其中 SKIP_INSPECTION 指的是跳过一般错误的INSPECTION,而 SKIP_HIGHLIGHT 指的是跳过syntax的HIGHLIGHT
)。不过这样设置对于已经打开的文件时无效的,不会触发重新分析,无法自动动态设置,只能用作默认值,在新开idea窗口、双击打开代码文件时起作用。
效果(关闭文件,重新打开后,高亮级别自动变成syntax):
Idea插件:全局自动设置代码高亮级别为 Syntax
附插件开发记录。插件项目“vectorpathtweak”是几年前完成的,用idea 2019开发。用新idea打开,却不能编译了。需要新建一个plugin sdk,自动包含新idea的一些jar。编译成功,还是无法运行(报错 could not find idea.main class),于是下载了idea2019,用idea2019新建plugin sdk,才启动成功。
而用新idea新建的插件项目,里面既有gradle,又有kotlin,很乱,失去了纯粹性,有点失望。。。
打包好的插件jar资源,包含上述功能:VectorPathTweaker.jar
Android studio 插件之 GsonFormat (自动生成javabean)
概述
相信大家在做开发的过程中都写过很多的javabean ,很多情况下 都是一个列表数据就是一个单独的javabean,如果大家自己敲的话费时费力 还很容易敲错。
今天给大家推荐一个插件 GsonFormat 这个插件就是可以让我们直接将服务端返回的json转化为实体类,Android studio和idea都有, eclipse我没有试过。大家可以找找有没有。
操作步骤
第一步 下载插件
下载插件的过程我前面有一篇博客已经有详细步骤 Android Studio插件之sexy editor(设置AS背景) 大家 只需要将那篇博客中介绍的插件名称换成GsonFormat
按照那个步骤下载好 插件。记得下载安装好后要重启哦。
第二步 自动生成bean
下面我们介绍怎么通过这个插件将服务端返回的json自动生成javabean
首先 我们要建一个空的java类,然后 在空白出点击鼠标右键 选择 Generate... 如果你已经成功安装好插件会弹出下面这个框
选择GsonFormat会弹出下面这个框
将服务端接返回的json 粘贴到这个里面 点击ok
粘贴完成后 点击ok会弹出如下图所示的选择 字段界面
这个界面是让你自己选择 保留哪些字段,可能有的字段不需要 就把前面的对勾去掉就可以了。最后点击ok,大功告成。
贴上json字符串和自动生成出的bean代码.
json字符串
{"retval":"ok","reterr":"","retinfo":{"count":2,"groups":[{"groupid":1,"title":"\\u65b0\\u670b\\u53cb","icon":"http:\\/\\/aliimg.yizhibo.tv\\/online\\/message\\/2f\\/74\\/hi.png","type":"1","readcnt":67,"unread":2,"total":19,"update_time":"2016-04-05 09:11:09","lastest_content":{"type":2,"data":{"name":"16837286","nickname":"\\u963f\\u83b2","gender":"female","signature":"","vip":"0","logourl":"http:\\/\\/aliimg.yizhibo.tv\\/online\\/user\\/2c\\/17\\/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c"}}},{"groupid":0,"title":"\\u6613\\u76f4\\u64ad\\u5c0f\\u79d8\\u4e66","icon":"http:\\/\\/aliimg.yizhibo.tv\\/online\\/message\\/07\\/fd\\/Secretary.png","type":0,"readcnt":86,"unread":0,"total":86,"update_time":"2016-04-01 00:09:28","lastest_content":{"type":0,"data":{"text":"\\u611a\\u4eba\\u8282\\u6765\\u4e34\\u4e4b\\u9645\\uff0c\\u5fc3\\u4e2d\\u7684\\u5c0f\\u6076\\u9b54\\u6b63\\u5728\\u9a9a\\u52a8\\u3002\\u6613\\u76f4\\u64ad\\u611a\\u4eba\\u8282\\u65e0\\u654c\\u5927\\u738b\\u4e89\\u9738\\u96c6\\u7ed3\\u53f7\\u4e5f\\u501f\\u52bf\\u90aa\\u6076\\u5439\\u54cd\\uff01\\u770b\\u5404\\u5927\\u5bb6\\u5982\\u4f55\\u5927\\u65bd\\u8ff7\\u9b42\\u9635\\uff0c\\u6597\\u5fd7\\u6597\\u52c7\\uff0c\\u620e\\u9a6c\\u800c\\u5f52\\u3002\\u201c\\u611a\\u4eba\\u5927\\u738b\\u201d\\u3001\\u201c\\u8303\\u4e8c\\u5927\\u738b\\u201d\\u7a76\\u7adf\\u82b1\\u843d\\u8c01\\u5bb6\\u5462\\uff1f\\u54b1\\u4eec\\u62ed\\u76ee\\u4ee5\\u5f85\\uff01"}}}]}}
生成的bean
<span style="font-size:14px;">
import java.util.List;
public class MessageGroupEntity {
/**
* retval : ok
* reterr :
* retinfo : {"count":2,"groups":[{"groupid":1,"title":"新朋友",
* "icon":"http://aliimg.yizhibo.tv/online/message/2f/74/hi.png","type":"1","readcnt":67,
* "unread":2,"total":19,"update_time":"2016-04-05 09:11:09","lastest_content":{"type":2,
* "data":{"name":"16837286","nickname":"阿莲","gender":"female","signature":"","vip":"0",
* "logourl":"http://aliimg.yizhibo.tv/online/user/2c/17/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c"}}}
* ,{"groupid":0,"title":"易直播小秘书","icon":"http://aliimg.yizhibo.tv/online/message/07/fd/Secretary.png","type":0,"readcnt":86,
* "unread":0,"total":86,"update_time":"2016-04-01 00:09:28",
* "lastest_content":{"type":0,"data":{"text":"愚人节来临之际,心中的小恶魔正在骚动。易直播愚人节无敌大王争霸集结号也借势邪恶吹响!
* 看各大家如何大施迷魂阵,斗志斗勇,戎马而归。\\u201c愚人大王\\u201d、\\u201c范二大王\\u201d究竟花落谁家呢?咱们拭目以待!"}}}]}
*/
private String retval;
private String reterr;
/**
* count : 2
* groups : [{"groupid":1,"title":"新朋友",
* "icon":"http://aliimg.yizhibo.tv/online/message/2f/74/hi.png","type":"1","readcnt":67,
* "unread":2,"total":19,"update_time":"2016-04-05 09:11:09",
* "lastest_content":{"type":2,"data":{"name":"16837286","nickname":"阿莲","gender":"female",
* "signature":"","vip":"0","logourl":"http://aliimg.yizhibo.tv/online/user/2c/17/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c"}}},
* {"groupid":0,"title":"易直播小秘书","icon":"http://aliimg.yizhibo.tv/online/message/07/fd/Secretary.png",
* "type":0,"readcnt":86,"unread":0,"total":86,"update_time":"2016-04-01 00:09:28","lastest_content":
* {"type":0,"data":{"text":"愚人节来临之际,心中的小恶魔正在骚动。易直播愚人节无敌大王争霸集结号也借势邪恶吹响!看各大家如何大施迷魂阵,斗志斗勇,戎马而归。
* \\u201c愚人大王\\u201d、\\u201c范二大王\\u201d究竟花落谁家呢?咱们拭目以待!"}}}]
*/
private RetinfoEntity retinfo;
public void setRetval(String retval) {
this.retval = retval;
}
public void setReterr(String reterr) {
this.reterr = reterr;
}
public void setRetinfo(RetinfoEntity retinfo) {
this.retinfo = retinfo;
}
public String getRetval() {
return retval;
}
public String getReterr() {
return reterr;
}
public RetinfoEntity getRetinfo() {
return retinfo;
}
public static class RetinfoEntity {
private int count;
/**
* groupid : 1
* title : 新朋友
* icon : http://aliimg.yizhibo.tv/online/message/2f/74/hi.png
* type : 1
* readcnt : 67
* unread : 2
* total : 19
* update_time : 2016-04-05 09:11:09
* lastest_content : {"type":2,"data":{"name":"16837286","nickname":"阿莲","gender":"female","signature":"","vip":"0","logourl":"http://aliimg.yizhibo.tv/online/user/2c/17/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c"}}
*/
private List<GroupsEntity> groups;
public void setCount(int count) {
this.count = count;
}
public void setGroups(List<GroupsEntity> groups) {
this.groups = groups;
}
public int getCount() {
return count;
}
public List<GroupsEntity> getGroups() {
return groups;
}
public static class GroupsEntity {
private int groupid;
private String title;
private String icon;
private String type;
private int readcnt;
private int unread;
private int total;
private String update_time;
/**
* type : 2
* data : {"name":"16837286","nickname":"阿莲","gender":"female","signature":"","vip":"0","logourl":"http://aliimg.yizhibo.tv/online/user/2c/17/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c"}
*/
private LastestContentEntity lastest_content;
public void setGroupid(int groupid) {
this.groupid = groupid;
}
public void setTitle(String title) {
this.title = title;
}
public void setIcon(String icon) {
this.icon = icon;
}
public void setType(String type) {
this.type = type;
}
public void setReadcnt(int readcnt) {
this.readcnt = readcnt;
}
public void setUnread(int unread) {
this.unread = unread;
}
public void setTotal(int total) {
this.total = total;
}
public void setUpdate_time(String update_time) {
this.update_time = update_time;
}
public void setLastest_content(LastestContentEntity lastest_content) {
this.lastest_content = lastest_content;
}
public int getGroupid() {
return groupid;
}
public String getTitle() {
return title;
}
public String getIcon() {
return icon;
}
public String getType() {
return type;
}
public int getReadcnt() {
return readcnt;
}
public int getUnread() {
return unread;
}
public int getTotal() {
return total;
}
public String getUpdate_time() {
return update_time;
}
public LastestContentEntity getLastest_content() {
return lastest_content;
}
public static class LastestContentEntity {
private int type;
/**
* name : 16837286
* nickname : 阿莲
* gender : female
* signature :
* vip : 0
* logourl : http://aliimg.yizhibo.tv/online/user/2c/17/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c
*/
private DataEntity data;
public void setType(int type) {
this.type = type;
}
public void setData(DataEntity data) {
this.data = data;
}
public int getType() {
return type;
}
public DataEntity getData() {
return data;
}
public static class DataEntity {
private String name;
private String nickname;
private String gender;
private String signature;
private String vip;
private String logourl;
public void setName(String name) {
this.name = name;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setSignature(String signature) {
this.signature = signature;
}
public void setVip(String vip) {
this.vip = vip;
}
public void setLogourl(String logourl) {
this.logourl = logourl;
}
public String getName() {
return name;
}
public String getNickname() {
return nickname;
}
public String getGender() {
return gender;
}
public String getSignature() {
return signature;
}
public String getVip() {
return vip;
}
public String getLogourl() {
return logourl;
}
}
}
}
}
}
</span>
大概流程就是这样,希望对大家有所帮助。
欢迎大家加入我的qq 群:454430053
以上是关于Idea和Android Studio插件全局自动设置代码高亮级别为 Syntax的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio 和带有插件的 IntelliJ IDEA 之间的区别?
在IDEA/Android Studio中调试Gradle Plugin插件
Android Studio 4.1 插件错误:插件 * 不兼容(仅在 IntelliJ IDEA 中支持)