Search in sources :

Example 1 with IContentFormatterExtension

use of org.eclipse.jface.text.formatter.IContentFormatterExtension in project webtools.sourceediting by eclipse.

the class TestJSPContentFormatter method formatAndAssertEquals.

private void formatAndAssertEquals(String beforePath, String afterPath, boolean resetPreferences) 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);
        if (resetPreferences) {
            resetPreferencesToDefault();
        }
        SourceViewerConfiguration configuration = (SourceViewerConfiguration) ExtendedConfigurationBuilder.getInstance().getConfiguration(ExtendedConfigurationBuilder.SOURCEVIEWERCONFIGURATION, "org.eclipse.jst.jsp.core.jspsource");
        IContentFormatterExtension formatter = (IContentFormatterExtension) configuration.getContentFormatter(null);
        IDocument document = beforeModel.getStructuredDocument();
        Region region = new Region(0, document.getLength());
        fContext.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
        formatter.format(document, fContext);
        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);
        expectedContents = StringUtils.replace(expectedContents, "\r\n", "\r");
        expectedContents = StringUtils.replace(expectedContents, "\r", "\n");
        String actualContents = new String(formattedBytes.toByteArray(), UTF_8);
        actualContents = StringUtils.replace(actualContents, "\r\n", "\r");
        actualContents = StringUtils.replace(actualContents, "\r", "\n");
        assertTrue(onlyWhiteSpaceDiffers(expectedContents, actualContents));
        assertEquals("Formatted document differs from the expected.", expectedContents, actualContents);
    } finally {
        if (beforeModel != null)
            beforeModel.releaseFromEdit();
        if (afterModel != null)
            afterModel.releaseFromEdit();
    }
}
Also used : SourceViewerConfiguration(org.eclipse.jface.text.source.SourceViewerConfiguration) Region(org.eclipse.jface.text.Region) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IContentFormatterExtension(org.eclipse.jface.text.formatter.IContentFormatterExtension) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with IContentFormatterExtension

use of org.eclipse.jface.text.formatter.IContentFormatterExtension in project webtools.sourceediting by eclipse.

the class StructuredTextViewer method doOperation.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.ITextOperationTarget#doOperation(int)
	 */
public void doOperation(int operation) {
    Point selection = getTextWidget().getSelection();
    int cursorPosition = selection.x;
    int selectionLength = selection.y - selection.x;
    switch(operation) {
        case CUT:
            beginRecording(TEXT_CUT, TEXT_CUT, cursorPosition, selectionLength);
            super.doOperation(operation);
            selection = getTextWidget().getSelection();
            cursorPosition = selection.x;
            selectionLength = selection.y - selection.x;
            endRecording(cursorPosition, selectionLength);
            break;
        case PASTE:
            beginRecording(TEXT_PASTE, TEXT_PASTE, cursorPosition, selectionLength);
            super.doOperation(operation);
            selection = getTextWidget().getSelection();
            cursorPosition = selection.x;
            selectionLength = selection.y - selection.x;
            endRecording(cursorPosition, selectionLength);
            break;
        case CONTENTASSIST_PROPOSALS:
            // maybe not configured?
            if (fContentAssistant != null && isEditable()) {
                // position
                if (canDoOperation(CONTENTASSIST_PROPOSALS)) {
                    String err = fContentAssistant.showPossibleCompletions();
                    if (err != null) {
                        // don't wanna beep if there is no error
                        PlatformStatusLineUtil.displayTemporaryErrorMessage(this, err);
                    }
                } else
                    beep();
            }
            break;
        case CONTENTASSIST_CONTEXT_INFORMATION:
            if (fContentAssistant != null) {
                String err = fContentAssistant.showContextInformation();
                if (err != null) {
                    // don't wanna beep if there is no error
                    PlatformStatusLineUtil.displayTemporaryErrorMessage(this, err);
                }
            }
            break;
        case SHIFT_RIGHT:
            beginRecording(TEXT_SHIFT_RIGHT, TEXT_SHIFT_RIGHT, cursorPosition, selectionLength);
            updateIndentationPrefixes();
            doModelOperation(SHIFT_RIGHT);
            selection = getTextWidget().getSelection();
            cursorPosition = selection.x;
            selectionLength = selection.y - selection.x;
            endRecording(cursorPosition, selectionLength);
            break;
        case SHIFT_LEFT:
            beginRecording(TEXT_SHIFT_LEFT, TEXT_SHIFT_LEFT, cursorPosition, selectionLength);
            updateIndentationPrefixes();
            doModelOperation(SHIFT_LEFT);
            selection = getTextWidget().getSelection();
            cursorPosition = selection.x;
            selectionLength = selection.y - selection.x;
            endRecording(cursorPosition, selectionLength);
            break;
        case FORMAT_DOCUMENT:
            DocumentRewriteSession rewriteSession = null;
            IDocument document = getDocument();
            try {
                /*
					 * This command will actually format selection if text is
					 * selected, otherwise format entire document
					 */
                // begin recording
                beginRecording(FORMAT_DOCUMENT_TEXT, FORMAT_DOCUMENT_TEXT, cursorPosition, selectionLength);
                boolean formatDocument = false;
                IRegion region = null;
                Point s = getSelectedRange();
                if (s.y > 0) {
                    // only format currently selected text
                    region = new Region(s.x, s.y);
                } else {
                    // no selection, so format entire document
                    region = getModelCoverage();
                    formatDocument = true;
                }
                if (document instanceof IDocumentExtension4) {
                    IDocumentExtension4 extension = (IDocumentExtension4) document;
                    DocumentRewriteSessionType type = (selection.y == 0 || selection.y > MAX_SMALL_FORMAT_LENGTH) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
                    rewriteSession = (extension.getActiveRewriteSession() != null) ? null : extension.startRewriteSession(type);
                } else {
                    setRedraw(false);
                }
                if (fContentFormatter instanceof IContentFormatterExtension) {
                    IContentFormatterExtension extension = (IContentFormatterExtension) fContentFormatter;
                    IFormattingContext context = new FormattingContext();
                    context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(formatDocument));
                    context.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
                    extension.format(document, context);
                } else {
                    fContentFormatter.format(document, region);
                }
            } finally {
                try {
                    if (rewriteSession != null) {
                        IDocumentExtension4 extension = (IDocumentExtension4) document;
                        extension.stopRewriteSession(rewriteSession);
                    } else {
                        setRedraw(true);
                    }
                } finally {
                    // end recording
                    selection = getTextWidget().getSelection();
                    cursorPosition = selection.x;
                    selectionLength = selection.y - selection.x;
                    endRecording(cursorPosition, selectionLength);
                }
            }
            break;
        case FORMAT_ACTIVE_ELEMENTS:
            rewriteSession = null;
            document = getDocument();
            try {
                /*
					 * This command will format the node at cursor position
					 * (and all its children)
					 */
                // begin recording
                beginRecording(FORMAT_ACTIVE_ELEMENTS_TEXT, FORMAT_ACTIVE_ELEMENTS_TEXT, cursorPosition, selectionLength);
                IRegion region = null;
                Point s = getSelectedRange();
                if (s.y > -1) {
                    // only format node at cursor position
                    region = new Region(s.x, s.y);
                }
                if (document instanceof IDocumentExtension4) {
                    IDocumentExtension4 extension = (IDocumentExtension4) document;
                    DocumentRewriteSessionType type = (selection.y == 0 || selection.y > MAX_SMALL_FORMAT_LENGTH) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
                    rewriteSession = (extension.getActiveRewriteSession() != null) ? null : extension.startRewriteSession(type);
                } else {
                    setRedraw(false);
                }
                if (fContentFormatter instanceof IContentFormatterExtension) {
                    IContentFormatterExtension extension = (IContentFormatterExtension) fContentFormatter;
                    IFormattingContext context = new FormattingContext();
                    context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE);
                    context.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
                    extension.format(getDocument(), context);
                } else {
                    fContentFormatter.format(getDocument(), region);
                }
            } finally {
                try {
                    if (rewriteSession != null) {
                        IDocumentExtension4 extension = (IDocumentExtension4) document;
                        extension.stopRewriteSession(rewriteSession);
                    } else {
                        setRedraw(true);
                    }
                } finally {
                    // end recording
                    selection = getTextWidget().getSelection();
                    cursorPosition = selection.x;
                    selectionLength = selection.y - selection.x;
                    endRecording(cursorPosition, selectionLength);
                }
            }
            break;
        default:
            super.doOperation(operation);
    }
}
Also used : IFormattingContext(org.eclipse.jface.text.formatter.IFormattingContext) FormattingContext(org.eclipse.jface.text.formatter.FormattingContext) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) DocumentRewriteSessionType(org.eclipse.jface.text.DocumentRewriteSessionType) IFormattingContext(org.eclipse.jface.text.formatter.IFormattingContext) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) IRegion(org.eclipse.jface.text.IRegion) Region(org.eclipse.jface.text.Region) IDocument(org.eclipse.jface.text.IDocument) IContentFormatterExtension(org.eclipse.jface.text.formatter.IContentFormatterExtension)

Example 3 with IContentFormatterExtension

use of org.eclipse.jface.text.formatter.IContentFormatterExtension in project webtools.sourceediting by eclipse.

the class TestContentFormatter method formatAndAssertEquals.

private void formatAndAssertEquals(String beforePath, String afterPath, boolean resetPreferences) 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);
        if (resetPreferences) {
            resetPreferencesToDefault();
        }
        SourceViewerConfiguration configuration = new StructuredTextViewerConfigurationJSP();
        IContentFormatterExtension formatter = (IContentFormatterExtension) configuration.getContentFormatter(null);
        IDocument document = beforeModel.getStructuredDocument();
        Region region = new Region(0, document.getLength());
        fContext.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
        formatter.format(document, fContext);
        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);
        expectedContents = StringUtils.replace(expectedContents, "\r\n", "\r");
        expectedContents = StringUtils.replace(expectedContents, "\r", "\n");
        String actualContents = new String(formattedBytes.toByteArray(), UTF_8);
        actualContents = StringUtils.replace(actualContents, "\r\n", "\r");
        actualContents = StringUtils.replace(actualContents, "\r", "\n");
        assertTrue(onlyWhiteSpaceDiffers(expectedContents, actualContents));
        assertEquals("Formatted document differs from the expected.", expectedContents, actualContents);
    } finally {
        if (beforeModel != null)
            beforeModel.releaseFromEdit();
        if (afterModel != null)
            afterModel.releaseFromEdit();
    }
}
Also used : StructuredTextViewerConfigurationJSP(org.eclipse.jst.jsp.ui.StructuredTextViewerConfigurationJSP) SourceViewerConfiguration(org.eclipse.jface.text.source.SourceViewerConfiguration) Region(org.eclipse.jface.text.Region) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IContentFormatterExtension(org.eclipse.jface.text.formatter.IContentFormatterExtension) IDocument(org.eclipse.jface.text.IDocument)

Example 4 with IContentFormatterExtension

use of org.eclipse.jface.text.formatter.IContentFormatterExtension in project webtools.sourceediting by eclipse.

the class FormattingTests method formatAndAssertEquals.

/**
 * @param beforePath - the path of the before file
 * @param afterPath - the path of the after file, <b>must not be the same as the before file</b>
 * @param configuration
 * @throws UnsupportedEncodingException
 * @throws IOException
 * @throws CoreException
 *
 * @see org.eclipse.wst.xml.core.tests.format.TestPartitionFormatterXML#formatAndAssertEquals
 */
private void formatAndAssertEquals(String beforePath, String afterPath, SourceViewerConfiguration configuration) throws UnsupportedEncodingException, IOException, CoreException {
    IStructuredModel beforeModel = null, afterModel = null;
    ISourceViewer viewer = null;
    try {
        beforeModel = getModelForEdit(beforePath);
        assertNotNull("could not retrieve structured model for : " + beforePath, beforeModel);
        JsTranslationAdapterFactory.setupAdapterFactory(beforeModel);
        afterModel = getModelForEdit(afterPath);
        assertNotNull("could not retrieve structured model for : " + afterPath, afterModel);
        // normalize contents
        IStructuredDocument document = beforeModel.getStructuredDocument();
        String normalizedContents = document.get();
        normalizedContents = StringUtils.replace(normalizedContents, "\r\n", "\n");
        normalizedContents = StringUtils.replace(normalizedContents, "\r", "\n");
        document.set(normalizedContents);
        viewer = getConfiguredViewer(document, configuration);
        assertNotNull("Could not get viewer to run test", viewer);
        // do the format
        IContentFormatterExtension formatter = (IContentFormatterExtension) configuration.getContentFormatter(viewer);
        IFormattingContext fContext = new FormattingContext();
        Region region = new Region(0, document.getLength());
        fContext.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
        fContext.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
        formatter.format(document, fContext);
        // get the contents
        String actualContents = beforeModel.getStructuredDocument().get();
        String expectedContents = afterModel.getStructuredDocument().get();
        /* 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");
        onlyWhiteSpaceDiffers(expectedContents, actualContents);
        assertEquals("Formatted document differs from the expected.", expectedContents, actualContents);
    } finally {
        if (beforeModel != null) {
            try {
                beforeModel.releaseFromEdit();
            } catch (Exception e) {
            // ignore
            }
        }
        if (afterModel != null) {
            try {
                afterModel.releaseFromEdit();
            } catch (Exception e) {
            // ignore
            }
        }
        if (viewer != null) {
            StyledText text = viewer.getTextWidget();
            if (text != null && !text.isDisposed()) {
                text.dispose();
            }
        }
    }
}
Also used : FormattingContext(org.eclipse.jface.text.formatter.FormattingContext) IFormattingContext(org.eclipse.jface.text.formatter.IFormattingContext) StyledText(org.eclipse.swt.custom.StyledText) IFormattingContext(org.eclipse.jface.text.formatter.IFormattingContext) Region(org.eclipse.jface.text.Region) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) IContentFormatterExtension(org.eclipse.jface.text.formatter.IContentFormatterExtension) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

Region (org.eclipse.jface.text.Region)4 IContentFormatterExtension (org.eclipse.jface.text.formatter.IContentFormatterExtension)4 IDocument (org.eclipse.jface.text.IDocument)3 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FormattingContext (org.eclipse.jface.text.formatter.FormattingContext)2 IFormattingContext (org.eclipse.jface.text.formatter.IFormattingContext)2 SourceViewerConfiguration (org.eclipse.jface.text.source.SourceViewerConfiguration)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CoreException (org.eclipse.core.runtime.CoreException)1 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)1 DocumentRewriteSessionType (org.eclipse.jface.text.DocumentRewriteSessionType)1 IDocumentExtension4 (org.eclipse.jface.text.IDocumentExtension4)1 IRegion (org.eclipse.jface.text.IRegion)1 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)1 StructuredTextViewerConfigurationJSP (org.eclipse.jst.jsp.ui.StructuredTextViewerConfigurationJSP)1 StyledText (org.eclipse.swt.custom.StyledText)1 Point (org.eclipse.swt.graphics.Point)1 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)1