Search in sources :

Example 36 with MultiTextEdit

use of org.eclipse.text.edits.MultiTextEdit in project flux by eclipse.

the class StubUtility method fixEmptyVariables.

// remove lines for empty variables
private static String fixEmptyVariables(TemplateBuffer buffer, String[] variables) throws MalformedTreeException, BadLocationException {
    IDocument doc = new Document(buffer.getString());
    int nLines = doc.getNumberOfLines();
    MultiTextEdit edit = new MultiTextEdit();
    HashSet<Integer> removedLines = new HashSet<Integer>();
    for (int i = 0; i < variables.length; i++) {
        // look if Javadoc tags have to be added
        TemplateVariable position = findVariable(buffer, variables[i]);
        if (position == null || position.getLength() > 0) {
            continue;
        }
        int[] offsets = position.getOffsets();
        for (int k = 0; k < offsets.length; k++) {
            int line = doc.getLineOfOffset(offsets[k]);
            IRegion lineInfo = doc.getLineInformation(line);
            int offset = lineInfo.getOffset();
            String str = doc.get(offset, lineInfo.getLength());
            if (Strings.containsOnlyWhitespaces(str) && nLines > line + 1 && removedLines.add(new Integer(line))) {
                int nextStart = doc.getLineOffset(line + 1);
                edit.addChild(new DeleteEdit(offset, nextStart - offset));
            }
        }
    }
    edit.apply(doc, 0);
    return doc.get();
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IRegion(org.eclipse.jface.text.IRegion) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 37 with MultiTextEdit

use of org.eclipse.text.edits.MultiTextEdit in project che by eclipse.

the class TextEditUtil method flatten.

/**
	 * Degenerates the given edit tree into a list.<br>
	 * All nodes of the result are leafs.<br>
	 * <strong>The given edit is modified and can no longer be used.</strong>
	 *
	 * @param edit the edit tree to flatten
	 * @return a list of edits
	 * @since 3.4
	 */
public static MultiTextEdit flatten(TextEdit edit) {
    MultiTextEdit result = new MultiTextEdit();
    flatten(edit, result);
    return result;
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 38 with MultiTextEdit

use of org.eclipse.text.edits.MultiTextEdit in project che by eclipse.

the class TextEditUtil method overlaps.

/**
	 * Does any node in <code>edit1</code> overlap with any other node
	 * in <code>edit2</code>.
	 * <p>If this returns true then the two edit trees can be merged into one.</p>
	 *
	 * @param edit1 the edit to compare against edit2
	 * @param edit2 the edit to compare against edit1
	 * @return true of no node overlaps with any other node
	 * @since 3.4
	 */
public static boolean overlaps(TextEdit edit1, TextEdit edit2) {
    if (edit1 instanceof MultiTextEdit && edit2 instanceof MultiTextEdit) {
        MultiTextEdit multiTextEdit1 = (MultiTextEdit) edit1;
        if (!multiTextEdit1.hasChildren())
            return false;
        MultiTextEdit multiTextEdit2 = (MultiTextEdit) edit2;
        if (!multiTextEdit2.hasChildren())
            return false;
        TextEdit[] children1 = multiTextEdit1.getChildren();
        TextEdit[] children2 = multiTextEdit2.getChildren();
        int i1 = 0;
        int i2 = 0;
        while (i1 < children1.length && i2 < children2.length) {
            while (children1[i1].getExclusiveEnd() < children2[i2].getOffset()) {
                i1++;
                if (i1 >= children1.length)
                    return false;
            }
            while (children2[i2].getExclusiveEnd() < children1[i1].getOffset()) {
                i2++;
                if (i2 >= children2.length)
                    return false;
            }
            if (children1[i1].getExclusiveEnd() < children2[i2].getOffset())
                continue;
            if (overlaps(children1[i1], children2[i2]))
                return true;
            int mergeEnd = Math.max(children1[i1].getExclusiveEnd(), children2[i2].getExclusiveEnd());
            i1++;
            i2++;
            if (i1 < children1.length && children1[i1].getOffset() < mergeEnd) {
                return true;
            }
            if (i2 < children2.length && children2[i2].getOffset() < mergeEnd) {
                return true;
            }
        }
        return false;
    } else if (edit1 instanceof MultiTextEdit) {
        MultiTextEdit multiTextEdit1 = (MultiTextEdit) edit1;
        if (!multiTextEdit1.hasChildren())
            return false;
        TextEdit[] children = multiTextEdit1.getChildren();
        int i = 0;
        while (children[i].getExclusiveEnd() < edit2.getOffset()) {
            i++;
            if (i >= children.length)
                return false;
        }
        if (overlaps(children[i], edit2))
            return true;
        return false;
    } else if (edit2 instanceof MultiTextEdit) {
        MultiTextEdit multiTextEdit2 = (MultiTextEdit) edit2;
        if (!multiTextEdit2.hasChildren())
            return false;
        TextEdit[] children = multiTextEdit2.getChildren();
        int i = 0;
        while (children[i].getExclusiveEnd() < edit1.getOffset()) {
            i++;
            if (i >= children.length)
                return false;
        }
        if (overlaps(children[i], edit1))
            return true;
        return false;
    } else {
        int start1 = edit1.getOffset();
        int end1 = start1 + edit1.getLength();
        int start2 = edit2.getOffset();
        int end2 = start2 + edit2.getLength();
        if (start1 > end2)
            return false;
        if (start2 > end1)
            return false;
        return true;
    }
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 39 with MultiTextEdit

use of org.eclipse.text.edits.MultiTextEdit in project che by eclipse.

the class TextEditUtil method merge.

/**
	 * Create an edit which contains <code>edit1</code> and <code>edit2</code>
	 * <p>If <code>edit1</code> overlaps <code>edit2</code> this method fails with a {@link MalformedTreeException}</p>
	 * <p><strong>The given edits are modified and they can no longer be used.</strong></p>
	 *
	 * @param edit1 the edit to merge with edit2
	 * @param edit2 the edit to merge with edit1
	 * @return the merged tree
	 * @throws MalformedTreeException if {@link #overlaps(TextEdit, TextEdit)} returns <b>true</b>
	 * @see #overlaps(TextEdit, TextEdit)
	 * @since 3.4
	 */
public static TextEdit merge(TextEdit edit1, TextEdit edit2) {
    if (edit1 instanceof MultiTextEdit && !edit1.hasChildren()) {
        return edit2;
    }
    if (edit2 instanceof MultiTextEdit && !edit2.hasChildren()) {
        return edit1;
    }
    MultiTextEdit result = new MultiTextEdit();
    merge(edit1, edit2, result);
    return result;
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 40 with MultiTextEdit

use of org.eclipse.text.edits.MultiTextEdit in project che by eclipse.

the class AddImportsOperation method run.

/**
	 * Runs the operation.
	 *
	 * @param monitor The progress monitor
	 * @throws CoreException if accessing the CU or rewritting the imports fails
	 * @throws OperationCanceledException Runtime error thrown when operation is canceled.
	 */
public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    try {
        monitor.beginTask(CodeGenerationMessages.AddImportsOperation_description, 4);
        CompilationUnit astRoot = SharedASTProvider.getAST(fCompilationUnit, SharedASTProvider.WAIT_YES, new SubProgressMonitor(monitor, 1));
        if (astRoot == null)
            throw new OperationCanceledException();
        ImportRewrite importRewrite = StubUtility.createImportRewrite(astRoot, true);
        MultiTextEdit res = new MultiTextEdit();
        TextEdit edit = evaluateEdits(astRoot, importRewrite, fSelectionOffset, fSelectionLength, new SubProgressMonitor(monitor, 1));
        if (edit == null) {
            return;
        }
        res.addChild(edit);
        TextEdit importsEdit = importRewrite.rewriteImports(new SubProgressMonitor(monitor, 1));
        res.addChild(importsEdit);
        fResultingEdit = res;
        if (fApply) {
            JavaModelUtil.applyEdit(fCompilationUnit, res, fDoSave, new SubProgressMonitor(monitor, 1));
        }
    } finally {
        monitor.done();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Aggregations

MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)51 TextEdit (org.eclipse.text.edits.TextEdit)33 Test (org.junit.Test)18 IDocument (org.eclipse.jface.text.IDocument)13 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)13 Document (org.eclipse.jface.text.Document)11 TextEditGroup (org.eclipse.text.edits.TextEditGroup)10 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)9 DeleteEdit (org.eclipse.text.edits.DeleteEdit)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 ArrayList (java.util.ArrayList)5 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)5 HashSet (java.util.HashSet)4 List (java.util.List)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 TextChange (org.eclipse.ltk.core.refactoring.TextChange)4 CopySourceEdit (org.eclipse.text.edits.CopySourceEdit)4 TextEditCopier (org.eclipse.text.edits.TextEditCopier)4