|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Vegard IT GmbH and others. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Sebastian Thomschke (Vegard IT GmbH) - initial implementation |
| 11 | + *******************************************************************************/ |
| 12 | +package org.eclipse.lsp4e.test.operations.rename; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.*; |
| 15 | + |
| 16 | +import java.net.URI; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import org.eclipse.core.resources.IFile; |
| 20 | +import org.eclipse.core.resources.IFolder; |
| 21 | +import org.eclipse.lsp4e.LSPEclipseUtils; |
| 22 | +import org.eclipse.lsp4e.LanguageServers; |
| 23 | +import org.eclipse.lsp4e.operations.rename.LSPFileOperationParticipantSupport; |
| 24 | +import org.eclipse.lsp4e.test.utils.AbstractTestWithProject; |
| 25 | +import org.eclipse.lsp4e.test.utils.TestUtils; |
| 26 | +import org.eclipse.lsp4e.tests.mock.MockLanguageServer; |
| 27 | +import org.eclipse.lsp4e.tests.mock.MockWorkspaceService; |
| 28 | +import org.eclipse.lsp4j.CreateFilesParams; |
| 29 | +import org.eclipse.lsp4j.DeleteFilesParams; |
| 30 | +import org.eclipse.lsp4j.FileCreate; |
| 31 | +import org.eclipse.lsp4j.FileDelete; |
| 32 | +import org.eclipse.lsp4j.FileOperationFilter; |
| 33 | +import org.eclipse.lsp4j.FileOperationOptions; |
| 34 | +import org.eclipse.lsp4j.FileOperationPattern; |
| 35 | +import org.eclipse.lsp4j.FileOperationPatternKind; |
| 36 | +import org.eclipse.lsp4j.FileOperationsServerCapabilities; |
| 37 | +import org.eclipse.lsp4j.FileRename; |
| 38 | +import org.eclipse.lsp4j.RenameFilesParams; |
| 39 | +import org.eclipse.lsp4j.ServerCapabilities; |
| 40 | +import org.eclipse.lsp4j.WorkspaceServerCapabilities; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | + |
| 43 | +class FolderOperationParticipantsTest extends AbstractTestWithProject { |
| 44 | + |
| 45 | + @Test |
| 46 | + void testFolderFilterGlobMatching() throws Exception { |
| 47 | + // Reconfigure with a folder-only glob filter |
| 48 | + MockLanguageServer.reset(() -> { |
| 49 | + ServerCapabilities caps = MockLanguageServer.defaultServerCapabilities(); |
| 50 | + var ws = new WorkspaceServerCapabilities(); |
| 51 | + var fileOps = new FileOperationsServerCapabilities(); |
| 52 | + var pattern = new FileOperationPattern("**/foo"); |
| 53 | + pattern.setMatches(FileOperationPatternKind.Folder); |
| 54 | + var filter = new FileOperationFilter(pattern, "file"); |
| 55 | + var opts = new FileOperationOptions(List.of(filter)); |
| 56 | + fileOps.setWillRename(opts); |
| 57 | + ws.setFileOperations(fileOps); |
| 58 | + caps.setWorkspace(ws); |
| 59 | + return caps; |
| 60 | + }); |
| 61 | + |
| 62 | + // Start LS with updated capabilities |
| 63 | + IFile starter = TestUtils.createUniqueTestFile(project, "content"); |
| 64 | + TestUtils.openTextViewer(starter); |
| 65 | + TestUtils.waitForAndAssertCondition(5_000, () -> LanguageServers.forProject(project).anyMatching()); |
| 66 | + |
| 67 | + // Create a folder named 'foo' |
| 68 | + IFolder folder = project.getFolder("foo"); |
| 69 | + if (!folder.exists()) { |
| 70 | + folder.create(true, true, null); |
| 71 | + } |
| 72 | + |
| 73 | + var executor = LSPFileOperationParticipantSupport.createFileOperationExecutor(folder, |
| 74 | + FileOperationsServerCapabilities::getWillRename); |
| 75 | + assertTrue(executor.anyMatching()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testFolderWillRename() throws Exception { |
| 80 | + // Enable unfiltered file ops |
| 81 | + MockLanguageServer.reset(() -> { |
| 82 | + ServerCapabilities caps = MockLanguageServer.defaultServerCapabilities(); |
| 83 | + var ws = new WorkspaceServerCapabilities(); |
| 84 | + var fileOps = new FileOperationsServerCapabilities(); |
| 85 | + fileOps.setWillRename(new FileOperationOptions()); |
| 86 | + ws.setFileOperations(fileOps); |
| 87 | + caps.setWorkspace(ws); |
| 88 | + return caps; |
| 89 | + }); |
| 90 | + |
| 91 | + // Start LS with updated capabilities |
| 92 | + IFile starter = TestUtils.createUniqueTestFile(project, "content"); |
| 93 | + TestUtils.openTextViewer(starter); |
| 94 | + TestUtils.waitForAndAssertCondition(5_000, () -> LanguageServers.forProject(project).anyMatching()); |
| 95 | + |
| 96 | + IFolder folder = project.getFolder("oldDir"); |
| 97 | + if (!folder.exists()) { |
| 98 | + folder.create(true, true, null); |
| 99 | + } |
| 100 | + URI oldUri = LSPEclipseUtils.toUri(folder); |
| 101 | + assertNotNull(oldUri); |
| 102 | + |
| 103 | + var executor = LSPFileOperationParticipantSupport.createFileOperationExecutor(folder, |
| 104 | + FileOperationsServerCapabilities::getWillRename); |
| 105 | + assertTrue(executor.anyMatching()); |
| 106 | + |
| 107 | + var params = new RenameFilesParams(); |
| 108 | + URI newUri = LSPEclipseUtils.toUri(project.getFolder("newDir")); |
| 109 | + params.getFiles().add(new FileRename(oldUri.toString(), newUri.toString())); |
| 110 | + |
| 111 | + LSPFileOperationParticipantSupport.computePreChange("rename-folder", params, executor, |
| 112 | + (ws, p) -> ws.willRenameFiles(p)); |
| 113 | + |
| 114 | + MockWorkspaceService ws = MockLanguageServer.INSTANCE.getWorkspaceService(); |
| 115 | + assertNotNull(ws.getLastWillRename()); |
| 116 | + assertEquals(1, ws.getLastWillRename().getFiles().size()); |
| 117 | + assertEquals(oldUri.toString(), ws.getLastWillRename().getFiles().get(0).getOldUri()); |
| 118 | + assertEquals(newUri.toString(), ws.getLastWillRename().getFiles().get(0).getNewUri()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void testFolderWillCreate() throws Exception { |
| 123 | + MockLanguageServer.reset(() -> { |
| 124 | + ServerCapabilities caps = MockLanguageServer.defaultServerCapabilities(); |
| 125 | + var ws = new WorkspaceServerCapabilities(); |
| 126 | + var fileOps = new FileOperationsServerCapabilities(); |
| 127 | + fileOps.setWillCreate(new FileOperationOptions()); |
| 128 | + ws.setFileOperations(fileOps); |
| 129 | + caps.setWorkspace(ws); |
| 130 | + return caps; |
| 131 | + }); |
| 132 | + |
| 133 | + IFile starter = TestUtils.createUniqueTestFile(project, "content"); |
| 134 | + TestUtils.openTextViewer(starter); |
| 135 | + TestUtils.waitForAndAssertCondition(5_000, () -> LanguageServers.forProject(project).anyMatching()); |
| 136 | + |
| 137 | + IFolder folder = project.getFolder("toCreate"); |
| 138 | + URI uri = LSPEclipseUtils.toUri(folder); |
| 139 | + assertNotNull(uri); |
| 140 | + |
| 141 | + var executor = LSPFileOperationParticipantSupport.createFileOperationExecutor(folder, |
| 142 | + FileOperationsServerCapabilities::getWillCreate); |
| 143 | + assertTrue(executor.anyMatching()); |
| 144 | + |
| 145 | + var params = new CreateFilesParams(); |
| 146 | + params.getFiles().add(new FileCreate(uri.toString())); |
| 147 | + |
| 148 | + LSPFileOperationParticipantSupport.computePreChange("create-folder", params, executor, |
| 149 | + (ws, p) -> ws.willCreateFiles(p)); |
| 150 | + |
| 151 | + MockWorkspaceService ws = MockLanguageServer.INSTANCE.getWorkspaceService(); |
| 152 | + assertNotNull(ws.getLastWillCreate()); |
| 153 | + assertEquals(1, ws.getLastWillCreate().getFiles().size()); |
| 154 | + assertEquals(uri.toString(), ws.getLastWillCreate().getFiles().get(0).getUri()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void testFolderWillDelete() throws Exception { |
| 159 | + MockLanguageServer.reset(() -> { |
| 160 | + ServerCapabilities caps = MockLanguageServer.defaultServerCapabilities(); |
| 161 | + var ws = new WorkspaceServerCapabilities(); |
| 162 | + var fileOps = new FileOperationsServerCapabilities(); |
| 163 | + fileOps.setWillDelete(new FileOperationOptions()); |
| 164 | + ws.setFileOperations(fileOps); |
| 165 | + caps.setWorkspace(ws); |
| 166 | + return caps; |
| 167 | + }); |
| 168 | + |
| 169 | + IFile starter = TestUtils.createUniqueTestFile(project, "content"); |
| 170 | + TestUtils.openTextViewer(starter); |
| 171 | + TestUtils.waitForAndAssertCondition(5_000, () -> LanguageServers.forProject(project).anyMatching()); |
| 172 | + |
| 173 | + IFolder folder = project.getFolder("toDelete"); |
| 174 | + if (!folder.exists()) { |
| 175 | + folder.create(true, true, null); |
| 176 | + } |
| 177 | + URI uri = LSPEclipseUtils.toUri(folder); |
| 178 | + assertNotNull(uri); |
| 179 | + |
| 180 | + var executor = LSPFileOperationParticipantSupport.createFileOperationExecutor(folder, |
| 181 | + FileOperationsServerCapabilities::getWillDelete); |
| 182 | + |
| 183 | + var params = new DeleteFilesParams(); |
| 184 | + params.getFiles().add(new FileDelete(uri.toString())); |
| 185 | + |
| 186 | + LSPFileOperationParticipantSupport.computePreChange("delete-folder", params, executor, |
| 187 | + (ws, p) -> ws.willDeleteFiles(p)); |
| 188 | + |
| 189 | + MockWorkspaceService ws = MockLanguageServer.INSTANCE.getWorkspaceService(); |
| 190 | + assertNotNull(ws.getLastWillDelete()); |
| 191 | + assertEquals(1, ws.getLastWillDelete().getFiles().size()); |
| 192 | + assertEquals(uri.toString(), ws.getLastWillDelete().getFiles().get(0).getUri()); |
| 193 | + } |
| 194 | +} |
0 commit comments