Search in sources :

Example 21 with DeleteEdit

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

the class JSPTranslationUtil method translateTextEdit.

public TextEdit translateTextEdit(TextEdit textEdit) {
    TextEdit translatedTextEdit = null;
    int javaOffset = textEdit.getOffset();
    int jspOffset = getTranslation().getJspOffset(textEdit.getOffset());
    int length = textEdit.getLength();
    if (textEdit instanceof MultiTextEdit) {
        translatedTextEdit = new MultiTextEdit();
        TextEdit[] children = ((MultiTextEdit) textEdit).getChildren();
        for (int i = 0; i < children.length; i++) {
            TextEdit translatedChildTextEdit = translateTextEdit(children[i]);
            if (translatedChildTextEdit != null)
                ((MultiTextEdit) translatedTextEdit).addChild(translatedChildTextEdit);
        }
    } else if (textEdit instanceof ReplaceEdit) {
        if (jspOffset == -1)
            return null;
        if (!getTranslation().javaSpansMultipleJspPartitions(javaOffset, length))
            translatedTextEdit = new ReplaceEdit(jspOffset, length, ((ReplaceEdit) textEdit).getText());
    } else if (textEdit instanceof InsertEdit) {
        translatedTextEdit = new InsertEdit(jspOffset, ((InsertEdit) textEdit).getText());
    } else if (textEdit instanceof DeleteEdit) {
        translatedTextEdit = new DeleteEdit(jspOffset, length);
        TextEdit[] children = ((DeleteEdit) textEdit).getChildren();
        for (int i = 0; i < children.length; i++) {
            TextEdit translatedChildTextEdit = translateTextEdit(children[i]);
            if (translatedChildTextEdit != null)
                ((DeleteEdit) translatedTextEdit).addChild(translatedChildTextEdit);
        }
    } else if (textEdit instanceof CopySourceEdit) {
        translatedTextEdit = new CopySourceEdit(jspOffset, length);
        ((CopySourceEdit) translatedTextEdit).setTargetEdit(((CopySourceEdit) textEdit).getTargetEdit());
        ((CopySourceEdit) translatedTextEdit).setSourceModifier(((CopySourceEdit) textEdit).getSourceModifier());
    } else if (textEdit instanceof CopyTargetEdit) {
        translatedTextEdit = new CopyTargetEdit(jspOffset);
        ((CopyTargetEdit) textEdit).getSourceEdit().setTargetEdit((CopyTargetEdit) translatedTextEdit);
    } else if (textEdit instanceof MoveSourceEdit) {
        translatedTextEdit = new MoveSourceEdit(jspOffset, length);
        ((MoveSourceEdit) translatedTextEdit).setTargetEdit(((MoveSourceEdit) textEdit).getTargetEdit());
    } else if (textEdit instanceof MoveTargetEdit) {
        translatedTextEdit = new MoveTargetEdit(jspOffset);
        ((MoveTargetEdit) textEdit).getSourceEdit().setTargetEdit((MoveTargetEdit) translatedTextEdit);
    } else {
        // $NON-NLS-1$
        System.out.println("Need to translate " + textEdit);
    }
    return translatedTextEdit;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) CopySourceEdit(org.eclipse.text.edits.CopySourceEdit) MoveTargetEdit(org.eclipse.text.edits.MoveTargetEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CopyTargetEdit(org.eclipse.text.edits.CopyTargetEdit) MoveSourceEdit(org.eclipse.text.edits.MoveSourceEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 22 with DeleteEdit

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

the class DefaultXMLPartitionFormatter method formatTextInContent.

/**
 * Format the text in xml content
 *
 * @param textEdit
 * @param parentConstraints
 * @param currentRegion
 * @param fullText
 * @param whitespaceMode
 */
private void formatTextInContent(TextEdit textEdit, XMLFormattingConstraints parentConstraints, IStructuredDocumentRegion currentRegion, IStructuredDocumentRegion nextRegion, String fullText, String whitespaceMode) {
    int availableLineWidth = parentConstraints.getAvailableLineWidth();
    // determine indentation
    boolean forceInitialIndent = false;
    int indentLevel = parentConstraints.getIndentLevel() + 1;
    String indentMode = parentConstraints.getIndentStrategy();
    if (XMLFormattingConstraints.INDENT.equals(indentMode)) {
        forceInitialIndent = true;
    }
    if (XMLFormattingConstraints.NEW_LINE.equals(indentMode)) {
        indentLevel = parentConstraints.getIndentLevel();
        forceInitialIndent = true;
    }
    int fullTextOffset = 0;
    char[] fullTextArray = fullText.toCharArray();
    while (fullTextOffset < fullTextArray.length) {
        // gather all whitespaces
        String whitespaceRun = getCharacterRun(fullTextArray, fullTextOffset, true);
        if (whitespaceRun.length() > 0) {
            // offset where whitespace starts
            int whitespaceStart = fullTextOffset;
            // update current offset in fullText
            fullTextOffset += whitespaceRun.length();
            // gather following word
            String characterRun = getCharacterRun(fullTextArray, fullTextOffset, false);
            int characterRunLength = characterRun.length();
            if (characterRunLength > 0) {
                // indent if word is too long or forcing initial
                // indent
                availableLineWidth -= characterRunLength;
                // offset where indent/collapse will happen
                int startOffset = currentRegion.getStartOffset() + whitespaceStart;
                if (forceInitialIndent || (availableLineWidth <= 0)) {
                    // indent if not already indented
                    availableLineWidth = indentIfNotAlreadyIndented(textEdit, currentRegion, indentLevel, startOffset, whitespaceRun);
                    // remember to subtract word length
                    availableLineWidth -= characterRunLength;
                    // initial indent done
                    forceInitialIndent = false;
                } else {
                    // just collapse spaces, but adjust for any indenting that may result from preserving line delimiters
                    if (whitespaceStart == 0 && XMLFormattingConstraints.IGNOREANDTRIM.equals(whitespaceMode)) {
                        // if ignore, trim
                        DeleteEdit deleteTrailing = new DeleteEdit(startOffset, whitespaceRun.length());
                        textEdit.addChild(deleteTrailing);
                    } else if (XMLFormattingConstraints.REPLACE.equals(whitespaceMode))
                        availableLineWidth = replaceSpaces(textEdit, startOffset, availableLineWidth, whitespaceRun);
                    else
                        availableLineWidth = collapseAndIndent(textEdit, startOffset, availableLineWidth, indentLevel, whitespaceRun, currentRegion);
                }
                fullTextOffset += characterRunLength;
            } else {
                // handle trailing whitespace
                int whitespaceOffset = currentRegion.getStartOffset() + whitespaceStart;
                if (XMLFormattingConstraints.REPLACE.equals(whitespaceMode))
                    availableLineWidth = replaceSpaces(textEdit, whitespaceOffset, availableLineWidth, whitespaceRun);
                else if (XMLFormattingConstraints.IGNOREANDTRIM.equals(whitespaceMode)) {
                    // always trim
                    DeleteEdit deleteTrailing = new DeleteEdit(whitespaceOffset, whitespaceRun.length());
                    textEdit.addChild(deleteTrailing);
                } else if (getFormattingPreferences().getClearAllBlankLines()) {
                    if (!nextRegionHandlesTrailingWhitespace(nextRegion)) {
                        if (XMLFormattingConstraints.IGNORE.equals(whitespaceMode)) {
                            // if ignore, trim
                            DeleteEdit deleteTrailing = new DeleteEdit(whitespaceOffset, whitespaceRun.length());
                            textEdit.addChild(deleteTrailing);
                        } else {
                            // if collapse, leave a space. but what if end up
                            // wanting to add indent? then need to delete space
                            // added and add indent instead
                            availableLineWidth = collapseSpaces(textEdit, whitespaceOffset, availableLineWidth, whitespaceRun);
                        }
                    }
                }
            }
        } else {
            // gather word
            String characterRun = getCharacterRun(fullTextArray, fullTextOffset, false);
            int characterRunLength = characterRun.length();
            if (characterRunLength > 0) {
                // availableLineWidth = availableLineWidth - characterRunLength;
                if ((XMLFormattingConstraints.IGNORE.equals(whitespaceMode) || XMLFormattingConstraints.IGNOREANDTRIM.equals(whitespaceMode)) && (forceInitialIndent || (availableLineWidth <= 0))) {
                    // indent if not already indented
                    availableLineWidth = indentIfNotAlreadyIndented(textEdit, currentRegion, indentLevel, currentRegion.getStartOffset(), whitespaceRun);
                    // remember to subtract word length
                    availableLineWidth -= characterRunLength;
                    // initial indent done
                    forceInitialIndent = false;
                } else {
                    // just collapse spaces
                    availableLineWidth -= characterRunLength;
                }
                fullTextOffset += characterRunLength;
            }
        }
    }
    // update available line width
    parentConstraints.setAvailableLineWidth(availableLineWidth);
}
Also used : DeleteEdit(org.eclipse.text.edits.DeleteEdit)

Example 23 with DeleteEdit

use of org.eclipse.text.edits.DeleteEdit 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 24 with DeleteEdit

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

the class DefaultXMLPartitionFormatter method deleteTrailingSpaces.

private void deleteTrailingSpaces(TextEdit textEdit, ITextRegion currentTextRegion, IStructuredDocumentRegion currentDocumentRegion) {
    int textEnd = currentTextRegion.getTextEnd();
    int textEndOffset = currentDocumentRegion.getStartOffset() + textEnd;
    int difference = currentTextRegion.getEnd() - textEnd;
    DeleteEdit deleteEdit = new DeleteEdit(textEndOffset, difference);
    textEdit.addChild(deleteEdit);
}
Also used : DeleteEdit(org.eclipse.text.edits.DeleteEdit)

Example 25 with DeleteEdit

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

the class RemoveUnknownElementQuickFixProposal method apply.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer,
	 *      char, int, int)
	 */
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    int startTagOffset = ((Integer) ((Object[]) fAdditionalFixInfo)[0]).intValue();
    int startTagLength = ((Integer) ((Object[]) fAdditionalFixInfo)[1]).intValue();
    int endTagOffset = ((Integer) ((Object[]) fAdditionalFixInfo)[2]).intValue();
    int endTagLength = ((Integer) ((Object[]) fAdditionalFixInfo)[3]).intValue();
    MultiTextEdit multiTextEdit = new MultiTextEdit();
    if (endTagOffset != -1) {
        multiTextEdit.addChild(new DeleteEdit(endTagOffset, endTagLength));
        fSelection = new Point(endTagOffset, 0);
    }
    if (startTagOffset != -1) {
        multiTextEdit.addChild(new DeleteEdit(startTagOffset, startTagLength));
        fSelection = new Point(startTagOffset, 0);
    }
    try {
        multiTextEdit.apply(viewer.getDocument());
    } catch (MalformedTreeException e) {
        // log for now, unless find reasons not to.
        Logger.log(Logger.INFO, e.getMessage());
    } catch (BadLocationException e) {
        // log for now, unless find reasons not to.
        Logger.log(Logger.INFO, e.getMessage());
    }
}
Also used : MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Point(org.eclipse.swt.graphics.Point) DeleteEdit(org.eclipse.text.edits.DeleteEdit) Point(org.eclipse.swt.graphics.Point) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

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