Example usage for org.eclipse.jdt.core.formatter IndentManipulation getIndentWidth

List of usage examples for org.eclipse.jdt.core.formatter IndentManipulation getIndentWidth

Introduction

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

Prototype

public static int getIndentWidth(Map options) 

Source Link

Document

Returns the tab width as configured in the given map.

Usage

From source file:com.google.gwt.eclipse.core.editors.java.JsniFormattingUtil.java

License:Open Source License

public static TextEdit format(IDocument document, TypedPosition partition,
        Map<String, String> javaFormattingPrefs, Map<String, String> javaScriptFormattingPrefs,
        String original) {//  ww  w  . ja v  a2 s  . c  o  m
    try {
        // Extract the JSNI block out of the document
        int offset = partition.getOffset();
        int length = partition.getLength();

        // Determine the line delimiter, indent string, and tab/indent widths
        String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
        int tabWidth = IndentManipulation.getTabWidth(javaFormattingPrefs);
        int indentWidth = IndentManipulation.getIndentWidth(javaFormattingPrefs);

        // Get indentation level of the first line of the JSNI block (this should
        // be the line containing the JSNI method declaration)
        int methodDeclarationOffset = getMethodDeclarationOffset(document, offset);
        int jsniLine1 = document.getLineOfOffset(methodDeclarationOffset);
        int methodIndentLevel = getLineIndentLevel(document, jsniLine1, tabWidth, indentWidth);
        DefaultCodeFormatter defaultCodeFormatter = new DefaultCodeFormatter(javaFormattingPrefs);
        String indentLine = defaultCodeFormatter.createIndentationString(methodIndentLevel);

        // Extract the JSNI body out of the block and split it up by line
        String jsniSource = document.get(offset, length);
        String body = JsniParser.extractMethodBody(jsniSource);

        String formattedJs;

        // JSNI Java references mess up the JS formatter, so replace them
        // with place holder values
        JsniJavaRefReplacementResult replacementResults = replaceJsniJavaRefs(body);
        body = replacementResults.getJsni();

        TextEdit formatEdit = CodeFormatterUtil.format2(CodeFormatter.K_STATEMENTS, body, methodIndentLevel + 1,
                lineDelimiter, javaScriptFormattingPrefs);

        if (formatEdit != null) {

            body = restoreJsniJavaRefs(replacementResults);

            Document d = new Document(body);
            formatEdit.apply(d);

            formattedJs = d.get();

            if (!formattedJs.startsWith(lineDelimiter)) {
                formattedJs = lineDelimiter + formattedJs;
            }

            if (!formattedJs.endsWith(lineDelimiter)) {
                formattedJs = formattedJs + lineDelimiter;
            }

            formattedJs = formattedJs + indentLine;

            formattedJs = "/*-{" + formattedJs + "}-*/";

        } else {

            if (original == null) {
                return null;
            }

            formattedJs = original; // formatting failed, use the original string
        }

        return new ReplaceEdit(offset, length, formattedJs);

    } catch (Exception e) {
        GWTPluginLog.logError(e);
        return null;
    }
}

From source file:com.google.gwt.eclipse.core.editors.java.JsniMethodBodyCompletionProposalComputer.java

License:Open Source License

/**
 * Returns the indentation units for a given project, document, line and line
 * offset./*from  w ww.j a  v  a  2 s  .  c  o  m*/
 */
static int measureIndentationUnits(IDocument document, int lineOfInvocationOffset, int lineOffset,
        IJavaProject project) throws BadLocationException {
    Map<?, ?> options = project.getOptions(true);
    String lineText = document.get(lineOffset, document.getLineLength(lineOfInvocationOffset));
    int indentationUnits = IndentManipulation.measureIndentUnits(lineText,
            IndentManipulation.getTabWidth(options), IndentManipulation.getIndentWidth(options));
    return indentationUnits;
}

From source file:krasa.formatter.JsniFormattingUtil.java

License:Open Source License

public static TextEdit format(IDocument document, TypedPosition partition,
        Map<String, String> javaFormattingPrefs, Map<String, String> javaScriptFormattingPrefs,
        String original) {/*  w  ww  .j av  a2s.  c om*/
    try {
        // Extract the JSNI block out of the document
        int offset = partition.getOffset();
        int length = partition.getLength();

        // Determine the line delimiter, indent string, and tab/indent widths
        String lineDelimiter = Settings.LINE_SEPARATOR;
        int tabWidth = IndentManipulation.getTabWidth(javaFormattingPrefs);
        int indentWidth = IndentManipulation.getIndentWidth(javaFormattingPrefs);

        // Get indentation level of the first line of the JSNI block (this should
        // be the line containing the JSNI method declaration)
        int methodDeclarationOffset = getMethodDeclarationOffset(document, offset);

        int jsniLine1 = document.getLineOfOffset(methodDeclarationOffset);
        int methodIndentLevel = getLineIndentLevel(document, jsniLine1, tabWidth, indentWidth);
        DefaultCodeFormatter defaultCodeFormatter = new DefaultCodeFormatter(javaFormattingPrefs);
        String indentLine = defaultCodeFormatter.createIndentationString(methodIndentLevel);

        // Extract the JSNI body out of the block and split it up by line
        String jsniSource = document.get(offset, length);
        String body = JsniParser.extractMethodBody(jsniSource);

        String formattedJs;

        // JSNI Java references mess up the JS formatter, so replace them
        // with place holder values
        JsniJavaRefReplacementResult replacementResults = replaceJsniJavaRefs(body);
        body = replacementResults.getJsni();
        CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(javaScriptFormattingPrefs);

        TextEdit formatEdit = codeFormatter.format(CodeFormatter.K_STATEMENTS, body, 0, body.length(),
                methodIndentLevel + 1, lineDelimiter);

        if (formatEdit != null) {

            body = restoreJsniJavaRefs(replacementResults);

            Document d = new Document(body);
            formatEdit.apply(d);

            formattedJs = d.get();

            if (!formattedJs.startsWith(lineDelimiter)) {
                formattedJs = lineDelimiter + formattedJs;
            }

            if (!formattedJs.endsWith(lineDelimiter)) {
                formattedJs = formattedJs + lineDelimiter;
            }

            formattedJs = formattedJs + indentLine;

            formattedJs = "/*-{" + formattedJs + "}-*/";

        } else {

            if (original == null) {
                return null;
            }

            formattedJs = original; // formatting failed, use the original string
        }

        return new ReplaceEdit(offset, length, formattedJs);

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}