Search in sources :

Example 51 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 for 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 52 with InsertEdit

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

the class ImportRewriter method addNewImportDeclaration.

private AliasLocation addNewImportDeclaration(QualifiedName moduleName, QualifiedName qualifiedName, String optionalAlias, int insertionOffset, MultiTextEdit result) {
    final String spacer = lazySpacer.get();
    String syntacticModuleName = syntacticModuleName(moduleName);
    AliasLocation aliasLocation = null;
    String importSpec = (insertionOffset != 0 ? lineDelimiter : "") + "import ";
    if (!N4JSLanguageUtils.isDefaultExport(qualifiedName)) {
        // not an 'default' export
        importSpec = importSpec + "{" + spacer + qualifiedName.getLastSegment();
        if (optionalAlias != null) {
            importSpec = importSpec + " as ";
            aliasLocation = new AliasLocation(insertionOffset, importSpec.length(), optionalAlias);
            importSpec = importSpec + optionalAlias;
        }
        importSpec = importSpec + spacer + "}";
    } else {
        // import default exported element
        if (optionalAlias == null) {
            importSpec = importSpec + N4JSLanguageUtils.lastSegmentOrDefaultHost(qualifiedName);
        } else {
            aliasLocation = new AliasLocation(insertionOffset, importSpec.length(), optionalAlias);
            importSpec = importSpec + optionalAlias;
        }
    }
    result.addChild(new InsertEdit(insertionOffset, importSpec + " from " + syntacticModuleName + ";" + (insertionOffset != 0 ? "" : lineDelimiter)));
    return aliasLocation;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit)

Example 53 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project liferay-ide by liferay.

the class InputContext method flushModel.

protected void flushModel(IDocument doc) {
    boolean flushed = true;
    if (fEditOperations.size() > 0) {
        try {
            MultiTextEdit edit = new MultiTextEdit();
            if (isNewlineNeeded(doc)) {
                insert(edit, new InsertEdit(doc.getLength(), TextUtilities.getDefaultLineDelimiter(doc)));
            }
            for (int i = 0; i < fEditOperations.size(); i++) {
                insert(edit, (TextEdit) fEditOperations.get(i));
            }
            if (_fModel instanceof IEditingModel) {
                ((IEditingModel) _fModel).setStale(true);
            }
            edit.apply(doc);
            fEditOperations.clear();
        } catch (MalformedTreeException mte) {
            LiferayUIPlugin.logError(mte);
            flushed = false;
        } catch (BadLocationException ble) {
            LiferayUIPlugin.logError(ble);
            flushed = false;
        }
    }
    if (flushed && (_fModel instanceof IEditable)) {
        ((IEditable) _fModel).setDirty(false);
    }
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) IEditingModel(com.liferay.ide.core.model.IEditingModel) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IEditable(com.liferay.ide.core.model.IEditable) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 54 with InsertEdit

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

Example 55 with InsertEdit

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

the class Utils method getOffsetAdjustment.

public static int getOffsetAdjustment(TextEdit edit, final int offset) {
    final int[] holder = new int[] { 0 };
    edit.accept(new TextEditVisitor() {

        @Override
        public boolean visit(DeleteEdit edit) {
            if (offset >= edit.getOffset()) {
                holder[0] -= edit.getLength();
            }
            return super.visit(edit);
        }

        @Override
        public boolean visit(InsertEdit edit) {
            if (offset >= edit.getOffset()) {
                holder[0] += edit.getText().length();
            }
            return super.visit(edit);
        }

        @Override
        public boolean visit(ReplaceEdit edit) {
            if (offset >= edit.getOffset()) {
                holder[0] += edit.getText().length() - edit.getLength();
            }
            return super.visit(edit);
        }
    });
    return holder[0];
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) TextEditVisitor(org.eclipse.text.edits.TextEditVisitor) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit)

Aggregations

InsertEdit (org.eclipse.text.edits.InsertEdit)65 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)33 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)24 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