|
| 1 | +package org.ollide.java2smali |
| 2 | + |
| 3 | +import com.intellij.openapi.actionSystem.AnAction |
| 4 | +import com.intellij.openapi.actionSystem.AnActionEvent |
| 5 | +import com.intellij.openapi.actionSystem.LangDataKeys |
| 6 | +import com.intellij.openapi.compiler.CompilerManager |
| 7 | +import com.intellij.openapi.roots.ProjectRootManager |
| 8 | +import com.intellij.openapi.vfs.VirtualFile |
| 9 | +import com.intellij.psi.PsiClassOwner |
| 10 | +import com.intellij.psi.PsiManager |
| 11 | + |
| 12 | +class GenerateAction : AnAction() { |
| 13 | + |
| 14 | + override fun actionPerformed(e: AnActionEvent) { |
| 15 | + val vFile = getVirtualFileFromContext(e) ?: return |
| 16 | + |
| 17 | + val project = e.project!! |
| 18 | + val module = ProjectRootManager.getInstance(project).fileIndex.getModuleForFile(vFile) ?: return |
| 19 | + val file = PsiManager.getInstance(project).findFile(vFile) as PsiClassOwner |
| 20 | + |
| 21 | + // Compile the vFile's module |
| 22 | + val compilerCallback = CompilerCallback(module, file) |
| 23 | + CompilerManager.getInstance(project).compile(module, compilerCallback) |
| 24 | + } |
| 25 | + |
| 26 | + override fun update(e: AnActionEvent) { |
| 27 | + var enabled = false |
| 28 | + |
| 29 | + val vFile = getVirtualFileFromContext(e) |
| 30 | + if (vFile != null) { |
| 31 | + val extension = vFile.fileType.defaultExtension |
| 32 | + val m = ProjectRootManager.getInstance(e.project!!).fileIndex.getModuleForFile(vFile) |
| 33 | + enabled = (JAVA == extension || KOTLIN == extension) && m != null |
| 34 | + } |
| 35 | + e.presentation.isEnabled = enabled |
| 36 | + } |
| 37 | + |
| 38 | + private fun getVirtualFileFromContext(e: AnActionEvent): VirtualFile? { |
| 39 | + val psiFile = e.getData(LangDataKeys.PSI_FILE) ?: return null |
| 40 | + return psiFile.virtualFile |
| 41 | + } |
| 42 | + |
| 43 | + companion object { |
| 44 | + private val JAVA = "java" |
| 45 | + private val KOTLIN = "kt" |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments