From d5595072973ebfdc001de7a145f39208d0ad747f Mon Sep 17 00:00:00 2001 From: Roland Grunberg Date: Fri, 5 Apr 2024 11:16:30 -0400 Subject: [PATCH] Correctly declare support for "workspace/willRenameFiles". - The server capabilities in the initialize response must contain a "willRename" object in the workspace's "fileOperations" object if the client indicates support for the request. Signed-off-by: Roland Grunberg --- .../ls/core/internal/handlers/InitHandler.java | 14 ++++++++++++++ .../preferences/ClientPreferences.java | 4 ++++ .../internal/handlers/InitHandlerTest.java | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java index 4500be4c64..3978a35056 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandler.java @@ -52,6 +52,11 @@ import org.eclipse.lsp4j.DocumentFilter; import org.eclipse.lsp4j.DocumentOnTypeFormattingOptions; import org.eclipse.lsp4j.ExecuteCommandOptions; +import org.eclipse.lsp4j.FileOperationFilter; +import org.eclipse.lsp4j.FileOperationOptions; +import org.eclipse.lsp4j.FileOperationPattern; +import org.eclipse.lsp4j.FileOperationPatternKind; +import org.eclipse.lsp4j.FileOperationsServerCapabilities; import org.eclipse.lsp4j.InitializeParams; import org.eclipse.lsp4j.InitializeResult; import org.eclipse.lsp4j.SaveOptions; @@ -219,6 +224,15 @@ public void registerCapabilities(InitializeResult initializeResult) { WorkspaceFoldersOptions wsFoldersOptions = new WorkspaceFoldersOptions(); wsFoldersOptions.setSupported(Boolean.TRUE); wsFoldersOptions.setChangeNotifications(Boolean.TRUE); + + if (preferenceManager.getClientPreferences().isWorkspaceWillRenameFilesSupported()) { + FileOperationsServerCapabilities wsFileOperations = new FileOperationsServerCapabilities(); + FileOperationPattern fileOpPattern = new FileOperationPattern("**/*.java"); + fileOpPattern.setMatches(FileOperationPatternKind.File); + wsFileOperations.setWillRename(new FileOperationOptions(List.of(new FileOperationFilter(fileOpPattern, "file")))); + wsCapabilities.setFileOperations(wsFileOperations); + } + wsCapabilities.setWorkspaceFolders(wsFoldersOptions); capabilities.setWorkspace(wsCapabilities); diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java index 74cd454160..4ad13ba811 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/ClientPreferences.java @@ -65,6 +65,10 @@ public boolean isWorkspaceFoldersSupported() { return capabilities.getWorkspace() != null && isTrue(capabilities.getWorkspace().getWorkspaceFolders()); } + public boolean isWorkspaceWillRenameFilesSupported() { + return v3supported && capabilities.getWorkspace() != null && capabilities.getWorkspace().getFileOperations() != null && isTrue(capabilities.getWorkspace().getFileOperations().getWillRename()); + } + public boolean isCompletionDynamicRegistered() { return v3supported && isDynamicRegistrationSupported(capabilities.getTextDocument().getCompletion()); } diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandlerTest.java index 70e544af44..a4888454fd 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/InitHandlerTest.java @@ -76,6 +76,9 @@ import org.eclipse.lsp4j.ExecuteCommandOptions; import org.eclipse.lsp4j.FileChangeType; import org.eclipse.lsp4j.FileEvent; +import org.eclipse.lsp4j.FileOperationOptions; +import org.eclipse.lsp4j.FileOperationPatternKind; +import org.eclipse.lsp4j.FileOperationsServerCapabilities; import org.eclipse.lsp4j.FileSystemWatcher; import org.eclipse.lsp4j.InitializeParams; import org.eclipse.lsp4j.InitializeResult; @@ -368,6 +371,21 @@ public void testWatchers() throws Exception { assertEquals(newWatchers, watchers); } + @Test + public void testWorkspaceWillRenameFiles() throws Exception { + ClientPreferences mockCapabilies = mock(ClientPreferences.class); + when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies); + when(mockCapabilies.isWorkspaceWillRenameFilesSupported()).thenReturn(Boolean.TRUE); + InitializeResult result = initialize(true); + FileOperationsServerCapabilities fileOpSrvCap = result.getCapabilities().getWorkspace().getFileOperations(); + assertNotNull(fileOpSrvCap); + FileOperationOptions fileOpOps = fileOpSrvCap.getWillRename(); + assertNotNull(fileOpOps); + assertEquals("file", fileOpOps.getFilters().get(0).getScheme()); + assertEquals(FileOperationPatternKind.File, fileOpOps.getFilters().get(0).getPattern().getMatches()); + assertEquals("**/*.java", fileOpOps.getFilters().get(0).getPattern().getGlob()); + } + // https://github.com/redhat-developer/vscode-java/issues/2429 @Test public void testSettingsWatchers() throws Exception {