1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-05 07:15:39 +02:00

Added support for formatter.indent_access_specifier_extra_spaces preference.

This commit is contained in:
Sergey Prigogin 2011-01-17 02:00:16 +00:00
parent e5c42af4c9
commit 81261aee47
4 changed files with 78 additions and 52 deletions

View file

@ -8,6 +8,7 @@
* Contributors: * Contributors:
* Anton Leherbauer (Wind River Systems) - initial API and implementation * Anton Leherbauer (Wind River Systems) - initial API and implementation
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.formatter; package org.eclipse.cdt.internal.formatter;
@ -156,7 +157,6 @@ import org.eclipse.text.edits.TextEdit;
* @since 4.0 * @since 4.0
*/ */
public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, ICASTVisitor { public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, ICASTVisitor {
private static boolean DEBUG = "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.cdt.core/debug/formatter")); //$NON-NLS-1$ //$NON-NLS-2$ private static boolean DEBUG = "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.cdt.core/debug/formatter")); //$NON-NLS-1$ //$NON-NLS-2$
private static class ASTProblemException extends RuntimeException { private static class ASTProblemException extends RuntimeException {
@ -1557,6 +1557,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (node.getNodeLocations()[0] instanceof IASTMacroExpansionLocation) { if (node.getNodeLocations()[0] instanceof IASTMacroExpansionLocation) {
skipNode(node); skipNode(node);
} else { } else {
scribe.printSpaces(preferences.indent_access_specifier_extra_spaces);
switch (node.getVisibility()) { switch (node.getVisibility()) {
case ICPPASTVisibilityLabel.v_private: case ICPPASTVisibilityLabel.v_private:
scribe.printNextToken(Token.t_private, false); scribe.printNextToken(Token.t_private, false);

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2008 IBM Corporation and others. * Copyright (c) 2000, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -9,6 +9,7 @@
* IBM Corporation - initial API and implementation * IBM Corporation - initial API and implementation
* Sergey Prigogin, Google * Sergey Prigogin, Google
* Anton Leherbauer (Wind River Systems) * Anton Leherbauer (Wind River Systems)
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.formatter; package org.eclipse.cdt.internal.formatter;
@ -19,7 +20,6 @@ import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.cdt.internal.formatter.align.Alignment; import org.eclipse.cdt.internal.formatter.align.Alignment;
public class DefaultCodeFormatterOptions { public class DefaultCodeFormatterOptions {
public static final int TAB = 1; public static final int TAB = 1;
public static final int SPACE = 2; public static final int SPACE = 2;
@ -103,6 +103,7 @@ public class DefaultCodeFormatterOptions {
public boolean indent_statements_compare_to_body; public boolean indent_statements_compare_to_body;
public boolean indent_body_declarations_compare_to_access_specifier; public boolean indent_body_declarations_compare_to_access_specifier;
public boolean indent_access_specifier_compare_to_type_header; public boolean indent_access_specifier_compare_to_type_header;
public int indent_access_specifier_extra_spaces;
public boolean indent_body_declarations_compare_to_namespace_header; public boolean indent_body_declarations_compare_to_namespace_header;
public boolean indent_declaration_compare_to_template_header; public boolean indent_declaration_compare_to_template_header;
public boolean indent_breaks_compare_to_cases; public boolean indent_breaks_compare_to_cases;
@ -111,7 +112,6 @@ public class DefaultCodeFormatterOptions {
public boolean indent_switchstatements_compare_to_switch; public boolean indent_switchstatements_compare_to_switch;
public int indentation_size; public int indentation_size;
public boolean insert_new_line_after_opening_brace_in_initializer_list; public boolean insert_new_line_after_opening_brace_in_initializer_list;
public boolean insert_new_line_after_template_declaration; public boolean insert_new_line_after_template_declaration;
public boolean insert_new_line_at_end_of_file_if_missing; public boolean insert_new_line_at_end_of_file_if_missing;
@ -307,6 +307,7 @@ public class DefaultCodeFormatterOptions {
options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BODY, this.indent_statements_compare_to_body ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BODY, this.indent_statements_compare_to_body ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, this.indent_body_declarations_compare_to_access_specifier ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER, this.indent_body_declarations_compare_to_access_specifier ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, this.indent_access_specifier_compare_to_type_header ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_COMPARE_TO_TYPE_HEADER, this.indent_access_specifier_compare_to_type_header ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_EXTRA_SPACES, String.valueOf(this.indent_access_specifier_extra_spaces));
options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_DECLARATION_COMPARE_TO_TEMPLATE_HEADER, this.indent_declaration_compare_to_template_header? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_DECLARATION_COMPARE_TO_TEMPLATE_HEADER, this.indent_declaration_compare_to_template_header? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_NAMESPACE_HEADER, this.indent_body_declarations_compare_to_namespace_header ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_NAMESPACE_HEADER, this.indent_body_declarations_compare_to_namespace_header ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BREAKS_COMPARE_TO_CASES, this.indent_breaks_compare_to_cases ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE); options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_BREAKS_COMPARE_TO_CASES, this.indent_breaks_compare_to_cases ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
@ -810,6 +811,16 @@ public class DefaultCodeFormatterOptions {
if (indentAccessSpecifierCompareToTypeHeaderOption != null) { if (indentAccessSpecifierCompareToTypeHeaderOption != null) {
this.indent_access_specifier_compare_to_type_header = DefaultCodeFormatterConstants.TRUE.equals(indentAccessSpecifierCompareToTypeHeaderOption); this.indent_access_specifier_compare_to_type_header = DefaultCodeFormatterConstants.TRUE.equals(indentAccessSpecifierCompareToTypeHeaderOption);
} }
final Object indentAccessSpecifierExtraSpaces = settings.get(DefaultCodeFormatterConstants.FORMATTER_INDENT_ACCESS_SPECIFIER_EXTRA_SPACES);
if (indentAccessSpecifierExtraSpaces != null) {
try {
this.indent_access_specifier_extra_spaces = Integer.parseInt((String) indentAccessSpecifierExtraSpaces);
} catch (NumberFormatException e) {
this.indent_access_specifier_extra_spaces = 0;
} catch (ClassCastException e) {
this.indent_access_specifier_extra_spaces = 0;
}
}
final Object indentBodyDeclarationsCompareToAccessSpecifierOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER); final Object indentBodyDeclarationsCompareToAccessSpecifierOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ACCESS_SPECIFIER);
if (indentBodyDeclarationsCompareToAccessSpecifierOption != null) { if (indentBodyDeclarationsCompareToAccessSpecifierOption != null) {
this.indent_body_declarations_compare_to_access_specifier = DefaultCodeFormatterConstants.TRUE.equals(indentBodyDeclarationsCompareToAccessSpecifierOption); this.indent_body_declarations_compare_to_access_specifier = DefaultCodeFormatterConstants.TRUE.equals(indentBodyDeclarationsCompareToAccessSpecifierOption);

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2008 IBM Corporation and others. * Copyright (c) 2000, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -7,19 +7,19 @@
* *
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * IBM Corporation - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.formatter; package org.eclipse.cdt.internal.formatter;
public class OptimizedReplaceEdit { public class OptimizedReplaceEdit {
int offset; int offset;
int length; int length;
String replacement; String replacement;
public OptimizedReplaceEdit(int offset, int length, String replacement) { public OptimizedReplaceEdit(int offset, int length, CharSequence replacement) {
this.offset = offset; this.offset = offset;
this.length = length; this.length = length;
this.replacement = replacement; this.replacement = replacement.toString();
} }
@Override @Override

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2009 IBM Corporation and others. * Copyright (c) 2000, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -8,6 +8,7 @@
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * IBM Corporation - initial API and implementation
* Anton Leherbauer (Wind River Systems) * Anton Leherbauer (Wind River Systems)
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.formatter; package org.eclipse.cdt.internal.formatter;
@ -32,7 +33,6 @@ import org.eclipse.text.edits.TextEdit;
* @since 4.0 * @since 4.0
*/ */
public class Scribe { public class Scribe {
private static final String EMPTY_STRING= ""; //$NON-NLS-1$ private static final String EMPTY_STRING= ""; //$NON-NLS-1$
private static final String SPACE= " "; //$NON-NLS-1$ private static final String SPACE= " "; //$NON-NLS-1$
@ -119,7 +119,7 @@ public class Scribe {
addOptimizedReplaceEdit(start, end - start + 1, EMPTY_STRING); addOptimizedReplaceEdit(start, end - start + 1, EMPTY_STRING);
} }
public final void addInsertEdit(int insertPosition, String insertedString) { public final void addInsertEdit(int insertPosition, CharSequence insertedString) {
if (edits.length == editsIndex) { if (edits.length == editsIndex) {
// resize // resize
resize(); resize();
@ -127,7 +127,7 @@ public class Scribe {
addOptimizedReplaceEdit(insertPosition, 0, insertedString); addOptimizedReplaceEdit(insertPosition, 0, insertedString);
} }
private final void addOptimizedReplaceEdit(int offset, int length, String replacement) { private final void addOptimizedReplaceEdit(int offset, int length, CharSequence replacement) {
if (editsIndex > 0) { if (editsIndex > 0) {
// try to merge last two edits // try to merge last two edits
final OptimizedReplaceEdit previous= edits[editsIndex - 1]; final OptimizedReplaceEdit previous= edits[editsIndex - 1];
@ -1340,6 +1340,19 @@ public class Scribe {
} }
} }
public void printSpaces(int numSpaces) {
if (numSpaces > 0) {
int currentPosition= scanner.getCurrentPosition();
StringBuilder spaces = new StringBuilder(numSpaces);
for (int i = 0; i < numSpaces; i++) {
spaces.append(' ');
}
addInsertEdit(currentPosition, spaces);
pendingSpace= false;
needSpace= false;
}
}
public void printTrailingComment() { public void printTrailingComment() {
// if we have a space between two tokens we ensure it will be dumped in // if we have a space between two tokens we ensure it will be dumped in
// the formatted string // the formatted string