Search in sources :

Example 31 with DeleteEdit

use of org.eclipse.text.edits.DeleteEdit in project eclipse.platform.text by eclipse.

the class TextEditTests method testDelete1.

@Test
public void testDelete1() throws Exception {
    TextEdit e1 = new DeleteEdit(3, 1);
    fRoot.addChild(e1);
    UndoEdit undo = fRoot.apply(fDocument);
    assertEquals(fRoot, 3, 0);
    assertEquals(e1, 3, 0);
    Assert.assertEquals("Buffer content", "012456789", fDocument.get());
    doUndoRedo(undo, "012456789");
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Example 32 with DeleteEdit

use of org.eclipse.text.edits.DeleteEdit in project eclipse.platform.text by eclipse.

the class RemoveTrailingWhitespaceOperation method computeTextEdit.

@Override
protected MultiTextEditWithProgress computeTextEdit(ITextFileBuffer fileBuffer, IProgressMonitor progressMonitor) throws CoreException {
    IDocument document = fileBuffer.getDocument();
    int lineCount = document.getNumberOfLines();
    SubMonitor subMonitor = SubMonitor.convert(progressMonitor, FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_generatingChanges, lineCount);
    try {
        MultiTextEditWithProgress multiEdit = new MultiTextEditWithProgress(FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_applyingChanges);
        for (int i = 0; i < lineCount; i++) {
            IRegion region = document.getLineInformation(i);
            if (region.getLength() == 0)
                continue;
            int lineStart = region.getOffset();
            int lineExclusiveEnd = lineStart + region.getLength();
            int j = lineExclusiveEnd - 1;
            while (j >= lineStart && Character.isWhitespace(document.getChar(j))) --j;
            ++j;
            if (j < lineExclusiveEnd)
                multiEdit.addChild(new DeleteEdit(j, lineExclusiveEnd - j));
            subMonitor.split(1);
        }
        return multiEdit.getChildrenSize() <= 0 ? null : multiEdit;
    } catch (BadLocationException x) {
        // $NON-NLS-1$
        throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, "", x));
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) SubMonitor(org.eclipse.core.runtime.SubMonitor) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 33 with DeleteEdit

use of org.eclipse.text.edits.DeleteEdit in project erlide_eclipse by erlang.

the class ChangesetMaker method createReplaceEdit.

/**
 * Creates a <code>ReplaceEdit</code> object from the given parameters and
 * the stored input/output strings.
 *
 * @param addedStart
 * @param addedEnd
 * @param deletedStart
 * @param deletedEnd
 * @return
 */
private static TextEdit createReplaceEdit(final int addedStart, final int addedEnd, final int deletedStart, final int deletedEnd) {
    final TextEdit result = new MultiTextEdit();
    final int addedLength = addedEnd - addedStart + 1;
    final int deletedLength = deletedEnd - deletedStart + 1;
    final int minLength = Math.min(addedLength, deletedLength);
    if (deletedLength < addedLength) {
        result.addChild(new InsertEdit(deletedStart + minLength, ChangesetMaker.getString(addedStart + minLength, addedEnd)));
    }
    result.addChild(new ReplaceEdit(deletedStart, minLength, ChangesetMaker.getString(addedStart, addedStart + minLength - 1)));
    if (addedLength < deletedLength) {
        result.addChild(new DeleteEdit(deletedStart + minLength, deletedLength - minLength));
    }
    return result;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 34 with DeleteEdit

use of org.eclipse.text.edits.DeleteEdit in project webtools.sourceediting by eclipse.

the class JSPTranslationExtension method getJspEdit.

/**
 * Returns a corresponding TextEdit for the JSP file given a TextEdit for
 * a Java file.
 *
 * @param javaEdit
 * @return the corresponding JSP edits (not applied to the document yet)
 */
public TextEdit getJspEdit(TextEdit javaEdit) {
    if (javaEdit == null)
        return null;
    List jspEdits = new ArrayList();
    int offset = javaEdit.getOffset();
    int length = javaEdit.getLength();
    if (javaEdit instanceof MultiTextEdit && javaEdit.getChildren().length > 0) {
        IRegion r = TextEdit.getCoverage(getAllEdits(javaEdit));
        offset = r.getOffset();
        length = r.getLength();
    }
    // get java ranges that will be affected by the edit
    Position[] javaPositions = getJavaRanges(offset, length);
    // record position data before the change
    Position[] jspPositions = new Position[javaPositions.length];
    PositionDelta[] deltas = new PositionDelta[javaPositions.length];
    for (int i = 0; i < javaPositions.length; i++) {
        deltas[i] = new PositionDelta(javaPositions[i].offset, javaPositions[i].length);
        // mapping from java <-> jsp (eg. an import statement)
        if (!isIndirect(javaPositions[i].offset))
            jspPositions[i] = (Position) getJava2JspMap().get(javaPositions[i]);
    }
    if (DEBUG) {
        // $NON-NLS-1$
        System.out.println("================================================");
        // $NON-NLS-1$
        System.out.println("deltas:");
        String javaText = getJavaText();
        for (int i = 0; i < deltas.length; i++) // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        System.out.println("pos[" + deltas[i].preOffset + ":" + deltas[i].preLength + "]" + javaText.substring(deltas[i].preOffset, deltas[i].preOffset + deltas[i].preLength));
        // $NON-NLS-1$
        System.out.println("===============================================");
    }
    UndoEdit undo = null;
    // apply the edit to the java document
    try {
        undo = javaEdit.apply(getJavaDocument());
    } catch (MalformedTreeException e) {
        Logger.logException(e);
    } catch (BadLocationException e) {
        Logger.logException(e);
    }
    // now at this point Java positions are unreliable since they were
    // updated after applying java edit.
    String newJavaText = getJavaDocument().get();
    if (DEBUG)
        // $NON-NLS-1$
        System.out.println("java post format text:\n" + newJavaText);
    // record post edit data
    for (int i = 0; i < javaPositions.length; i++) deltas[i].setPostEditData(javaPositions[i].offset, javaPositions[i].length, javaPositions[i].isDeleted);
    // create appropriate text edits for deltas
    Position jspPos = null;
    // $NON-NLS-1$
    String replaceText = "";
    for (int i = 0; i < deltas.length; i++) {
        jspPos = jspPositions[i];
        if (jspPos != null) {
            if (deltas[i].isDeleted) {
                jspEdits.add(new DeleteEdit(jspPos.offset, jspPos.length));
            } else {
                replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
                // get rid of pre and post white space or fine tuned
                // adjustment later.
                // fix text here...
                replaceText = fixJspReplaceText(replaceText, jspPos);
                if (// Unwanted TextEdit can lead to MalformedTreeException.See: Bug 321977
                !(replaceText.length() == 0 && jspPos.length == 0))
                    jspEdits.add(new ReplaceEdit(jspPos.offset, jspPos.length, replaceText));
            }
            if (DEBUG)
                debugReplace(deltas, jspPos, replaceText, i);
        } else {
            // possible new import?
            if (isImport(javaPositions[i].getOffset()) && replaceText.lastIndexOf("import ") != -1) {
                // $NON-NLS-1$
                replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
                // $NON-NLS-1$ //$NON-NLS-2$
                String importText = replaceText.substring(replaceText.lastIndexOf("import "), replaceText.indexOf(";"));
                // evenutally need to check if it's XML-JSP
                // $NON-NLS-1$ //$NON-NLS-2$
                importText = "<%@page import=\"" + importText + "\" %>\n";
                jspEdits.add(new InsertEdit(0, importText));
            }
        }
    }
    TextEdit allJspEdits = createMultiTextEdit((TextEdit[]) jspEdits.toArray(new TextEdit[jspEdits.size()]));
    // editor)
    if (undo != null) {
        try {
            undo.apply(getJavaDocument());
        } catch (MalformedTreeException e) {
            Logger.logException(e);
        } catch (BadLocationException e) {
            Logger.logException(e);
        }
    }
    return allJspEdits;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IRegion(org.eclipse.jface.text.IRegion) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) ArrayList(java.util.ArrayList) List(java.util.List) UndoEdit(org.eclipse.text.edits.UndoEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 35 with DeleteEdit

use of org.eclipse.text.edits.DeleteEdit in project eclipse.jdt.ls by eclipse.

the class CorrectPackageDeclarationProposal method addEdits.

@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";
        // $NON-NLS-1$
        String str = "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim;
        root.addChild(new InsertEdit(0, str));
        return;
    }
    root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) DeleteEdit(org.eclipse.text.edits.DeleteEdit) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

DeleteEdit (org.eclipse.text.edits.DeleteEdit)40 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)26 InsertEdit (org.eclipse.text.edits.InsertEdit)20 TextEdit (org.eclipse.text.edits.TextEdit)17 Test (org.junit.Test)15 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)11 UndoEdit (org.eclipse.text.edits.UndoEdit)10 IRegion (org.eclipse.jface.text.IRegion)6 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)6 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)6 ArrayList (java.util.ArrayList)5 IDocument (org.eclipse.jface.text.IDocument)5 HashSet (java.util.HashSet)4 BadLocationException (org.eclipse.jface.text.BadLocationException)4 RangeMarker (org.eclipse.text.edits.RangeMarker)4 Location (org.eclipse.titan.designer.AST.Location)4 Module (org.eclipse.titan.designer.AST.Module)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 Document (org.eclipse.jface.text.Document)3 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)3