Search in sources :

Example 11 with Command

use of org.eclipse.lsp4j.Command in project xtext-core by eclipse.

the class AbstractLanguageServerTest method testCodeAction.

protected void testCodeAction(final Procedure1<? super AbstractLanguageServerTest.TestCodeActionConfiguration> configurator) {
    try {
        @Extension final AbstractLanguageServerTest.TestCodeActionConfiguration configuration = new AbstractLanguageServerTest.TestCodeActionConfiguration();
        configuration.setFilePath(("MyModel." + this.fileExtension));
        configurator.apply(configuration);
        final String filePath = this.initializeContext(configuration).getUri();
        CodeActionParams _codeActionParams = new CodeActionParams();
        final Procedure1<CodeActionParams> _function = (CodeActionParams it) -> {
            TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(filePath);
            it.setTextDocument(_textDocumentIdentifier);
            Range _range = new Range();
            final Procedure1<Range> _function_1 = (Range it_1) -> {
                int _line = configuration.getLine();
                int _column = configuration.getColumn();
                Position _position = new Position(_line, _column);
                it_1.setStart(_position);
                it_1.setEnd(it_1.getStart());
            };
            Range _doubleArrow = ObjectExtensions.<Range>operator_doubleArrow(_range, _function_1);
            it.setRange(_doubleArrow);
            CodeActionContext _codeActionContext = new CodeActionContext();
            final Procedure1<CodeActionContext> _function_2 = (CodeActionContext it_1) -> {
                it_1.setDiagnostics(this.getDiagnostics().get(filePath));
            };
            CodeActionContext _doubleArrow_1 = ObjectExtensions.<CodeActionContext>operator_doubleArrow(_codeActionContext, _function_2);
            it.setContext(_doubleArrow_1);
        };
        CodeActionParams _doubleArrow = ObjectExtensions.<CodeActionParams>operator_doubleArrow(_codeActionParams, _function);
        final CompletableFuture<List<Either<Command, CodeAction>>> result = this.languageServer.codeAction(_doubleArrow);
        if ((configuration.assertCodeActions != null)) {
            configuration.assertCodeActions.apply(result.get());
        } else {
            this.assertEquals(configuration.expectedCodeActions, this.toExpectation(result.get()));
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) FoldingRange(org.eclipse.lsp4j.FoldingRange) Range(org.eclipse.lsp4j.Range) Endpoint(org.eclipse.lsp4j.jsonrpc.Endpoint) Extension(org.eclipse.xtext.xbase.lib.Extension) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList)

Example 12 with Command

use of org.eclipse.lsp4j.Command in project xtext-core by eclipse.

the class CodeLensService method computeCodeLenses.

@Override
public List<? extends CodeLens> computeCodeLenses(Document document, XtextResource resource, CodeLensParams params, CancelIndicator indicator) {
    CodeLens codeLens = new CodeLens();
    Command command = new Command();
    command.setCommand("do.this");
    command.setTitle("Do Awesome Stuff");
    command.setArguments(Lists.newArrayList("foo", Integer.valueOf(1), Boolean.valueOf(true)));
    codeLens.setCommand(command);
    codeLens.setData(new Position(1, 2));
    codeLens.setRange(new Range(new Position(1, 1), new Position(1, 2)));
    return Lists.newArrayList(codeLens);
}
Also used : CodeLens(org.eclipse.lsp4j.CodeLens) Command(org.eclipse.lsp4j.Command) Position(org.eclipse.lsp4j.Position) Range(org.eclipse.lsp4j.Range)

Example 13 with Command

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

the class CodeActionHandler method getCodeActionFromProposal.

private Optional<Either<Command, CodeAction>> getCodeActionFromProposal(ChangeCorrectionProposal proposal, CodeActionContext context) throws CoreException {
    String name = proposal.getName();
    Command command = null;
    if (proposal instanceof CUCorrectionCommandProposal) {
        CUCorrectionCommandProposal commandProposal = (CUCorrectionCommandProposal) proposal;
        command = new Command(name, commandProposal.getCommand(), commandProposal.getCommandArguments());
    } else if (proposal instanceof RefactoringCorrectionCommandProposal) {
        RefactoringCorrectionCommandProposal commandProposal = (RefactoringCorrectionCommandProposal) proposal;
        command = new Command(name, commandProposal.getCommand(), commandProposal.getCommandArguments());
    } else if (proposal instanceof AssignToVariableAssistCommandProposal) {
        AssignToVariableAssistCommandProposal commandProposal = (AssignToVariableAssistCommandProposal) proposal;
        command = new Command(name, commandProposal.getCommand(), commandProposal.getCommandArguments());
    } else {
        if (!this.preferenceManager.getClientPreferences().isResolveCodeActionSupported()) {
            WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(proposal.getChange());
            if (!ChangeUtil.hasChanges(edit)) {
                return Optional.empty();
            }
            command = new Command(name, COMMAND_ID_APPLY_EDIT, Collections.singletonList(edit));
        }
    }
    if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(proposal.getKind())) {
        // TODO: Should set WorkspaceEdit directly instead of Command
        CodeAction codeAction = new CodeAction(name);
        codeAction.setKind(proposal.getKind());
        if (command == null) {
            // lazy resolve the edit.
            codeAction.setData(proposal);
        } else {
            codeAction.setCommand(command);
        }
        codeAction.setDiagnostics(context.getDiagnostics());
        return Optional.of(Either.forRight(codeAction));
    } else {
        return Optional.of(Either.forLeft(command));
    }
}
Also used : Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) CUCorrectionCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.CUCorrectionCommandProposal) RefactoringCorrectionCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.RefactoringCorrectionCommandProposal) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) AssignToVariableAssistCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.AssignToVariableAssistCommandProposal)

Example 14 with Command

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

the class AssignToVariableRefactorTest method testAssignField.

private void testAssignField(ICompilationUnit cu, Range range) throws JavaModelException {
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
    assertEquals(1, codeActions.size());
    Either<Command, CodeAction> codeAction = codeActions.get(0);
    CodeAction action = codeAction.getRight();
    assertEquals(JavaCodeActionKind.REFACTOR_ASSIGN_FIELD, action.getKind());
    assertEquals("Assign statement to new field", action.getTitle());
    Command c = action.getCommand();
    assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, c.getCommand());
    assertNotNull(c.getArguments());
    assertEquals(RefactorProposalUtility.ASSIGN_FIELD_COMMAND, c.getArguments().get(0));
}
Also used : Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either)

Example 15 with Command

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

the class AssignToVariableRefactorTest method testAssignVariable.

private void testAssignVariable(ICompilationUnit cu, Range range) throws JavaModelException {
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
    assertEquals(1, codeActions.size());
    Either<Command, CodeAction> codeAction = codeActions.get(0);
    CodeAction action = codeAction.getRight();
    assertEquals(JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE, action.getKind());
    assertEquals("Assign statement to new local variable", action.getTitle());
    Command c = action.getCommand();
    assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, c.getCommand());
    assertNotNull(c.getArguments());
    assertEquals(RefactorProposalUtility.ASSIGN_VARIABLE_COMMAND, c.getArguments().get(0));
}
Also used : Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either)

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