From 0aaa0b22ade3a1d1e2a16d08bcbc9e470189f58f Mon Sep 17 00:00:00 2001 From: scorphus Date: Mon, 7 Apr 2008 15:15:18 +0000 Subject: [PATCH 1/1] Adding two small options for code formatter (zhil's patch). These features are being added for 1.2.0, but in 1.3.0 this will not be present because the new formatter will allow people to build custom format rules. (fixes #662) --- .../src/net/sourceforge/phpdt/core/JavaCore.java | 23 +++++++++++++ .../phpdt/internal/formatter/CodeFormatter.java | 35 +++++++++++++++---- .../internal/formatter/impl/FormatterOptions.java | 24 +++++++++++++ .../phpdt/internal/ui/PHPUIMessages.properties | 2 + .../preferences/CodeFormatterPreferencePage.java | 18 +++++++++- 5 files changed, 93 insertions(+), 9 deletions(-) diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/core/JavaCore.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/core/JavaCore.java index 47e6e39..7e8a366 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/core/JavaCore.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/core/JavaCore.java @@ -117,6 +117,23 @@ public class JavaCore { */ public static final String FORMATTER_COMPACT_ASSIGNMENT = PLUGIN_ID + ".formatter.style.assignment"; //$NON-NLS-1$ + + /** + * Possible configurable option ID. + * + * @see #getDefaultOptions + * @since 2.0 + */ + public static final String FORMATTER_COMPACT_STRING_CONCATENATION = PLUGIN_ID + + ".formatter.style.compactStringConcatenation"; //$NON-NLS-1$ + /** + * Possible configurable option ID. + * + * @see #getDefaultOptions + * @since 2.0 + */ + public static final String FORMATTER_COMPACT_ARRAYS = PLUGIN_ID + + ".formatter.style.compactArrays"; //$NON-NLS-1$ /** * Possible configurable option ID. @@ -3090,6 +3107,12 @@ public class JavaCore { preferences.setDefault(FORMATTER_COMPACT_ASSIGNMENT, NORMAL); optionNames.add(FORMATTER_COMPACT_ASSIGNMENT); + + preferences.setDefault(FORMATTER_COMPACT_ARRAYS, NORMAL); + optionNames.add(FORMATTER_COMPACT_ARRAYS); + + preferences.setDefault(FORMATTER_COMPACT_STRING_CONCATENATION, NORMAL); + optionNames.add(FORMATTER_COMPACT_STRING_CONCATENATION); preferences.setDefault(FORMATTER_TAB_CHAR, TAB); optionNames.add(FORMATTER_TAB_CHAR); diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java index c65a883..e7d7075 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java @@ -18,6 +18,8 @@ import java.util.Hashtable; import java.util.Locale; import java.util.Map; +import javax.swing.text.html.Option; + import net.sourceforge.phpdt.core.ICodeFormatter; import net.sourceforge.phpdt.core.compiler.CharOperation; import net.sourceforge.phpdt.core.compiler.ITerminalSymbols; @@ -781,7 +783,9 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { && previousToken != TokenNameRBRACE && previousToken != TokenNamesuper) { // && previousToken != TokenNamethis) { - space(); + if (!options.compactArrays) { + space(); + } } // If in a for/if/while statement, increase the parenthesis // count @@ -796,8 +800,10 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { if (previousCompilableToken == TokenNamearray) { arrayDeclarationCount++; arrayDeclarationParenthesis[arrayDeclarationCount] = openParenthesis[openParenthesisCount - 1]; - indentationLevel++; - pendingNewLines = 1; + if (!options.compactArrays) { + indentationLevel++; + pendingNewLines = 1; + } } // S } break; @@ -806,14 +812,21 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { if (arrayDeclarationCount > 0) { if (arrayDeclarationParenthesis[arrayDeclarationCount] == openParenthesis[openParenthesisCount - 1]) { if (previousCompilableToken != TokenNameLPAREN) { - newLine(1); + if (!options.compactArrays) { + newLine(1); + } } else if (previousToken == TokenNameCOMMENT_LINE || previousToken == TokenNameCOMMENT_BLOCK || previousToken == TokenNameCOMMENT_PHPDOC) { // prevent to combine comment line and statement line (#1475484) - newLine(1); + if (!options.compactArrays) { + newLine(1); + } + } + if (!options.compactArrays) { + indentationLevel--; + } - indentationLevel--; currentLineIndentationLevel = indentationLevel; pendingNewLines = 0; arrayDeclarationCount--; @@ -920,12 +933,16 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { if (arrayDeclarationCount > 0) { if (arrayDeclarationParenthesis[arrayDeclarationCount] == openParenthesis[openParenthesisCount - 1]) { // there is no left parenthesis to close in current array declaration (#1475484) - pendingNewLines = 1; + if (!options.compactArrays) { + pendingNewLines = 1; + } } } break; case TokenNameDOT: - space(); + if (!options.compactStringConcatenation) { + space(); + } pendingSpace = false; break; case TokenNameSEMICOLON: @@ -1447,6 +1464,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { case TokenNameDOLLAR: case Scanner.TokenNameCOMMENT_LINE: return false; + case TokenNameDOT: + return !options.compactStringConcatenation; default: return true; } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/impl/FormatterOptions.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/impl/FormatterOptions.java index b379e6a..5297a14 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/impl/FormatterOptions.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/impl/FormatterOptions.java @@ -31,6 +31,10 @@ public class FormatterOptions { public static final String OPTION_CompactAssignment = "net.sourceforge.phpeclipse.formatter.style.assignment"; //$NON-NLS-1$ + public static final String OPTION_CompactStringConcatenation = "net.sourceforge.phpeclipse.formatter.style.compactStringConcatenation"; //$NON-NLS-1$ + + public static final String OPTION_CompactArrays = "net.sourceforge.phpeclipse.formatter.style.compactArrays"; //$NON-NLS-1$ + public static final String OPTION_TabulationChar = "net.sourceforge.phpeclipse.formatter.tabulation.char"; //$NON-NLS-1$ public static final String OPTION_TabulationSize = "net.sourceforge.phpeclipse.formatter.tabulation.size"; //$NON-NLS-1$ @@ -69,7 +73,11 @@ public class FormatterOptions { public int maxLineLength = 80; public boolean compactAssignmentMode = false; + + public boolean compactStringConcatenation = false; + public boolean compactArrays = false; + // if isTrue, assignments look like x= 12 (not like x = 12); public boolean compactDereferencingMode = true; @@ -170,6 +178,22 @@ public class FormatterOptions { } continue; } + if (optionID.equals(OPTION_CompactArrays)) { + if (optionValue.equals(COMPACT)) { + this.compactArrays = true; + } else if (optionValue.equals(NORMAL)) { + this.compactArrays = false; + } + continue; + } + if (optionID.equals(OPTION_CompactStringConcatenation)) { + if (optionValue.equals(COMPACT)) { + this.compactStringConcatenation = true; + } else if (optionValue.equals(NORMAL)) { + this.compactStringConcatenation = false; + } + continue; + } if (optionID.equals(OPTION_TabulationChar)) { if (optionValue.equals(TAB)) { this.indentWithTab = true; diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/PHPUIMessages.properties b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/PHPUIMessages.properties index 333caed..e8bbead 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/PHPUIMessages.properties +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/PHPUIMessages.properties @@ -399,6 +399,8 @@ CodeFormatterPreferencePage.newline_else_if.label=&Insert new line between 'else CodeFormatterPreferencePage.newline_empty_block.label=In&sert a new line inside an empty block CodeFormatterPreferencePage.split_line.label=Ma&ximum line length: CodeFormatterPreferencePage.style_compact_assignement.label=&Compact assignment +CodeFormatterPreferencePage.style_compact_arrays.label=Compact &arrays +CodeFormatterPreferencePage.style_compact_string_concatenation.label=Compact &string concatenation CodeFormatterPreferencePage.tab_char.label=Indentation is represented by a &tab CodeFormatterPreferencePage.tab_size.label=&Number of spaces representing a tab: diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/CodeFormatterPreferencePage.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/CodeFormatterPreferencePage.java index f448ac6..e08920c 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/CodeFormatterPreferencePage.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/CodeFormatterPreferencePage.java @@ -61,6 +61,10 @@ public class CodeFormatterPreferencePage extends PreferencePage implements private static final String PREF_STYLE_COMPACT_ASSIGNEMENT = JavaCore.FORMATTER_COMPACT_ASSIGNMENT; + private static final String PREF_STYLE_COMPACT_STRING_CONCATENATION = JavaCore.FORMATTER_COMPACT_STRING_CONCATENATION; + + private static final String PREF_STYLE_COMPACT_ARRAYS = JavaCore.FORMATTER_COMPACT_ARRAYS; + private static final String PREF_TAB_CHAR = JavaCore.FORMATTER_TAB_CHAR; private static final String PREF_TAB_SIZE = JavaCore.FORMATTER_TAB_SIZE; @@ -87,7 +91,9 @@ public class CodeFormatterPreferencePage extends PreferencePage implements PREF_NEWLINE_CONTROL_STATEMENT, PREF_NEWLINE_CLEAR_ALL, // PREF_NEWLINE_ELSE_IF, PREF_NEWLINE_EMPTY_BLOCK, PREF_LINE_SPLIT, - PREF_STYLE_COMPACT_ASSIGNEMENT, PREF_TAB_CHAR, PREF_TAB_SIZE }; + PREF_STYLE_COMPACT_ASSIGNEMENT, PREF_STYLE_COMPACT_STRING_CONCATENATION, + PREF_STYLE_COMPACT_ARRAYS, + PREF_TAB_CHAR, PREF_TAB_SIZE }; } /** @@ -298,6 +304,16 @@ public class CodeFormatterPreferencePage extends PreferencePage implements .getString("CodeFormatterPreferencePage.style_compact_assignement.label"); //$NON-NLS-1$ addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_ASSIGNEMENT, new String[] { COMPACT, NORMAL }); + + label = PHPUIMessages + .getString("CodeFormatterPreferencePage.style_compact_string_concatenation.label"); //$NON-NLS-1$ + addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_STRING_CONCATENATION, + new String[] { COMPACT, NORMAL }); + + label = PHPUIMessages + .getString("CodeFormatterPreferencePage.style_compact_arrays.label"); //$NON-NLS-1$ + addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_ARRAYS, + new String[] { COMPACT, NORMAL }); label = PHPUIMessages .getString("CodeFormatterPreferencePage.tab_char.label"); //$NON-NLS-1$ -- 1.7.1