Java Code Examples for org.eclipse.lsp4j.CodeAction#setEdit()
The following examples show how to use
org.eclipse.lsp4j.CodeAction#setEdit() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CodeActionFactory.java From lemminx with Eclipse Public License 2.0 | 6 votes |
public static CodeAction replaceAt(String title, String replaceText, TextDocumentItem document, Diagnostic diagnostic, Collection<Range> ranges) { CodeAction insertContentAction = new CodeAction(title); insertContentAction.setKind(CodeActionKind.QuickFix); insertContentAction.setDiagnostics(Arrays.asList(diagnostic)); VersionedTextDocumentIdentifier versionedTextDocumentIdentifier = new VersionedTextDocumentIdentifier( document.getUri(), document.getVersion()); List<TextEdit> edits = new ArrayList<TextEdit>(); for (Range range : ranges) { TextEdit edit = new TextEdit(range, replaceText); edits.add(edit); } TextDocumentEdit textDocumentEdit = new TextDocumentEdit(versionedTextDocumentIdentifier, edits); WorkspaceEdit workspaceEdit = new WorkspaceEdit(Collections.singletonList(Either.forLeft(textDocumentEdit))); insertContentAction.setEdit(workspaceEdit); return insertContentAction; }
Example 2
Source File: CodeActionFactory.java From lemminx with Eclipse Public License 2.0 | 6 votes |
/** * Makes a CodeAction to create a file and add content to the file. * * @param title The displayed name of the CodeAction * @param docURI The file to create * @param content The text to put into the newly created document. * @param diagnostic The diagnostic that this CodeAction will fix */ public static CodeAction createFile(String title, String docURI, String content, Diagnostic diagnostic) { List<Either<TextDocumentEdit, ResourceOperation>> actionsToTake = new ArrayList<>(2); // 1. create an empty file actionsToTake.add(Either.forRight(new CreateFile(docURI, new CreateFileOptions(false, true)))); // 2. update the created file with the given content VersionedTextDocumentIdentifier identifier = new VersionedTextDocumentIdentifier(docURI, 0); TextEdit te = new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), content); actionsToTake.add(Either.forLeft(new TextDocumentEdit(identifier, Collections.singletonList(te)))); WorkspaceEdit createAndAddContentEdit = new WorkspaceEdit(actionsToTake); CodeAction codeAction = new CodeAction(title); codeAction.setEdit(createAndAddContentEdit); codeAction.setDiagnostics(Collections.singletonList(diagnostic)); codeAction.setKind(CodeActionKind.QuickFix); return codeAction; }
Example 3
Source File: CodeActionAcceptor.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** Adds a quick-fix code action with the given title, edit and command */ public void acceptQuickfixCodeAction(QuickfixContext context, String title, WorkspaceEdit edit, Command command) { if (edit == null && command == null) { return; } CodeAction codeAction = new CodeAction(); codeAction.setTitle(title); codeAction.setEdit(edit); codeAction.setCommand(command); codeAction.setKind(CodeActionKind.QuickFix); if (context.options != null && context.options.getCodeActionParams() != null) { CodeActionContext cac = context.options.getCodeActionParams().getContext(); if (cac != null && cac.getDiagnostics() != null) { codeAction.setDiagnostics(cac.getDiagnostics()); } } codeActions.add(Either.forRight(codeAction)); }
Example 4
Source File: CodeActionsUtils.java From vscode-as3mxml with Apache License 2.0 | 6 votes |
private static void createCodeActionsForGenerateGetterAndSetter(IVariableNode variableNode, String uri, String fileText, Range codeActionsRange, List<Either<Command, CodeAction>> codeActions) { WorkspaceEdit getSetEdit = createWorkspaceEditForGenerateGetterAndSetter( variableNode, uri, fileText, true, true); CodeAction getAndSetCodeAction = new CodeAction(); getAndSetCodeAction.setTitle("Generate 'get' and 'set' accessors"); getAndSetCodeAction.setEdit(getSetEdit); getAndSetCodeAction.setKind(CodeActionKind.RefactorRewrite); codeActions.add(Either.forRight(getAndSetCodeAction)); WorkspaceEdit getterEdit = createWorkspaceEditForGenerateGetterAndSetter( variableNode, uri, fileText, true, false); CodeAction getterCodeAction = new CodeAction(); getterCodeAction.setTitle("Generate 'get' accessor (make read-only)"); getterCodeAction.setEdit(getterEdit); getterCodeAction.setKind(CodeActionKind.RefactorRewrite); codeActions.add(Either.forRight(getterCodeAction)); WorkspaceEdit setterEdit = createWorkspaceEditForGenerateGetterAndSetter( variableNode, uri, fileText, false, true); CodeAction setterCodeAction = new CodeAction(); setterCodeAction.setTitle("Generate 'set' accessor (make write-only)"); setterCodeAction.setEdit(setterEdit); setterCodeAction.setKind(CodeActionKind.RefactorRewrite); codeActions.add(Either.forRight(setterCodeAction)); }
Example 5
Source File: CodeActionProvider.java From vscode-as3mxml with Apache License 2.0 | 6 votes |
private void createCodeActionsForUnusedImport(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions) { String fileText = fileTracker.getText(path); if(fileText == null) { return; } Range range = diagnostic.getRange(); WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForRemoveUnusedImport(fileText, path.toUri().toString(), diagnostic.getRange()); if (edit == null) { return; } int startOffset = LanguageServerCompilerUtils.getOffsetFromPosition(new StringReader(fileText), range.getStart()); int endOffset = LanguageServerCompilerUtils.getOffsetFromPosition(new StringReader(fileText), range.getEnd()); String importText = fileText.substring(startOffset, endOffset); CodeAction codeAction = new CodeAction(); codeAction.setTitle("Remove " + importText); codeAction.setEdit(edit); codeAction.setKind(CodeActionKind.QuickFix); codeAction.setDiagnostics(Collections.singletonList(diagnostic)); codeActions.add(Either.forRight(codeAction)); }
Example 6
Source File: CodeActionFactory.java From lemminx with Eclipse Public License 2.0 | 5 votes |
/** * Create a CodeAction to insert a new content at the end of the given range. * * @param title * @param range * @param insertText * @param document * @param diagnostic * * @return the CodeAction to insert a new content at the end of the given range. */ public static CodeAction insert(String title, Position position, String insertText, TextDocumentItem document, Diagnostic diagnostic) { CodeAction insertContentAction = new CodeAction(title); insertContentAction.setKind(CodeActionKind.QuickFix); insertContentAction.setDiagnostics(Arrays.asList(diagnostic)); TextDocumentEdit textDocumentEdit = insertEdit(insertText, position, document); WorkspaceEdit workspaceEdit = new WorkspaceEdit(Collections.singletonList(Either.forLeft(textDocumentEdit))); insertContentAction.setEdit(workspaceEdit); return insertContentAction; }
Example 7
Source File: XMLAssert.java From lemminx with Eclipse Public License 2.0 | 5 votes |
public static CodeAction ca(Diagnostic d, TextEdit... te) { CodeAction codeAction = new CodeAction(); codeAction.setTitle(""); codeAction.setDiagnostics(Arrays.asList(d)); VersionedTextDocumentIdentifier versionedTextDocumentIdentifier = new VersionedTextDocumentIdentifier(FILE_URI, 0); TextDocumentEdit textDocumentEdit = new TextDocumentEdit(versionedTextDocumentIdentifier, Arrays.asList(te)); WorkspaceEdit workspaceEdit = new WorkspaceEdit(Collections.singletonList(Either.forLeft(textDocumentEdit))); codeAction.setEdit(workspaceEdit); return codeAction; }
Example 8
Source File: XMLAssert.java From lemminx with Eclipse Public License 2.0 | 5 votes |
public static CodeAction ca(Diagnostic d, Either<TextDocumentEdit, ResourceOperation>... ops) { CodeAction codeAction = new CodeAction(); codeAction.setDiagnostics(Collections.singletonList(d)); codeAction.setEdit(new WorkspaceEdit(Arrays.asList(ops))); codeAction.setTitle(""); return codeAction; }
Example 9
Source File: AbstractQuickfix.java From camel-language-server with Apache License 2.0 | 5 votes |
protected CodeAction createCodeAction(CodeActionParams params, Diagnostic diagnostic, String possibleProperty) { CodeAction codeAction = new CodeAction("Did you mean "+possibleProperty + "?"); codeAction.setDiagnostics(Collections.singletonList(diagnostic)); codeAction.setKind(CodeActionKind.QuickFix); Map<String, List<TextEdit>> changes = new HashMap<>(); TextEdit textEdit = new TextEdit(diagnostic.getRange(), possibleProperty); changes.put(params.getTextDocument().getUri(), Arrays.asList(textEdit)); codeAction.setEdit(new WorkspaceEdit(changes)); return codeAction; }
Example 10
Source File: CodeActionProvider.java From vscode-as3mxml with Apache License 2.0 | 5 votes |
private void createCodeActionForMissingCatchOrFinally(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions) { ILspProject project = folderData.project; Position position = diagnostic.getRange().getStart(); IncludeFileData includeFileData = folderData.includedFiles.get(path.toString()); int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData); IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData); if(!(offsetNode instanceof ITryNode)) { return; } ITryNode tryNode = (ITryNode) offsetNode; String fileText = fileTracker.getText(path); if(fileText == null) { return; } WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForGenerateCatch( tryNode, path.toUri().toString(), fileText, project); if(edit == null) { return; } CodeAction codeAction = new CodeAction(); codeAction.setDiagnostics(Collections.singletonList(diagnostic)); codeAction.setTitle("Generate catch"); codeAction.setEdit(edit); codeAction.setKind(CodeActionKind.QuickFix); codeActions.add(Either.forRight(codeAction)); }
Example 11
Source File: CodeActionService.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
private CodeAction fixUnsortedMembers(Diagnostic d, ICodeActionService2.Options options) { WorkspaceEdit wsEdit = recordWorkspaceEdit(options, (Resource copiedResource) -> { Model model = Iterables.getFirst(Iterables.filter(copiedResource.getContents(), Model.class), null); if (model != null) { for (TypeDeclaration type : Iterables.filter(model.getElements(), TypeDeclaration.class)) { ECollections.sort(type.getMembers(), (Member a, Member b) -> a.getName().compareTo(b.getName())); } } }); CodeAction codeAction = new CodeAction(); codeAction.setTitle("Sort Members"); codeAction.setDiagnostics(Lists.newArrayList(d)); codeAction.setEdit(wsEdit); return codeAction; }
Example 12
Source File: CodeActionProvider.java From vscode-as3mxml with Apache License 2.0 | 4 votes |
private void createCodeActionForMissingField(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions) { Position position = diagnostic.getRange().getStart(); IncludeFileData includeFileData = folderData.includedFiles.get(path.toString()); int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData); IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData); if (offsetNode instanceof IMXMLInstanceNode) { MXMLData mxmlData = workspaceFolderManager.getMXMLDataForPath(path, folderData); if (mxmlData != null) { IMXMLTagData offsetTag = MXMLDataUtils.getOffsetMXMLTag(mxmlData, currentOffset); if(offsetTag != null) { //workaround for bug in Royale compiler Position newPosition = new Position(position.getLine(), position.getCharacter() + 1); int newOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), newPosition, includeFileData); offsetNode = workspaceFolderManager.getEmbeddedActionScriptNodeInMXMLTag(offsetTag, path, newOffset, folderData); } } } IIdentifierNode identifierNode = null; if (offsetNode instanceof IIdentifierNode) { IASNode parentNode = offsetNode.getParent(); if (parentNode instanceof IMemberAccessExpressionNode) { IMemberAccessExpressionNode memberAccessExpressionNode = (IMemberAccessExpressionNode) offsetNode.getParent(); IExpressionNode leftOperandNode = memberAccessExpressionNode.getLeftOperandNode(); if (leftOperandNode instanceof ILanguageIdentifierNode) { ILanguageIdentifierNode leftIdentifierNode = (ILanguageIdentifierNode) leftOperandNode; if (leftIdentifierNode.getKind() == ILanguageIdentifierNode.LanguageIdentifierKind.THIS) { identifierNode = (IIdentifierNode) offsetNode; } } } else //no member access { identifierNode = (IIdentifierNode) offsetNode; } } if (identifierNode == null) { return; } String fileText = fileTracker.getText(path); if(fileText == null) { return; } WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForGenerateFieldVariable( identifierNode, path.toUri().toString(), fileText); if(edit == null) { return; } CodeAction codeAction = new CodeAction(); codeAction.setDiagnostics(Collections.singletonList(diagnostic)); codeAction.setTitle("Generate Field Variable"); codeAction.setEdit(edit); codeAction.setKind(CodeActionKind.QuickFix); codeActions.add(Either.forRight(codeAction)); }
Example 13
Source File: CodeActionProvider.java From vscode-as3mxml with Apache License 2.0 | 4 votes |
private void createCodeActionForMissingLocalVariable(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions) { Position position = diagnostic.getRange().getStart(); IncludeFileData includeFileData = folderData.includedFiles.get(path.toString()); int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData); IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData); if (offsetNode instanceof IMXMLInstanceNode) { MXMLData mxmlData = workspaceFolderManager.getMXMLDataForPath(path, folderData); if (mxmlData != null) { IMXMLTagData offsetTag = MXMLDataUtils.getOffsetMXMLTag(mxmlData, currentOffset); if(offsetTag != null) { //workaround for bug in Royale compiler Position newPosition = new Position(position.getLine(), position.getCharacter() + 1); int newOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), newPosition, includeFileData); offsetNode = workspaceFolderManager.getEmbeddedActionScriptNodeInMXMLTag(offsetTag, path, newOffset, folderData); } } } IIdentifierNode identifierNode = null; if (offsetNode instanceof IIdentifierNode) { identifierNode = (IIdentifierNode) offsetNode; } if (identifierNode == null) { return; } String fileText = fileTracker.getText(path); if(fileText == null) { return; } WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForGenerateLocalVariable( identifierNode, path.toUri().toString(), fileText); if(edit == null) { return; } CodeAction codeAction = new CodeAction(); codeAction.setDiagnostics(Collections.singletonList(diagnostic)); codeAction.setTitle("Generate Local Variable"); codeAction.setEdit(edit); codeAction.setKind(CodeActionKind.QuickFix); codeActions.add(Either.forRight(codeAction)); }
Example 14
Source File: CodeActionProvider.java From vscode-as3mxml with Apache License 2.0 | 4 votes |
private void createCodeActionForUnimplementedMethods(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions) { ILspProject project = folderData.project; Position position = diagnostic.getRange().getStart(); IncludeFileData includeFileData = folderData.includedFiles.get(path.toString()); int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData); IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData); if (offsetNode == null) { return; } IClassNode classNode = (IClassNode) offsetNode.getAncestorOfType(IClassNode.class); if (classNode == null) { return; } String fileText = fileTracker.getText(path); if(fileText == null) { return; } for (IExpressionNode exprNode : classNode.getImplementedInterfaceNodes()) { IInterfaceDefinition interfaceDefinition = (IInterfaceDefinition) exprNode.resolve(project); if (interfaceDefinition == null) { continue; } WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForImplementInterface( classNode, interfaceDefinition, path.toUri().toString(), fileText, project); if (edit == null) { continue; } CodeAction codeAction = new CodeAction(); codeAction.setDiagnostics(Collections.singletonList(diagnostic)); codeAction.setTitle("Implement interface '" + interfaceDefinition.getBaseName() + "'"); codeAction.setEdit(edit); codeAction.setKind(CodeActionKind.QuickFix); codeActions.add(Either.forRight(codeAction)); } }
Example 15
Source File: CodeActionProvider.java From vscode-as3mxml with Apache License 2.0 | 4 votes |
private void createCodeActionForMissingMethod(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions) { ILspProject project = folderData.project; Position position = diagnostic.getRange().getStart(); IncludeFileData includeFileData = folderData.includedFiles.get(path.toString()); int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData); IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData); if (offsetNode == null) { return; } if (offsetNode instanceof IMXMLInstanceNode) { MXMLData mxmlData = workspaceFolderManager.getMXMLDataForPath(path, folderData); if (mxmlData != null) { IMXMLTagData offsetTag = MXMLDataUtils.getOffsetMXMLTag(mxmlData, currentOffset); if(offsetTag != null) { //workaround for bug in Royale compiler Position newPosition = new Position(position.getLine(), position.getCharacter() + 1); int newOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), newPosition, includeFileData); offsetNode = workspaceFolderManager.getEmbeddedActionScriptNodeInMXMLTag(offsetTag, path, newOffset, folderData); } } } IASNode parentNode = offsetNode.getParent(); IFunctionCallNode functionCallNode = null; if (offsetNode instanceof IFunctionCallNode) { functionCallNode = (IFunctionCallNode) offsetNode; } else if (parentNode instanceof IFunctionCallNode) { functionCallNode = (IFunctionCallNode) offsetNode.getParent(); } else if(offsetNode instanceof IIdentifierNode && parentNode instanceof IMemberAccessExpressionNode) { IASNode gpNode = parentNode.getParent(); if (gpNode instanceof IFunctionCallNode) { functionCallNode = (IFunctionCallNode) gpNode; } } if (functionCallNode == null) { return; } String fileText = fileTracker.getText(path); if(fileText == null) { return; } WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForGenerateMethod( functionCallNode, path.toUri().toString(), fileText, project); if(edit == null) { return; } CodeAction codeAction = new CodeAction(); codeAction.setDiagnostics(Collections.singletonList(diagnostic)); codeAction.setTitle("Generate Method"); codeAction.setEdit(edit); codeAction.setKind(CodeActionKind.QuickFix); codeActions.add(Either.forRight(codeAction)); }
Example 16
Source File: CodeActionProvider.java From vscode-as3mxml with Apache License 2.0 | 4 votes |
private void createCodeActionsForImport(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions) { ILspProject project = folderData.project; Position position = diagnostic.getRange().getStart(); IncludeFileData includeFileData = folderData.includedFiles.get(path.toString()); int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData); IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData); IMXMLTagData offsetTag = null; boolean isMXML = path.toUri().toString().endsWith(FILE_EXTENSION_MXML); if (isMXML) { MXMLData mxmlData = workspaceFolderManager.getMXMLDataForPath(path, folderData); if (mxmlData != null) { offsetTag = MXMLDataUtils.getOffsetMXMLTag(mxmlData, currentOffset); } } if (offsetNode instanceof IMXMLInstanceNode && offsetTag != null) { //workaround for bug in Royale compiler Position newPosition = new Position(position.getLine(), position.getCharacter() + 1); int newOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), newPosition, includeFileData); offsetNode = workspaceFolderManager.getEmbeddedActionScriptNodeInMXMLTag(offsetTag, path, newOffset, folderData); } if (offsetNode == null || !(offsetNode instanceof IIdentifierNode)) { return; } ImportRange importRange = null; if (offsetTag != null) { importRange = ImportRange.fromOffsetTag(offsetTag, currentOffset); } else { importRange = ImportRange.fromOffsetNode(offsetNode); } String uri = importRange.uri; String fileText = fileTracker.getText(path); if(fileText == null) { return; } IIdentifierNode identifierNode = (IIdentifierNode) offsetNode; String typeString = identifierNode.getName(); List<IDefinition> types = ASTUtils.findTypesThatMatchName(typeString, project.getCompilationUnits()); for (IDefinition definitionToImport : types) { WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForAddImport(definitionToImport, fileText, uri, importRange); if (edit == null) { continue; } CodeAction codeAction = new CodeAction(); codeAction.setTitle("Import " + definitionToImport.getQualifiedName()); codeAction.setEdit(edit); codeAction.setKind(CodeActionKind.QuickFix); codeAction.setDiagnostics(Collections.singletonList(diagnostic)); codeActions.add(Either.forRight(codeAction)); } }