Search in sources :

Example 1 with MultiPassContentFormatter

use of org.eclipse.jface.text.formatter.MultiPassContentFormatter in project tdi-studio-se by Talend.

the class JavaProcessor method formatCode.

/**
     * DOC nrousseau Comment method "formatCode".
     *
     * from SourceViewer.doOperation for FORMAT.
     *
     * @param processCode
     * @return
     */
@SuppressWarnings({ "unchecked" })
private String formatCode(String processCode) {
    // we cannot make calls to Ui in headless mode
    if (ProcessorUtilities.isExportConfig() || CommonsPlugin.isHeadless()) {
        // nothing to do
        return processCode;
    }
    final IDocument document = new Document(processCode);
    JavaTextTools tools = JavaPlugin.getDefault().getJavaTextTools();
    tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING);
    IFormattingContext context = null;
    DocumentRewriteSession rewriteSession = null;
    if (document instanceof IDocumentExtension4) {
        rewriteSession = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL);
    }
    try {
        final String rememberedContents = document.get();
        try {
            final MultiPassContentFormatter formatter = new MultiPassContentFormatter(IJavaPartitions.JAVA_PARTITIONING, IDocument.DEFAULT_CONTENT_TYPE);
            formatter.setMasterStrategy(new JavaFormattingStrategy());
            // formatter.setSlaveStrategy(new CommentFormattingStrategy(),
            // IJavaPartitions.JAVA_DOC);
            // formatter.setSlaveStrategy(new CommentFormattingStrategy(),
            // IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
            // formatter.setSlaveStrategy(new CommentFormattingStrategy(),
            // IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
            context = new FormattingContext();
            context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
            Map<String, String> preferences;
            if (this.getTalendJavaProject() == null) {
                preferences = new HashMap<String, String>(JavaCore.getOptions());
            } else {
                // use project options
                preferences = new HashMap<String, String>(this.getTalendJavaProject().getJavaProject().getOptions(true));
            }
            context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, preferences);
            formatter.format(document, context);
        } catch (RuntimeException x) {
            // fire wall for
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=47472
            // if something went wrong we undo the changes we just did
            // TODO to be removed after 3.0 M8
            document.set(rememberedContents);
            throw x;
        }
    } finally {
        if (rewriteSession != null && document instanceof IDocumentExtension4) {
            ((IDocumentExtension4) document).stopRewriteSession(rewriteSession);
        }
        if (context != null) {
            context.dispose();
        }
    }
    return document.get();
}
Also used : IFormattingContext(org.eclipse.jface.text.formatter.IFormattingContext) FormattingContext(org.eclipse.jface.text.formatter.FormattingContext) IFormattingContext(org.eclipse.jface.text.formatter.IFormattingContext) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) JavaTextTools(org.eclipse.jdt.ui.text.JavaTextTools) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) MultiPassContentFormatter(org.eclipse.jface.text.formatter.MultiPassContentFormatter) JavaFormattingStrategy(org.eclipse.jdt.internal.ui.text.java.JavaFormattingStrategy) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with MultiPassContentFormatter

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

the class StructuredTextViewerConfigurationHTML method getContentFormatter.

public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
    IContentFormatter formatter = super.getContentFormatter(sourceViewer);
    // generic one
    if (!(formatter instanceof MultiPassContentFormatter))
        formatter = new MultiPassContentFormatter(getConfiguredDocumentPartitioning(sourceViewer), IHTMLPartitions.HTML_DEFAULT);
    ((MultiPassContentFormatter) formatter).setMasterStrategy(new StructuredFormattingStrategy(new HTMLFormatProcessorImpl()));
    return formatter;
}
Also used : IContentFormatter(org.eclipse.jface.text.formatter.IContentFormatter) HTMLFormatProcessorImpl(org.eclipse.wst.html.core.internal.format.HTMLFormatProcessorImpl) MultiPassContentFormatter(org.eclipse.jface.text.formatter.MultiPassContentFormatter) StructuredFormattingStrategy(org.eclipse.wst.sse.ui.internal.format.StructuredFormattingStrategy)

Example 3 with MultiPassContentFormatter

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

the class StructuredTextViewerConfigurationJSP method getContentFormatter.

public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
    IContentFormatter formatter = super.getContentFormatter(sourceViewer);
    // generic one
    if (!(formatter instanceof MultiPassContentFormatter))
        formatter = new MultiPassContentFormatter(getConfiguredDocumentPartitioning(sourceViewer), IXMLPartitions.XML_DEFAULT);
    MultiPassContentFormatter multiFormatter = (MultiPassContentFormatter) formatter;
    multiFormatter.setMasterStrategy(new StructuredFormattingStrategyJSP());
    multiFormatter.setSlaveStrategy(new FormattingStrategyJSPJava(), IJSPPartitions.JSP_CONTENT_JAVA);
    return formatter;
}
Also used : FormattingStrategyJSPJava(org.eclipse.jst.jsp.ui.internal.format.FormattingStrategyJSPJava) IContentFormatter(org.eclipse.jface.text.formatter.IContentFormatter) StructuredFormattingStrategyJSP(org.eclipse.jst.jsp.ui.internal.format.StructuredFormattingStrategyJSP) MultiPassContentFormatter(org.eclipse.jface.text.formatter.MultiPassContentFormatter)

Example 4 with MultiPassContentFormatter

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

the class StructuredTextViewerConfigurationCSS method getContentFormatter.

public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
    IContentFormatter formatter = super.getContentFormatter(sourceViewer);
    // generic one
    if (!(formatter instanceof MultiPassContentFormatter))
        formatter = new MultiPassContentFormatter(getConfiguredDocumentPartitioning(sourceViewer), ICSSPartitions.STYLE);
    ((MultiPassContentFormatter) formatter).setMasterStrategy(new StructuredFormattingStrategy(new FormatProcessorCSS()));
    return formatter;
}
Also used : IContentFormatter(org.eclipse.jface.text.formatter.IContentFormatter) MultiPassContentFormatter(org.eclipse.jface.text.formatter.MultiPassContentFormatter) FormatProcessorCSS(org.eclipse.wst.css.core.internal.format.FormatProcessorCSS) StructuredFormattingStrategy(org.eclipse.wst.sse.ui.internal.format.StructuredFormattingStrategy)

Example 5 with MultiPassContentFormatter

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

the class StructuredTextViewerConfigurationJSON method getContentFormatter.

@Override
public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
    IContentFormatter formatter = super.getContentFormatter(sourceViewer);
    // generic one
    if (!(formatter instanceof MultiPassContentFormatter))
        formatter = new MultiPassContentFormatter(getConfiguredDocumentPartitioning(sourceViewer), IJSONPartitions.JSON);
    ((MultiPassContentFormatter) formatter).setMasterStrategy(new StructuredFormattingStrategy(new FormatProcessorJSON()));
    return formatter;
}
Also used : IContentFormatter(org.eclipse.jface.text.formatter.IContentFormatter) MultiPassContentFormatter(org.eclipse.jface.text.formatter.MultiPassContentFormatter) FormatProcessorJSON(org.eclipse.wst.json.core.format.FormatProcessorJSON) StructuredFormattingStrategy(org.eclipse.wst.sse.ui.internal.format.StructuredFormattingStrategy)

Aggregations

MultiPassContentFormatter (org.eclipse.jface.text.formatter.MultiPassContentFormatter)6 IContentFormatter (org.eclipse.jface.text.formatter.IContentFormatter)5 StructuredFormattingStrategy (org.eclipse.wst.sse.ui.internal.format.StructuredFormattingStrategy)3 JavaFormattingStrategy (org.eclipse.jdt.internal.ui.text.java.JavaFormattingStrategy)1 JavaTextTools (org.eclipse.jdt.ui.text.JavaTextTools)1 Document (org.eclipse.jface.text.Document)1 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)1 IDocument (org.eclipse.jface.text.IDocument)1 IDocumentExtension4 (org.eclipse.jface.text.IDocumentExtension4)1 FormattingContext (org.eclipse.jface.text.formatter.FormattingContext)1 IFormattingContext (org.eclipse.jface.text.formatter.IFormattingContext)1 FormattingStrategyJSPJava (org.eclipse.jst.jsp.ui.internal.format.FormattingStrategyJSPJava)1 StructuredFormattingStrategyJSP (org.eclipse.jst.jsp.ui.internal.format.StructuredFormattingStrategyJSP)1 FormatProcessorCSS (org.eclipse.wst.css.core.internal.format.FormatProcessorCSS)1 HTMLFormatProcessorImpl (org.eclipse.wst.html.core.internal.format.HTMLFormatProcessorImpl)1 FormatProcessorJSON (org.eclipse.wst.json.core.format.FormatProcessorJSON)1 XMLFormattingStrategy (org.eclipse.wst.xml.ui.internal.XMLFormattingStrategy)1