|
| 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.rename; |
| 13 | + |
| 14 | +import static org.eclipse.lsp4e.test.utils.TestUtils.waitForAndAssertCondition; |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | + |
| 17 | +import java.lang.reflect.Method; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.eclipse.core.resources.IFile; |
| 22 | +import org.eclipse.core.runtime.preferences.InstanceScope; |
| 23 | +import org.eclipse.jface.text.IDocument; |
| 24 | +import org.eclipse.jface.text.IRegion; |
| 25 | +import org.eclipse.jface.text.ITextViewer; |
| 26 | +import org.eclipse.jface.text.Region; |
| 27 | +import org.eclipse.jface.text.link.LinkedPosition; |
| 28 | +import org.eclipse.lsp4e.LSPEclipseUtils; |
| 29 | +import org.eclipse.lsp4e.LanguageServerPlugin; |
| 30 | +import org.eclipse.lsp4e.LanguageServerWrapper; |
| 31 | +import org.eclipse.lsp4e.operations.rename.LSPInlineRenameLinkedMode; |
| 32 | +import org.eclipse.lsp4e.test.utils.AbstractTestWithProject; |
| 33 | +import org.eclipse.lsp4e.test.utils.TestUtils; |
| 34 | +import org.eclipse.lsp4e.tests.mock.MockLanguageServer; |
| 35 | +import org.eclipse.lsp4j.DocumentHighlight; |
| 36 | +import org.eclipse.lsp4j.DocumentHighlightKind; |
| 37 | +import org.eclipse.lsp4j.Position; |
| 38 | +import org.eclipse.lsp4j.Range; |
| 39 | +import org.eclipse.lsp4j.TextEdit; |
| 40 | +import org.eclipse.lsp4j.WorkspaceEdit; |
| 41 | +import org.eclipse.lsp4j.jsonrpc.messages.Either; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | + |
| 44 | +/** |
| 45 | + * Tests the behavior of {@link LSPInlineRenameLinkedMode} in isolation from the |
| 46 | + * UI layer to keep the tests deterministic and avoid brittle dependencies on |
| 47 | + * SWT widgets or key-event timing. |
| 48 | + * <p> |
| 49 | + * The scenarios focus on: |
| 50 | + * </p> |
| 51 | + * <ul> |
| 52 | + * <li>Collecting same-file occurrences from |
| 53 | + * {@code textDocument/documentHighlight} and mapping them to {@link IRegion} |
| 54 | + * instances.</li> |
| 55 | + * <li>Driving the non-UI inline rename pipeline, including |
| 56 | + * {@code scheduleRenameJob()}, revert of the in-buffer edit, and application of |
| 57 | + * the {@link WorkspaceEdit} returned by the language server.</li> |
| 58 | + * </ul> |
| 59 | + */ |
| 60 | +class LSPInlineRenameLinkedModeTest extends AbstractTestWithProject { |
| 61 | + |
| 62 | + /** |
| 63 | + * Unit-style test for |
| 64 | + * {@code LSPInlineRenameLinkedMode.collectSameFileOccurrences(...)}. |
| 65 | + * <p> |
| 66 | + * Verifies that document highlights for the caret position are converted into a |
| 67 | + * de-duplicated list of same-file regions and that the primary region is always |
| 68 | + * present as the first element. |
| 69 | + * </p> |
| 70 | + */ |
| 71 | + @Test |
| 72 | + void testInlineLinkedEditingSameFile() throws Exception { |
| 73 | + // Ensure inline rename is enabled for this test |
| 74 | + InstanceScope.INSTANCE.getNode(LanguageServerPlugin.PLUGIN_ID).putBoolean("org.eclipse.lsp4e.inlineRename", //$NON-NLS-1$ |
| 75 | + true); |
| 76 | + |
| 77 | + // Prepare a simple document with two occurrences of the same identifier |
| 78 | + var content = "compute();\ncompute();"; |
| 79 | + IFile file = TestUtils.createUniqueTestFile(project, content); |
| 80 | + IDocument document = LSPEclipseUtils.getDocument(file); |
| 81 | + assertNotNull(document); |
| 82 | + |
| 83 | + int idLength = "compute".length(); |
| 84 | + int firstOffset = content.indexOf("compute"); |
| 85 | + int secondOffset = content.indexOf("compute", firstOffset + 1); |
| 86 | + var primaryRegion = new Region(firstOffset, idLength); |
| 87 | + |
| 88 | + // Configure mock document highlights: |
| 89 | + // - two valid occurrences |
| 90 | + // - a duplicate of the primary occurrence (should be de-duplicated) |
| 91 | + var caretPosition = LSPEclipseUtils.toPosition(firstOffset, document); |
| 92 | + var highlights = new HashMap<Position, List<? extends DocumentHighlight>>(); |
| 93 | + highlights.put(caretPosition, List.of( // |
| 94 | + new DocumentHighlight(new Range(new Position(0, 0), new Position(0, idLength)), |
| 95 | + DocumentHighlightKind.Read), |
| 96 | + new DocumentHighlight(new Range(new Position(1, 0), new Position(1, idLength)), |
| 97 | + DocumentHighlightKind.Read), |
| 98 | + new DocumentHighlight(new Range(new Position(0, 0), new Position(0, idLength)), |
| 99 | + DocumentHighlightKind.Text))); |
| 100 | + MockLanguageServer.INSTANCE.setDocumentHighlights(highlights); |
| 101 | + |
| 102 | + // Call the internal helper directly via reflection to avoid depending on |
| 103 | + // UI/command wiring |
| 104 | + Method collectSameFileOccurrences = LSPInlineRenameLinkedMode.class |
| 105 | + .getDeclaredMethod("collectSameFileOccurrences", IDocument.class, int.class, IRegion.class); //$NON-NLS-1$ |
| 106 | + collectSameFileOccurrences.setAccessible(true); |
| 107 | + |
| 108 | + @SuppressWarnings("unchecked") // |
| 109 | + var regions = (List<IRegion>) collectSameFileOccurrences.invoke(null, document, firstOffset, primaryRegion); |
| 110 | + |
| 111 | + // Primary region is always present and first |
| 112 | + assertEquals(firstOffset, regions.get(0).getOffset()); |
| 113 | + assertEquals(idLength, regions.get(0).getLength()); |
| 114 | + |
| 115 | + // Duplicates must be filtered out |
| 116 | + assertEquals(2, regions.size()); |
| 117 | + assertEquals(secondOffset, regions.get(1).getOffset()); |
| 118 | + assertEquals(idLength, regions.get(1).getLength()); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Drives the inline-rename pipeline without relying on SWT key events. |
| 123 | + * <p> |
| 124 | + * The test simulates a user-typed identifier in the primary linked position, |
| 125 | + * then calls {@code scheduleRenameJob()} and asserts that the |
| 126 | + * {@link WorkspaceEdit} returned by the language server is applied back to the |
| 127 | + * document. |
| 128 | + * </p> |
| 129 | + */ |
| 130 | + @Test |
| 131 | + void testInlineRenameEndToEndSameFile() throws Exception { |
| 132 | + // Prepare a simple document with two occurrences of the same identifier |
| 133 | + var content = "compute();\ncompute();"; |
| 134 | + IFile file = TestUtils.createUniqueTestFile(project, content); |
| 135 | + IDocument document = LSPEclipseUtils.getDocument(file); |
| 136 | + assertNotNull(document); |
| 137 | + |
| 138 | + int idLength = "compute".length(); |
| 139 | + int firstOffset = content.indexOf("compute"); |
| 140 | + int secondOffset = content.indexOf("compute", firstOffset + 1); |
| 141 | + |
| 142 | + // Simulated name typed by the user in linked mode |
| 143 | + String typedName = "inlineName"; |
| 144 | + |
| 145 | + // Prepare rename result for realism (not strictly required for this test) |
| 146 | + Position firstPos = LSPEclipseUtils.toPosition(firstOffset, document); |
| 147 | + Position secondPos = LSPEclipseUtils.toPosition(secondOffset, document); |
| 148 | + |
| 149 | + var prepareRange = new Range(firstPos, new Position(firstPos.getLine(), firstPos.getCharacter() + idLength)); |
| 150 | + MockLanguageServer.INSTANCE.getTextDocumentService().setPrepareRenameResult(Either.forLeft(prepareRange)); |
| 151 | + |
| 152 | + // WorkspaceEdit returned by textDocument/rename: both occurrences updated to |
| 153 | + // typedName. |
| 154 | + // This verifies that scheduleRenameJob(): |
| 155 | + // - restores the original name before calling textDocument/rename |
| 156 | + // - applies the WorkspaceEdit from the language server. |
| 157 | + var edits = new HashMap<String, List<TextEdit>>(); |
| 158 | + String uri = LSPEclipseUtils.toUri(file).toString(); |
| 159 | + edits.put(uri, List.of( |
| 160 | + new TextEdit(new Range(firstPos, new Position(firstPos.getLine(), firstPos.getCharacter() + idLength)), |
| 161 | + typedName), |
| 162 | + new TextEdit( |
| 163 | + new Range(secondPos, new Position(secondPos.getLine(), secondPos.getCharacter() + idLength)), |
| 164 | + typedName))); |
| 165 | + MockLanguageServer.INSTANCE.getTextDocumentService().setRenameEdit(new WorkspaceEdit(edits)); |
| 166 | + |
| 167 | + // Open a viewer so that the document is wired to a text viewer like in real |
| 168 | + // usage |
| 169 | + final ITextViewer viewer = TestUtils.openTextViewer(file); |
| 170 | + |
| 171 | + // Manually construct LSPInlineRenameLinkedMode to avoid brittle UI key-event |
| 172 | + // simulation |
| 173 | + var constructor = LSPInlineRenameLinkedMode.class.getDeclaredConstructor(IDocument.class, ITextViewer.class, |
| 174 | + int.class, IRegion.class, String.class, List.class, LanguageServerWrapper.class); |
| 175 | + constructor.setAccessible(true); |
| 176 | + |
| 177 | + var renameRegion = new Region(firstOffset, idLength); |
| 178 | + List<IRegion> occurrences = List.of(renameRegion, new Region(secondOffset, idLength)); |
| 179 | + var mode = constructor.newInstance(document, viewer, Integer.valueOf(firstOffset), renameRegion, "compute", |
| 180 | + occurrences, null); |
| 181 | + |
| 182 | + // Simulate that the user has typed a new name in the primary linked position. |
| 183 | + // We update the document and the internal LinkedPosition that scheduleRenameJob |
| 184 | + // reads. |
| 185 | + int delta = typedName.length() - idLength; |
| 186 | + document.replace(firstOffset, idLength, typedName); |
| 187 | + document.replace(secondOffset + delta, idLength, typedName); |
| 188 | + |
| 189 | + var linkedPosField = LSPInlineRenameLinkedMode.class.getDeclaredField("linkedPosition"); |
| 190 | + linkedPosField.setAccessible(true); |
| 191 | + linkedPosField.set(mode, new LinkedPosition(document, firstOffset, typedName.length())); |
| 192 | + |
| 193 | + // Invoke scheduleRenameJob() directly; it will: |
| 194 | + // - restore the original identifier |
| 195 | + // - call textDocument/rename(typedName) |
| 196 | + // - apply the WorkspaceEdit we configured above. |
| 197 | + Method schedule = LSPInlineRenameLinkedMode.class.getDeclaredMethod("scheduleRenameJob"); |
| 198 | + schedule.setAccessible(true); |
| 199 | + schedule.invoke(mode); |
| 200 | + |
| 201 | + waitForAndAssertCondition("Inline rename workspace edit not applied", 5_000, () -> { |
| 202 | + assertEquals(typedName + "();\n" + typedName + "();", document.get()); |
| 203 | + return true; |
| 204 | + }); |
| 205 | + } |
| 206 | + |
| 207 | +} |
0 commit comments