Last active
November 1, 2022 11:46
-
-
Save tordjon/e22c293f918261385a5497f7a79e9408 to your computer and use it in GitHub Desktop.
LivePlugin for navigating to the build.gradle or build.gradle.kts file of the module that hosts the currently open file in editor
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
import com.intellij.openapi.project.Project | |
import com.intellij.openapi.roots.ProjectRootManager | |
import com.intellij.openapi.vfs.VirtualFile | |
import com.intellij.psi.search.FilenameIndex | |
import com.intellij.psi.search.GlobalSearchScope | |
import liveplugin.registerAction | |
import liveplugin.show | |
import liveplugin.virtualFile | |
registerAction("Find gradle build file", "alt G") { event -> | |
val project = event.project | |
val virtualFile = event.virtualFile | |
if (project == null) { | |
show("No project found") | |
} else if (virtualFile == null) { | |
show("Virtual file not found") | |
} else { | |
navigateToBuildFile(project, virtualFile) | |
} | |
} | |
fun navigateToBuildFile( | |
project: Project, | |
virtualFile: VirtualFile | |
) { | |
val scope = GlobalSearchScope.projectScope(project) | |
val fileIndex = ProjectRootManager.getInstance(project).fileIndex | |
val moduleName = fileIndex.getModuleForFile(virtualFile)?.moduleFile?.parent?.name | |
if (moduleName == null) { | |
show("Could not find target module name for file: ${virtualFile.name}") | |
} else { | |
val buildFiles = FilenameIndex.getFilesByName(project, "build.gradle", scope).toList() + | |
FilenameIndex.getFilesByName(project, "build.gradle.kts", scope).toList() | |
val buildFile = buildFiles.find { | |
it.containingDirectory.name == moduleName | |
} | |
if (buildFile == null) { | |
show("Could not find build file for module: $moduleName") | |
} else { | |
buildFile.navigate(true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment