Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New upload algorithm and modified actions #350

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions src/main/kotlin/com/jetbrains/micropython/nova/actions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.intellij.util.asSafely
import com.jetbrains.micropython.run.MicroPythonRunConfiguration
import com.jetbrains.micropython.settings.MicroPythonProjectConfigurable
import com.jetbrains.micropython.settings.microPythonFacet
import com.jetbrains.python.PythonFileType
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -178,15 +179,20 @@ class InstantRun : DumbAwareAction() {
override fun getActionUpdateThread(): ActionUpdateThread = BGT

override fun update(e: AnActionEvent) {
e.presentation.isEnabled = e.getData(CommonDataKeys.VIRTUAL_FILE) != null

val file = e.getData(CommonDataKeys.VIRTUAL_FILE)
val files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY)
e.presentation.isEnabled = file != null &&
!file.isDirectory &&
files?.size == 1 &&
(file.fileType == PythonFileType.INSTANCE ||
file.extension == "mpy")
}

override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
FileDocumentManager.getInstance().saveAllDocuments()
val code = e.getData(CommonDataKeys.VIRTUAL_FILE)?.readText() ?: return
performReplAction(project,true,"Run code") {
performReplAction(project, true, "Run code") {
it.instantRun(code, false)
}
}
Expand Down Expand Up @@ -267,28 +273,57 @@ class OpenMpyFile : ReplAction("Open file", true, false) {
}
}

open class UploadFile() : DumbAwareAction("Upload File(s) to MicroPython Device") {
open class UploadFile() : DumbAwareAction("Upload Items(s) to MicroPython Device") {
override fun getActionUpdateThread(): ActionUpdateThread = BGT

override fun update(e: AnActionEvent) {
val project = e.project
val file = e.getData(CommonDataKeys.VIRTUAL_FILE)
if (project != null
&& file?.isInLocalFileSystem == true
&& ModuleUtil.findModuleForFile(file, project)?.microPythonFacet != null
) {
e.presentation.text =
if (file.isDirectory) "Upload Directory to MicroPython Device" else "Upload File to MicroPython Device"
val files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY)
if (project != null && files != null) {
var directoryCount = 0
var fileCount = 0

for (file in files.iterator()) {
if (file == null || !file.isInLocalFileSystem || ModuleUtil.findModuleForFile(
file,
project
)?.microPythonFacet == null
) {
return
}

if (file.isDirectory) {
directoryCount++
} else {
fileCount++
}
}

if (fileCount == 0) {
if (directoryCount == 1) {
e.presentation.text = "Upload Directory to MicroPython Device"
} else {
e.presentation.text = "Upload Directories to MicroPython Device"
}
} else if (directoryCount == 0) {
if (fileCount == 1) {
e.presentation.text = "Upload File to MicroPython Device"
} else {
e.presentation.text = "Upload Files to MicroPython Device"
}
} else {
e.presentation.text = "Upload Items to MicroPython Device"
}
} else {
e.presentation.isEnabledAndVisible = false
}
}

override fun actionPerformed(e: AnActionEvent) {
FileDocumentManager.getInstance().saveAllDocuments()
val file = e.getData(CommonDataKeys.VIRTUAL_FILE)
if (file != null) {
MicroPythonRunConfiguration.uploadFileOrFolder(e.project ?: return, file)
FileDocumentManager.getInstance().saveAllDocuments()
val files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY)
if (files != null) {
MicroPythonRunConfiguration.uploadItems(e.project ?: return, files.toSet())
}
}
}
Expand All @@ -302,7 +337,7 @@ class OpenSettingsAction : DumbAwareAction("Settings") {

class InterruptAction : ReplAction("Interrupt", true, false) {
override fun getActionUpdateThread(): ActionUpdateThread = BGT
override fun update(e: AnActionEvent) = enableIfConnected(e)
override fun update(e: AnActionEvent) = enableIfConnected(e)
override val actionDescription: @NlsContexts.DialogMessage String = "Interrupt..."

override suspend fun performAction(fileSystemWidget: FileSystemWidget) {
Expand All @@ -312,7 +347,7 @@ class InterruptAction : ReplAction("Interrupt", true, false) {

class SoftResetAction : ReplAction("Reset", true, false) {
override fun getActionUpdateThread(): ActionUpdateThread = BGT
override fun update(e: AnActionEvent) = enableIfConnected(e)
override fun update(e: AnActionEvent) = enableIfConnected(e)
override val actionDescription: @NlsContexts.DialogMessage String = "Reset..."

override suspend fun performAction(fileSystemWidget: FileSystemWidget) {
Expand All @@ -334,7 +369,7 @@ class CreateDeviceFolderAction : ReplAction("New Folder", true, false) {
}
}

override val actionDescription: @NlsContexts.DialogMessage String = "Creating new folder..."
override val actionDescription: @NlsContexts.DialogMessage String = "Creating New Folder..."

override suspend fun performAction(fileSystemWidget: FileSystemWidget) {

Expand Down
Loading
Loading