Search in sources :

Example 21 with InsertEdit

use of org.eclipse.text.edits.InsertEdit 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 InsertEdit

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

the class ElementNodeCleanupHandler method insertRequiredAttrs.

private IDOMNode insertRequiredAttrs(IDOMNode node) {
    boolean insertRequiredAttrs = getCleanupPreferences().getInsertRequiredAttrs();
    IDOMNode newNode = node;
    if (insertRequiredAttrs) {
        List requiredAttrs = getRequiredAttrs(newNode);
        if (requiredAttrs.size() > 0) {
            NamedNodeMap currentAttrs = node.getAttributes();
            List insertAttrs = new ArrayList();
            if (currentAttrs.getLength() == 0)
                insertAttrs.addAll(requiredAttrs);
            else {
                for (int i = 0; i < requiredAttrs.size(); i++) {
                    String requiredAttrName = ((CMAttributeDeclaration) requiredAttrs.get(i)).getAttrName();
                    boolean found = false;
                    for (int j = 0; j < currentAttrs.getLength(); j++) {
                        String currentAttrName = currentAttrs.item(j).getNodeName();
                        if (requiredAttrName.compareToIgnoreCase(currentAttrName) == 0) {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                        insertAttrs.add(requiredAttrs.get(i));
                }
            }
            if (insertAttrs.size() > 0) {
                IStructuredDocumentRegion startStructuredDocumentRegion = newNode.getStartStructuredDocumentRegion();
                int index = startStructuredDocumentRegion.getEndOffset();
                ITextRegion lastRegion = startStructuredDocumentRegion.getLastRegion();
                if (lastRegion.getType() == DOMRegionContext.XML_TAG_CLOSE) {
                    index--;
                    lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
                } else if (lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
                    index = index - 2;
                    lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
                }
                MultiTextEdit multiTextEdit = new MultiTextEdit();
                try {
                    for (int i = insertAttrs.size() - 1; i >= 0; i--) {
                        CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) insertAttrs.get(i);
                        String requiredAttributeName = attrDecl.getAttrName();
                        String defaultValue = attrDecl.getDefaultValue();
                        if (defaultValue == null)
                            // $NON-NLS-1$
                            defaultValue = "";
                        // $NON-NLS-1$
                        String nameAndDefaultValue = " ";
                        if (i == 0 && lastRegion.getLength() > lastRegion.getTextLength())
                            // $NON-NLS-1$
                            nameAndDefaultValue = "";
                        // $NON-NLS-1$ //$NON-NLS-2$
                        nameAndDefaultValue += requiredAttributeName + "=\"" + defaultValue + "\"";
                        multiTextEdit.addChild(new InsertEdit(index, nameAndDefaultValue));
                    // BUG3381: MultiTextEdit applies all child
                    // TextEdit's basing on offsets
                    // in the document before the first TextEdit, not
                    // after each
                    // child TextEdit. Therefore, do not need to
                    // advance the index.
                    // index += nameAndDefaultValue.length();
                    }
                    multiTextEdit.apply(newNode.getStructuredDocument());
                } catch (BadLocationException e) {
                    // log or now, unless we find reason not to
                    Logger.log(Logger.INFO, e.getMessage());
                }
            }
        }
    }
    return newNode;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) InsertEdit(org.eclipse.text.edits.InsertEdit) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) ArrayList(java.util.ArrayList) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 23 with InsertEdit

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

the class InsertRequiredAttrsQuickAssistProposal 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) {
    IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, offset);
    IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
    int index = startStructuredDocumentRegion.getEndOffset();
    ITextRegion lastRegion = startStructuredDocumentRegion.getLastRegion();
    if (lastRegion.getType() == DOMRegionContext.XML_TAG_CLOSE) {
        index--;
        lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
    } else if (lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
        index = index - 2;
        lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
    }
    MultiTextEdit multiTextEdit = new MultiTextEdit();
    try {
        for (int i = 0; i < fRequiredAttrs.size(); i++) {
            CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) fRequiredAttrs.get(i);
            String requiredAttributeName = attrDecl.getAttrName();
            String defaultValue = attrDecl.getDefaultValue();
            if (defaultValue == null) {
                // $NON-NLS-1$
                defaultValue = "";
            }
            // $NON-NLS-1$
            String nameAndDefaultValue = " ";
            if ((i == 0) && (lastRegion.getLength() > lastRegion.getTextLength())) {
                // $NON-NLS-1$
                nameAndDefaultValue = "";
            }
            // $NON-NLS-1$//$NON-NLS-2$
            nameAndDefaultValue += requiredAttributeName + "=\"" + defaultValue + "\"";
            multiTextEdit.addChild(new InsertEdit(index, nameAndDefaultValue));
        // BUG3381: MultiTextEdit applies all child TextEdit's basing
        // on offsets
        // in the document before the first TextEdit, not after each
        // child TextEdit. Therefore, do not need to advance the
        // index.
        // index += nameAndDefaultValue.length();
        }
        multiTextEdit.apply(viewer.getDocument());
    } catch (BadLocationException e) {
        // log, for now, unless we find there's reasons why we get some
        // here.
        Logger.log(Logger.INFO, e.getMessage());
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) InsertEdit(org.eclipse.text.edits.InsertEdit) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) Point(org.eclipse.swt.graphics.Point) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 24 with InsertEdit

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

the class DefaultXMLPartitionFormatter method indentIfNotAlreadyIndented.

/**
 * Indent if whitespaceRun does not already contain an indent
 *
 * @param textEdit
 * @param indentLevel
 * @param indentStartOffset
 * @param maxAvailableLineWidth
 * @param whitespaceRun
 * @return new available line width up to where indented
 */
private int indentIfNotAlreadyIndented(TextEdit textEdit, IStructuredDocumentRegion currentRegion, int indentLevel, int indentStartOffset, String whitespaceRun) {
    int maxAvailableLineWidth = getFormattingPreferences().getMaxLineWidth();
    int availableLineWidth;
    String indentString = getIndentString(indentLevel);
    String lineDelimiter = getLineDelimiter(currentRegion);
    String newLineAndIndent = lineDelimiter + indentString;
    TextEdit indentation = null;
    // if not already correctly indented
    if (!newLineAndIndent.equals(whitespaceRun)) {
        if (getFormattingPreferences().getClearAllBlankLines()) {
            if (whitespaceRun != null) {
                // replace existing whitespace run
                indentation = new ReplaceEdit(indentStartOffset, whitespaceRun.length(), newLineAndIndent);
            } else {
                // just insert correct indent
                indentation = new InsertEdit(indentStartOffset, newLineAndIndent);
            }
        } else // Keep the empty lines
        {
            // just insert correct indent
            if (whitespaceRun == null)
                indentation = new InsertEdit(indentStartOffset, newLineAndIndent);
            else // Need to preserve the number of empty lines, but still indent on the current line properly
            {
                String existingDelimiters = extractLineDelimiters(whitespaceRun, currentRegion);
                if (existingDelimiters != null && existingDelimiters.length() > 0) {
                    String formatted = existingDelimiters + indentString;
                    // Don't perform a replace if the formatted string is the same as the existing whitespaceRun
                    if (!formatted.equals(whitespaceRun))
                        indentation = new ReplaceEdit(indentStartOffset, whitespaceRun.length(), formatted);
                } else
                    // No blank lines to preserve - correct the indent
                    indentation = new ReplaceEdit(indentStartOffset, whitespaceRun.length(), newLineAndIndent);
            }
        }
    }
    if (indentation != null)
        textEdit.addChild(indentation);
    // update line width
    availableLineWidth = maxAvailableLineWidth - indentString.length();
    return availableLineWidth;
}
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)

Example 25 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)

Aggregations

InsertEdit (org.eclipse.text.edits.InsertEdit)73 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)33 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)25 TextEdit (org.eclipse.text.edits.TextEdit)24 DeleteEdit (org.eclipse.text.edits.DeleteEdit)22 Test (org.junit.Test)22 BadLocationException (org.eclipse.jface.text.BadLocationException)13 UndoEdit (org.eclipse.text.edits.UndoEdit)13 ArrayList (java.util.ArrayList)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)10 IDocument (org.eclipse.jface.text.IDocument)8 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)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 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4