Created
August 21, 2025 10:35
-
-
Save maio/70353d23877ccc3e6c54e86f042fd891 to your computer and use it in GitHub Desktop.
Open build.gradle.kts IntelliJ IDEA action (Live Plugin)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/maio/70353d23877ccc3e6c54e86f042fd891 | |
import com.intellij.openapi.actionSystem.AnAction | |
import com.intellij.openapi.actionSystem.AnActionEvent | |
import com.intellij.openapi.actionSystem.CommonDataKeys | |
import com.intellij.openapi.fileEditor.FileEditorManager | |
import com.intellij.openapi.vfs.VirtualFile | |
import liveplugin.PluginUtil.registerAction | |
import liveplugin.PluginUtil.show | |
class OpenBuildGradleKtsAction : AnAction() { | |
override fun actionPerformed(event: AnActionEvent) { | |
val project = event.getData(CommonDataKeys.PROJECT) | |
val currentFile = event.getData(CommonDataKeys.VIRTUAL_FILE) | |
if (project == null || currentFile == null) { | |
show("No file or project is currently open.") | |
return | |
} | |
val buildFile = findBuildGradleKts(currentFile) | |
if (buildFile == null) { | |
show("No build.gradle.kts found") | |
return | |
} | |
FileEditorManager.getInstance(project).openFile(buildFile, true) | |
} | |
private fun findBuildGradleKts(file: VirtualFile): VirtualFile? { | |
var currentDir = if (file.isDirectory) file else file.parent | |
while (currentDir != null) { | |
val buildFile = currentDir.findChild("build.gradle.kts")?.takeIf { it.exists() } | |
if (buildFile != null) { | |
return buildFile | |
} | |
currentDir = currentDir.parent | |
} | |
return null | |
} | |
} | |
registerAction("OpenBuildGradleKts", OpenBuildGradleKtsAction()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use with https://plugins.jetbrains.com/plugin/7282-liveplugin