-
Notifications
You must be signed in to change notification settings - Fork 65
feat: support dynamic registration for textDocument/completion #1361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
...e.lsp4e.test/src/org/eclipse/lsp4e/test/completion/DynamicCompletionRegistrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2025 Vegard IT GmbH and others. | ||
| * This program and the accompanying materials are made | ||
| * available under the terms of the Eclipse Public License 2.0 | ||
| * which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * Sebastian Thomschke (Vegard IT GmbH) - initial implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.lsp4e.test.completion; | ||
|
|
||
| import static org.eclipse.lsp4e.test.utils.TestUtils.waitForAndAssertCondition; | ||
| import static org.junit.Assert.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.eclipse.core.resources.IFile; | ||
| import org.eclipse.jface.text.ITextViewer; | ||
| import org.eclipse.jface.text.contentassist.ICompletionProposal; | ||
| import org.eclipse.lsp4e.operations.completion.LSContentAssistProcessor; | ||
| import org.eclipse.lsp4e.test.utils.AbstractTestWithProject; | ||
| import org.eclipse.lsp4e.test.utils.TestUtils; | ||
| import org.eclipse.lsp4e.tests.mock.MockLanguageServer; | ||
| import org.eclipse.lsp4j.CompletionItem; | ||
| import org.eclipse.lsp4j.CompletionItemKind; | ||
| import org.eclipse.lsp4j.CompletionList; | ||
| import org.eclipse.lsp4j.CompletionOptions; | ||
| import org.eclipse.lsp4j.Position; | ||
| import org.eclipse.lsp4j.Range; | ||
| import org.eclipse.lsp4j.Registration; | ||
| import org.eclipse.lsp4j.RegistrationParams; | ||
| import org.eclipse.lsp4j.TextEdit; | ||
| import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
| import org.eclipse.lsp4j.services.LanguageClient; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import com.google.gson.Gson; | ||
|
|
||
| /** | ||
| * Verifies that dynamic registration of completion updates LSP4E server | ||
| * capabilities and enables content assist proposals. | ||
| */ | ||
| public class DynamicCompletionRegistrationTest extends AbstractTestWithProject { | ||
|
|
||
| private LSContentAssistProcessor contentAssistProcessor; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| contentAssistProcessor = new LSContentAssistProcessor(true, false); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDynamicCompletionRegistrationProvidesProposalsAndTriggers() throws Exception { | ||
| // Prepare a file and open a viewer | ||
| IFile file = TestUtils.createUniqueTestFile(project, ""); | ||
| ITextViewer viewer = TestUtils.openTextViewer(file); | ||
|
|
||
| // Ensure the mock LS is up | ||
| waitForAndAssertCondition(5_000, () -> !MockLanguageServer.INSTANCE.getRemoteProxies().isEmpty()); | ||
| LanguageClient client = getMockClient(); | ||
| assertNotNull(client); | ||
|
|
||
| // Provide a simple completion item in the mock LS | ||
| var items = new ArrayList<CompletionItem>(); | ||
| var item = new CompletionItem(); | ||
| item.setLabel("Alpha"); | ||
| item.setKind(CompletionItemKind.Text); | ||
| item.setTextEdit(Either.forLeft(new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), "Alpha"))); | ||
| items.add(item); | ||
| MockLanguageServer.INSTANCE.setCompletionList(new CompletionList(false, items)); | ||
|
|
||
| // Dynamically register completion with trigger characters (server-like behavior) | ||
| var registration = new Registration(); | ||
| registration.setId("test-completion-reg"); | ||
| registration.setMethod("textDocument/completion"); | ||
| var opts = new CompletionOptions(); | ||
| opts.setTriggerCharacters(List.of(".", "/", "#")); | ||
| registration.setRegisterOptions(new Gson().toJsonTree(opts)); | ||
| client.registerCapability(new RegistrationParams(List.of(registration))).get(2, TimeUnit.SECONDS); | ||
|
|
||
| // Compute proposals (manual invocation). Should return the mock item. | ||
| ICompletionProposal[] proposals = contentAssistProcessor.computeCompletionProposals(viewer, 0); | ||
| assertEquals(1, proposals.length); | ||
| assertEquals("Alpha", proposals[0].getDisplayString()); | ||
|
|
||
| // Ask processor for auto-activation triggers and assert they include the registered ones | ||
| char[] triggers = contentAssistProcessor.getCompletionProposalAutoActivationCharacters(); | ||
| String trig = new String(triggers != null ? triggers : new char[0]); | ||
| assertTrue(trig.indexOf('.') >= 0); | ||
| assertTrue(trig.indexOf('/') >= 0); | ||
| assertTrue(trig.indexOf('#') >= 0); | ||
| } | ||
|
|
||
| private LanguageClient getMockClient() { | ||
| var proxies = MockLanguageServer.INSTANCE.getRemoteProxies(); | ||
| assertEquals(1, proxies.size()); | ||
| return proxies.get(0); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sebthom:
I think the blocks for
case "workspace/executeCommand"andcase "textDocument/completion"should be structured in the same way. With that in mind, should we not just re-raise the exception to the caller, as he provided a malformed Json as we would do incase "workspace/executeCommand"on line 1071?Or alternative, what exceptions are we catching? A
JsonSyntaxException? If so, could we be more specific and catch just that one and then do the same for the blockcase "workspace/executeCommand"?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it was for the gson. I thought rather catch any error and not have that capability than not initialize the LS properly at all.