|
| 1 | +// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.jetbrains.services.amazonq.lsp.workspace |
| 5 | + |
| 6 | +import com.intellij.openapi.Disposable |
| 7 | +import com.intellij.openapi.project.Project |
| 8 | +import com.intellij.openapi.roots.ModuleRootEvent |
| 9 | +import com.intellij.openapi.roots.ModuleRootListener |
| 10 | +import com.intellij.openapi.vfs.VirtualFile |
| 11 | +import com.intellij.openapi.vfs.VirtualFileManager |
| 12 | +import com.intellij.openapi.vfs.newvfs.BulkFileListener |
| 13 | +import com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent |
| 14 | +import com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent |
| 15 | +import com.intellij.openapi.vfs.newvfs.events.VFileEvent |
| 16 | +import com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent |
| 17 | +import org.eclipse.lsp4j.CreateFilesParams |
| 18 | +import org.eclipse.lsp4j.DeleteFilesParams |
| 19 | +import org.eclipse.lsp4j.DidChangeWatchedFilesParams |
| 20 | +import org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams |
| 21 | +import org.eclipse.lsp4j.FileChangeType |
| 22 | +import org.eclipse.lsp4j.FileCreate |
| 23 | +import org.eclipse.lsp4j.FileDelete |
| 24 | +import org.eclipse.lsp4j.FileEvent |
| 25 | +import org.eclipse.lsp4j.FileRename |
| 26 | +import org.eclipse.lsp4j.RenameFilesParams |
| 27 | +import org.eclipse.lsp4j.WorkspaceFolder |
| 28 | +import org.eclipse.lsp4j.WorkspaceFoldersChangeEvent |
| 29 | +import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService |
| 30 | +import software.aws.toolkits.jetbrains.services.amazonq.lsp.util.WorkspaceFolderUtil.createWorkspaceFolders |
| 31 | +import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread |
| 32 | +import java.nio.file.FileSystems |
| 33 | +import java.nio.file.Paths |
| 34 | + |
| 35 | +class WorkspaceServiceHandler( |
| 36 | + private val project: Project, |
| 37 | + serverInstance: Disposable, |
| 38 | +) : BulkFileListener, |
| 39 | + ModuleRootListener { |
| 40 | + |
| 41 | + private var lastSnapshot: List<WorkspaceFolder> = emptyList() |
| 42 | + private val supportedFilePatterns = FileSystems.getDefault().getPathMatcher( |
| 43 | + "glob:**/*.{ts,js,py,java}" |
| 44 | + ) |
| 45 | + |
| 46 | + init { |
| 47 | + project.messageBus.connect(serverInstance).subscribe( |
| 48 | + VirtualFileManager.VFS_CHANGES, |
| 49 | + this |
| 50 | + ) |
| 51 | + |
| 52 | + project.messageBus.connect(serverInstance).subscribe( |
| 53 | + ModuleRootListener.TOPIC, |
| 54 | + this |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + private fun didCreateFiles(events: List<VFileEvent>) { |
| 59 | + AmazonQLspService.executeIfRunning(project) { languageServer -> |
| 60 | + val validFiles = events.mapNotNull { event -> |
| 61 | + val file = event.file?.takeIf { shouldHandleFile(it) } ?: return@mapNotNull null |
| 62 | + file.toNioPath().toUri().toString().takeIf { it.isNotEmpty() }?.let { uri -> |
| 63 | + FileCreate().apply { |
| 64 | + this.uri = uri |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (validFiles.isNotEmpty()) { |
| 70 | + languageServer.workspaceService.didCreateFiles( |
| 71 | + CreateFilesParams().apply { |
| 72 | + files = validFiles |
| 73 | + } |
| 74 | + ) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private fun didDeleteFiles(events: List<VFileEvent>) { |
| 80 | + AmazonQLspService.executeIfRunning(project) { languageServer -> |
| 81 | + val validFiles = events.mapNotNull { event -> |
| 82 | + val file = event.file?.takeIf { shouldHandleFile(it) } ?: return@mapNotNull null |
| 83 | + file.toNioPath().toUri().toString().takeIf { it.isNotEmpty() }?.let { uri -> |
| 84 | + FileDelete().apply { |
| 85 | + this.uri = uri |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (validFiles.isNotEmpty()) { |
| 91 | + languageServer.workspaceService.didDeleteFiles( |
| 92 | + DeleteFilesParams().apply { |
| 93 | + files = validFiles |
| 94 | + } |
| 95 | + ) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private fun didRenameFiles(events: List<VFilePropertyChangeEvent>) { |
| 101 | + AmazonQLspService.executeIfRunning(project) { languageServer -> |
| 102 | + val validRenames = events |
| 103 | + .filter { it.propertyName == VirtualFile.PROP_NAME } |
| 104 | + .mapNotNull { event -> |
| 105 | + val file = event.file.takeIf { shouldHandleFile(it) } ?: return@mapNotNull null |
| 106 | + val oldName = event.oldValue as? String ?: return@mapNotNull null |
| 107 | + if (event.newValue !is String) return@mapNotNull null |
| 108 | + |
| 109 | + // Construct old and new URIs |
| 110 | + val parentPath = file.parent?.toNioPath() ?: return@mapNotNull null |
| 111 | + val oldUri = parentPath.resolve(oldName).toUri().toString() |
| 112 | + val newUri = file.toNioPath().toUri().toString() |
| 113 | + |
| 114 | + FileRename().apply { |
| 115 | + this.oldUri = oldUri |
| 116 | + this.newUri = newUri |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (validRenames.isNotEmpty()) { |
| 121 | + languageServer.workspaceService.didRenameFiles( |
| 122 | + RenameFilesParams().apply { |
| 123 | + files = validRenames |
| 124 | + } |
| 125 | + ) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private fun didChangeWatchedFiles(events: List<VFileEvent>) { |
| 131 | + AmazonQLspService.executeIfRunning(project) { languageServer -> |
| 132 | + val validChanges = events.mapNotNull { event -> |
| 133 | + event.file?.toNioPath()?.toUri()?.toString()?.takeIf { it.isNotEmpty() }?.let { uri -> |
| 134 | + FileEvent().apply { |
| 135 | + this.uri = uri |
| 136 | + type = when (event) { |
| 137 | + is VFileCreateEvent -> FileChangeType.Created |
| 138 | + is VFileDeleteEvent -> FileChangeType.Deleted |
| 139 | + else -> FileChangeType.Changed |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if (validChanges.isNotEmpty()) { |
| 146 | + languageServer.workspaceService.didChangeWatchedFiles( |
| 147 | + DidChangeWatchedFilesParams().apply { |
| 148 | + changes = validChanges |
| 149 | + } |
| 150 | + ) |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + override fun after(events: List<VFileEvent>) { |
| 156 | + // since we are using synchronous FileListener |
| 157 | + pluginAwareExecuteOnPooledThread { |
| 158 | + didCreateFiles(events.filterIsInstance<VFileCreateEvent>()) |
| 159 | + didDeleteFiles(events.filterIsInstance<VFileDeleteEvent>()) |
| 160 | + didRenameFiles(events.filterIsInstance<VFilePropertyChangeEvent>()) |
| 161 | + didChangeWatchedFiles(events) |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + override fun beforeRootsChange(event: ModuleRootEvent) { |
| 166 | + lastSnapshot = createWorkspaceFolders(project) |
| 167 | + } |
| 168 | + |
| 169 | + override fun rootsChanged(event: ModuleRootEvent) { |
| 170 | + AmazonQLspService.executeIfRunning(project) { languageServer -> |
| 171 | + val currentSnapshot = createWorkspaceFolders(project) |
| 172 | + val addedFolders = currentSnapshot.filter { folder -> lastSnapshot.none { it.uri == folder.uri } } |
| 173 | + val removedFolders = lastSnapshot.filter { folder -> currentSnapshot.none { it.uri == folder.uri } } |
| 174 | + |
| 175 | + if (addedFolders.isNotEmpty() || removedFolders.isNotEmpty()) { |
| 176 | + languageServer.workspaceService.didChangeWorkspaceFolders( |
| 177 | + DidChangeWorkspaceFoldersParams().apply { |
| 178 | + this.event = WorkspaceFoldersChangeEvent().apply { |
| 179 | + added = addedFolders |
| 180 | + removed = removedFolders |
| 181 | + } |
| 182 | + } |
| 183 | + ) |
| 184 | + } |
| 185 | + |
| 186 | + lastSnapshot = currentSnapshot |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + private fun shouldHandleFile(file: VirtualFile): Boolean { |
| 191 | + if (file.isDirectory) { |
| 192 | + return true // Matches "**/*" with matches: "folder" |
| 193 | + } |
| 194 | + val path = Paths.get(file.path) |
| 195 | + val result = supportedFilePatterns.matches(path) |
| 196 | + return result |
| 197 | + } |
| 198 | +} |
0 commit comments