Example usage for org.eclipse.jdt.core.formatter CodeFormatter K_STATEMENTS

List of usage examples for org.eclipse.jdt.core.formatter CodeFormatter K_STATEMENTS

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.formatter CodeFormatter K_STATEMENTS.

Prototype

int K_STATEMENTS

To view the source code for org.eclipse.jdt.core.formatter CodeFormatter K_STATEMENTS.

Click Source Link

Document

Kind used to format a set of statements

This kind is not applicable to module descriptions.

Usage

From source file:com.gwtplatform.plugin.SourceWriter.java

License:Apache License

SourceWriter(IMethod methodToAppendTo) throws JavaModelException {
    IScopeContext[] scopeContext = new IScopeContext[] { InstanceScope.INSTANCE };
    String lineSeparator = Platform.getPreferencesService().getString(Platform.PI_RUNTIME,
            Platform.PREF_LINE_SEPARATOR, System.getProperty("line.separator"), scopeContext);
    this.formatKind = CodeFormatter.K_STATEMENTS;
    this.indentationLevel = 2;
    codeFormatter = ToolFactory.createCodeFormatter(null);
    methodRange = methodToAppendTo.getSourceRange();
    String source = methodToAppendTo.getSource();
    int closingBracketIndex = source.lastIndexOf('}');
    assert closingBracketIndex > 0 : "Cannot append to a method without a closing bracket.";
    int lastBodyLineEndingIndex = source.lastIndexOf(lineSeparator, closingBracketIndex);
    if (lastBodyLineEndingIndex > 0) {
        prefix = source.substring(0, lastBodyLineEndingIndex + lineSeparator.length()) + lineSeparator;
        suffix = lineSeparator + source.substring(lastBodyLineEndingIndex + lineSeparator.length());
    } else {//from   www.j  a  va  2  s.co m
        // Insert a CR in the prefix
        prefix = source.substring(0, closingBracketIndex) + lineSeparator;
        suffix = lineSeparator + source.substring(closingBracketIndex);
    }
}

From source file:org.deved.antlride.integration.jdt.AntlrJavaTargetService.java

License:Open Source License

public String format(String source) {
    try {//from  w  ww.jav  a 2 s .co  m
        @SuppressWarnings("unchecked")
        Map<Object, Object> options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
        // initialize the compiler settings to be able to format 1.5 code
        options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);
        options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
        options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);

        final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options);
        final int[] K = { CodeFormatter.K_STATEMENTS, CodeFormatter.K_CLASS_BODY_DECLARATIONS,
                CodeFormatter.K_EXPRESSION };
        TextEdit edit = null;

        for (int i = 0; i < K.length; i++) {
            edit = codeFormatter.format(K[i], source, 0, source.length(), 0,
                    System.getProperty("line.separator"));
            if (edit != null) {
                // found
                break;
            }
        }
        if (edit != null) {
            IDocument document = new Document(source);
            edit.apply(document);
            return document.get();
        }
    } catch (MalformedTreeException ex) {
        AntlrCore.error(ex);
    } catch (BadLocationException ex) {
        AntlrCore.error(ex);
    }
    return source;
}

From source file:org.eclipse.jet.internal.taglib.java.JavaFormatTag.java

License:Open Source License

private int getSnippetKind(String snippetKindString, JET2Context context) {
    if (DEBUG)//from   www . j  ava  2s  .  c o  m
        System.out.println("JavaFormatTag.getSnippetKind - value specified=" + snippetKindString); //$NON-NLS-1$

    if ("K_COMPILATION_UNIT".equals(snippetKindString)) { //$NON-NLS-1$
        return CodeFormatter.K_COMPILATION_UNIT + CodeFormatter.F_INCLUDE_COMMENTS;
    } else if ("K_CLASS_BODY_DECLARATIONS".equals(snippetKindString)) { //$NON-NLS-1$
        return CodeFormatter.K_CLASS_BODY_DECLARATIONS;
    } else if ("K_EXPRESSION".equals(snippetKindString)) { //$NON-NLS-1$
        return CodeFormatter.K_EXPRESSION;
    } else if ("K_STATEMENTS".equals(snippetKindString)) { //$NON-NLS-1$
        return CodeFormatter.K_STATEMENTS;
    }

    if (DEBUG)
        System.out.println("JavaFormatTag.getSnippetKind - Falling back to K_UNKNOWN"); //$NON-NLS-1$
    return CodeFormatter.K_UNKNOWN + CodeFormatter.F_INCLUDE_COMMENTS;
}

From source file:org.eclipse.recommenders.internal.apidocs.rcp.SourceCodeArea.java

License:Open Source License

private static void format(final IDocument document) {
    final String sourceCode = document.get();
    final int length = document.getLength();
    final TextEdit edit = FORMATTER.format(CodeFormatter.K_STATEMENTS, sourceCode, 0, length, 0,
            LINE_SEPARATOR);/*from  ww w .j  a  v  a  2  s. c  om*/
    if (edit != null) {
        applyTextFormattings(document, edit);
    }
}