@Test public void testCodeAction_exception() throws JavaModelException { URI uri = project.getFile("nopackage/Test.java").getRawLocationURI(); ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri); try { cu.becomeWorkingCopy(new NullProgressMonitor()); CodeActionParams params = new CodeActionParams(); params.setTextDocument(new TextDocumentIdentifier(uri.toString())); final Range range = new Range(); range.setStart(new Position(0, 17)); range.setEnd(new Position(0, 17)); params.setRange(range); CodeActionContext context = new CodeActionContext(); context.setDiagnostics(Collections.emptyList()); params.setContext(context); List<? extends Command> commands = server.codeAction(params).join(); Assert.assertNotNull(commands); Assert.assertEquals(0, commands.size()); } finally { cu.discardWorkingCopy(); } }
@Override public CompletableFuture<List<? extends Command>> codeAction(CodeActionParams params) { logInfo(">> document/codeAction"); CodeActionHandler handler = new CodeActionHandler(); return computeAsync((cc) -> { IProgressMonitor monitor = toMonitor(cc); try { Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, monitor); } catch (OperationCanceledException ignorable) { // No need to pollute logs when query is cancelled } catch (InterruptedException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } return handler.getCodeActionCommands(params, toMonitor(cc)); }); }
@Test public void testCodeAction_removeUnusedImport() throws Exception{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "import java.sql.*; \n" + "public class Foo {\n"+ " void foo() {\n"+ " }\n"+ "}\n"); CodeActionParams params = new CodeActionParams(); params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit))); final Range range = getRange(unit, "java.sql"); params.setRange(range); params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UnusedImport), range)))); List<? extends Command> commands = server.codeAction(params).join(); Assert.assertNotNull(commands); Assert.assertEquals(2, commands.size()); Command c = commands.get(0); Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand()); }
@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()); }
protected List<Command> evaluateCodeActions(ICompilationUnit cu) throws JavaModelException { CompilationUnit astRoot = SharedASTProvider.getInstance().getAST(cu, 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(Arrays.asList(problems))); parms.setContext(context); return new CodeActionHandler().getCodeActionCommands(parms, new NullProgressMonitor()); }
@Override public List<? extends Command> getCodeActions(final Document document, final XtextResource resource, final CodeActionParams params, final CancelIndicator indicator) { final ArrayList<Command> commands = CollectionLiterals.<Command>newArrayList(); List<Diagnostic> _diagnostics = params.getContext().getDiagnostics(); for (final Diagnostic d : _diagnostics) { String _code = d.getCode(); if (_code != null) { switch (_code) { case TestLanguageValidator.INVALID_NAME: Command _fixInvalidName = this.fixInvalidName(d, document, resource, params); commands.add(_fixInvalidName); break; case TestLanguageValidator.UNSORTED_MEMBERS: Command _fixUnsortedMembers = this.fixUnsortedMembers(d, document, resource, params); commands.add(_fixUnsortedMembers); break; } } } return commands; }
@Override public CompletableFuture<List<? extends Command>> codeAction(final CodeActionParams params) { final Function1<CancelIndicator, List<? extends Command>> _function = (CancelIndicator cancelIndicator) -> { final URI uri = this._uriExtensions.toUri(params.getTextDocument().getUri()); final IResourceServiceProvider serviceProvider = this.languagesRegistry.getResourceServiceProvider(uri); ICodeActionService _get = null; if (serviceProvider!=null) { _get=serviceProvider.<ICodeActionService>get(ICodeActionService.class); } final ICodeActionService service = _get; if ((service == null)) { return CollectionLiterals.<Command>emptyList(); } final Function2<Document, XtextResource, List<? extends Command>> _function_1 = (Document doc, XtextResource resource) -> { return service.getCodeActions(doc, resource, params, cancelIndicator); }; return this.workspaceManager.<List<? extends Command>>doRead(uri, _function_1); }; return this.requestManager.<List<? extends Command>>runRead(_function); }
public List<Command> find(CodeActionParams params) { return params.getContext() .getDiagnostics() .stream() .flatMap(diagnostic -> findCodeActionsForDiagnostic(diagnostic)) .collect(Collectors.toList()); }
@Override public CompletableFuture<List<? extends Command>> codeAction(final CodeActionParams params) { // TODO Auto-generated method stub return null; }
@Test @Ignore public void testCodeAction_superfluousSemicolon() throws Exception{ ICompilationUnit unit = getWorkingCopy( "src/java/Foo.java", "public class Foo {\n"+ " void foo() {\n"+ ";" + " }\n"+ "}\n"); CodeActionParams params = new CodeActionParams(); params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit))); final Range range = getRange(unit, ";"); params.setRange(range); params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.SuperfluousSemicolon), 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()); Assert.assertNotNull(c.getArguments()); Assert.assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit); WorkspaceEdit we = (WorkspaceEdit) c.getArguments().get(0); List<org.eclipse.lsp4j.TextEdit> edits = we.getChanges().get(JDTUtils.toURI(unit)); Assert.assertEquals(1, edits.size()); Assert.assertEquals("", edits.get(0).getNewText()); Assert.assertEquals(range, edits.get(0).getRange()); }
@Override public CompletableFuture<List<? extends Command>> codeAction(CodeActionParams params) { LOGGER.info("codeAction: " + params.getTextDocument()); return CompletableFuture.completedFuture(Collections.emptyList()); }
@Override public CompletableFuture<List<? extends Command>> codeAction(CodeActionParams params) { throw new UnsupportedOperationException(); }
public Promise<List<Command>> codeAction(CodeActionParams params) { return transmitDtoAndReceiveDtoList(params, "textDocument/codeAction", Command.class); }
@Override public void computeQuickAssistProposals( QuickAssistInvocationContext invocationContext, CodeAssistCallback callback) { LinearRange range = invocationContext.getTextEditor().getSelectedLinearRange(); Document document = invocationContext.getTextEditor().getDocument(); QueryAnnotationsEvent.QueryCallback annotationCallback = new QueryAnnotationsEvent.QueryCallback() { @SuppressWarnings("ReturnValueIgnored") @Override public void respond( Map<Annotation, org.eclipse.che.ide.api.editor.text.Position> annotations) { // iteration with range never returns anything; need to filter ourselves. // https://github.com/eclipse/che/issues/4338 List<Diagnostic> diagnostics = annotations .entrySet() .stream() .filter( (e) -> e.getValue().overlapsWith(range.getStartOffset(), range.getLength())) .map(Entry::getKey) .map(a -> (DiagnosticAnnotation) a) .map(DiagnosticAnnotation::getDiagnostic) .collect(Collectors.toList()); CodeActionContext context = new CodeActionContext(diagnostics); TextPosition start = document.getPositionFromIndex(range.getStartOffset()); TextPosition end = document.getPositionFromIndex(range.getStartOffset() + range.getLength()); Position rangeStart = new Position(start.getLine(), start.getCharacter()); Position rangeEnd = new Position(end.getLine(), end.getCharacter()); Range rangeParam = new Range(rangeStart, rangeEnd); rangeParam.setEnd(rangeEnd); TextDocumentIdentifier textDocumentIdentifier = new TextDocumentIdentifier(document.getFile().getLocation().toString()); CodeActionParams params = new CodeActionParams(textDocumentIdentifier, rangeParam, context); Promise<List<Command>> codeAction = textDocumentService.codeAction(new CodeActionParamsDto(params)); List<CompletionProposal> proposals = new ArrayList<>(); codeAction.then( (commands) -> { for (Command command : commands) { Action action = actionManager.getAction(command.getCommand()); if (action != null) { proposals.add(new ActionCompletionProposal(command, action)); } } ; callback.proposalComputed(proposals); }); } }; QueryAnnotationsEvent event = new QueryAnnotationsEvent.Builder() .withFilter(a -> a instanceof DiagnosticAnnotation) .withCallback(annotationCallback) .build(); document.getDocumentHandle().getDocEventBus().fireEvent(event); }
@Override public CompletableFuture<List<? extends Command>> codeAction(CodeActionParams params) { return null; }
@PostConstruct public void configureMethods() { dtoToDtoList( "definition", TextDocumentPositionParams.class, LocationDto.class, this::definition); dtoToDtoList("codeAction", CodeActionParams.class, CommandDto.class, this::codeAction); dtoToDtoList( "documentSymbol", DocumentSymbolParams.class, SymbolInformationDto.class, this::documentSymbol); dtoToDtoList("formatting", DocumentFormattingParams.class, TextEditDto.class, this::formatting); dtoToDtoList( "rangeFormatting", DocumentRangeFormattingParams.class, TextEditDto.class, this::rangeFormatting); dtoToDtoList("references", ReferenceParams.class, LocationDto.class, this::references); dtoToDtoList( "onTypeFormatting", DocumentOnTypeFormattingParams.class, TextEditDto.class, this::onTypeFormatting); dtoToDto( "completionItem/resolve", ExtendedCompletionItem.class, ExtendedCompletionItemDto.class, this::completionItemResolve); dtoToDto( "documentHighlight", TextDocumentPositionParams.class, DocumentHighlight.class, this::documentHighlight); dtoToDto( "completion", TextDocumentPositionParams.class, ExtendedCompletionListDto.class, this::completion); dtoToDto("hover", TextDocumentPositionParams.class, HoverDto.class, this::hover); dtoToDto( "signatureHelp", TextDocumentPositionParams.class, SignatureHelpDto.class, this::signatureHelp); dtoToDto("rename", RenameParams.class, RenameResultDto.class, this::rename); dtoToNothing("didChange", DidChangeTextDocumentParams.class, this::didChange); dtoToNothing("didClose", DidCloseTextDocumentParams.class, this::didClose); dtoToNothing("didOpen", DidOpenTextDocumentParams.class, this::didOpen); dtoToNothing("didSave", DidSaveTextDocumentParams.class, this::didSave); }
/** * The code action request is sent from the client to the server to compute * commands for a given text document and range. These commands are * typically code fixes to either fix problems or to beautify/refactor code. * * Registration Options: TextDocumentRegistrationOptions */ @JsonRequest CompletableFuture<List<? extends Command>> codeAction(CodeActionParams params);
public abstract List<? extends Command> getCodeActions(final Document document, final XtextResource resource, final CodeActionParams params, final CancelIndicator indicator);