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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值