|
| 1 | +// SPDX-FileCopyrightText: 2025 Weibo, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package org.zoocode.jetbrains.actors |
| 6 | + |
| 7 | +import com.intellij.openapi.application.ApplicationManager |
| 8 | +import com.intellij.openapi.diagnostic.Logger |
| 9 | +import com.intellij.openapi.project.Project |
| 10 | +import com.intellij.openapi.vfs.LocalFileSystem |
| 11 | +import org.zoocode.jetbrains.editor.EditorAndDocManager |
| 12 | +import org.zoocode.jetbrains.editor.EditorHolder |
| 13 | +import org.zoocode.jetbrains.editor.WorkspaceEdit |
| 14 | +import org.zoocode.jetbrains.ipc.proxy.SerializableObjectWithBuffers |
| 15 | +import kotlinx.coroutines.delay |
| 16 | +import java.io.File |
| 17 | +import java.nio.file.Files |
| 18 | +/** |
| 19 | + * Interface for handling bulk edits in the main thread. |
| 20 | + * Provides functionality to apply workspace edits that may include multiple file and text changes. |
| 21 | + */ |
| 22 | +interface MainThreadBulkEditsShape { |
| 23 | + /** |
| 24 | + * Attempts to apply a workspace edit. |
| 25 | + * |
| 26 | + * @param workspaceEditDto The workspace edit data transfer object |
| 27 | + * @param undoRedoGroupId Optional ID for grouping undo/redo operations |
| 28 | + * @param respectAutoSaveConfig Whether to respect auto-save configuration |
| 29 | + * @return True if all edits were applied successfully, false otherwise |
| 30 | + */ |
| 31 | + suspend fun tryApplyWorkspaceEdit(workspaceEditDto: SerializableObjectWithBuffers<Any>, undoRedoGroupId: Int?, respectAutoSaveConfig: Boolean?): Boolean |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Implementation of MainThreadBulkEditsShape that handles bulk edits in the main thread. |
| 36 | + * Processes workspace edits including file operations (create, delete, rename) and text edits. |
| 37 | + * |
| 38 | + * @property project The current project context |
| 39 | + */ |
| 40 | +class MainThreadBulkEdits(val project: Project) : MainThreadBulkEditsShape { |
| 41 | + val logger = Logger.getInstance(MainThreadBulkEditsShape::class.java) |
| 42 | + |
| 43 | + /** |
| 44 | + * Attempts to apply a workspace edit by processing file operations and text edits. |
| 45 | + * |
| 46 | + * @param workspaceEditDto The workspace edit data transfer object |
| 47 | + * @param undoRedoGroupId Optional ID for grouping undo/redo operations |
| 48 | + * @param respectAutoSaveConfig Whether to respect auto-save configuration |
| 49 | + * @return True if all edits were applied successfully, false otherwise |
| 50 | + */ |
| 51 | + override suspend fun tryApplyWorkspaceEdit(workspaceEditDto: SerializableObjectWithBuffers<Any>, undoRedoGroupId: Int?, respectAutoSaveConfig: Boolean?): Boolean { |
| 52 | + val json = workspaceEditDto.value as String |
| 53 | + logger.info("[Bulk Edit] Starting process: $json") |
| 54 | + val cto = WorkspaceEdit.from(json) |
| 55 | + var allSuccess = true |
| 56 | + |
| 57 | + // Process file edits - using background thread to avoid EDT violations |
| 58 | + cto.files.forEach { fileEdit -> |
| 59 | + if (fileEdit.oldResource != null && fileEdit.newResource != null) { |
| 60 | + val oldResource = File(fileEdit.oldResource.path) |
| 61 | + val newResource = File(fileEdit.newResource.path) |
| 62 | + try { |
| 63 | + Files.move(oldResource.toPath(), newResource.toPath()) |
| 64 | + // Move VFS refresh operations to background thread |
| 65 | + ApplicationManager.getApplication().executeOnPooledThread { |
| 66 | + val vfs = LocalFileSystem.getInstance() |
| 67 | + vfs.refreshIoFiles(listOf(oldResource, newResource)) |
| 68 | + } |
| 69 | + logger.info("[Bulk Edit] Renamed file: ${oldResource.path} -> ${newResource.path}") |
| 70 | + } catch (e: Exception) { |
| 71 | + logger.error("[Bulk Edit] Failed to rename file: ${oldResource.path} -> ${newResource.path}", e) |
| 72 | + allSuccess = false |
| 73 | + } |
| 74 | + } else if (fileEdit.oldResource != null) { |
| 75 | + val oldResource = File(fileEdit.oldResource.path) |
| 76 | + try { |
| 77 | + oldResource.delete() |
| 78 | + // Move VFS refresh operations to background thread |
| 79 | + ApplicationManager.getApplication().executeOnPooledThread { |
| 80 | + val vfs = LocalFileSystem.getInstance() |
| 81 | + vfs.refreshIoFiles(listOf(oldResource.parentFile)) |
| 82 | + } |
| 83 | + logger.info("[Bulk Edit] Deleted file: ${oldResource.path}") |
| 84 | + } catch (e: Exception) { |
| 85 | + logger.error("[Bulk Edit] Failed to delete file: ${oldResource.path}", e) |
| 86 | + allSuccess = false |
| 87 | + } |
| 88 | + } else if (fileEdit.newResource != null) { |
| 89 | + val newResource = File(fileEdit.newResource.path) |
| 90 | + try { |
| 91 | + val parentDir = newResource.parentFile |
| 92 | + if (!parentDir.exists()) { |
| 93 | + parentDir.mkdirs() |
| 94 | + } |
| 95 | + if (fileEdit.options?.contents != null) { |
| 96 | + Files.write(newResource.toPath(), fileEdit.options!!.contents!!.toByteArray(Charsets.UTF_8)) |
| 97 | + } else { |
| 98 | + newResource.createNewFile() |
| 99 | + } |
| 100 | + // Move VFS refresh operations to background thread |
| 101 | + ApplicationManager.getApplication().executeOnPooledThread { |
| 102 | + val vfs = LocalFileSystem.getInstance() |
| 103 | + vfs.refreshIoFiles(listOf(newResource)) |
| 104 | + } |
| 105 | + logger.info("[Bulk Edit] Created file: ${newResource.path}") |
| 106 | + } catch (e: Exception) { |
| 107 | + logger.error("[Bulk Edit] Failed to create file: ${newResource.path}", e) |
| 108 | + allSuccess = false |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + // Process text edits |
| 113 | + cto.texts.forEach { textEdit -> |
| 114 | + logger.info("[Bulk Edit] Processing text edit: ${textEdit.resource.path}") |
| 115 | + if (textEdit.resource.scheme != "file") { |
| 116 | + logger.error("[Bulk Edit] Non-file resources not supported: ${textEdit.resource.path}") |
| 117 | + allSuccess = false |
| 118 | + return@forEach |
| 119 | + } |
| 120 | + |
| 121 | + var handle:EditorHolder? = null; |
| 122 | + try { |
| 123 | + handle = project.getService(EditorAndDocManager::class.java).getEditorHandleByUri(textEdit.resource,true) |
| 124 | + if (handle == null) { |
| 125 | + handle = project.getService(EditorAndDocManager::class.java).sync2ExtHost(textEdit.resource,true) |
| 126 | + } |
| 127 | + } catch (e: Exception) { |
| 128 | + logger.info("[Bulk Edit] Failed to get editor handle: ${textEdit.resource.path}", e) |
| 129 | + } |
| 130 | + |
| 131 | + if (handle == null) { |
| 132 | + logger.info("[Bulk Edit] Editor handle not found: ${textEdit.resource.path}") |
| 133 | + allSuccess = false |
| 134 | + return@forEach |
| 135 | + } |
| 136 | + |
| 137 | + try { |
| 138 | + val result = handle.applyEdit(textEdit) |
| 139 | + if (!result) { |
| 140 | + logger.info("[Bulk Edit] Failed to apply edit: ${textEdit.resource.path}") |
| 141 | + allSuccess = false |
| 142 | + } else { |
| 143 | + logger.info("[Bulk Edit] Successfully updated file: ${textEdit.resource.path}") |
| 144 | + } |
| 145 | + } catch (e: Exception) { |
| 146 | + logger.error("[Bulk Edit] Exception applying edit: ${textEdit.resource.path}", e) |
| 147 | + allSuccess = false |
| 148 | + } |
| 149 | + } |
| 150 | + return allSuccess |
| 151 | + } |
| 152 | +} |
0 commit comments