Search in sources :

Example 1 with CodeActionContext

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

the class AbstractQuickFixTest method evaluateCodeActions.

protected List<Command> evaluateCodeActions(ICompilationUnit cu) throws JavaModelException {
    CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
    IProblem[] problems = astRoot.getProblems();
    Range range = getRange(cu, problems);
    CodeActionParams parms = new CodeActionParams();
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
    textDocument.setUri(JDTUtils.toURI(cu));
    parms.setTextDocument(textDocument);
    parms.setRange(range);
    CodeActionContext context = new CodeActionContext();
    context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems)));
    parms.setContext(context);
    List<Command> commands = new CodeActionHandler().getCodeActionCommands(parms, new NullProgressMonitor());
    if (this.ignoredCommands != null) {
        List<Command> filteredList = new ArrayList<>();
        for (Command command : commands) {
            for (String str : this.ignoredCommands) {
                if (command.getTitle().matches(str)) {
                    filteredList.add(command);
                    break;
                }
            }
        }
        commands.removeAll(filteredList);
    }
    return commands;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) ArrayList(java.util.ArrayList) Range(org.eclipse.lsp4j.Range) IProblem(org.eclipse.jdt.core.compiler.IProblem) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Command(org.eclipse.lsp4j.Command)

Example 2 with CodeActionContext

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

the class CodeActionHandlerTest method testCodeAction_removeUnterminatedString.

@Test
public void testCodeAction_removeUnterminatedString() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + "	void foo() {\n" + "String s = \"some str\n" + "	}\n" + "}\n");
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    final Range range = getRange(unit, "some str");
    params.setRange(range);
    params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UnterminatedString), range))));
    List<? extends Command> commands = server.codeAction(params).join();
    Assert.assertNotNull(commands);
    Assert.assertEquals(1, commands.size());
    Command c = commands.get(0);
    Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test)

Example 3 with CodeActionContext

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

the class DocumentLifeCycleHandlerTest method getCodeActions.

protected List<Command> getCodeActions(ICompilationUnit cu) throws JavaModelException {
    CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
    IProblem[] problems = astRoot.getProblems();
    Range range = getRange(cu, problems);
    CodeActionParams parms = new CodeActionParams();
    TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
    textDocument.setUri(JDTUtils.toURI(cu));
    parms.setTextDocument(textDocument);
    parms.setRange(range);
    CodeActionContext context = new CodeActionContext();
    context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems)));
    parms.setContext(context);
    return new CodeActionHandler().getCodeActionCommands(parms, new NullProgressMonitor());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Range(org.eclipse.lsp4j.Range) Lsp4jAssertions.assertRange(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertRange) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 4 with CodeActionContext

use of org.eclipse.lsp4j.CodeActionContext 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 5 with CodeActionContext

use of org.eclipse.lsp4j.CodeActionContext 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)

Aggregations

CodeActionContext (org.eclipse.lsp4j.CodeActionContext)10 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)10 Range (org.eclipse.lsp4j.Range)10 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)10 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 Command (org.eclipse.lsp4j.Command)6 Position (org.eclipse.lsp4j.Position)5 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)4 Test (org.junit.Test)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 IProblem (org.eclipse.jdt.core.compiler.IProblem)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 CompletionList (org.eclipse.lsp4j.CompletionList)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableList (com.google.common.collect.ImmutableList)1 Multimap (com.google.common.collect.Multimap)1