/** * Old API. Consider to use format2 (TextEdit) */ public static String format(int kind, String string, int indentationLevel, int[] positions, String lineSeparator, Map options) { return format(kind, string, 0, string.length(), indentationLevel, positions, lineSeparator, options); }
public static TextEdit format2(int kind, String string, int indentationLevel, String lineSeparator, Map options) { return format2(kind, string, 0, string.length(), indentationLevel, lineSeparator, options); }
/** * Removes the common number of indents from all lines. If a line * only consists out of white space it is ignored. * @param project the java project from which to get the formatter * preferences, or <code>null</code> for global preferences * @since 3.1 */ public static void trimIndentation(String[] lines, IJavaProject project) { trimIndentation(lines, CodeFormatterUtil.getTabWidth(project), CodeFormatterUtil.getIndentWidth(project), true); } /**
/** * Old API. Consider to use format2 (TextEdit) */ public static String format(ASTNode node, String string, int indentationLevel, int[] positions, String lineSeparator, Map options) { TextEdit edit= format2(node, string, indentationLevel, lineSeparator, options); if (edit == null) { //JavaPlugin.logErrorMessage("formatter failed to format (no edit returned). Will use unformatted text instead. node: " + node.getNodeType() + ", string: " + string); //$NON-NLS-1$ //$NON-NLS-2$ return string; } return getOldAPICompatibleResult(string, edit, indentationLevel, positions, lineSeparator, options); }
public static String asFormattedString(ASTNode node, int indent, String lineDelim, Map options) { String unformatted= asString(node); TextEdit edit= CodeFormatterUtil.format2(node, unformatted, indent, lineDelim, options); if (edit != null) { return CodeFormatterUtil.evaluateFormatterEdit(unformatted, edit, null); } return unformatted; // unknown node }
int indent= Strings.computeIndentUnits(document.get(region.getOffset(), region.getLength()), project); String replacement= CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, buf.toString(), 0, null, lineDelim, project); replacement= Strings.changeIndent(replacement, 0, project, CodeFormatterUtil.createIndentString(indent, project), lineDelim); setReplacementString(replacement.substring(replacement.indexOf('(') + 1));
public static TextEdit reformat(int kind, String string, int indentationLevel, String lineSeparator, Map options) { return reformat(kind, string, 0, string.length(), indentationLevel, lineSeparator, options); }
/** * The preference setting for the visual tabulator display. * * @return the number of spaces displayed for a tabulator in the editor */ private int getVisualTabLengthPreference() { return CodeFormatterUtil.getTabWidth(fProject); }
private int getIndentSize() { return CodeFormatterUtil.getIndentWidth(null); }
/** * Returns the current indent width. * * @param project the project where the source is used or <code>null</code> * if the project is unknown and the workspace default should be used * @return the indent width * @since 3.1 */ public static int getIndentWidth(IJavaProject project) { String key; if (DefaultCodeFormatterConstants.MIXED.equals(getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR))) key= DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE; else key= DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE; return getCoreOption(project, key, 4); }
/** * Returns extra indentation string for strings that are broken by a newline * based on the value of the formatter preferences for tabs vs. spaces. * * @return two tabs or equivalent number of spaces */ private String getExtraIndentAfterNewLine() { // read settings int formatterContinuationIndentationSize= getContinuationIndentationSize(); int binaryAlignmentValue= getBinaryOperatorAlignmentStyle(); // work out indent int indentSize= formatterContinuationIndentationSize; if (binaryAlignmentValue == DefaultCodeFormatterConstants.INDENT_BY_ONE) { indentSize= 1; } else if (binaryAlignmentValue == DefaultCodeFormatterConstants.INDENT_ON_COLUMN) { // there is no obvious way to work out the current column indent } // generate indentation string with correct size return CodeFormatterUtil.createIndentString(indentSize, fProject); }
String replacementString= CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, buf.toString(), 0, lineDelim, options); replacementString= replacementString + ';'; replacementString= Strings.changeIndent(replacementString, 0, project, CodeFormatterUtil.createIndentString(indent, project), lineDelim);
/** * Creates edits that describe how to re-format the given string. * This method should be used for formatting existing code. * Returns <code>null</code> if the code could not be formatted for the given kind. * * @param kind * Use to specify the kind of the code snippet to format. * It can be any of the kind constants defined in {@link CodeFormatter} * @param source * The source to format * @param indentationLevel * The initial indentation level, used to shift left/right the entire source fragment. * An initial indentation level of zero or below has no effect. * @param lineSeparator * The line separator to use in formatted source, * if set to <code>null</code>, then the platform default one will be used. * @param options * The options map to use for formatting with the default code formatter. * Recognized options are documented on {@link JavaCore#getDefaultOptions()}. * If set to <code>null</code>, then use the current settings from {@link JavaCore#getOptions()}. * @return an TextEdit describing the changes required to format source * @throws IllegalArgumentException * If the offset and length are not inside the string, a IllegalArgumentException is thrown. */ public static TextEdit reformat(int kind, String source, int indentationLevel, String lineSeparator, Map<String, String> options) { return reformat(kind, source, 0, source.length(), indentationLevel, lineSeparator, options); }
/** * The preference setting for the visual tabulator display. * * @return the number of spaces displayed for a tabulator in the editor */ private int getVisualTabLengthPreference() { return CodeFormatterUtil.getTabWidth(fProject); }
private int prefIndentationSize() { return CodeFormatterUtil.getIndentWidth(fProject); }
/** * Returns the current indent width. * * @param project * the project where the source is used or, * <code>null</code> if the project is unknown and the workspace default should be used * @return the indent width * @since 3.1 */ public static int getIndentWidth(IJavaProject project) { String key; if (DefaultCodeFormatterConstants.MIXED.equals(getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR))) key= DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE; else key= DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE; return getCoreOption(project, key, 4); }
/** * Old API. Consider to use format2 (TextEdit) */ public static String format(int kind, String string, int offset, int length, int indentationLevel, int[] positions, String lineSeparator, Map options) { TextEdit edit= format2(kind, string, offset, length, indentationLevel, lineSeparator, options); if (edit == null) { //JavaPlugin.logErrorMessage("formatter failed to format (no edit returned). Will use unformatted text instead. kind: " + kind + ", string: " + string); //$NON-NLS-1$ //$NON-NLS-2$ return string.substring(offset, offset + length); } String formatted= getOldAPICompatibleResult(string, edit, indentationLevel, positions, lineSeparator, options); return formatted.substring(offset, formatted.length() - (string.length() - (offset + length))); }
/** * Returns extra indentation string for strings that are broken by a newline * based on the value of the formatter preferences for tabs vs. spaces. * * @return two tabs or equivalent number of spaces */ private String getExtraIndentAfterNewLine() { // read settings int formatterContinuationIndentationSize= getContinuationIndentationSize(); int binaryAlignmentValue= getBinaryOperatorAlignmentStyle(); // work out indent int indentSize= formatterContinuationIndentationSize; if (binaryAlignmentValue == DefaultCodeFormatterConstants.INDENT_BY_ONE) { indentSize= 1; } else if (binaryAlignmentValue == DefaultCodeFormatterConstants.INDENT_ON_COLUMN) { // there is no obvious way to work out the current column indent } // generate indentation string with correct size return CodeFormatterUtil.createIndentString(indentSize, fProject); }
/** * Removes the common number of indents from all lines. If a line * only consists out of white space it is ignored. * @param lines the lines * @param project the java project from which to get the formatter * preferences, or <code>null</code> for global preferences * @since 3.1 */ public static void trimIndentation(String[] lines, IJavaProject project) { trimIndentation(lines, CodeFormatterUtil.getTabWidth(project), CodeFormatterUtil.getIndentWidth(project), true); } /**
public static String format(int kind, String string, int indentationLevel, int[] positions, String lineSeparator, IJavaProject project) { Map options= project != null ? project.getOptions(true) : null; return format(kind, string, 0, string.length(), indentationLevel, positions, lineSeparator, options); }