diff --git a/src/org/ollide/java2smali/GenerateAction.java b/src/org/ollide/java2smali/GenerateAction.java deleted file mode 100644 index 9b7226a..0000000 --- a/src/org/ollide/java2smali/GenerateAction.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.ollide.java2smali; - -import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.openapi.actionSystem.AnActionEvent; -import com.intellij.openapi.actionSystem.LangDataKeys; -import com.intellij.openapi.compiler.CompilerManager; -import com.intellij.openapi.module.Module; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.roots.ProjectRootManager; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.psi.PsiClassOwner; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiManager; - -public class GenerateAction extends AnAction { - - private static final String JAVA = "java"; - private static final String KOTLIN = "kt"; - - public void actionPerformed(AnActionEvent e) { - VirtualFile vFile = getVirtualFileFromContext(e); - - Project p = e.getProject(); - Module module = ProjectRootManager.getInstance(p).getFileIndex().getModuleForFile(vFile); - PsiClassOwner file = (PsiClassOwner) PsiManager.getInstance(p).findFile(vFile); - - // Compile the vFile's module - CompilerCallback compilerCallback = new CompilerCallback(module, file); - CompilerManager.getInstance(p).compile(module, compilerCallback); - } - - @Override - public void update(AnActionEvent e) { - boolean enabled = false; - - VirtualFile vFile = getVirtualFileFromContext(e); - if (vFile != null) { - String extension = vFile.getFileType().getDefaultExtension(); - Module m = ProjectRootManager.getInstance(e.getProject()).getFileIndex().getModuleForFile(vFile); - enabled = (JAVA.equals(extension) || KOTLIN.equals(extension)) && m != null; - } - e.getPresentation().setEnabled(enabled); - } - - private VirtualFile getVirtualFileFromContext(AnActionEvent e) { - PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE); - if (psiFile == null) { - return null; - } - return psiFile.getVirtualFile(); - } - -} diff --git a/src/org/ollide/java2smali/GenerateAction.kt b/src/org/ollide/java2smali/GenerateAction.kt new file mode 100644 index 0000000..4033743 --- /dev/null +++ b/src/org/ollide/java2smali/GenerateAction.kt @@ -0,0 +1,48 @@ +package org.ollide.java2smali + +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.actionSystem.LangDataKeys +import com.intellij.openapi.compiler.CompilerManager +import com.intellij.openapi.roots.ProjectRootManager +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.psi.PsiClassOwner +import com.intellij.psi.PsiManager + +class GenerateAction : AnAction() { + + override fun actionPerformed(e: AnActionEvent) { + val vFile = getVirtualFileFromContext(e) ?: return + + val project = e.project!! + val module = ProjectRootManager.getInstance(project).fileIndex.getModuleForFile(vFile) ?: return + val file = PsiManager.getInstance(project).findFile(vFile) as PsiClassOwner + + // Compile the vFile's module + val compilerCallback = CompilerCallback(module, file) + CompilerManager.getInstance(project).compile(module, compilerCallback) + } + + override fun update(e: AnActionEvent) { + var enabled = false + + val vFile = getVirtualFileFromContext(e) + if (vFile != null) { + val extension = vFile.fileType.defaultExtension + val m = ProjectRootManager.getInstance(e.project!!).fileIndex.getModuleForFile(vFile) + enabled = (JAVA == extension || KOTLIN == extension) && m != null + } + e.presentation.isEnabled = enabled + } + + private fun getVirtualFileFromContext(e: AnActionEvent): VirtualFile? { + val psiFile = e.getData(LangDataKeys.PSI_FILE) ?: return null + return psiFile.virtualFile + } + + companion object { + private val JAVA = "java" + private val KOTLIN = "kt" + } + +}