Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active May 2, 2024 13:01
Show Gist options
  • Save dkandalov/590bdf339efe643a124e43082bed22d1 to your computer and use it in GitHub Desktop.
Save dkandalov/590bdf339efe643a124e43082bed22d1 to your computer and use it in GitHub Desktop.
Prototype of "suppress until commit" IntelliJ mini-plugin (to be executed via https://github.com/dkandalov/live-plugin)
import Plugin.SuppressInspectionTillCommit.Companion.resetSuppressions
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
import com.intellij.codeInspection.InspectionSuppressor
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.SuppressQuickFix
import com.intellij.lang.LanguageExtensionPoint
import com.intellij.openapi.extensions.DefaultPluginDescriptor
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.CheckinProjectPanel
import com.intellij.openapi.vcs.changes.CommitContext
import com.intellij.openapi.vcs.checkin.CheckinHandler
import com.intellij.openapi.vcs.checkin.CheckinHandlerFactory
import com.intellij.psi.PsiElement
import com.intellij.util.KeyedLazyInstance
import com.intellij.util.application
import liveplugin.registerAction
fun registerSuppressInspectionTillCommit() {
CheckinHandlerFactory.EP_NAME.point
.registerExtension(object : CheckinHandlerFactory() {
override fun createHandler(panel: CheckinProjectPanel, context: CommitContext) =
object : CheckinHandler() {
override fun checkinSuccessful() = resetSuppressions(panel.project)
}
}, pluginDisposable)
val pluginDescriptor = DefaultPluginDescriptor(PluginId.getId("LivePlugin"), SuppressInspectionTillCommit::class.java.classLoader)
val extensionPoint: KeyedLazyInstance<InspectionSuppressor> = LanguageExtensionPoint("kotlin", "Plugin\$SuppressInspectionTillCommit", pluginDescriptor)
application.extensionArea
.getExtensionPoint<Any>("com.intellij.lang.inspectionSuppressor")
.registerExtension(extensionPoint, pluginDisposable)
registerAction("Reset Suppressed Inspections until Commit") {
resetSuppressions(it.project!!)
}
}
registerSuppressInspectionTillCommit()
class SuppressInspectionTillCommit : InspectionSuppressor {
override fun isSuppressedFor(psiElement: PsiElement, toolId: String) =
(psiElement.project.name to toolId) in suppressedTools
override fun getSuppressActions(psiElement: PsiElement?, toolId: String) =
arrayOf<SuppressQuickFix>(
object : SuppressQuickFix {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
suppressedTools.add(project.name to toolId)
DaemonCodeAnalyzer.getInstance(project).restart()
}
override fun isAvailable(project: Project, context: PsiElement) = true
override fun isSuppressAll() = true
override fun getName() = "Suppress '$toolId' until commit"
override fun getFamilyName() = "LivePlugin"
}
)
companion object {
val suppressedTools = HashSet<Pair<String, String>>()
fun resetSuppressions(project: Project) {
suppressedTools.clear()
DaemonCodeAnalyzer.getInstance(project).restart()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment