Search in sources :

Example 26 with InsertEdit

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

the class DefaultXMLPartitionFormatter method collapseSpaces.

private int collapseSpaces(TextEdit textEdit, int spaceStartOffset, int availableLineWidth, String whitespaceRun) {
    // prefer to use use existing whitespace
    int existingWhitespaceOffset = whitespaceRun.indexOf(' ');
    if (existingWhitespaceOffset > -1) {
        // delete whitespaces before and after existing whitespace
        if (existingWhitespaceOffset > 0) {
            DeleteEdit deleteEdit = new DeleteEdit(spaceStartOffset, existingWhitespaceOffset);
            textEdit.addChild(deleteEdit);
        }
        if (existingWhitespaceOffset < whitespaceRun.length() - 1) {
            int nextOffset = existingWhitespaceOffset + 1;
            DeleteEdit deleteEdit = new DeleteEdit(spaceStartOffset + nextOffset, whitespaceRun.length() - nextOffset);
            textEdit.addChild(deleteEdit);
        }
    } else {
        // delete all whitespace and insert new one
        // collapse whitespace by deleting whitespace
        DeleteEdit deleteEdit = new DeleteEdit(spaceStartOffset, whitespaceRun.length());
        textEdit.addChild(deleteEdit);
        // then insert one space
        InsertEdit insertEdit = new InsertEdit(spaceStartOffset, SPACE);
        textEdit.addChild(insertEdit);
    }
    // remember to account for space added
    --availableLineWidth;
    return availableLineWidth;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit)

Example 27 with InsertEdit

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

the class Utils method editsToJsonArray.

public static JSONArray editsToJsonArray(TextEdit edit) {
    final LinkedList<JSONObject> list = new LinkedList<JSONObject>();
    edit.accept(new TextEditVisitor() {

        @Override
        public boolean visit(DeleteEdit delete) {
            try {
                JSONObject json = new JSONObject();
                json.put("offset", delete.getOffset());
                json.put("length", delete.getLength());
                json.put("text", "");
                list.addFirst(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return super.visit(delete);
        }

        @Override
        public boolean visit(InsertEdit insert) {
            try {
                JSONObject json = new JSONObject();
                json.put("offset", insert.getOffset());
                json.put("length", 0);
                json.put("text", insert.getText());
                list.addFirst(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return super.visit(insert);
        }

        @Override
        public boolean visit(ReplaceEdit replace) {
            try {
                JSONObject json = new JSONObject();
                json.put("offset", replace.getOffset());
                json.put("length", replace.getLength());
                json.put("text", replace.getText());
                list.addFirst(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return super.visit(replace);
        }
    });
    return new JSONArray(list);
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) JSONObject(org.json.JSONObject) TextEditVisitor(org.eclipse.text.edits.TextEditVisitor) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) DeleteEdit(org.eclipse.text.edits.DeleteEdit) LinkedList(java.util.LinkedList)

Example 28 with InsertEdit

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

the class RefactoringCorrectionProposal method createTextChange.

@Override
protected TextChange createTextChange() throws CoreException {
    init(fRefactoring);
    fRefactoringStatus = fRefactoring.checkFinalConditions(new NullProgressMonitor());
    if (fRefactoringStatus.hasFatalError()) {
        //$NON-NLS-1$
        TextFileChange dummyChange = new TextFileChange("fatal error", (IFile) getCompilationUnit().getResource());
        //$NON-NLS-1$
        dummyChange.setEdit(new InsertEdit(0, ""));
        return dummyChange;
    }
    return (TextChange) fRefactoring.createChange(new NullProgressMonitor());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) InsertEdit(org.eclipse.text.edits.InsertEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange)

Example 29 with InsertEdit

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

the class CorrectPackageDeclarationProposal method addEdits.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jdt.internal.corext.textmanipulation
	 * .TextBuffer)
	 */
@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);
        //$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)

Example 30 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method replaceLineCommentBeforeJavaElement.

private void replaceLineCommentBeforeJavaElement(List<TextEdit> commentEdits, LineComment lineComment, List<LineComment> lineComments, int i, String source, TreeSet<Integer> lineStarts) {
    final int replaceLength = "//".length();
    final boolean isFirst = i == 0;
    String replacementText;
    final SourceLocation indentLoc = getIndentForJavadoc(lineComment, source, lineStarts);
    if (isFirst) {
        // TODO JNR how to obey configured indentation?
        replacementText = "/**" + lineSeparator + indentLoc.substring(source) + " *";
    } else {
        replacementText = " *";
    }
    final boolean commentStartsWithSlash = source.charAt(lineComment.getStartPosition() + replaceLength) == '/';
    if (commentStartsWithSlash) {
        replacementText += " ";
    }
    commentEdits.add(new ReplaceEdit(lineComment.getStartPosition(), replaceLength, replacementText));
    replaceEndsOfBlockCommentFromCommentText(commentEdits, lineComment, source);
    final boolean isLast = i == lineComments.size() - 1;
    if (isLast) {
        // TODO JNR how to obey configured indentation?
        final int position = getEndPosition(lineComment);
        commentEdits.add(new InsertEdit(position, lineSeparator + indentLoc.substring(source) + " */"));
    }
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Aggregations

InsertEdit (org.eclipse.text.edits.InsertEdit)61 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)33 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)22 TextEdit (org.eclipse.text.edits.TextEdit)22 DeleteEdit (org.eclipse.text.edits.DeleteEdit)20 Test (org.junit.Test)18 UndoEdit (org.eclipse.text.edits.UndoEdit)13 BadLocationException (org.eclipse.jface.text.BadLocationException)12 ArrayList (java.util.ArrayList)9 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)8 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)8 Location (org.eclipse.titan.designer.AST.Location)8 Module (org.eclipse.titan.designer.AST.Module)7 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)7 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)5 CoreException (org.eclipse.core.runtime.CoreException)5 IFile (org.eclipse.core.resources.IFile)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IDocument (org.eclipse.jface.text.IDocument)4 IRegion (org.eclipse.jface.text.IRegion)4