|
| 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.definition; |
| 13 | + |
| 14 | +import static org.junit.Assert.*; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | + |
| 19 | +import org.eclipse.core.resources.IFile; |
| 20 | +import org.eclipse.jface.text.ITextViewer; |
| 21 | +import org.eclipse.jface.text.Region; |
| 22 | +import org.eclipse.jface.text.hyperlink.IHyperlink; |
| 23 | +import org.eclipse.lsp4e.operations.declaration.OpenDeclarationHyperlinkDetector; |
| 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.MockTextDocumentService; |
| 28 | +import org.eclipse.lsp4j.DeclarationParams; |
| 29 | +import org.eclipse.lsp4j.ImplementationParams; |
| 30 | +import org.eclipse.lsp4j.Location; |
| 31 | +import org.eclipse.lsp4j.LocationLink; |
| 32 | +import org.eclipse.lsp4j.Position; |
| 33 | +import org.eclipse.lsp4j.Range; |
| 34 | +import org.eclipse.lsp4j.ServerCapabilities; |
| 35 | +import org.eclipse.lsp4j.TypeDefinitionParams; |
| 36 | +import org.eclipse.lsp4j.jsonrpc.messages.Either; |
| 37 | +import org.junit.Test; |
| 38 | + |
| 39 | +public class HyperlinkDetectorErrorHandlingTest extends AbstractTestWithProject { |
| 40 | + |
| 41 | + private final OpenDeclarationHyperlinkDetector detector = new OpenDeclarationHyperlinkDetector(); |
| 42 | + |
| 43 | + @Override |
| 44 | + protected ServerCapabilities getServerCapabilities() { |
| 45 | + // Ensure providers are enabled to exercise all branches |
| 46 | + var caps = MockLanguageServer.defaultServerCapabilities(); |
| 47 | + caps.setDefinitionProvider(true); |
| 48 | + caps.setTypeDefinitionProvider(true); |
| 49 | + caps.setDeclarationProvider(true); |
| 50 | + caps.setImplementationProvider(true); |
| 51 | + return caps; |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testDefinitionRemainsWhenTypeDefinitionErrors() throws Exception { |
| 56 | + MockLanguageServer.INSTANCE.setTextDocumentService( |
| 57 | + // Simulate server error for typeDefinition (mirrors issue |
| 58 | + // https://github.com/eclipse-lsp4e/lsp4e/issues/1169) |
| 59 | + new MockTextDocumentService(MockLanguageServer.INSTANCE::buildMaybeDelayedFuture) { |
| 60 | + @Override |
| 61 | + public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> typeDefinition( |
| 62 | + TypeDefinitionParams params) { |
| 63 | + var f = new CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>>(); |
| 64 | + f.completeExceptionally( |
| 65 | + new RuntimeException("unexpected error during typeDefinition retrieval")); |
| 66 | + return f; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> implementation( |
| 71 | + ImplementationParams params) { |
| 72 | + throw new RuntimeException("unexpected error during implementation retrieval"); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> declaration( |
| 77 | + DeclarationParams params) { |
| 78 | + throw new RuntimeException("unexpected error during declaration retrieval"); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + // ensure TextDocumentService is faulty |
| 83 | + assertThrows(RuntimeException.class, |
| 84 | + () -> MockLanguageServer.INSTANCE.getTextDocumentService().declaration(null)); |
| 85 | + assertThrows(RuntimeException.class, |
| 86 | + () -> MockLanguageServer.INSTANCE.getTextDocumentService().implementation(null)); |
| 87 | + assertTrue( |
| 88 | + MockLanguageServer.INSTANCE.getTextDocumentService().typeDefinition(null).isCompletedExceptionally()); |
| 89 | + |
| 90 | + // Configure 1 good definition result |
| 91 | + MockLanguageServer.INSTANCE.setDefinition(List.of( // |
| 92 | + new Location("file://def", new Range(new Position(0, 0), new Position(0, 10))), // |
| 93 | + new Location("file://def", new Range(new Position(1, 10), new Position(1, 20))))); |
| 94 | + |
| 95 | + IFile file = TestUtils.createUniqueTestFile(project, "Example Text"); |
| 96 | + ITextViewer viewer = TestUtils.openTextViewer(file); |
| 97 | + |
| 98 | + IHyperlink[] links = detector.detectHyperlinks(viewer, new Region(0, 0), true); |
| 99 | + |
| 100 | + // Expected: 1 link (from definition) even if typeDefinition fails |
| 101 | + assertNotNull("Hyperlinks should not be null when definition succeeds despite typeDefinition error", links); |
| 102 | + assertEquals(2, links.length); |
| 103 | + } |
| 104 | +} |
0 commit comments