Search in sources :

Example 21 with MalformedTreeException

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

the class FormattingStrategyJSPJava method format.

/*
	 * @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#format()
	 */
public void format() {
    super.format();
    final IDocument document = (IDocument) fDocuments.removeFirst();
    final TypedPosition partition = (TypedPosition) fPartitions.removeFirst();
    if (document != null && partition != null) {
        try {
            JSPTranslationUtil translationUtil = new JSPTranslationUtil(document);
            ICompilationUnit cu = translationUtil.getCompilationUnit();
            if (cu != null) {
                String cuSource = cu.getSource();
                /*
					 * Format the entire compilation unit, but only create
					 * edits for the requested JSP partition's range in the
					 * Java source
					 */
                TextEdit textEdit = formatString(CodeFormatter.K_COMPILATION_UNIT, cuSource, translationUtil.getTranslation().getJavaOffset(partition.getOffset()), partition.getLength(), TextUtilities.getDefaultLineDelimiter(document), getPreferences());
                TextEdit jspEdit = translationUtil.getTranslation().getJspEdit(textEdit);
                if (jspEdit != null && jspEdit.hasChildren())
                    jspEdit.apply(document);
            }
        } catch (MalformedTreeException exception) {
            Logger.logException(exception);
        } catch (BadLocationException exception) {
            // Can only happen on concurrent document modification - log
            // and bail out
            Logger.logException(exception);
        } catch (JavaModelException exception) {
            Logger.logException(exception);
        }
    }
}
Also used : JSPTranslationUtil(org.eclipse.jst.jsp.core.internal.java.JSPTranslationUtil) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) TypedPosition(org.eclipse.jface.text.TypedPosition) TextEdit(org.eclipse.text.edits.TextEdit) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 22 with MalformedTreeException

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

the class JSPRenameChange method perform.

/**
 * Performs this change and returns a {@link JSPRenameUndoChange} to undo the change.
 *
 * @return a {@link JSPRenameUndoChange} to undo this performed {@link Change}
 * @see org.eclipse.ltk.core.refactoring.TextChange#perform(org.eclipse.core.runtime.IProgressMonitor)
 */
public Change perform(IProgressMonitor pm) throws CoreException {
    Change undoChange = null;
    try {
        // apply edit
        undoChange = super.perform(pm);
        undoChange = new JSPRenameUndoChange(this, undoChange);
        // save the model
        saveJSPFile(this.fJSPFile, this.getJSPDoc());
    } catch (MalformedTreeException e) {
        Logger.logException(e);
    }
    return undoChange;
}
Also used : MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Change(org.eclipse.ltk.core.refactoring.Change) DocumentChange(org.eclipse.ltk.core.refactoring.DocumentChange)

Example 23 with MalformedTreeException

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

the class BasicRefactorSearchRequestor method createChange.

/**
 * </p>If a {@link TextChange} does not already exist for the given {@link JavaSearchDocumentDelegate}
 * then a new one will be created with the given {@link TextEdit}.  Otherwise the given {@link TextEdit}
 * will be added to a new group and added to the existing change and <code>null</code> will be returned.</p>
 *
 * @param searchDoc the {@link JavaSearchDocumentDelegate} that the <code>edit</code> will be applied to
 * @param edit the {@link TextEdit} that needs to be added to a new {@link TextChange} or appended to an
 * existing one
 * @param participant the {@link RefactoringParticipant} that knows about the existing {@link TextChange}s
 * @return a new {@link Change} if there was not one already existing for the document in question,
 * else <code>null</code>
 */
private Change createChange(JavaSearchDocumentDelegate searchDoc, TextEdit edit, RefactoringParticipant participant) {
    IDocument doc = searchDoc.getJspTranslation().getJspDocument();
    String description = getDescription();
    TextChange existingChange = participant.getTextChange(searchDoc.getFile());
    TextChange change = null;
    if (existingChange != null) {
        try {
            existingChange.addEdit(edit);
        } catch (MalformedTreeException e) {
            Logger.logException(// $NON-NLS-1$
            "MalformedTreeException while adding edit " + edit + " to existing change " + change, // $NON-NLS-1$
            e);
        }
        TextEditGroup group = new TextEditGroup(description, edit);
        existingChange.addTextEditGroup(group);
    } else {
        change = new JSPRenameChange(searchDoc.getFile(), doc, edit, searchDoc.getFile().getName());
        TextEditGroup group = new TextEditGroup(description, edit);
        change.addTextEditGroup(group);
    }
    return change;
}
Also used : TextChange(org.eclipse.ltk.core.refactoring.TextChange) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) TextEditGroup(org.eclipse.text.edits.TextEditGroup) IDocument(org.eclipse.jface.text.IDocument)

Example 24 with MalformedTreeException

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

the class TestPartitionFormatterXML method formatAndAssertEquals.

private void formatAndAssertEquals(String beforePath, String afterPath, XMLFormattingPreferences prefs) throws UnsupportedEncodingException, IOException, CoreException {
    IStructuredModel beforeModel = null, afterModel = null;
    try {
        beforeModel = getModelForEdit(beforePath);
        assertNotNull("could not retrieve structured model for : " + beforePath, beforeModel);
        afterModel = getModelForEdit(afterPath);
        assertNotNull("could not retrieve structured model for : " + afterPath, afterModel);
        IStructuredDocument document = beforeModel.getStructuredDocument();
        String normalizedContents = document.get();
        normalizedContents = StringUtils.replace(normalizedContents, "\r\n", "\n");
        normalizedContents = StringUtils.replace(normalizedContents, "\r", "\n");
        document.set(normalizedContents);
        if (prefs == null)
            prefs = new XMLFormattingPreferences();
        TextEdit edit = partitionFormatter.format(beforeModel, 0, document.getLength(), prefs);
        try {
            edit.apply(document);
        } catch (MalformedTreeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ByteArrayOutputStream formattedBytes = new ByteArrayOutputStream();
        // "beforeModel" should now be
        beforeModel.save(formattedBytes);
        // after the formatter
        ByteArrayOutputStream afterBytes = new ByteArrayOutputStream();
        afterModel.save(afterBytes);
        String expectedContents = new String(afterBytes.toByteArray(), UTF_8);
        String actualContents = new String(formattedBytes.toByteArray(), UTF_8);
        /* Make some adjustments to ignore cross platform line delimiter issues */
        expectedContents = StringUtils.replace(expectedContents, "\r\n", "\n");
        expectedContents = StringUtils.replace(expectedContents, "\r", "\n");
        actualContents = StringUtils.replace(actualContents, "\r\n", "\n");
        actualContents = StringUtils.replace(actualContents, "\r", "\n");
        if (!fStringCompareUtil.equalsIgnoreLineSeperator(expectedContents, actualContents)) {
            assertEquals("Formatted document differs from the expected.", expectedContents, actualContents);
        }
    } finally {
        if (beforeModel != null)
            beforeModel.releaseFromEdit();
        if (afterModel != null)
            afterModel.releaseFromEdit();
    }
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) 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) XMLFormattingPreferences(org.eclipse.wst.xml.core.internal.formatter.XMLFormattingPreferences) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 25 with MalformedTreeException

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

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