Java Code Examples for org.eclipse.text.edits.TextEdit#addChild()
The following examples show how to use
org.eclipse.text.edits.TextEdit#addChild() .
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: TextEditConverter.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private String applySourceModifier(String content, ISourceModifier modifier) { if (StringUtils.isBlank(content) || modifier == null) { return content; } SimpleDocument subDocument = new SimpleDocument(content); TextEdit newEdit = new MultiTextEdit(0, subDocument.getLength()); ReplaceEdit[] replaces = modifier.getModifications(content); for (ReplaceEdit replace : replaces) { newEdit.addChild(replace); } try { newEdit.apply(subDocument, TextEdit.NONE); } catch (BadLocationException e) { JavaLanguageServerPlugin.logException("Error applying edit to document", e); } return subDocument.get(); }
Example 2
Source File: RenameNodeCorrectionProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Override protected void addEdits(IDocument doc, TextEdit root) throws CoreException { super.addEdits(doc, root); // build a full AST CompilationUnit unit = CoreASTProvider.getInstance().getAST(getCompilationUnit(), CoreASTProvider.WAIT_YES, null); ASTNode name= NodeFinder.perform(unit, fOffset, fLength); if (name instanceof SimpleName) { SimpleName[] names= LinkedNodeFinder.findByProblems(unit, (SimpleName) name); if (names != null) { for (int i= 0; i < names.length; i++) { SimpleName curr= names[i]; root.addChild(new ReplaceEdit(curr.getStartPosition(), curr.getLength(), fNewName)); } return; } } root.addChild(new ReplaceEdit(fOffset, fLength, fNewName)); }
Example 3
Source File: CorrectPackageDeclarationProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Override protected void addEdits(IDocument doc, TextEdit root) throws CoreException { super.addEdits(doc, root); ICompilationUnit cu= getCompilationUnit(); IPackageFragment parentPack= (IPackageFragment) cu.getParent(); IPackageDeclaration[] decls= cu.getPackageDeclarations(); if (parentPack.isDefaultPackage() && decls.length > 0) { for (int i= 0; i < decls.length; i++) { ISourceRange range= decls[i].getSourceRange(); root.addChild(new DeleteEdit(range.getOffset(), range.getLength())); } return; } if (!parentPack.isDefaultPackage() && decls.length == 0) { String lineDelim = "\n"; String str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; //$NON-NLS-1$ root.addChild(new InsertEdit(0, str)); return; } root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName())); }
Example 4
Source File: ASTRewriteCorrectionProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Override protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException { super.addEdits(document, editRoot); ASTRewrite rewrite= getRewrite(); if (rewrite != null) { try { TextEdit edit= rewrite.rewriteAST(); editRoot.addChild(edit); } catch (IllegalArgumentException e) { throw new CoreException(StatusFactory.newErrorStatus("Invalid AST rewriter", e)); } } if (fImportRewrite != null) { editRoot.addChild(fImportRewrite.rewriteImports(new NullProgressMonitor())); } }
Example 5
Source File: OrganizeImportsCommand.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public void organizeImportsInCompilationUnit(ICompilationUnit unit, WorkspaceEdit rootEdit) { try { InnovationContext context = new InnovationContext(unit, 0, unit.getBuffer().getLength() - 1); CUCorrectionProposal proposal = new CUCorrectionProposal("OrganizeImports", CodeActionKind.SourceOrganizeImports, unit, null, IProposalRelevance.ORGANIZE_IMPORTS) { @Override protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException { CompilationUnit astRoot = context.getASTRoot(); OrganizeImportsOperation op = new OrganizeImportsOperation(unit, astRoot, true, false, true, null); TextEdit edit = op.createTextEdit(null); TextEdit staticEdit = OrganizeImportsHandler.wrapStaticImports(edit, astRoot, unit); if (staticEdit.getChildrenSize() > 0) { editRoot.addChild(staticEdit); } } }; addWorkspaceEdit(unit, proposal, rootEdit); } catch (CoreException e) { JavaLanguageServerPlugin.logException("Problem organize imports ", e); } }
Example 6
Source File: JavadocTagsSubProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Override protected void addEdits(IDocument document, TextEdit rootEdit) throws CoreException { try { String lineDelimiter= TextUtilities.getDefaultLineDelimiter(document); final IJavaProject project= getCompilationUnit().getJavaProject(); IRegion region= document.getLineInformationOfOffset(fInsertPosition); String lineContent= document.get(region.getOffset(), region.getLength()); String indentString= Strings.getIndentString(lineContent, project); String str= Strings.changeIndent(fComment, 0, project, indentString, lineDelimiter); InsertEdit edit= new InsertEdit(fInsertPosition, str); rootEdit.addChild(edit); if (fComment.charAt(fComment.length() - 1) != '\n') { rootEdit.addChild(new InsertEdit(fInsertPosition, lineDelimiter)); rootEdit.addChild(new InsertEdit(fInsertPosition, indentString)); } } catch (BadLocationException e) { throw new CoreException(StatusFactory.newErrorStatus("Invalid edit", e)); } }
Example 7
Source File: JsniReferenceChangeTest.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 6 votes |
public void testPerform() throws CoreException { // Create a dummy change for updating references in RefactorTest.java // to the class R to its new name 'RRR' ICompilationUnit cu = refactorTestClass.getCompilationUnit(); GWTRefactoringSupport support = new DummyRefactoringSupport(); JsniReferenceChangeFactory factory = new JsniReferenceChangeFactory(support); IJsniReferenceChange jsniChange = factory.createChange(cu); // Add one dummy edit to the change TextEdit oldRootEdit = new MultiTextEdit(); oldRootEdit.addChild(new ReplaceEdit(252, 0, "")); ((TextFileChange)jsniChange).setEdit(oldRootEdit); // Perform the change (this should replace the original edits with new ones // with the correct offsets). ((TextFileChange)jsniChange).perform(new NullProgressMonitor()); // Verify that the change still has one edit TextEdit newRootEdit = ((TextFileChange)jsniChange).getEdit(); assertEquals(1, newRootEdit.getChildrenSize()); }
Example 8
Source File: CorrectPackageDeclarationProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override protected void addEdits(IDocument doc, TextEdit root) throws CoreException { super.addEdits(doc, root); ICompilationUnit cu= getCompilationUnit(); IPackageFragment parentPack= (IPackageFragment) cu.getParent(); IPackageDeclaration[] decls= cu.getPackageDeclarations(); if (parentPack.isDefaultPackage() && decls.length > 0) { for (int i= 0; i < decls.length; i++) { ISourceRange range= decls[i].getSourceRange(); root.addChild(new DeleteEdit(range.getOffset(), range.getLength())); } return; } if (!parentPack.isDefaultPackage() && decls.length == 0) { String lineDelim= StubUtility.getLineDelimiterUsed(cu); String str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; //$NON-NLS-1$ root.addChild(new InsertEdit(0, str)); return; } root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName())); }
Example 9
Source File: RenameNodeCorrectionProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override protected void addEdits(IDocument doc, TextEdit root) throws CoreException { super.addEdits(doc, root); // build a full AST CompilationUnit unit= SharedASTProvider.getAST(getCompilationUnit(), SharedASTProvider.WAIT_YES, null); ASTNode name= NodeFinder.perform(unit, fOffset, fLength); if (name instanceof SimpleName) { SimpleName[] names= LinkedNodeFinder.findByProblems(unit, (SimpleName) name); if (names != null) { for (int i= 0; i < names.length; i++) { SimpleName curr= names[i]; root.addChild(new ReplaceEdit(curr.getStartPosition(), curr.getLength(), fNewName)); } return; } } root.addChild(new ReplaceEdit(fOffset, fLength, fNewName)); }
Example 10
Source File: JavadocTagsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override protected void addEdits(IDocument document, TextEdit rootEdit) throws CoreException { try { String lineDelimiter= TextUtilities.getDefaultLineDelimiter(document); final IJavaProject project= getCompilationUnit().getJavaProject(); IRegion region= document.getLineInformationOfOffset(fInsertPosition); String lineContent= document.get(region.getOffset(), region.getLength()); String indentString= Strings.getIndentString(lineContent, project); String str= Strings.changeIndent(fComment, 0, project, indentString, lineDelimiter); InsertEdit edit= new InsertEdit(fInsertPosition, str); rootEdit.addChild(edit); if (fComment.charAt(fComment.length() - 1) != '\n') { rootEdit.addChild(new InsertEdit(fInsertPosition, lineDelimiter)); rootEdit.addChild(new InsertEdit(fInsertPosition, indentString)); } } catch (BadLocationException e) { throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e)); } }
Example 11
Source File: ASTRewriteCorrectionProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException { super.addEdits(document, editRoot); ASTRewrite rewrite= getRewrite(); if (rewrite != null) { try { TextEdit edit= rewrite.rewriteAST(); editRoot.addChild(edit); } catch (IllegalArgumentException e) { throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e)); } } if (fImportRewrite != null) { editRoot.addChild(fImportRewrite.rewriteImports(new NullProgressMonitor())); } }
Example 12
Source File: TaskMarkerProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override protected void addEdits(IDocument document, TextEdit rootEdit) throws CoreException { super.addEdits(document, rootEdit); try { Position pos= getUpdatedPosition(document); if (pos != null) { rootEdit.addChild(new ReplaceEdit(pos.getOffset(), pos.getLength(), "")); //$NON-NLS-1$ } else { rootEdit.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), "")); //$NON-NLS-1$ } } catch (BadLocationException e) { throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e)); } }
Example 13
Source File: ReplaceCorrectionProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override protected void addEdits(IDocument doc, TextEdit rootEdit) throws CoreException { super.addEdits(doc, rootEdit); TextEdit edit= new ReplaceEdit(fOffset, fLength, fReplacementString); rootEdit.addChild(edit); }
Example 14
Source File: GoogleJavaFormatter.java From google-java-format with Apache License 2.0 | 5 votes |
private TextEdit editFromReplacements(List<Replacement> replacements) { // Split the replacements that cross line boundaries. TextEdit edit = new MultiTextEdit(); for (Replacement replacement : replacements) { Range<Integer> replaceRange = replacement.getReplaceRange(); edit.addChild( new ReplaceEdit( replaceRange.lowerEndpoint(), replaceRange.upperEndpoint() - replaceRange.lowerEndpoint(), replacement.getReplacementString())); } return edit; }
Example 15
Source File: AddResourcesToClientBundleAction.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
public void run(IProgressMonitor monitor) throws CoreException { ICompilationUnit icu = clientBundle.getCompilationUnit(); CompilationUnit cu = JavaASTUtils.parseCompilationUnit(icu); ImportRewrite importRewrite = StubUtility.createImportRewrite(cu, true); // Find the target type declaration TypeDeclaration typeDecl = JavaASTUtils.findTypeDeclaration(cu, clientBundle.getFullyQualifiedName()); if (typeDecl == null) { throw new CoreException( StatusUtilities.newErrorStatus("Missing ClientBundle type " + clientBundle.getFullyQualifiedName(), GWTPlugin.PLUGIN_ID)); } // We need to rewrite the AST of the ClientBundle type declaration ASTRewrite astRewrite = ASTRewrite.create(cu.getAST()); ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(typeDecl); ListRewrite listRewriter = astRewrite.getListRewrite(typeDecl, property); // Add the new resource methods boolean addComments = StubUtility.doAddComments(icu.getJavaProject()); for (ClientBundleResource resource : resources) { listRewriter.insertLast(resource.createMethodDeclaration(clientBundle, astRewrite, importRewrite, addComments), null); } // Create the edit to add the methods and update the imports TextEdit rootEdit = new MultiTextEdit(); rootEdit.addChild(astRewrite.rewriteAST()); rootEdit.addChild(importRewrite.rewriteImports(null)); // Apply the change to the compilation unit CompilationUnitChange cuChange = new CompilationUnitChange( "Update ClientBundle", icu); cuChange.setSaveMode(TextFileChange.KEEP_SAVE_STATE); cuChange.setEdit(rootEdit); cuChange.perform(new NullProgressMonitor()); }
Example 16
Source File: JsniFormattingUtil.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
/** * Returns a text edit that formats the given document according to the given * settings. * * @param document The document to format. * @param javaFormattingPrefs The formatting preferences for Java, used to * determine the method level indentation. * @param javaScriptFormattingPrefs The formatting preferences for JavaScript. * See org.eclipse.wst.jsdt.internal.formatter * .DefaultCodeFormatterOptions and * org.eclipse.wst.jsdt.core.formatter.DefaultCodeFormatterConstants * @param originalJsniMethods The original jsni methods to use if the * formatter fails to format the method. The original jsni Strings * must be in the same order that the jsni methods occur in the * document. This is to work around the Java formatter blasting the * jsni tabbing for the format-on-save action. May be null. * @return A text edit that when applied to the document, will format the jsni * methods. */ @SuppressWarnings({"unchecked", "rawtypes"}) public static TextEdit format(IDocument document, Map javaFormattingPrefs, Map javaScriptFormattingPrefs, String[] originalJsniMethods) { TextEdit combinedEdit = new MultiTextEdit(); try { ITypedRegion[] regions = TextUtilities.computePartitioning(document, GWTPartitions.GWT_PARTITIONING, 0, document.getLength(), false); // Format all JSNI blocks in the document int i = 0; for (ITypedRegion region : regions) { if (region.getType().equals(GWTPartitions.JSNI_METHOD)) { String originalJsniMethod = null; if (originalJsniMethods != null && i < originalJsniMethods.length) { originalJsniMethod = originalJsniMethods[i]; } TextEdit edit = format(document, new TypedPosition(region), javaFormattingPrefs, javaScriptFormattingPrefs, originalJsniMethod); if (edit != null) { combinedEdit.addChild(edit); } i++; } } return combinedEdit; } catch (BadLocationException e) { GWTPluginLog.logError(e); return null; } }
Example 17
Source File: ReplaceCorrectionProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Override protected void addEdits(IDocument doc, TextEdit rootEdit) throws CoreException { super.addEdits(doc, rootEdit); TextEdit edit= new ReplaceEdit(fOffset, fLength, fReplacementString); rootEdit.addChild(edit); }
Example 18
Source File: ReorgCorrectionsSubProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
@Override protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException { if (fResultingEdit != null) { editRoot.addChild(fResultingEdit); } }
Example 19
Source File: OrganizeImportsHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
public static TextEdit wrapStaticImports(TextEdit edit, CompilationUnit root, ICompilationUnit unit) throws MalformedTreeException, CoreException { String[] favourites = PreferenceManager.getPrefs(unit.getResource()).getJavaCompletionFavoriteMembers(); if (favourites.length == 0) { return edit; } IJavaProject project = unit.getJavaProject(); if (JavaModelUtil.is50OrHigher(project)) { List<SimpleName> typeReferences = new ArrayList<>(); List<SimpleName> staticReferences = new ArrayList<>(); ImportReferencesCollector.collect(root, project, null, typeReferences, staticReferences); if (staticReferences.isEmpty()) { return edit; } ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(root, true); AST ast = root.getAST(); ASTRewrite astRewrite = ASTRewrite.create(ast); for (SimpleName node : staticReferences) { addImports(root, unit, favourites, importRewrite, ast, astRewrite, node, true); addImports(root, unit, favourites, importRewrite, ast, astRewrite, node, false); } TextEdit staticEdit = importRewrite.rewriteImports(null); if (staticEdit != null && staticEdit.getChildrenSize() > 0) { TextEdit lastStatic = staticEdit.getChildren()[staticEdit.getChildrenSize() - 1]; if (lastStatic instanceof DeleteEdit) { if (edit.getChildrenSize() > 0) { TextEdit last = edit.getChildren()[edit.getChildrenSize() - 1]; if (last instanceof DeleteEdit && lastStatic.getOffset() == last.getOffset() && lastStatic.getLength() == last.getLength()) { edit.removeChild(last); } } } TextEdit firstStatic = staticEdit.getChildren()[0]; if (firstStatic instanceof InsertEdit) { if (edit.getChildrenSize() > 0) { TextEdit firstEdit = edit.getChildren()[0]; if (firstEdit instanceof InsertEdit) { if (areEqual((InsertEdit) firstEdit, (InsertEdit) firstStatic)) { edit.removeChild(firstEdit); } } } } try { staticEdit.addChild(edit); return staticEdit; } catch (MalformedTreeException e) { JavaLanguageServerPlugin.logException("Failed to resolve static organize imports source action", e); } } } return edit; }