IDEA默認(rèn)的代碼高亮級(jí)別是all problems,顯示所有可能存在的問(wèn)題。問(wèn)題是這樣雖然詳細(xì),但會(huì)把編輯器搞得很卡 —— 一方面,idea 在檢查代碼的時(shí)候會(huì)跑滿(mǎn)cpu,有時(shí)移動(dòng)鼠標(biāo)都很卡;另一方面,這個(gè)宇宙最強(qiáng)ide之一竟然沒(méi)有g(shù)pu加速功能,滾動(dòng)條上的標(biāo)記都是cpu繪制的。
網(wǎng)上已有一些全局設(shè)置的方法,比如inspect設(shè)置中,去掉整個(gè)java的勾選?;蛘吆Y選出weak warning后去掉這些較弱的提示。這些方法的缺點(diǎn)是修改了編輯器設(shè)置,如果需要重新開(kāi)啟,則需要重新設(shè)置,比較麻煩。
本文介紹一種使用插件完成類(lèi)似目的的方法 —— 通過(guò)開(kāi)發(fā)idea插件,將默認(rèn)的 highlighting level 設(shè)為 Syntax,如需要重新 inspect all problem,則將鼠標(biāo)移動(dòng)至滾動(dòng)條上方,等待彈出小型設(shè)置窗口,將 syntax 下拉菜單改成 all problem 即可(見(jiàn)視頻中的操作)。
插件的關(guān)鍵代碼如下(機(jī)器人輔助編寫(xiě)):
static { // 將本段代碼插入任意插件項(xiàng)目的啟動(dòng)調(diào)用代碼即可。
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 如下,如果沒(méi)有com.intellij則需要新建在jdk設(shè)置頁(yè)面新建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;
這段代碼雖說(shuō)是機(jī)器人輔助編寫(xiě)的,不過(guò)在這個(gè)例子中,機(jī)器人的作用僅僅是更好的搜索引擎了,回答的代碼都不能直接使用。
本代碼通過(guò)設(shè)置 highlighterEx.setHighlightingSettingForRoot(psiFile, FileHighlightingSetting.SKIP_INSPECTION);
來(lái)設(shè)置高亮級(jí)別(其中 SKIP_INSPECTION 指的是跳過(guò)一般錯(cuò)誤的INSPECTION,而 SKIP_HIGHLIGHT 指的是跳過(guò)syntax的HIGHLIGHT
)。不過(guò)這樣設(shè)置對(duì)于已經(jīng)打開(kāi)的文件時(shí)無(wú)效的,不會(huì)觸發(fā)重新分析,無(wú)法自動(dòng)動(dòng)態(tài)設(shè)置,只能用作默認(rèn)值,在新開(kāi)idea窗口、雙擊打開(kāi)代碼文件時(shí)起作用。
效果(關(guān)閉文件,重新打開(kāi)后,高亮級(jí)別自動(dòng)變成syntax):
Idea插件:全局自動(dòng)設(shè)置代碼高亮級(jí)別為 Syntax
附插件開(kāi)發(fā)記錄。插件項(xiàng)目“vectorpathtweak”是幾年前完成的,用idea 2019開(kāi)發(fā)。用新idea打開(kāi),卻不能編譯了。需要新建一個(gè)plugin sdk,自動(dòng)包含新idea的一些jar。編譯成功,還是無(wú)法運(yùn)行(報(bào)錯(cuò) could not find idea.main class),于是下載了idea2019,用idea2019新建plugin sdk,才啟動(dòng)成功。
而用新idea新建的插件項(xiàng)目,里面既有g(shù)radle,又有kotlin,很亂,失去了純粹性,有點(diǎn)失望。。。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-662280.html
打包好的插件jar資源,包含上述功能:VectorPathTweaker.jar文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-662280.html
到了這里,關(guān)于Idea和Android Studio【插件】全局自動(dòng)設(shè)置代碼高亮級(jí)別為 Syntax的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!