Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active December 13, 2024 17:41
Show Gist options
  • Save dkandalov/a48cae4ad5459b7036f3d52307d1afb4 to your computer and use it in GitHub Desktop.
Save dkandalov/a48cae4ad5459b7036f3d52307d1afb4 to your computer and use it in GitHub Desktop.
Mini-plugin to optimise imports in the background in all project Kotlin files
import com.intellij.codeInsight.actions.OptimizeImportsProcessor
import com.intellij.lang.Language
import com.intellij.psi.PsiFile import liveplugin.PluginUtil
import liveplugin.registerAction
registerAction("Background Optimize Imports") { event ->
val project = event.project ?: return@registerAction
val kotlinLanguage = Language.findLanguageByID("kotlin")!!
val psiFiles = PluginUtil.allPsiItemsIn(project)
.filterIsInstance<PsiFile>()
.filter { it.language == kotlinLanguage }
liveplugin.runBackgroundTask("Background Optimize Imports") { indicator ->
indicator.isIndeterminate = false
OptimizeImportsProcessor(project, psiFiles.toTypedArray(), null)
.apply { processFilesUnderProgress(indicator) }
.run()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment