org.eclipse.text.edits.InsertEdit Java Examples
The following examples show how to use
org.eclipse.text.edits.InsertEdit.
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: ImportRewriteAnalyzer.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private void removeAndInsertNew(IBuffer buffer, int contentOffset, int contentEnd, ArrayList stringsToInsert, MultiTextEdit resEdit) { int pos= contentOffset; for (int i= 0; i < stringsToInsert.size(); i++) { String curr= (String) stringsToInsert.get(i); int idx= findInBuffer(buffer, curr, pos, contentEnd); if (idx != -1) { if (idx != pos) { resEdit.addChild(new DeleteEdit(pos, idx - pos)); } pos= idx + curr.length(); } else { resEdit.addChild(new InsertEdit(pos, curr)); } } if (pos < contentEnd) { resEdit.addChild(new DeleteEdit(pos, contentEnd - pos)); } }
Example #2
Source File: NLSUtil.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
/** * Creates and returns an NLS tag edit for a string that is at the specified position in a * compilation unit. * * @param cu the compilation unit * @param position position of the string * @return the edit, or <code>null</code> if the string is already NLSed or the edit could not * be created for some other reason. * @throws CoreException if scanning fails */ public static TextEdit createNLSEdit(ICompilationUnit cu, int position) throws CoreException { NLSLine nlsLine= scanCurrentLine(cu, position); if (nlsLine == null) { return null; } NLSElement element= findElement(nlsLine, position); if (element.hasTag()) { return null; } NLSElement[] elements= nlsLine.getElements(); int indexInElementList= Arrays.asList(elements).indexOf(element); int editOffset= computeInsertOffset(elements, indexInElementList, cu); String editText= ' ' + NLSElement.createTagText(indexInElementList + 1); //tags are 1-based return new InsertEdit(editOffset, editText); }
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: 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 #5
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 #6
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 #7
Source File: PropertyFileDocumentModel.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Inserts the given key value pairs into this model at appropriate * positions. Records all required text changes in the given change * * @param keyValuePairs the key value pairs to insert * @param change the change to use to record text changes */ public void insert(KeyValuePair[] keyValuePairs, TextChange change) { ArrayList<KeyValuePair> sorted= new ArrayList<KeyValuePair>(Arrays.asList(keyValuePairs)); Collections.sort(sorted, new Comparator<KeyValuePair>() { public int compare(KeyValuePair p1, KeyValuePair p2) { return Collator.getInstance().compare(p1.fKey, p2.fKey); } }); for (int i = 0; i < sorted.size(); i++) { KeyValuePair curr= sorted.get(i); InsertEdit insertEdit= insert(curr); String message= Messages.format(NLSMessages.NLSPropertyFileModifier_add_entry, BasicElementLabels.getJavaElementName(curr.getKey())); TextChangeCompatibility.addTextEdit(change, message, insertEdit); } }
Example #8
Source File: ChangeUtilTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Test public void testConvertCompilationUnitChange() throws CoreException { IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null); ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null); CompilationUnitChange change = new CompilationUnitChange("insertText", cu); String newText = "// some content"; change.setEdit(new InsertEdit(0, newText)); WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change); assertEquals(edit.getDocumentChanges().size(), 1); TextDocumentEdit textDocumentEdit = edit.getDocumentChanges().get(0).getLeft(); assertNotNull(textDocumentEdit); assertEquals(textDocumentEdit.getEdits().size(), 1); TextEdit textEdit = textDocumentEdit.getEdits().get(0); assertEquals(textEdit.getNewText(), newText); }
Example #9
Source File: ChangeUtilTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Test public void testConvertSimpleCompositeChange() throws CoreException { IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null); ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null); CompositeChange change = new CompositeChange("simple composite change"); RenameCompilationUnitChange resourceChange = new RenameCompilationUnitChange(cu, "ENew.java"); change.add(resourceChange); CompilationUnitChange textChange = new CompilationUnitChange("insertText", cu); textChange.setEdit(new InsertEdit(0, "// some content")); change.add(textChange); WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change); assertEquals(edit.getDocumentChanges().size(), 2); assertTrue(edit.getDocumentChanges().get(0).getRight() instanceof RenameFile); assertTrue(edit.getDocumentChanges().get(1).getLeft() instanceof TextDocumentEdit); }
Example #10
Source File: DocumentUtils.java From typescript.java with MIT License | 6 votes |
private static void toTextEdit(CodeEdit codeEdit, IDocument document, MultiTextEdit textEdit) throws TypeScriptException { String newText = codeEdit.getNewText(); int startLine = codeEdit.getStart().getLine(); int startOffset = codeEdit.getStart().getOffset(); int endLine = codeEdit.getEnd().getLine(); int endOffset = codeEdit.getEnd().getOffset(); int start = DocumentUtils.getPosition(document, startLine, startOffset); int end = DocumentUtils.getPosition(document, endLine, endOffset); int length = end - start; if (newText.isEmpty()) { if (length > 0) { textEdit.addChild(new DeleteEdit(start, length)); } } else { if (length > 0) { textEdit.addChild(new ReplaceEdit(start, length, newText)); } else if (length == 0) { textEdit.addChild(new InsertEdit(start, newText)); } } }
Example #11
Source File: TextEditConverter.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Override public boolean visit(InsertEdit edit) { try { org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit(); te.setNewText(edit.getText()); te.setRange(JDTUtils.toRange(compilationUnit,edit.getOffset(),edit.getLength())); converted.add(te); } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Error converting TextEdits", e); } return super.visit(edit); }
Example #12
Source File: RefactoringCorrectionProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override protected TextChange createTextChange() throws CoreException { init(fRefactoring); fRefactoringStatus= fRefactoring.checkFinalConditions(new NullProgressMonitor()); if (fRefactoringStatus.hasFatalError()) { TextFileChange dummyChange= new TextFileChange("fatal error", (IFile) getCompilationUnit().getResource()); //$NON-NLS-1$ dummyChange.setEdit(new InsertEdit(0, "")); //$NON-NLS-1$ return dummyChange; } return (TextChange) fRefactoring.createChange(new NullProgressMonitor()); }
Example #13
Source File: NLSUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Creates and returns an NLS tag edit for a string that is at the specified position in a * compilation unit. * * @param cu the compilation unit * @param position position of the string * @return the edit, or <code>null</code> if the string is already NLSed or the edit could not * be created for some other reason. * @throws CoreException if scanning fails */ public static TextEdit createNLSEdit(ICompilationUnit cu, int position) throws CoreException { NLSLine nlsLine= scanCurrentLine(cu, position); if (nlsLine == null) return null; NLSElement element= findElement(nlsLine, position); if (element.hasTag()) return null; NLSElement[] elements= nlsLine.getElements(); int indexInElementList= Arrays.asList(elements).indexOf(element); int editOffset= computeInsertOffset(elements, indexInElementList, cu); String editText= ' ' + NLSElement.createTagText(indexInElementList + 1); //tags are 1-based return new InsertEdit(editOffset, editText); }
Example #14
Source File: PropertyFileDocumentModel.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private InsertEdit insert(KeyValuePair keyValuePair) { KeyValuePairModell keyValuePairModell = new KeyValuePairModell(keyValuePair); int index = findInsertPosition(keyValuePairModell); KeyValuePairModell insertHere = fKeyValuePairs.get(index); int offset = insertHere.fOffset; String extra= ""; //$NON-NLS-1$ if (insertHere instanceof LastKeyValuePair && ((LastKeyValuePair)insertHere).needsNewLine()) { extra= fLineDelimiter; ((LastKeyValuePair)insertHere).resetNeedsNewLine(); offset-= insertHere.fLeadingWhiteSpaces; } else if (index > 0) { String beforeKey= fKeyValuePairs.get(index - 1).fKey; String afterKey= insertHere.fKey; String key= keyValuePair.fKey; int distBefore= NLSUtil.invertDistance(key, beforeKey); int distAfter= NLSUtil.invertDistance(key, afterKey); if (distBefore > distAfter) { offset-= insertHere.fLeadingWhiteSpaces; } else if (distBefore == distAfter && Collator.getInstance().compare(beforeKey, afterKey) < 0) { offset-= insertHere.fLeadingWhiteSpaces; } else { //insert it before afterKey -> move the leading white spaces to the inserted pair keyValuePairModell.fLeadingWhiteSpaces= insertHere.fLeadingWhiteSpaces; insertHere.fLeadingWhiteSpaces= 0; } } String text= extra + keyValuePairModell.getKeyValueText(); keyValuePairModell.fOffset= offset; keyValuePairModell.fLength= text.length(); fKeyValuePairs.add(index, keyValuePairModell); return new InsertEdit(offset, text); }
Example #15
Source File: JavaCompilationParticipantTest.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
public void testBuildAddError() throws Exception { IProject project = getTestProject().getProject(); // Verify that we have 1 existing GWT problem marker IMarker[] markers = getGWTProblemMarkers(project); assertEquals(1, markers.length); ICompilationUnit cu = testClass.getCompilationUnit(); // Open the test class in the editor CompilationUnitEditor editor = (CompilationUnitEditor) JavaUI.openInEditor(cu); IEditorInput editorInput = editor.getEditorInput(); // Edit the document to create a new error (add 'foobar' to the front of // the class name in a Java reference) IDocument document = editor.getDocumentProvider().getDocument(editorInput); TextEdit errorEdit = new InsertEdit(254, "foobar"); errorEdit.apply(document); // Save the changes editor.doSave(null); // Rebuild the project rebuildTestProject(); // Verify that we now have 2 GWT problem markers markers = getGWTProblemMarkers(project); assertEquals(2, markers.length); }
Example #16
Source File: ChangeUtil.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private static void convertCreateCompilationUnitChange(WorkspaceEdit edit, CreateCompilationUnitChange change) { ICompilationUnit unit = change.getCu(); CreateFile createFile = new CreateFile(); createFile.setUri(JDTUtils.toURI(unit)); createFile.setOptions(new CreateFileOptions(false, true)); edit.getDocumentChanges().add(Either.forRight(createFile)); InsertEdit textEdit = new InsertEdit(0, change.getPreview()); convertTextEdit(edit, unit, textEdit); }
Example #17
Source File: NewCUProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private CompilationUnitChange constructNewCUChange(ICompilationUnit cu) throws CoreException { String lineDelimiter = StubUtility.getLineDelimiterUsed(fCompilationUnit.getJavaProject()); String typeStub = constructTypeStub(cu, fTypeNameWithParameters, Flags.AccPublic, lineDelimiter); String cuContent = constructCUContent(cu, typeStub, lineDelimiter); CompilationUnitChange cuChange = new CompilationUnitChange("", cu); cuChange.setEdit(new InsertEdit(0, cuContent)); return cuChange; }
Example #18
Source File: RefactoringCorrectionProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Override protected TextChange createTextChange() throws CoreException { init(fRefactoring); fRefactoringStatus = fRefactoring.checkFinalConditions(new NullProgressMonitor()); if (fRefactoringStatus.hasFatalError()) { TextFileChange dummyChange = new TextFileChange("fatal error", (IFile) getCompilationUnit().getResource()); //$NON-NLS-1$ dummyChange.setEdit(new InsertEdit(0, "")); //$NON-NLS-1$ return dummyChange; } return (TextChange) fRefactoring.createChange(new NullProgressMonitor()); }
Example #19
Source File: ImportRewriter.java From n4js with Eclipse Public License 1.0 | 5 votes |
private AliasLocation addNewImportDeclaration(QualifiedName moduleName, QualifiedName qualifiedName, String optionalAlias, int insertionOffset, MultiTextEdit result) { final String spacer = lazySpacer.get(); String syntacticModuleName = syntacticModuleName(moduleName); AliasLocation aliasLocation = null; String importSpec = (insertionOffset != 0 ? lineDelimiter : "") + "import "; if (!N4JSLanguageUtils.isDefaultExport(qualifiedName)) { // not an 'default' export importSpec = importSpec + "{" + spacer + qualifiedName.getLastSegment(); if (optionalAlias != null) { importSpec = importSpec + " as "; aliasLocation = new AliasLocation(insertionOffset, importSpec.length(), optionalAlias); importSpec = importSpec + optionalAlias; } importSpec = importSpec + spacer + "}"; } else { // import default exported element if (optionalAlias == null) { importSpec = importSpec + N4JSLanguageUtils.lastSegmentOrDefaultHost(qualifiedName); } else { aliasLocation = new AliasLocation(insertionOffset, importSpec.length(), optionalAlias); importSpec = importSpec + optionalAlias; } } result.addChild(new InsertEdit(insertionOffset, importSpec + " from " + syntacticModuleName + ";" + (insertionOffset != 0 ? "" : lineDelimiter))); return aliasLocation; }
Example #20
Source File: OrganizeImportsHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private static boolean areEqual(InsertEdit edit1, InsertEdit edit2) { if (edit1 != null && edit2 != null) { return edit1.getOffset() == edit2.getOffset() && edit1.getLength() == edit2.getLength() && edit1.getText().equals(edit2.getText()); } return false; }
Example #21
Source File: NLSSourceModifier.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private TextEdit createAddTagChange(NLSElement element) { int offset= element.getTagPosition().getOffset(); //to be changed String text= ' ' + element.getTagText(); return new InsertEdit(offset, text); }
Example #22
Source File: AddJavaDocStubOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private void addJavadocComments(IProgressMonitor monitor) throws CoreException { ICompilationUnit cu= fMembers[0].getCompilationUnit(); ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); IPath path= cu.getPath(); manager.connect(path, LocationKind.IFILE, new SubProgressMonitor(monitor, 1)); try { IDocument document= manager.getTextFileBuffer(path, LocationKind.IFILE).getDocument(); String lineDelim= TextUtilities.getDefaultLineDelimiter(document); MultiTextEdit edit= new MultiTextEdit(); for (int i= 0; i < fMembers.length; i++) { IMember curr= fMembers[i]; int memberStartOffset= getMemberStartOffset(curr, document); String comment= null; switch (curr.getElementType()) { case IJavaElement.TYPE: comment= createTypeComment((IType) curr, lineDelim); break; case IJavaElement.FIELD: comment= createFieldComment((IField) curr, lineDelim); break; case IJavaElement.METHOD: comment= createMethodComment((IMethod) curr, lineDelim); break; } if (comment == null) { StringBuffer buf= new StringBuffer(); buf.append("/**").append(lineDelim); //$NON-NLS-1$ buf.append(" *").append(lineDelim); //$NON-NLS-1$ buf.append(" */").append(lineDelim); //$NON-NLS-1$ comment= buf.toString(); } else { if (!comment.endsWith(lineDelim)) { comment= comment + lineDelim; } } final IJavaProject project= cu.getJavaProject(); IRegion region= document.getLineInformationOfOffset(memberStartOffset); String line= document.get(region.getOffset(), region.getLength()); String indentString= Strings.getIndentString(line, project); String indentedComment= Strings.changeIndent(comment, 0, project, indentString, lineDelim); edit.addChild(new InsertEdit(memberStartOffset, indentedComment)); monitor.worked(1); } edit.apply(document); // apply all edits } catch (BadLocationException e) { throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e)); } finally { manager.disconnect(path, LocationKind.IFILE,new SubProgressMonitor(monitor, 1)); } }
Example #23
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; }
Example #24
Source File: EditAnnotator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
@Override public boolean visit(InsertEdit edit) { return rangeAdded(edit); }
Example #25
Source File: AbstractInsertEdit.java From Pydev with Eclipse Public License 1.0 | 4 votes |
@Override public InsertEdit getEdit() throws MisconfigurationException { return new InsertEdit(getOffset(), getFormattedNode()); }