Search in sources :

Example 6 with Command

use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.

the class CodeActionHandler method textEditToCommand.

private static Command textEditToCommand(ICompilationUnit unit, String label, TextEdit textEdit) {
    TextEditConverter converter = new TextEditConverter(unit, textEdit);
    String uri = JDTUtils.toURI(unit);
    WorkspaceEdit $ = new WorkspaceEdit();
    $.getChanges().put(uri, converter.convert());
    return new Command(label, COMMAND_ID_APPLY_EDIT, Arrays.asList($));
}
Also used : Command(org.eclipse.lsp4j.Command) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextEditConverter(org.eclipse.jdt.ls.core.internal.TextEditConverter)

Example 7 with Command

use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.

the class CodeLensHandlerTest method testResolveImplementationsCodeLense.

@SuppressWarnings("unchecked")
@Test
public void testResolveImplementationsCodeLense() {
    String source = "src/java/IFoo.java";
    String payload = createCodeLensImplementationsRequest(source, 5, 17, 21);
    CodeLens lens = getParams(payload);
    Range range = lens.getRange();
    assertRange(5, 17, 21, range);
    CodeLens result = handler.resolve(lens, monitor);
    assertNotNull(result);
    // Check if command found
    Command command = result.getCommand();
    assertNotNull(command);
    assertEquals("2 implementations", command.getTitle());
    assertEquals("java.show.implementations", command.getCommand());
    // Check codelens args
    List<Object> args = command.getArguments();
    assertEquals(3, args.size());
    // Check we point to the Bar class
    String sourceUri = args.get(0).toString();
    assertTrue(sourceUri.endsWith("IFoo.java"));
    // CodeLens position
    Position p = (Position) args.get(1);
    assertEquals(5, p.getLine());
    assertEquals(17, p.getCharacter());
    // Reference location
    List<Location> locations = (List<Location>) args.get(2);
    assertEquals(2, locations.size());
    Location loc = locations.get(0);
    assertTrue(loc.getUri().endsWith("src/java/Foo2.java"));
    assertRange(5, 13, 17, loc.getRange());
}
Also used : CodeLens(org.eclipse.lsp4j.CodeLens) Command(org.eclipse.lsp4j.Command) Position(org.eclipse.lsp4j.Position) List(java.util.List) Range(org.eclipse.lsp4j.Range) Lsp4jAssertions.assertRange(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertRange) Location(org.eclipse.lsp4j.Location) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 8 with Command

use of org.eclipse.lsp4j.Command in project ballerina by ballerina-lang.

the class CommandUtil method getCommandsByDiagnostic.

/**
 * Get the command instances for a given diagnostic.
 * @param diagnostic        Diagnostic to get the command against
 * @param params            Code Action parameters
 * @param lsPackageCache    Lang Server Package cache
 * @return  {@link List}    List of commands related to the given diagnostic
 */
public static List<Command> getCommandsByDiagnostic(Diagnostic diagnostic, CodeActionParams params, LSPackageCache lsPackageCache) {
    String diagnosticMessage = diagnostic.getMessage();
    List<Command> commands = new ArrayList<>();
    if (isUndefinedPackage(diagnosticMessage)) {
        String packageAlias = diagnosticMessage.substring(diagnosticMessage.indexOf("'") + 1, diagnosticMessage.lastIndexOf("'"));
        LSDocument sourceDocument = new LSDocument(params.getTextDocument().getUri());
        Path openedPath = CommonUtil.getPath(sourceDocument);
        String sourceRoot = TextDocumentServiceUtil.getSourceRoot(openedPath);
        sourceDocument.setSourceRoot(sourceRoot);
        lsPackageCache.getPackageMap().entrySet().stream().filter(pkgEntry -> {
            String fullPkgName = pkgEntry.getValue().packageID.orgName.getValue() + "/" + pkgEntry.getValue().packageID.getName().getValue();
            return fullPkgName.endsWith("." + packageAlias) || fullPkgName.endsWith("/" + packageAlias);
        }).forEach(pkgEntry -> {
            PackageID packageID = pkgEntry.getValue().packageID;
            String commandTitle = CommandConstants.IMPORT_PKG_TITLE + " " + packageID.getName().toString();
            String fullPkgName = packageID.getOrgName() + "/" + packageID.getName().getValue();
            CommandArgument pkgArgument = new CommandArgument(CommandConstants.ARG_KEY_PKG_NAME, fullPkgName);
            CommandArgument docUriArgument = new CommandArgument(CommandConstants.ARG_KEY_DOC_URI, params.getTextDocument().getUri());
            commands.add(new Command(commandTitle, CommandConstants.CMD_IMPORT_PACKAGE, new ArrayList<>(Arrays.asList(pkgArgument, docUriArgument))));
        });
    }
    return commands;
}
Also used : Path(java.nio.file.Path) CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) CommandConstants(org.ballerinalang.langserver.common.constants.CommandConstants) Diagnostic(org.eclipse.lsp4j.Diagnostic) ArrayList(java.util.ArrayList) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) LSDocument(org.ballerinalang.langserver.common.LSDocument) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) DocTag(org.ballerinalang.model.elements.DocTag) Locale(java.util.Locale) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) Path(java.nio.file.Path) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) PackageID(org.ballerinalang.model.elements.PackageID) FunctionNode(org.ballerinalang.model.tree.FunctionNode) LSPackageCache(org.ballerinalang.langserver.LSPackageCache) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) TextDocumentServiceUtil(org.ballerinalang.langserver.TextDocumentServiceUtil) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) Command(org.eclipse.lsp4j.Command) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) Collections(java.util.Collections) Command(org.eclipse.lsp4j.Command) LSDocument(org.ballerinalang.langserver.common.LSDocument) ArrayList(java.util.ArrayList) PackageID(org.ballerinalang.model.elements.PackageID)

Example 9 with Command

use of org.eclipse.lsp4j.Command in project sts4 by spring-projects.

the class LanguageServerHarness method getCodeActions.

public List<CodeAction> getCodeActions(TextDocumentInfo doc, Diagnostic problem) throws Exception {
    CodeActionContext context = new CodeActionContext(ImmutableList.of(problem));
    List<? extends Command> actions = getServer().getTextDocumentService().codeAction(new CodeActionParams(doc.getId(), problem.getRange(), context)).get();
    return actions.stream().map((command) -> new CodeAction(this, command)).collect(Collectors.toList());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) Arrays(java.util.Arrays) MultimapBuilder(com.google.common.collect.MultimapBuilder) LanguageServerTestListener(org.springframework.ide.vscode.commons.languageserver.util.LanguageServerTestListener) DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) MessageActionItem(org.eclipse.lsp4j.MessageActionItem) Future(java.util.concurrent.Future) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) Duration(java.time.Duration) Map(java.util.Map) Path(java.nio.file.Path) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) ExceptionUtil(org.springframework.ide.vscode.commons.util.ExceptionUtil) IOUtil(org.springframework.ide.vscode.commons.util.IOUtil) HighlightParams(org.springframework.ide.vscode.commons.languageserver.HighlightParams) Assert(org.springframework.ide.vscode.commons.util.Assert) DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) Assert.assertFalse(org.junit.Assert.assertFalse) SimpleLanguageServer(org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer) LanguageClientAware(org.eclipse.lsp4j.services.LanguageClientAware) ApplyWorkspaceEditParams(org.eclipse.lsp4j.ApplyWorkspaceEditParams) Callable(java.util.concurrent.Callable) Diagnostic(org.eclipse.lsp4j.Diagnostic) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Hover(org.eclipse.lsp4j.Hover) ArrayList(java.util.ArrayList) FileChangeType(org.eclipse.lsp4j.FileChangeType) TextDocumentItem(org.eclipse.lsp4j.TextDocumentItem) TextEdit(org.eclipse.lsp4j.TextEdit) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) UriUtil(org.springframework.ide.vscode.commons.util.UriUtil) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) InitializeResult(org.eclipse.lsp4j.InitializeResult) Files(java.nio.file.Files) ProgressParams(org.springframework.ide.vscode.commons.languageserver.ProgressParams) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams) Assert.assertTrue(org.junit.Assert.assertTrue) ClasspathListenerParams(org.springframework.ide.vscode.commons.languageserver.jdt.ls.ClasspathListenerParams) Mono(reactor.core.publisher.Mono) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) File(java.io.File) TextDocumentSyncOptions(org.eclipse.lsp4j.TextDocumentSyncOptions) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) STS4LanguageClient(org.springframework.ide.vscode.commons.languageserver.STS4LanguageClient) Paths(java.nio.file.Paths) Condition(org.assertj.core.api.Condition) ClientCapabilities(org.eclipse.lsp4j.ClientCapabilities) Assert.assertEquals(org.junit.Assert.assertEquals) ApplyWorkspaceEditResponse(org.eclipse.lsp4j.ApplyWorkspaceEditResponse) WorkspaceClientCapabilities(org.eclipse.lsp4j.WorkspaceClientCapabilities) Random(java.util.Random) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) FileEvent(org.eclipse.lsp4j.FileEvent) ShowMessageRequestParams(org.eclipse.lsp4j.ShowMessageRequestParams) CodeLens(org.eclipse.lsp4j.CodeLens) Location(org.eclipse.lsp4j.Location) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Assert.fail(org.junit.Assert.fail) TextDocumentClientCapabilities(org.eclipse.lsp4j.TextDocumentClientCapabilities) URI(java.net.URI) DidCloseTextDocumentParams(org.eclipse.lsp4j.DidCloseTextDocumentParams) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) ProjectResponse(org.springframework.ide.vscode.commons.languageserver.ProjectResponse) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) CompletionItem(org.eclipse.lsp4j.CompletionItem) List(java.util.List) Command(org.eclipse.lsp4j.Command) DidOpenTextDocumentParams(org.eclipse.lsp4j.DidOpenTextDocumentParams) Entry(java.util.Map.Entry) SimpleLanguageServerWrapper(org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServerWrapper) InitializeParams(org.eclipse.lsp4j.InitializeParams) Settings(org.springframework.ide.vscode.commons.languageserver.util.Settings) DocumentSymbolParams(org.eclipse.lsp4j.DocumentSymbolParams) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Range(org.eclipse.lsp4j.Range) Multimap(com.google.common.collect.Multimap) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) Charset(java.nio.charset.Charset) ExecuteCommandCapabilities(org.eclipse.lsp4j.ExecuteCommandCapabilities) ImmutableList(com.google.common.collect.ImmutableList) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) MessageParams(org.eclipse.lsp4j.MessageParams) CursorMovement(org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixEdit.CursorMovement) Position(org.eclipse.lsp4j.Position) CodeLensParams(org.eclipse.lsp4j.CodeLensParams) CompletionItemCapabilities(org.eclipse.lsp4j.CompletionItemCapabilities) CompletionList(org.eclipse.lsp4j.CompletionList) DidChangeConfigurationParams(org.eclipse.lsp4j.DidChangeConfigurationParams) CompletionCapabilities(org.eclipse.lsp4j.CompletionCapabilities) TextDocumentSyncKind(org.eclipse.lsp4j.TextDocumentSyncKind) LanguageId(org.springframework.ide.vscode.commons.util.text.LanguageId) RegistrationParams(org.eclipse.lsp4j.RegistrationParams) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TimeUnit(java.util.concurrent.TimeUnit) Collections(java.util.Collections) InputStream(java.io.InputStream) CodeActionContext(org.eclipse.lsp4j.CodeActionContext)

Example 10 with Command

use of org.eclipse.lsp4j.Command in project sts4 by spring-projects.

the class WebFluxCodeLensProviderTest method containsCodeLens.

private boolean containsCodeLens(List<? extends CodeLens> codeLenses, String commandTitle, int startLine, int startPosition, int endLine, int endPosition) {
    for (CodeLens codeLens : codeLenses) {
        Command command = codeLens.getCommand();
        Range range = codeLens.getRange();
        if (command.getTitle().equals(commandTitle) && range.getStart().getLine() == startLine && range.getStart().getCharacter() == startPosition && range.getEnd().getLine() == endLine && range.getEnd().getCharacter() == endPosition) {
            return true;
        }
    }
    return false;
}
Also used : CodeLens(org.eclipse.lsp4j.CodeLens) Command(org.eclipse.lsp4j.Command) Range(org.eclipse.lsp4j.Range)

Aggregations

Command (org.eclipse.lsp4j.Command)95 CodeAction (org.eclipse.lsp4j.CodeAction)58 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)53 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 Test (org.junit.Test)46 Range (org.eclipse.lsp4j.Range)36 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)35 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)26 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)23 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)23 List (java.util.List)22 Position (org.eclipse.lsp4j.Position)22 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)21 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)17 Diagnostic (org.eclipse.lsp4j.Diagnostic)17 Arrays (java.util.Arrays)14 Collections (java.util.Collections)14 JDTUtils (org.eclipse.jdt.ls.core.internal.JDTUtils)13 CodeActionKind (org.eclipse.lsp4j.CodeActionKind)13 Optional (java.util.Optional)12