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_TabulationChar = "net.sourceforge.phpeclipse.formatter.tabulation.char"; //$NON-NLS-1$
36 public static final String OPTION_TabulationSize = "net.sourceforge.phpeclipse.formatter.tabulation.size"; //$NON-NLS-1$
38 public static final String OPTION_CompactDereferencing = "net.sourceforge.phpeclipse.formatter.style.assignment";
40 // TODO: add the checkbox in the preferences panel ; load/save
42 public static final String INSERT = "insert"; //$NON-NLS-1$
44 public static final String DO_NOT_INSERT = "do not insert"; //$NON-NLS-1$
46 public static final String PRESERVE_ONE = "preserve one"; //$NON-NLS-1$
48 public static final String CLEAR_ALL = "clear all"; //$NON-NLS-1$
50 public static final String NORMAL = "normal"; //$NON-NLS-1$
52 public static final String COMPACT = "compact"; //$NON-NLS-1$
54 public static final String TAB = "tab"; //$NON-NLS-1$
56 public static final String SPACE = "space"; //$NON-NLS-1$
58 // by default, do not insert blank line before opening brace
59 public boolean newLineBeforeOpeningBraceMode = false;
61 // by default, do not insert blank line behind keywords (ELSE, CATCH,
62 // FINALLY,...) in control statements
63 public boolean newlineInControlStatementMode = false;
65 // by default, preserve one blank line per sequence of blank lines
66 public boolean clearAllBlankLinesMode = false;
68 // line splitting will occur when line exceeds this length
69 public int maxLineLength = 80;
71 public boolean compactAssignmentMode = false;
73 // if isTrue, assignments look like x= 12 (not like x = 12);
74 public boolean compactDereferencingMode = true;
76 // if isTrue, dereferencing look like $obj->method (not like $obj ->
79 // number of consecutive spaces used to replace the tab char
80 public int tabSize = 4; // n spaces for one tab
82 public boolean indentWithTab = true;
84 // public boolean compactElseIfMode = true;
85 // if true, else and if are kept on the same line.
86 public boolean newLineInEmptyBlockMode = true;
88 // if false, no new line in {} if it's empty.
90 public char[] lineSeparatorSequence = System
91 .getProperty("line.separator").toCharArray(); //$NON-NLS-1$
94 * Initializing the formatter options with default settings
96 public FormatterOptions() {
100 * Initializing the formatter options with external settings
102 public FormatterOptions(Map settings) {
103 if (settings == null)
106 // filter options which are related to the assist component
107 Object[] entries = settings.entrySet().toArray();
108 for (int i = 0, max = entries.length; i < max; i++) {
109 Map.Entry entry = (Map.Entry) entries[i];
110 if (!(entry.getKey() instanceof String))
112 if (!(entry.getValue() instanceof String))
114 String optionID = (String) entry.getKey();
115 String optionValue = (String) entry.getValue();
117 if (optionID.equals(OPTION_InsertNewlineBeforeOpeningBrace)) {
118 if (optionValue.equals(INSERT)) {
119 this.newLineBeforeOpeningBraceMode = true;
120 } else if (optionValue.equals(DO_NOT_INSERT)) {
121 this.newLineBeforeOpeningBraceMode = false;
125 if (optionID.equals(OPTION_InsertNewlineInControlStatement)) {
126 if (optionValue.equals(INSERT)) {
127 this.newlineInControlStatementMode = true;
128 } else if (optionValue.equals(DO_NOT_INSERT)) {
129 this.newlineInControlStatementMode = false;
133 if (optionID.equals(OPTION_ClearAllBlankLines)) {
134 if (optionValue.equals(CLEAR_ALL)) {
135 this.clearAllBlankLinesMode = true;
136 } else if (optionValue.equals(PRESERVE_ONE)) {
137 this.clearAllBlankLinesMode = false;
141 // if (optionID.equals(OPTION_InsertNewLineBetweenElseAndIf)) {
142 // if (optionValue.equals(INSERT)) {
143 // this.compactElseIfMode = false;
144 // } else if (optionValue.equals(DO_NOT_INSERT)) {
145 // this.compactElseIfMode = true;
149 if (optionID.equals(OPTION_InsertNewLineInEmptyBlock)) {
150 if (optionValue.equals(INSERT)) {
151 this.newLineInEmptyBlockMode = true;
152 } else if (optionValue.equals(DO_NOT_INSERT)) {
153 this.newLineInEmptyBlockMode = false;
157 if (optionID.equals(OPTION_SplitLineExceedingLength)) {
159 int val = Integer.parseInt(optionValue);
161 this.maxLineLength = val;
162 } catch (NumberFormatException e) {
165 if (optionID.equals(OPTION_CompactAssignment)) {
166 if (optionValue.equals(COMPACT)) {
167 this.compactAssignmentMode = true;
168 } else if (optionValue.equals(NORMAL)) {
169 this.compactAssignmentMode = false;
173 if (optionID.equals(OPTION_TabulationChar)) {
174 if (optionValue.equals(TAB)) {
175 this.indentWithTab = true;
176 } else if (optionValue.equals(SPACE)) {
177 this.indentWithTab = false;
181 if (optionID.equals(OPTION_TabulationSize)) {
183 int val = Integer.parseInt(optionValue);
186 } catch (NumberFormatException e) {
196 public int getMaxLineLength() {
197 return maxLineLength;
200 public int getTabSize() {
204 public boolean isAddingNewLineBeforeOpeningBrace() {
205 return newLineBeforeOpeningBraceMode;
208 public boolean isAddingNewLineInControlStatement() {
209 return newlineInControlStatementMode;
212 public boolean isAddingNewLineInEmptyBlock() {
213 return newLineInEmptyBlockMode;
216 public boolean isClearingAllBlankLines() {
217 return clearAllBlankLinesMode;
220 public boolean isCompactingAssignment() {
221 return compactAssignmentMode;
224 public boolean isCompactingDereferencing() {
225 return compactDereferencingMode;
228 // public boolean isCompactingElseIf() {
229 // return compactElseIfMode;
231 public boolean isUsingTabForIndenting() {
232 return indentWithTab;
235 public void setLineSeparator(String lineSeparator) {
236 lineSeparatorSequence = lineSeparator.toCharArray();
240 * @deprecated - should use a Map when creating the options.
242 public void setMaxLineLength(int maxLineLength) {
243 this.maxLineLength = maxLineLength;
246 * @deprecated - should use a Map when creating the options.
248 // public void setCompactElseIfMode(boolean flag) {
249 // compactElseIfMode = flag;