Skip to content

Commit

Permalink
fix: No double compile (#116)
Browse files Browse the repository at this point in the history
* do not double compile

* redundant wrapper

* single place for checking if action is enabled
  • Loading branch information
MarcinVaadin authored Oct 4, 2024
1 parent 3ab168f commit 50dba47
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.vaadin.plugin.actions
import com.intellij.debugger.DebuggerManagerEx
import com.intellij.debugger.ui.HotSwapUI
import com.intellij.ide.actionsOnSave.impl.ActionsOnSaveFileDocumentManagerListener
import com.intellij.ide.util.PropertiesComponent
import com.intellij.openapi.application.WriteIntentReadAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Document
import com.intellij.openapi.fileEditor.FileDocumentManager
Expand All @@ -13,16 +11,14 @@ import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.vaadin.plugin.actions.VaadinCompileOnSaveActionInfo.Companion.DEFAULT
import com.vaadin.plugin.actions.VaadinCompileOnSaveActionInfo.Companion.PROPERTY
import com.vaadin.plugin.copilot.CopilotPluginUtil

class VaadinCompileOnSaveAction : ActionsOnSaveFileDocumentManagerListener.ActionOnSave() {

private val LOG: Logger = Logger.getInstance(CopilotPluginUtil::class.java)

override fun isEnabledForProject(project: Project): Boolean {
return PropertiesComponent.getInstance(project).getBoolean(PROPERTY, DEFAULT)
return VaadinCompileOnSaveActionInfo.isEnabledForProject(project)
}

override fun processDocuments(project: Project, documents: Array<Document?>) {
Expand All @@ -45,8 +41,8 @@ class VaadinCompileOnSaveAction : ActionsOnSaveFileDocumentManagerListener.Actio
override fun run(indicator: ProgressIndicator) {
val session = DebuggerManagerEx.getInstanceEx(project).context.debuggerSession
if (session != null) {
WriteIntentReadAction.run { HotSwapUI.getInstance(project).compileAndReload(session, vfsFile) }
LOG.info("File $vfsFile compiled")
LOG.info("${vfsFile.name} compiling...")
HotSwapUI.getInstance(project).compileAndReload(session, vfsFile)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.vaadin.plugin.actions
import com.intellij.codeInsight.actions.onSave.ActionOnSaveInfoBase
import com.intellij.ide.actionsOnSave.ActionOnSaveComment
import com.intellij.ide.actionsOnSave.ActionOnSaveContext
import com.intellij.ide.util.PropertiesComponent
import com.intellij.openapi.project.Project

class VaadinCompileOnSaveActionInfo(context: ActionOnSaveContext) :
ActionOnSaveInfoBase(context, NAME, PROPERTY, DEFAULT) {
Expand All @@ -12,6 +14,10 @@ class VaadinCompileOnSaveActionInfo(context: ActionOnSaveContext) :
const val PROPERTY = "vaadin.compileOnSave"
const val DEFAULT = true
const val DESCRIPTION = "Compiles *.java while debugging"

fun isEnabledForProject(project: Project): Boolean {
return PropertiesComponent.getInstance(project).getBoolean(PROPERTY, DEFAULT)
}
}

override fun getComment(): ActionOnSaveComment? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.intellij.openapi.vfs.findDocument
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.PsiManager
import com.vaadin.plugin.actions.VaadinCompileOnSaveAction
import com.vaadin.plugin.utils.IdeUtil
import io.netty.handler.codec.http.HttpResponseStatus
import java.io.File
Expand Down Expand Up @@ -70,8 +69,6 @@ open class WriteFileHandler(project: Project, data: Map<String, Any>) : Abstract
doWrite(vfsFile, it, content)
commitAndFlush(it)
LOG.info("File $ioFile contents saved")

compile(vfsFile)
openFileInEditor(vfsFile)
}
},
Expand Down Expand Up @@ -103,12 +100,9 @@ open class WriteFileHandler(project: Project, data: Map<String, Any>) : Abstract
psiDir.add(psiFile)
}

VfsUtil.findFileByIoFile(ioFile, true)?.let { vfsFile ->
compile(vfsFile)
openFileInEditor(vfsFile)
}
LOG.info("File $ioFile contents saved")
VfsUtil.findFileByIoFile(ioFile, true)?.let { vfsFile -> openFileInEditor(vfsFile) }
}
LOG.info("File $ioFile contents saved")
},
undoLabel ?: "Vaadin Copilot Write File",
null,
Expand All @@ -123,10 +117,6 @@ open class WriteFileHandler(project: Project, data: Map<String, Any>) : Abstract
FileEditorManager.getInstance(project).openTextEditor(openFileDescriptor, false)
}

private fun compile(vfsFile: VirtualFile) {
VaadinCompileOnSaveAction().compile(project, vfsFile)
}

private fun getOrCreateParentDir(): VirtualFile? {
if (!ioFile.parentFile.exists() && !ioFile.parentFile.mkdirs()) {
LOG.warn("Cannot create parent directories for ${ioFile.parent}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.vaadin.plugin.listeners
import com.intellij.debugger.JavaDebuggerBundle
import com.intellij.debugger.settings.DebuggerSettings
import com.intellij.ide.IdeCoreBundle
import com.intellij.ide.actionsOnSave.ActionsOnSaveConfigurable
import com.intellij.ide.util.RunOnceUtil
import com.intellij.notification.Notification
import com.intellij.notification.NotificationAction
Expand All @@ -15,6 +16,7 @@ import com.intellij.openapi.vcs.VcsBundle
import com.intellij.openapi.vcs.VcsConfiguration
import com.intellij.openapi.vcs.VcsShowConfirmationOption
import com.intellij.openapi.vcs.impl.ProjectLevelVcsManagerImpl
import com.vaadin.plugin.actions.VaadinCompileOnSaveActionInfo
import com.vaadin.plugin.copilot.CopilotPluginUtil
import com.vaadin.plugin.utils.VaadinHomeUtil
import com.vaadin.plugin.utils.VaadinIcons
Expand Down Expand Up @@ -44,6 +46,7 @@ class ConfigurationCheckVaadinProjectListener : VaadinProjectListener {
triggered = true
checkReloadClassesSetting(project)
checkVcsAddConfirmationSetting(project)
checkCompileOnSave(project)
RunOnceUtil.runOnceForApp("hotswap-version-check-" + CopilotPluginUtil.getPluginVersion()) {
VaadinHomeUtil.updateOrInstallHotSwapJar()
}
Expand Down Expand Up @@ -86,6 +89,16 @@ class ConfigurationCheckVaadinProjectListener : VaadinProjectListener {
}
}

private fun checkCompileOnSave(project: Project) {
if (!VaadinCompileOnSaveActionInfo.isEnabledForProject(project)) {
notify(
project,
"Turn on Compile Java Files action while Debugging using HotSwap to see instant changes.",
ActionsOnSaveConfigurable.CONFIGURABLE_ID,
)
}
}

private fun notify(project: Project, content: String, configurable: String) {
val notification =
Notification(NOTIFICATION_GROUP, content, NotificationType.INFORMATION).setIcon(VaadinIcons.VAADIN)
Expand Down

0 comments on commit 50dba47

Please sign in to comment.