-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support click on inlayhint label parts
- Loading branch information
Showing
2 changed files
with
209 additions
and
1 deletion.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
...4e.test/src/org/eclipse/lsp4e/test/operations/inlayhint/LSPLineContentCodeMiningTest.java
This file contains 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,115 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Broadcom, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Broadcom, Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lsp4e.test.operations.inlayhint; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.jface.text.IDocument; | ||
import org.eclipse.jface.text.ITextViewer; | ||
import org.eclipse.lsp4e.LanguageServerWrapper; | ||
import org.eclipse.lsp4e.LanguageServersRegistry; | ||
import org.eclipse.lsp4e.LanguageServiceAccessor; | ||
import org.eclipse.lsp4e.operations.inlayhint.InlayHintProvider; | ||
import org.eclipse.lsp4e.operations.inlayhint.LSPLineContentCodeMining; | ||
import org.eclipse.lsp4e.test.utils.AllCleanRule; | ||
import org.eclipse.lsp4e.test.utils.TestUtils; | ||
import org.eclipse.lsp4e.tests.mock.MockLanguageServer; | ||
import org.eclipse.lsp4j.Command; | ||
import org.eclipse.lsp4j.ExecuteCommandParams; | ||
import org.eclipse.lsp4j.InlayHint; | ||
import org.eclipse.lsp4j.InlayHintLabelPart; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.swt.SWT; | ||
import org.eclipse.swt.events.MouseEvent; | ||
import org.eclipse.swt.widgets.Display; | ||
import org.eclipse.swt.widgets.Event; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonPrimitive; | ||
|
||
public class LSPLineContentCodeMiningTest { | ||
|
||
private static final String MOCK_SERVER_ID = "org.eclipse.lsp4e.test.server"; | ||
|
||
@Rule | ||
public AllCleanRule clear = new AllCleanRule(); | ||
private IProject project; | ||
|
||
@Before | ||
public void setUp() throws CoreException { | ||
project = TestUtils.createProject(getClass().getName() + System.currentTimeMillis()); | ||
} | ||
|
||
@Test | ||
public void singleLabelPartCommand() | ||
throws Exception { | ||
final InlayHint inlay = createMultiLabelInlayHint(createInlayLabelPart("Label-Text", MockLanguageServer.SUPPORTED_COMMAND_ID)); | ||
Command command = inlay.getLabel().getRight().get(0).getCommand(); | ||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.addProperty("bar", 42); | ||
command.setArguments(Arrays.asList(new JsonPrimitive("Foo"), jsonObject)); | ||
|
||
// Setup test data | ||
IFile file = TestUtils.createUniqueTestFile(project, "lspt", "test content"); | ||
ITextViewer textViewer = TestUtils.openTextViewer(file); | ||
IDocument document = textViewer.getDocument(); | ||
|
||
MockLanguageServer languageServer = MockLanguageServer.INSTANCE; | ||
InlayHintProvider provider = new InlayHintProvider(); | ||
|
||
LanguageServerWrapper wrapper = LanguageServiceAccessor.getLSWrapper(project, LanguageServersRegistry.getInstance().getDefinition(MOCK_SERVER_ID)); | ||
|
||
LSPLineContentCodeMining sut = new LSPLineContentCodeMining(inlay, document, wrapper, provider); | ||
MouseEvent mouseEvent = createMouseEvent(); | ||
sut.getAction().accept(mouseEvent); | ||
|
||
// We expect that the language server will be called to execute the command | ||
ExecuteCommandParams executedCommand = languageServer.getWorkspaceService().getExecutedCommand().get(5, | ||
TimeUnit.SECONDS); | ||
|
||
assertEquals(MockLanguageServer.SUPPORTED_COMMAND_ID, executedCommand.getCommand()); | ||
assertEquals(command.getArguments(), executedCommand.getArguments()); | ||
} | ||
|
||
private static InlayHintLabelPart createInlayLabelPart(String text, String commandID) { | ||
InlayHintLabelPart labelPart = new InlayHintLabelPart(text); | ||
Command command = new Command(text, commandID); | ||
labelPart.setCommand(command); | ||
return labelPart; | ||
} | ||
|
||
private static InlayHint createMultiLabelInlayHint(InlayHintLabelPart... parts) { | ||
InlayHint inlay = new InlayHint(); | ||
inlay.setLabel(Arrays.asList(parts)); | ||
inlay.setPosition(new Position(0, 0)); | ||
return inlay; | ||
} | ||
|
||
private static MouseEvent createMouseEvent() { | ||
Event event = new Event(); | ||
event.button = SWT.BUTTON1; | ||
Display display = Display.getCurrent(); | ||
event.widget = display.getSystemTray(); | ||
return new MouseEvent(event); | ||
} | ||
|
||
} |
This file contains 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