Java Examples for org.eclipse.jface.text.formatter.IContentFormatterExtension

The following java examples will help you to understand the usage of org.eclipse.jface.text.formatter.IContentFormatterExtension. These source code samples are taken from different open source projects.

Example 1
Project: cdt-tests-runner-master  File: TranslationUnitPreview.java View source code
@Override
protected void doFormatPreview() {
    if (fPreviewText == null) {
        //$NON-NLS-1$
        fPreviewDocument.set("");
        return;
    }
    fPreviewDocument.set(fPreviewText);
    fSourceViewer.setVisibleRegion(fPreviewTextOffset, fPreviewText.length() - fPreviewTextOffset);
    fSourceViewer.setRedraw(false);
    final IFormattingContext context = new FormattingContext();
    try {
        final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer);
        if (formatter instanceof IContentFormatterExtension) {
            final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
            Map<String, String> prefs = fWorkingValues;
            if (fFormatterId != null) {
                prefs = new HashMap<String, String>(fWorkingValues);
                prefs.put(CCorePreferenceConstants.CODE_FORMATTER, fFormatterId);
            }
            context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, prefs);
            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
            extension.format(fPreviewDocument, context);
        } else {
            formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
        }
    } catch (Exception e) {
        final IStatus status = new Status(IStatus.ERROR, CUIPlugin.getPluginId(), ICStatusConstants.INTERNAL_ERROR, FormatterMessages.CPreview_formatter_exception, e);
        CUIPlugin.log(status);
    } finally {
        context.dispose();
        fSourceViewer.setRedraw(true);
    }
}
Example 2
Project: tern.java-master  File: CleanUpPreview.java View source code
private void format(String text) {
    if (text == null) {
        //$NON-NLS-1$
        fPreviewDocument.set("");
        return;
    }
    fPreviewDocument.set(text);
    if (!fFormat)
        return;
    fSourceViewer.setRedraw(false);
    final IFormattingContext context = new CommentFormattingContext();
    try {
        final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer);
        if (formatter instanceof IContentFormatterExtension) {
            final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
            context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, JavaScriptCore.getOptions());
            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
            extension.format(fPreviewDocument, context);
        } else
            formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
    } catch (Exception e) {
        final IStatus status = new Status(IStatus.ERROR, JavaScriptPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, MultiFixMessages.CleanUpRefactoringWizard_formatterException_errorMessage, e);
        JavaScriptPlugin.log(status);
    } finally {
        context.dispose();
        fSourceViewer.setRedraw(true);
    }
}
Example 3
Project: rdt-master  File: RubyScriptPreview.java View source code
protected void doFormatPreview() {
    if (fPreviewText == null) {
        //$NON-NLS-1$
        fPreviewDocument.set("");
        return;
    }
    fPreviewDocument.set(fPreviewText);
    fSourceViewer.setRedraw(false);
    final IFormattingContext context = new CommentFormattingContext();
    try {
        final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer);
        if (formatter instanceof IContentFormatterExtension) {
            final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
            context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, fWorkingValues);
            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
            extension.format(fPreviewDocument, context);
        } else
            formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
    } catch (Exception e) {
        final IStatus status = new Status(IStatus.ERROR, RubyPlugin.getPluginId(), IRubyStatusConstants.INTERNAL_ERROR, FormatterMessages.RubyPreview_formatter_exception, e);
        RubyPlugin.log(status);
    } finally {
        context.dispose();
        fSourceViewer.setRedraw(true);
    }
}
Example 4
Project: platform_sdk-master  File: AndroidXmlFormatter.java View source code
@Override
public final void format(IDocument document, IRegion region) {
    /**
         * This method is probably not going to be called. It is part of the
         * {@link IContentFormatter} but since we also implement
         * {@link IContentFormatterExtension} Eclipse should /* be calling
         * {@link #format(IDocument,IFormattingContext)} instead. However, for
         * completeness (and because other implementations of {@link IContentFormatter}
         * also do this we might as well make the method behave correctly
         */
    FormattingContext context = new FormattingContext();
    context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE);
    context.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
    format(document, context);
}
Example 5
Project: webtools.jsdt.tests-master  File: FormattingTests.java View source code
/**
		 * <p>
		 * This is run once before all of the tests
		 * </p>
		 * 
		 * @see junit.extensions.TestSetup#setUp()
		 */
public void setUp() throws Exception {
    // set up formatting tools
    fJavaScriptTextTools = JavaScriptPlugin.getDefault().getJavaTextTools();
    JavaScriptSourceViewerConfiguration config = new JavaScriptSourceViewerConfiguration(fJavaScriptTextTools.getColorManager(), JavaScriptPlugin.getDefault().getCombinedPreferenceStore(), null, IJavaScriptPartitions.JAVA_PARTITIONING);
    fFormatter = (IContentFormatterExtension) config.getContentFormatter(null);
    fFormattingContext = new FormattingContext();
    fFormattingContext.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, JavaScriptCore.getOptions());
    fFormattingContext.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
    // set non-interactive
    String noninteractive = System.getProperty(WTP_AUTOTEST_NONINTERACTIVE);
    if (noninteractive != null) {
        previousWTPAutoTestNonInteractivePropValue = noninteractive;
    } else {
        previousWTPAutoTestNonInteractivePropValue = "false";
    }
    System.setProperty(WTP_AUTOTEST_NONINTERACTIVE, "true");
}
Example 6
Project: webtools.sourceediting-master  File: StructuredTextViewer.java View source code
/*
	 * (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);
    }
}
Example 7
Project: edt-master  File: EGLPreview.java View source code
protected void doFormatPreview() {
    fPreviewDocument.set(fPreviewText);
    fSourceViewer.setRedraw(false);
    final IFormattingContext context = new FormattingContext();
    try {
        final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer);
        if (formatter instanceof IContentFormatterExtension) {
            final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
            context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, fPreferenceSetting);
            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
            extension.format(fPreviewDocument, context);
        }
    } finally {
        fSourceViewer.setRedraw(true);
    }
}
Example 8
Project: pdt-master  File: PHPStructuredTextViewer.java View source code
/**
	 * This method overrides WST since sometimes we get a subset of the document
	 * and NOT the whole document, although the case is FORMAT_DOCUMENT. In all
	 * other cases we call the parent method.
	 */
@Override
public void doOperation(int operation) {
    Point selection = getTextWidget().getSelection();
    int cursorPosition = selection.x;
    // save the last cursor position and the top visible line.
    int selectionLength = selection.y - selection.x;
    int topLine = getTextWidget().getTopIndex();
    switch(operation) {
        case FORMAT_DOCUMENT:
            try {
                setRedraw(false);
                // begin recording
                beginRecording(FORMAT_DOCUMENT_TEXT, FORMAT_DOCUMENT_TEXT, cursorPosition, selectionLength);
                IRegion region;
                // selection
                if (selectionLength != 0) {
                    region = new Region(cursorPosition, selectionLength);
                } else {
                    region = new Region(0, getDocument().getLength());
                }
                if (fContentFormatter instanceof IContentFormatterExtension) {
                    IContentFormatterExtension extension = (IContentFormatterExtension) fContentFormatter;
                    IFormattingContext context = new FormattingContext();
                    context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
                    context.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
                    extension.format(getDocument(), context);
                } else {
                    fContentFormatter.format(getDocument(), region);
                }
            } finally {
                // end recording
                selection = getTextWidget().getSelection();
                selectionLength = selection.y - selection.x;
                endRecording(cursorPosition, selectionLength);
                // return the cursor to its original position after the
                // formatter change its position.
                getTextWidget().setSelection(cursorPosition);
                getTextWidget().setTopIndex(topLine);
                setRedraw(true);
            }
            return;
        case CONTENTASSIST_PROPOSALS:
            // (instead of printing the stack trace)
            if (fViewerConfiguration != null) {
                IProject project = null;
                boolean isJavaScriptRegion = false;
                boolean hasJavaScriptNature = true;
                try {
                    // Resolve the partition type
                    IStructuredDocument sDoc = (IStructuredDocument) getDocument();
                    // get the "real" offset - adjusted according to the
                    // projection
                    int selectionOffset = getSelectedRange().x;
                    IStructuredDocumentRegion sdRegion = sDoc.getRegionAtCharacterOffset(selectionOffset);
                    if (sdRegion == null) {
                        super.doOperation(operation);
                        return;
                    }
                    ITextRegion textRegion = sdRegion.getRegionAtCharacterOffset(selectionOffset);
                    if (textRegion instanceof ForeignRegion) {
                        ForeignRegion foreignRegion = (ForeignRegion) textRegion;
                        //$NON-NLS-1$
                        isJavaScriptRegion = "script".equalsIgnoreCase(foreignRegion.getSurroundingTag());
                    }
                    // Check if the containing project has JS nature or not
                    if (fTextEditor instanceof PHPStructuredEditor) {
                        PHPStructuredEditor phpEditor = (PHPStructuredEditor) fTextEditor;
                        IModelElement modelElement = phpEditor.getModelElement();
                        if (modelElement != null) {
                            IScriptProject scriptProject = modelElement.getScriptProject();
                            project = scriptProject.getProject();
                            if (project != null && project.isAccessible() && project.getNature(JavaScriptCore.NATURE_ID) == null) {
                                hasJavaScriptNature = false;
                            }
                        }
                    }
                    // open dialog if required
                    if (isJavaScriptRegion && !hasJavaScriptNature) {
                        Shell activeWorkbenchShell = PHPUiPlugin.getActiveWorkbenchShell();
                        // Pop a question dialog - if the user selects 'Yes' JS
                        // Support is added, otherwise no change
                        int addJavaScriptSupport = OptionalMessageDialog.open("PROMPT_ADD_JAVASCRIPT_SUPPORT", activeWorkbenchShell, PHPUIMessages.PHPStructuredTextViewer_0, null, PHPUIMessages.PHPStructuredTextViewer_1, OptionalMessageDialog.QUESTION, new String[] { IDialogConstants.YES_LABEL, IDialogConstants// $NON-NLS-1$
                        .NO_LABEL }, 0);
                        // run the JSDT action for adding the JS nature
                        if (addJavaScriptSupport == 0 && project != null) {
                            SetupProjectsWizzard wiz = new SetupProjectsWizzard();
                            wiz.setActivePart(null, this.getTextEditor());
                            wiz.selectionChanged(null, new StructuredSelection(project));
                            wiz.run(null);
                        }
                    }
                } catch (CoreException e) {
                    Logger.logException(e);
                }
            }
            // an explicit request
            if (fViewerConfiguration != null) {
                PHPStructuredTextViewerConfiguration structuredTextViewerConfiguration = (PHPStructuredTextViewerConfiguration) fViewerConfiguration;
                IContentAssistProcessor[] all = structuredTextViewerConfiguration.getContentAssistProcessors(this, PHPPartitionTypes.PHP_DEFAULT);
                for (IContentAssistProcessor element : all) {
                    if (element instanceof PHPCompletionProcessor) {
                        ((PHPCompletionProcessor) element).setExplicit(true);
                    }
                }
            }
            super.doOperation(operation);
            return;
        case SHOW_OUTLINE:
            if (fOutlinePresenter != null) {
                fOutlinePresenter.showInformation();
            }
            return;
        case SHOW_HIERARCHY:
            if (fHierarchyPresenter != null) {
                fHierarchyPresenter.showInformation();
            }
            return;
        case DELETE:
            StyledText textWidget = getTextWidget();
            if (textWidget == null)
                return;
            ITextSelection textSelection = null;
            if (redraws()) {
                try {
                    textSelection = (ITextSelection) getSelection();
                    int length = textSelection.getLength();
                    if (!textWidget.getBlockSelection() && (length == 0 || length == textWidget.getSelectionRange().y))
                        getTextWidget().invokeAction(ST.DELETE_NEXT);
                    else
                        deleteSelection(textSelection, textWidget);
                    if (fFireSelectionChanged) {
                        Point range = textWidget.getSelectionRange();
                        fireSelectionChanged(range.x, range.y);
                    }
                } catch (BadLocationException x) {
                }
            }
            return;
    }
    super.doOperation(operation);
}
Example 9
Project: eclipse.platform.text-master  File: SourceViewer.java View source code
@Override
public void doOperation(int operation) {
    if (getTextWidget() == null || (!redraws() && operation != FORMAT))
        return;
    switch(operation) {
        case CONTENTASSIST_PROPOSALS:
            fContentAssistant.showPossibleCompletions();
            return;
        case CONTENTASSIST_CONTEXT_INFORMATION:
            fContentAssistant.showContextInformation();
            return;
        case QUICK_ASSIST:
            // FIXME: must find a way to post to the status line
            /* String msg= */
            fQuickAssistAssistant.showPossibleQuickAssists();
            // setStatusLineErrorMessage(msg);
            return;
        case INFORMATION:
            fInformationPresenter.showInformation();
            return;
        case FORMAT:
            {
                final Point selection = rememberSelection();
                final IRewriteTarget target = getRewriteTarget();
                final IDocument document = getDocument();
                IFormattingContext context = null;
                DocumentRewriteSession rewriteSession = null;
                if (document instanceof IDocumentExtension4) {
                    IDocumentExtension4 extension = (IDocumentExtension4) document;
                    DocumentRewriteSessionType type = (selection.y == 0 && document.getLength() > 1000) || selection.y > 1000 ? DocumentRewriteSessionType.SEQUENTIAL : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
                    rewriteSession = extension.startRewriteSession(type);
                } else {
                    setRedraw(false);
                    target.beginCompoundChange();
                }
                try {
                    final String rememberedContents = document.get();
                    try {
                        if (fContentFormatter instanceof IContentFormatterExtension) {
                            final IContentFormatterExtension extension = (IContentFormatterExtension) fContentFormatter;
                            context = createFormattingContext(selection.x, selection.y);
                            if (context == null) {
                                return;
                            }
                            Object region = context.getProperty(FormattingContextProperties.CONTEXT_REGION);
                            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(region == null));
                            extension.format(document, context);
                        } else {
                            IRegion r;
                            if (selection.y == 0) {
                                IRegion coverage = getModelCoverage();
                                r = coverage == null ? new Region(0, 0) : coverage;
                            } else {
                                r = new Region(selection.x, selection.y);
                            }
                            fContentFormatter.format(document, r);
                        }
                        updateSlaveDocuments(document);
                    } catch (RuntimeException x) {
                        document.set(rememberedContents);
                        throw x;
                    }
                } finally {
                    if (document instanceof IDocumentExtension4) {
                        IDocumentExtension4 extension = (IDocumentExtension4) document;
                        extension.stopRewriteSession(rewriteSession);
                    } else {
                        target.endCompoundChange();
                        setRedraw(true);
                    }
                    restoreSelection();
                    if (context != null)
                        context.dispose();
                }
                return;
            }
        default:
            super.doOperation(operation);
    }
}
Example 10
Project: eclipse3-master  File: StructuredTextViewer.java View source code
/*
   * (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);
    }
}
Example 11
Project: CodingSpectator-master  File: CleanUpPreview.java View source code
private void format(String text) {
    if (text == null) {
        //$NON-NLS-1$
        fPreviewDocument.set("");
        return;
    }
    fPreviewDocument.set(text);
    if (!fFormat) {
        if (!fCorrectIndentation)
            return;
        fSourceViewer.setRedraw(false);
        try {
            IndentAction.indent(fPreviewDocument, null);
        } catch (BadLocationException e) {
            JavaPlugin.log(e);
        } finally {
            fSourceViewer.setRedraw(true);
        }
        return;
    }
    fSourceViewer.setRedraw(false);
    final IFormattingContext context = new JavaFormattingContext();
    try {
        final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer);
        if (formatter instanceof IContentFormatterExtension) {
            final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
            context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, JavaCore.getOptions());
            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
            extension.format(fPreviewDocument, context);
        } else
            formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
    } catch (Exception e) {
        final IStatus status = new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, MultiFixMessages.CleanUpRefactoringWizard_formatterException_errorMessage, e);
        JavaPlugin.log(status);
    } finally {
        context.dispose();
        fSourceViewer.setRedraw(true);
    }
}
Example 12
Project: vjet.eclipse-master  File: FormattingTests.java View source code
/**
	 * Default constructor
	 * 
	 * @param test
	 *            do setup for the given test
	 */
/**
	 * <p>
	 * This is run once before all of the tests
	 * </p>
	 * 
	 * @see junit.extensions.TestSetup#setUp()
	 */
@BeforeClass
public void setUp() throws Exception {
    // set up formatting tools
    fJavaScriptTextTools = VjetUIPlugin.getDefault().getTextTools();
    JavascriptSourceViewerConfiguration config = new JavascriptSourceViewerConfiguration(fJavaScriptTextTools.getColorManager(), VjetUIPlugin.getDefault().getPreferenceStore(), null, IDocumentExtension3.DEFAULT_PARTITIONING);
    fFormatter = (IContentFormatterExtension) config.getContentFormatter(null);
    Assert.assertNotNull(fFormatter);
    fFormattingContext = new FormattingContext();
    fFormattingContext.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, VjetPlugin.getOptions());
    fFormattingContext.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
    // set non-interactive
    String noninteractive = System.getProperty(WTP_AUTOTEST_NONINTERACTIVE);
    if (noninteractive != null) {
        previousWTPAutoTestNonInteractivePropValue = noninteractive;
    } else {
        previousWTPAutoTestNonInteractivePropValue = "false";
    }
    System.setProperty(WTP_AUTOTEST_NONINTERACTIVE, "true");
}
Example 13
Project: Eclipse-Postfix-Code-Completion-master  File: CleanUpPreview.java View source code
private void format(String text) {
    if (text == null) {
        //$NON-NLS-1$
        fPreviewDocument.set("");
        return;
    }
    fPreviewDocument.set(text);
    if (!fFormat) {
        if (!fCorrectIndentation)
            return;
        fSourceViewer.setRedraw(false);
        try {
            IndentAction.indent(fPreviewDocument, null);
        } catch (BadLocationException e) {
            JavaPlugin.log(e);
        } finally {
            fSourceViewer.setRedraw(true);
        }
        return;
    }
    fSourceViewer.setRedraw(false);
    final IFormattingContext context = new JavaFormattingContext();
    try {
        final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer);
        if (formatter instanceof IContentFormatterExtension) {
            final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
            context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, JavaCore.getOptions());
            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
            extension.format(fPreviewDocument, context);
        } else
            formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
    } catch (Exception e) {
        final IStatus status = new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, MultiFixMessages.CleanUpRefactoringWizard_formatterException_errorMessage, e);
        JavaPlugin.log(status);
    } finally {
        context.dispose();
        fSourceViewer.setRedraw(true);
    }
}
Example 14
Project: cdt-master  File: TranslationUnitPreview.java View source code
@Override
protected void doFormatPreview() {
    if (fPreviewText == null) {
        //$NON-NLS-1$
        fPreviewDocument.set("");
        return;
    }
    fPreviewDocument.set(fPreviewText);
    fSourceViewer.setVisibleRegion(fPreviewTextOffset, fPreviewText.length() - fPreviewTextOffset);
    fSourceViewer.setRedraw(false);
    final IFormattingContext context = new FormattingContext();
    try {
        final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer);
        if (formatter instanceof IContentFormatterExtension) {
            final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
            Map<String, String> prefs = fWorkingValues;
            if (fFormatterId != null) {
                prefs = new HashMap<String, String>(fWorkingValues);
                prefs.put(CCorePreferenceConstants.CODE_FORMATTER, fFormatterId);
            }
            context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, prefs);
            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
            extension.format(fPreviewDocument, context);
        } else {
            formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
        }
    } catch (Exception e) {
        final IStatus status = new Status(IStatus.ERROR, CUIPlugin.getPluginId(), ICStatusConstants.INTERNAL_ERROR, FormatterMessages.CPreview_formatter_exception, e);
        CUIPlugin.log(status);
    } finally {
        context.dispose();
        fSourceViewer.setRedraw(true);
    }
}
Example 15
Project: eclipse.jdt.ui-master  File: CleanUpPreview.java View source code
private void format(String text) {
    if (text == null) {
        //$NON-NLS-1$
        fPreviewDocument.set("");
        return;
    }
    fPreviewDocument.set(text);
    if (!fFormat) {
        if (!fCorrectIndentation)
            return;
        fSourceViewer.setRedraw(false);
        try {
            IndentAction.indent(fPreviewDocument, null);
        } catch (BadLocationException e) {
            JavaPlugin.log(e);
        } finally {
            fSourceViewer.setRedraw(true);
        }
        return;
    }
    fSourceViewer.setRedraw(false);
    final IFormattingContext context = new JavaFormattingContext();
    try {
        final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer);
        if (formatter instanceof IContentFormatterExtension) {
            final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
            context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, JavaCore.getOptions());
            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
            extension.format(fPreviewDocument, context);
        } else
            formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
    } catch (Exception e) {
        final IStatus status = new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, MultiFixMessages.CleanUpRefactoringWizard_formatterException_errorMessage, e);
        JavaPlugin.log(status);
    } finally {
        context.dispose();
        fSourceViewer.setRedraw(true);
    }
}
Example 16
Project: lambda4jdt-master  File: SourceViewer.java View source code
/*
	 * @see ITextOperationTarget#doOperation(int)
	 */
public void doOperation(int operation) {
    if (getTextWidget() == null || (!redraws() && operation != FORMAT))
        return;
    switch(operation) {
        case CONTENTASSIST_PROPOSALS:
            fContentAssistant.showPossibleCompletions();
            return;
        case CONTENTASSIST_CONTEXT_INFORMATION:
            fContentAssistant.showContextInformation();
            return;
        case QUICK_ASSIST:
            // FIXME: must find a way to post to the status line
            /* String msg= */
            fQuickAssistAssistant.showPossibleQuickAssists();
            // setStatusLineErrorMessage(msg);
            return;
        case INFORMATION:
            fInformationPresenter.showInformation();
            return;
        case FORMAT:
            {
                final Point selection = rememberSelection();
                final IRewriteTarget target = getRewriteTarget();
                final IDocument document = getDocument();
                IFormattingContext context = null;
                DocumentRewriteSession rewriteSession = null;
                if (document instanceof IDocumentExtension4) {
                    IDocumentExtension4 extension = (IDocumentExtension4) document;
                    DocumentRewriteSessionType type = (selection.y == 0 && document.getLength() > 1000) || selection.y > 1000 ? DocumentRewriteSessionType.SEQUENTIAL : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
                    rewriteSession = extension.startRewriteSession(type);
                } else {
                    setRedraw(false);
                    target.beginCompoundChange();
                }
                try {
                    final String rememberedContents = document.get();
                    try {
                        if (fContentFormatter instanceof IContentFormatterExtension) {
                            final IContentFormatterExtension extension = (IContentFormatterExtension) fContentFormatter;
                            context = createFormattingContext();
                            if (selection.y == 0) {
                                context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
                            } else {
                                context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE);
                                context.setProperty(FormattingContextProperties.CONTEXT_REGION, new Region(selection.x, selection.y));
                            }
                            extension.format(document, context);
                        } else {
                            IRegion r;
                            if (selection.y == 0) {
                                IRegion coverage = getModelCoverage();
                                r = coverage == null ? new Region(0, 0) : coverage;
                            } else {
                                r = new Region(selection.x, selection.y);
                            }
                            fContentFormatter.format(document, r);
                        }
                        updateSlaveDocuments(document);
                    } catch (RuntimeException x) {
                        document.set(rememberedContents);
                        throw x;
                    }
                } finally {
                    if (document instanceof IDocumentExtension4) {
                        IDocumentExtension4 extension = (IDocumentExtension4) document;
                        extension.stopRewriteSession(rewriteSession);
                    } else {
                        target.endCompoundChange();
                        setRedraw(true);
                    }
                    restoreSelection();
                    if (context != null)
                        context.dispose();
                }
                return;
            }
        default:
            super.doOperation(operation);
    }
}