Example usage for org.eclipse.jdt.internal.formatter DefaultCodeFormatterOptions getJavaConventionsSettings

List of usage examples for org.eclipse.jdt.internal.formatter DefaultCodeFormatterOptions getJavaConventionsSettings

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.formatter DefaultCodeFormatterOptions getJavaConventionsSettings.

Prototype

public static DefaultCodeFormatterOptions getJavaConventionsSettings() 

Source Link

Usage

From source file:com.iw.plugins.spindle.ui.wizards.factories.ClassFactory.java

License:Mozilla Public License

/**
 * Format an entire Java source file/*from www. ja v a2s . com*/
 * 
 * @param sourceString
 *          entire file as one unformatted String
 * @return formatted String
 */
public static String formatJava(String sourceString, int initialIndentationLevel, String lineDelim) {
    String ret = sourceString;
    DefaultCodeFormatterOptions dcfo = DefaultCodeFormatterOptions.getJavaConventionsSettings();
    dcfo.line_separator = lineDelim;
    CodeFormatter cformatter = ToolFactory.createCodeFormatter(dcfo.getMap());
    TextEdit te = cformatter.format(CodeFormatter.K_COMPILATION_UNIT, sourceString, 0, sourceString.length(),
            initialIndentationLevel, lineDelim);
    IDocument l_doc = new Document(sourceString);
    try {
        te.apply(l_doc);
    } catch (MalformedTreeException e) {
        // not fatal, since we still have our original contents
        UIPlugin.warn(e);
    } catch (BadLocationException e) {
        // not fatal, since we still have our original contents
        UIPlugin.warn(e);
    }
    String maybe = l_doc.get();
    // in case formatting fails, we'll just return what we got
    if (!didFormatChoke(maybe)) {
        ret = maybe;
    } else {
        UIPlugin.warn("Java formatter choked"); //TODO choked
    }
    return ret + lineDelim; // dunno why we add the extra, but we did
}