Skip to content

Commit

Permalink
rewrote GenerateAction in Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
ollide committed Nov 16, 2015
1 parent 42b0533 commit f4f733a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 53 deletions.
53 changes: 0 additions & 53 deletions src/org/ollide/java2smali/GenerateAction.java

This file was deleted.

48 changes: 48 additions & 0 deletions src/org/ollide/java2smali/GenerateAction.kt
Original file line number Diff line number Diff line change
@@ -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"
}

}

0 comments on commit f4f733a

Please sign in to comment.