1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.formatter.impl;
15 public class FormatterOptions {
20 public static final String OPTION_InsertNewlineBeforeOpeningBrace = "net.sourceforge.phpeclipse.formatter.newline.openingBrace"; //$NON-NLS-1$
22 public static final String OPTION_InsertNewlineInControlStatement = "net.sourceforge.phpeclipse.formatter.newline.controlStatement"; //$NON-NLS-1$
24 // public static final String OPTION_InsertNewLineBetweenElseAndIf =
25 // "net.sourceforge.phpeclipse.formatter.newline.elseIf"; //$NON-NLS-1$
26 public static final String OPTION_InsertNewLineInEmptyBlock = "net.sourceforge.phpeclipse.formatter.newline.emptyBlock"; //$NON-NLS-1$
28 public static final String OPTION_ClearAllBlankLines = "net.sourceforge.phpeclipse.formatter.newline.clearAll"; //$NON-NLS-1$
30 public static final String OPTION_SplitLineExceedingLength = "net.sourceforge.phpeclipse.formatter.lineSplit"; //$NON-NLS-1$
32 public static final String OPTION_CompactAssignment = "net.sourceforge.phpeclipse.formatter.style.assignment"; //$NON-NLS-1$
34 public static final String OPTION_CompactStringConcatenation = "net.sourceforge.phpeclipse.formatter.style.compactStringConcatenation"; //$NON-NLS-1$
36 public static final String OPTION_CompactArrays = "net.sourceforge.phpeclipse.formatter.style.compactArrays"; //$NON-NLS-1$
38 public static final String OPTION_TabulationChar = "net.sourceforge.phpeclipse.formatter.tabulation.char"; //$NON-NLS-1$
40 public static final String OPTION_TabulationSize = "net.sourceforge.phpeclipse.formatter.tabulation.size"; //$NON-NLS-1$
42 public static final String OPTION_CompactDereferencing = "net.sourceforge.phpeclipse.formatter.style.assignment";
44 // TODO: add the checkbox in the preferences panel ; load/save
46 public static final String INSERT = "insert"; //$NON-NLS-1$
48 public static final String DO_NOT_INSERT = "do not insert"; //$NON-NLS-1$
50 public static final String PRESERVE_ONE = "preserve one"; //$NON-NLS-1$
52 public static final String CLEAR_ALL = "clear all"; //$NON-NLS-1$
54 public static final String NORMAL = "normal"; //$NON-NLS-1$
56 public static final String COMPACT = "compact"; //$NON-NLS-1$
58 public static final String TAB = "tab"; //$NON-NLS-1$
60 public static final String SPACE = "space"; //$NON-NLS-1$
62 // by default, do not insert blank line before opening brace
63 public boolean newLineBeforeOpeningBraceMode = false;
65 // by default, do not insert blank line behind keywords (ELSE, CATCH,
66 // FINALLY,...) in control statements
67 public boolean newlineInControlStatementMode = false;
69 // by default, preserve one blank line per sequence of blank lines
70 public boolean clearAllBlankLinesMode = false;
72 // line splitting will occur when line exceeds this length
73 public int maxLineLength = 80;
75 public boolean compactAssignmentMode = false;
77 public boolean compactStringConcatenation = false;
79 public boolean compactArrays = false;
81 // if isTrue, assignments look like x= 12 (not like x = 12);
82 public boolean compactDereferencingMode = true;
84 // if isTrue, dereferencing look like $obj->method (not like $obj ->
87 // number of consecutive spaces used to replace the tab char
88 public int tabSize = 4; // n spaces for one tab
90 public boolean indentWithTab = true;
92 // public boolean compactElseIfMode = true;
93 // if true, else and if are kept on the same line.
94 public boolean newLineInEmptyBlockMode = true;
96 // if false, no new line in {} if it's empty.
98 public char[] lineSeparatorSequence = System
99 .getProperty("line.separator").toCharArray(); //$NON-NLS-1$
102 * Initializing the formatter options with default settings
104 public FormatterOptions() {
108 * Initializing the formatter options with external settings
110 public FormatterOptions(Map settings) {
111 if (settings == null)
114 // filter options which are related to the assist component
115 Object[] entries = settings.entrySet().toArray();
116 for (int i = 0, max = entries.length; i < max; i++) {
117 Map.Entry entry = (Map.Entry) entries[i];
118 if (!(entry.getKey() instanceof String))
120 if (!(entry.getValue() instanceof String))
122 String optionID = (String) entry.getKey();
123 String optionValue = (String) entry.getValue();
125 if (optionID.equals(OPTION_InsertNewlineBeforeOpeningBrace)) {
126 if (optionValue.equals(INSERT)) {
127 this.newLineBeforeOpeningBraceMode = true;
128 } else if (optionValue.equals(DO_NOT_INSERT)) {
129 this.newLineBeforeOpeningBraceMode = false;
133 if (optionID.equals(OPTION_InsertNewlineInControlStatement)) {
134 if (optionValue.equals(INSERT)) {
135 this.newlineInControlStatementMode = true;
136 } else if (optionValue.equals(DO_NOT_INSERT)) {
137 this.newlineInControlStatementMode = false;
141 if (optionID.equals(OPTION_ClearAllBlankLines)) {
142 if (optionValue.equals(CLEAR_ALL)) {
143 this.clearAllBlankLinesMode = true;
144 } else if (optionValue.equals(PRESERVE_ONE)) {
145 this.clearAllBlankLinesMode = false;
149 // if (optionID.equals(OPTION_InsertNewLineBetweenElseAndIf)) {
150 // if (optionValue.equals(INSERT)) {
151 // this.compactElseIfMode = false;
152 // } else if (optionValue.equals(DO_NOT_INSERT)) {
153 // this.compactElseIfMode = true;
157 if (optionID.equals(OPTION_InsertNewLineInEmptyBlock)) {
158 if (optionValue.equals(INSERT)) {
159 this.newLineInEmptyBlockMode = true;
160 } else if (optionValue.equals(DO_NOT_INSERT)) {
161 this.newLineInEmptyBlockMode = false;
165 if (optionID.equals(OPTION_SplitLineExceedingLength)) {
167 int val = Integer.parseInt(optionValue);
169 this.maxLineLength = val;
170 } catch (NumberFormatException e) {
173 if (optionID.equals(OPTION_CompactAssignment)) {
174 if (optionValue.equals(COMPACT)) {
175 this.compactAssignmentMode = true;
176 } else if (optionValue.equals(NORMAL)) {
177 this.compactAssignmentMode = false;
181 if (optionID.equals(OPTION_CompactArrays)) {
182 if (optionValue.equals(COMPACT)) {
183 this.compactArrays = true;
184 } else if (optionValue.equals(NORMAL)) {
185 this.compactArrays = false;
189 if (optionID.equals(OPTION_CompactStringConcatenation)) {
190 if (optionValue.equals(COMPACT)) {
191 this.compactStringConcatenation = true;
192 } else if (optionValue.equals(NORMAL)) {
193 this.compactStringConcatenation = false;
197 if (optionID.equals(OPTION_TabulationChar)) {
198 if (optionValue.equals(TAB)) {
199 this.indentWithTab = true;
200 } else if (optionValue.equals(SPACE)) {
201 this.indentWithTab = false;
205 if (optionID.equals(OPTION_TabulationSize)) {
207 int val = Integer.parseInt(optionValue);
210 } catch (NumberFormatException e) {
220 public int getMaxLineLength() {
221 return maxLineLength;
224 public int getTabSize() {
228 public boolean isAddingNewLineBeforeOpeningBrace() {
229 return newLineBeforeOpeningBraceMode;
232 public boolean isAddingNewLineInControlStatement() {
233 return newlineInControlStatementMode;
236 public boolean isAddingNewLineInEmptyBlock() {
237 return newLineInEmptyBlockMode;
240 public boolean isClearingAllBlankLines() {
241 return clearAllBlankLinesMode;
244 public boolean isCompactingAssignment() {
245 return compactAssignmentMode;
248 public boolean isCompactingDereferencing() {
249 return compactDereferencingMode;
252 // public boolean isCompactingElseIf() {
253 // return compactElseIfMode;
255 public boolean isUsingTabForIndenting() {
256 return indentWithTab;
259 public void setLineSeparator(String lineSeparator) {
260 lineSeparatorSequence = lineSeparator.toCharArray();
264 * @deprecated - should use a Map when creating the options.
266 public void setMaxLineLength(int maxLineLength) {
267 this.maxLineLength = maxLineLength;
270 * @deprecated - should use a Map when creating the options.
272 // public void setCompactElseIfMode(boolean flag) {
273 // compactElseIfMode = flag;