Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active August 21, 2019 19:06
Show Gist options
  • Save dkandalov/da7f58c4b0cfbed4e3ee1cade3cc5174 to your computer and use it in GitHub Desktop.
Save dkandalov/da7f58c4b0cfbed4e3ee1cade3cc5174 to your computer and use it in GitHub Desktop.
Plugin for toggling syntax highlighting in the current file (same as built-in "Highlighting level" slider in the inspector popup)
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
import com.intellij.codeInsight.daemon.impl.analysis.FileHighlightingSetting
import com.intellij.codeInsight.daemon.impl.analysis.HighlightLevelUtil
import com.intellij.codeInsight.daemon.impl.analysis.HighlightingLevelManager
import com.intellij.lang.injection.InjectedLanguageManager
import com.intellij.openapi.project.Project
import liveplugin.PluginUtil
import liveplugin.currentFile
import liveplugin.registerAction
import liveplugin.show
registerAction("Toggle Highlighting", "ctrl shift H") { event ->
toggleCurrentEditorHightlighting(event.project)
}
if (!isIdeStartup) show("Reloaded 'Toggle Highlighting'")
fun toggleCurrentEditorHightlighting(project: Project?) {
if (project == null) return
val virtualFile = project.currentFile ?: return
val psiFile = PluginUtil.psiFile(virtualFile, project) ?: return
val manager = HighlightingLevelManager.getInstance(project)
val isSyntaxHighlightingEnabled = manager.shouldHighlight(psiFile)
val isInspectionsHighlightingEnabled = manager.shouldInspect(psiFile)
if (isSyntaxHighlightingEnabled && isInspectionsHighlightingEnabled) {
HighlightLevelUtil.forceRootHighlighting(psiFile, FileHighlightingSetting.SKIP_HIGHLIGHTING)
} else {
HighlightLevelUtil.forceRootHighlighting(psiFile, FileHighlightingSetting.FORCE_HIGHLIGHTING)
}
InjectedLanguageManager.getInstance(project).dropFileCaches(psiFile)
DaemonCodeAnalyzer.getInstance(project).restart()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment