Search in sources :

Example 41 with MalformedTreeException

use of org.eclipse.text.edits.MalformedTreeException in project eclipse.platform.text by eclipse.

the class TextEditTests method testOverlap1.

@Test
public void testOverlap1() throws Exception {
    // [ [ ] ]
    fRoot.addChild(new ReplaceEdit(0, 2, "01"));
    boolean exception = false;
    try {
        fRoot.addChild(new ReplaceEdit(1, 2, "12"));
    } catch (MalformedTreeException e) {
        exception = true;
    }
    assertTrue(exception);
}
Also used : ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Test(org.junit.Test)

Example 42 with MalformedTreeException

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

the class SurroundWithNewElementQuickAssistProposal 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) {
    try {
        int startTagOffset = offset;
        int endTagOffset = offset + viewer.getSelectedRange().y;
        // surround the node if no selection
        if (startTagOffset == endTagOffset) {
            IDOMNode cursorNode = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, offset);
            // use parent node if text node is empty
            if ((cursorNode.getNodeType() == Node.TEXT_NODE) && (cursorNode.getNodeValue().trim().length() == 0)) {
                cursorNode = (IDOMNode) cursorNode.getParentNode();
            }
            startTagOffset = cursorNode.getStartOffset();
            endTagOffset = cursorNode.getEndOffset();
        }
        // insert new element
        MultiTextEdit multiTextEdit = new MultiTextEdit();
        // element tag name cannot be DBCS, do not translate "<element>"
        // and "</element>"
        // $NON-NLS-1$ //$NON-NLS-2$
        final String startElement = "<" + ELEMENT_NAME + ">";
        multiTextEdit.addChild(new InsertEdit(startTagOffset, startElement));
        // $NON-NLS-1$ //$NON-NLS-2$
        multiTextEdit.addChild(new InsertEdit(endTagOffset, "</" + ELEMENT_NAME + ">"));
        multiTextEdit.apply(viewer.getDocument());
        Position start = new Position(startTagOffset);
        Position end = new Position(endTagOffset + startElement.length());
        try {
            viewer.getDocument().addPosition(start);
            viewer.getDocument().addPosition(end);
            // get new element node
            IDOMNode newElementNode = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, startTagOffset);
            IStructuredFormatProcessor formatProcessor = new FormatProcessorXML();
            formatProcessor.formatNode(newElementNode);
            // rename new element
            apply(viewer, trigger, stateMask, start, end, ELEMENT_NAME.length());
        } finally {
            viewer.getDocument().removePosition(start);
            viewer.getDocument().removePosition(end);
        }
    } catch (MalformedTreeException e) {
        // log for now, unless find reason not to
        Logger.log(Logger.INFO, e.getMessage());
    } catch (BadLocationException e) {
        // log for now, unless find reason not to
        Logger.log(Logger.INFO, e.getMessage());
    }
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) InsertEdit(org.eclipse.text.edits.InsertEdit) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) Position(org.eclipse.jface.text.Position) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) FormatProcessorXML(org.eclipse.wst.xml.core.internal.provisional.format.FormatProcessorXML) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 43 with MalformedTreeException

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

the class AddJavaDocStubOperation method applyChanges.

protected void applyChanges() {
    IModelManager modelManager = StructuredModelManager.getModelManager();
    IStructuredModel model = null;
    IStructuredDocument doc = node.getStructuredDocument();
    try {
        MultiTextEdit edits = textEditListener.getTextEdits();
        model = modelManager.getExistingModelForEdit(doc);
        model.aboutToChangeModel();
        // $NON-NLS-1$ //$NON-NLS-2$
        model.beginRecording(this, Messages.getString("AddJavaDocStubOperation.0"), Messages.getString("AddJavaDocStubOperation.1"));
        edits.apply(doc);
    } catch (MalformedTreeException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    } catch (BadLocationException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    } finally {
        if (model != null) {
            model.endRecording(this);
            model.changedModel();
            model.releaseFromEdit();
        }
    }
}
Also used : IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 44 with MalformedTreeException

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

the class PageImport method insertImportDeclaration.

private int insertImportDeclaration(IDocument document, int position, boolean isXML) {
    String delim = (document instanceof IStructuredDocument) ? ((IStructuredDocument) document).getLineDelimiter() : TextUtilities.getDefaultLineDelimiter(document);
    boolean isCustomTag = isCustomTagDocument(document);
    final String opening;
    final String closing;
    if (isCustomTag) {
        if (isXML) {
            // $NON-NLS-1$
            opening = "<jsp:directive.tag import=\"";
            // $NON-NLS-1$
            closing = "\"/>";
        } else {
            // $NON-NLS-1$
            opening = "<%@tag import=\"";
            // $NON-NLS-1$
            closing = "\"%>";
        }
    } else {
        if (isXML) {
            // $NON-NLS-1$
            opening = "<jsp:directive.page import=\"";
            // $NON-NLS-1$
            closing = "\"/>";
        } else {
            // $NON-NLS-1$
            opening = "<%@page import=\"";
            // $NON-NLS-1$
            closing = "\"%>";
        }
    }
    final String declaration = opening + name + closing + delim;
    final InsertEdit edit = new InsertEdit(position, declaration);
    try {
        edit.apply(document);
        return declaration.length();
    } catch (BadLocationException e) {
    } catch (MalformedTreeException e) {
    }
    return -1;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 45 with MalformedTreeException

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

the class JSPTranslationExtension method getJspEdit.

/**
 * Returns a corresponding TextEdit for the JSP file given a TextEdit for
 * a Java file.
 *
 * @param javaEdit
 * @return the corresponding JSP edits (not applied to the document yet)
 */
public TextEdit getJspEdit(TextEdit javaEdit) {
    if (javaEdit == null)
        return null;
    List jspEdits = new ArrayList();
    int offset = javaEdit.getOffset();
    int length = javaEdit.getLength();
    if (javaEdit instanceof MultiTextEdit && javaEdit.getChildren().length > 0) {
        IRegion r = TextEdit.getCoverage(getAllEdits(javaEdit));
        offset = r.getOffset();
        length = r.getLength();
    }
    // get java ranges that will be affected by the edit
    Position[] javaPositions = getJavaRanges(offset, length);
    // record position data before the change
    Position[] jspPositions = new Position[javaPositions.length];
    PositionDelta[] deltas = new PositionDelta[javaPositions.length];
    for (int i = 0; i < javaPositions.length; i++) {
        deltas[i] = new PositionDelta(javaPositions[i].offset, javaPositions[i].length);
        // mapping from java <-> jsp (eg. an import statement)
        if (!isIndirect(javaPositions[i].offset))
            jspPositions[i] = (Position) getJava2JspMap().get(javaPositions[i]);
    }
    if (DEBUG) {
        // $NON-NLS-1$
        System.out.println("================================================");
        // $NON-NLS-1$
        System.out.println("deltas:");
        String javaText = getJavaText();
        for (int i = 0; i < deltas.length; i++) // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        System.out.println("pos[" + deltas[i].preOffset + ":" + deltas[i].preLength + "]" + javaText.substring(deltas[i].preOffset, deltas[i].preOffset + deltas[i].preLength));
        // $NON-NLS-1$
        System.out.println("===============================================");
    }
    UndoEdit undo = null;
    // apply the edit to the java document
    try {
        undo = javaEdit.apply(getJavaDocument());
    } catch (MalformedTreeException e) {
        Logger.logException(e);
    } catch (BadLocationException e) {
        Logger.logException(e);
    }
    // now at this point Java positions are unreliable since they were
    // updated after applying java edit.
    String newJavaText = getJavaDocument().get();
    if (DEBUG)
        // $NON-NLS-1$
        System.out.println("java post format text:\n" + newJavaText);
    // record post edit data
    for (int i = 0; i < javaPositions.length; i++) deltas[i].setPostEditData(javaPositions[i].offset, javaPositions[i].length, javaPositions[i].isDeleted);
    // create appropriate text edits for deltas
    Position jspPos = null;
    // $NON-NLS-1$
    String replaceText = "";
    for (int i = 0; i < deltas.length; i++) {
        jspPos = jspPositions[i];
        if (jspPos != null) {
            if (deltas[i].isDeleted) {
                jspEdits.add(new DeleteEdit(jspPos.offset, jspPos.length));
            } else {
                replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
                // get rid of pre and post white space or fine tuned
                // adjustment later.
                // fix text here...
                replaceText = fixJspReplaceText(replaceText, jspPos);
                if (// Unwanted TextEdit can lead to MalformedTreeException.See: Bug 321977
                !(replaceText.length() == 0 && jspPos.length == 0))
                    jspEdits.add(new ReplaceEdit(jspPos.offset, jspPos.length, replaceText));
            }
            if (DEBUG)
                debugReplace(deltas, jspPos, replaceText, i);
        } else {
            // possible new import?
            if (isImport(javaPositions[i].getOffset()) && replaceText.lastIndexOf("import ") != -1) {
                // $NON-NLS-1$
                replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
                // $NON-NLS-1$ //$NON-NLS-2$
                String importText = replaceText.substring(replaceText.lastIndexOf("import "), replaceText.indexOf(";"));
                // evenutally need to check if it's XML-JSP
                // $NON-NLS-1$ //$NON-NLS-2$
                importText = "<%@page import=\"" + importText + "\" %>\n";
                jspEdits.add(new InsertEdit(0, importText));
            }
        }
    }
    TextEdit allJspEdits = createMultiTextEdit((TextEdit[]) jspEdits.toArray(new TextEdit[jspEdits.size()]));
    // editor)
    if (undo != null) {
        try {
            undo.apply(getJavaDocument());
        } catch (MalformedTreeException e) {
            Logger.logException(e);
        } catch (BadLocationException e) {
            Logger.logException(e);
        }
    }
    return allJspEdits;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IRegion(org.eclipse.jface.text.IRegion) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) ArrayList(java.util.ArrayList) List(java.util.List) UndoEdit(org.eclipse.text.edits.UndoEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)52 BadLocationException (org.eclipse.jface.text.BadLocationException)38 IDocument (org.eclipse.jface.text.IDocument)25 TextEdit (org.eclipse.text.edits.TextEdit)19 CoreException (org.eclipse.core.runtime.CoreException)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)12 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)11 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)11 Document (org.eclipse.jface.text.Document)11 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)11 Test (org.junit.Test)11 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)10 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)10 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)9 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)9 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)8 InsertEdit (org.eclipse.text.edits.InsertEdit)8 IPath (org.eclipse.core.runtime.IPath)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)7 ArrayList (java.util.ArrayList)6