Skip to content

Instantly share code, notes, and snippets.

@Mishkun
Created February 25, 2020 15:15
Show Gist options
  • Save Mishkun/055e5b489b12b588f244bde04cf849a1 to your computer and use it in GitHub Desktop.
Save Mishkun/055e5b489b12b588f244bde04cf849a1 to your computer and use it in GitHub Desktop.
registerAction(id = "OptimizeImportsInBranchAction") { event: AnActionEvent ->
val project = project ?: run {
show("Project is null")
return@registerAction
}
val cmd = exec("git diff --name-only origin/dev .")
// val diffs = exec("git status")
if (cmd.first.isNotBlank()) {
show(cmd.first)
return@registerAction
}
val diffs = cmd.second.split('\n')
for (diff in diffs) {
val psiFiles = FilenameIndex.getFilesByName(project, diff, GlobalSearchScope.projectScope(project))
for (psiFile in psiFiles) {
val optimizer = OptimizeImportsProcessor(project, psiFile)
optimizer.run()
}
}
show("Optimized imports in ${diffs.count()} files")
}
fun exec(command: String): Pair<String, String> =
Runtime.getRuntime().exec(command, null, File(project?.basePath)).run {
errorStream.bufferedReader().use { it.readText() }.trimEnd() to
inputStream.bufferedReader().use { it.readText() }.trimEnd()
}
if (!isIdeStartup) show("Loaded OptimizeImportsInBranchAction")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment