Search in sources :

Example 1 with CommentingStrategy

use of org.eclipse.wst.sse.ui.internal.comment.CommentingStrategy in project webtools.sourceediting by eclipse.

the class AddBlockCommentHandler method processAction.

/**
 * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractCommentHandler#processAction(
 * 	org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection)
 */
protected void processAction(ITextEditor textEditor, IStructuredDocument document, ITextSelection textSelection) {
    IStructuredModel model = null;
    boolean changed = false;
    DocumentRewriteSession session = null;
    try {
        model = StructuredModelManager.getModelManager().getModelForEdit(document);
        if (model != null) {
            // makes it so one undo will undo all the edits to the document
            model.beginRecording(this, SSEUIMessages.AddBlockComment_label, SSEUIMessages.AddBlockComment_description);
            // keeps listeners from doing anything until updates are all done
            model.aboutToChangeModel();
            if (document instanceof IDocumentExtension4) {
                session = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
            }
            changed = true;
            ITypedRegion[] typedRegions = document.computePartitioning(textSelection.getOffset(), textSelection.getLength());
            CommentingStrategy commentType = CommentingStrategyRegistry.getDefault().getBlockCommentingStrategy(model.getContentTypeIdentifier(), typedRegions);
            if (commentType != null) {
                commentType.apply(document, textSelection.getOffset(), textSelection.getLength());
            }
        }
    } catch (BadLocationException e) {
        // $NON-NLS-1$ //$NON-NLS-2$
        Logger.logException("The given selection " + textSelection + " must be invalid", e);
    } finally {
        // clean everything up
        if (session != null && document instanceof IDocumentExtension4) {
            ((IDocumentExtension4) document).stopRewriteSession(session);
        }
        if (model != null) {
            model.endRecording(this);
            if (changed) {
                model.changedModel();
            }
            model.releaseFromEdit();
        }
    }
}
Also used : DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) CommentingStrategy(org.eclipse.wst.sse.ui.internal.comment.CommentingStrategy) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with CommentingStrategy

use of org.eclipse.wst.sse.ui.internal.comment.CommentingStrategy in project webtools.sourceediting by eclipse.

the class RemoveBlockCommentHandler method processAction.

/**
 * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractCommentHandler#processAction(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection)
 */
protected void processAction(ITextEditor textEditor, IStructuredDocument document, ITextSelection textSelection) {
    IStructuredModel model = null;
    boolean changed = false;
    DocumentRewriteSession session = null;
    try {
        model = StructuredModelManager.getModelManager().getModelForEdit(document);
        if (model != null) {
            // makes it so one undo will undo all the edits to the document
            model.beginRecording(this, SSEUIMessages.RemoveBlockComment_label, SSEUIMessages.RemoveBlockComment_label);
            // keeps listeners from doing anything until updates are all done
            model.aboutToChangeModel();
            if (document instanceof IDocumentExtension4) {
                session = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
            }
            changed = true;
            ITypedRegion[] typedRegions = document.computePartitioning(textSelection.getOffset(), textSelection.getLength());
            CommentingStrategy commentType = CommentingStrategyRegistry.getDefault().getBlockCommentingStrategy(model.getContentTypeIdentifier(), typedRegions);
            if (commentType != null) {
                commentType.remove(document, textSelection.getOffset(), textSelection.getLength(), true);
            }
        }
    } catch (BadLocationException e) {
        // $NON-NLS-1$ //$NON-NLS-2$
        Logger.logException("The given selection " + textSelection + " must be invalid", e);
    } finally {
        // clean everything up
        if (session != null && document instanceof IDocumentExtension4) {
            ((IDocumentExtension4) document).stopRewriteSession(session);
        }
        if (model != null) {
            model.endRecording(this);
            if (changed) {
                model.changedModel();
            }
            model.releaseFromEdit();
        }
    }
}
Also used : DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) CommentingStrategy(org.eclipse.wst.sse.ui.internal.comment.CommentingStrategy) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)2 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)2 IDocumentExtension4 (org.eclipse.jface.text.IDocumentExtension4)2 ITypedRegion (org.eclipse.jface.text.ITypedRegion)2 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)2 CommentingStrategy (org.eclipse.wst.sse.ui.internal.comment.CommentingStrategy)2