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.core.formatter.newline.openingBrace"; //$NON-NLS-1$
21 public static final String OPTION_InsertNewlineInControlStatement = "net.sourceforge.phpeclipse.core.formatter.newline.controlStatement"; //$NON-NLS-1$
22 // public static final String OPTION_InsertNewLineBetweenElseAndIf = "net.sourceforge.phpeclipse.core.formatter.newline.elseIf"; //$NON-NLS-1$
23 public static final String OPTION_InsertNewLineInEmptyBlock = "net.sourceforge.phpeclipse.core.formatter.newline.emptyBlock"; //$NON-NLS-1$
24 public static final String OPTION_ClearAllBlankLines = "net.sourceforge.phpeclipse.core.formatter.newline.clearAll"; //$NON-NLS-1$
25 public static final String OPTION_SplitLineExceedingLength = "net.sourceforge.phpeclipse.core.formatter.lineSplit"; //$NON-NLS-1$
26 public static final String OPTION_CompactAssignment = "net.sourceforge.phpeclipse.core.formatter.style.assignment"; //$NON-NLS-1$
27 public static final String OPTION_TabulationChar = "net.sourceforge.phpeclipse.core.formatter.tabulation.char"; //$NON-NLS-1$
28 public static final String OPTION_TabulationSize = "net.sourceforge.phpeclipse.core.formatter.tabulation.size"; //$NON-NLS-1$
29 public static final String OPTION_CompactDereferencing = "net.sourceforge.phpeclipse.core.formatter.style.assignment";
30 // TODO: add the checkbox in the preferences panel ; load/save
32 public static final String INSERT = "insert"; //$NON-NLS-1$
33 public static final String DO_NOT_INSERT = "do not insert"; //$NON-NLS-1$
34 public static final String PRESERVE_ONE = "preserve one"; //$NON-NLS-1$
35 public static final String CLEAR_ALL = "clear all"; //$NON-NLS-1$
36 public static final String NORMAL = "normal"; //$NON-NLS-1$
37 public static final String COMPACT = "compact"; //$NON-NLS-1$
38 public static final String TAB = "tab"; //$NON-NLS-1$
39 public static final String SPACE = "space"; //$NON-NLS-1$
41 // by default, do not insert blank line before opening brace
42 public boolean newLineBeforeOpeningBraceMode = false;
44 // by default, do not insert blank line behind keywords (ELSE, CATCH, FINALLY,...) in control statements
45 public boolean newlineInControlStatementMode = false;
47 // by default, preserve one blank line per sequence of blank lines
48 public boolean clearAllBlankLinesMode = false;
50 // line splitting will occur when line exceeds this length
51 public int maxLineLength = 80;
53 public boolean compactAssignmentMode = false;
54 // if isTrue, assignments look like x= 12 (not like x = 12);
55 public boolean compactDereferencingMode = true;
56 // if isTrue, dereferencing look like $obj->method (not like $obj -> method);
58 //number of consecutive spaces used to replace the tab char
59 public int tabSize = 4; // n spaces for one tab
60 public boolean indentWithTab = true;
62 //public boolean compactElseIfMode = true;
63 // if true, else and if are kept on the same line.
64 public boolean newLineInEmptyBlockMode = true;
65 // if false, no new line in {} if it's empty.
67 public char[] lineSeparatorSequence = System.getProperty("line.separator").toCharArray(); //$NON-NLS-1$
69 * Initializing the formatter options with default settings
71 public FormatterOptions() {
74 * Initializing the formatter options with external settings
76 public FormatterOptions(Map settings) {
80 // filter options which are related to the assist component
81 Object[] entries = settings.entrySet().toArray();
82 for (int i = 0, max = entries.length; i < max; i++) {
83 Map.Entry entry = (Map.Entry) entries[i];
84 if (!(entry.getKey() instanceof String))
86 if (!(entry.getValue() instanceof String))
88 String optionID = (String) entry.getKey();
89 String optionValue = (String) entry.getValue();
91 if (optionID.equals(OPTION_InsertNewlineBeforeOpeningBrace)) {
92 if (optionValue.equals(INSERT)) {
93 this.newLineBeforeOpeningBraceMode = true;
94 } else if (optionValue.equals(DO_NOT_INSERT)) {
95 this.newLineBeforeOpeningBraceMode = false;
99 if (optionID.equals(OPTION_InsertNewlineInControlStatement)) {
100 if (optionValue.equals(INSERT)) {
101 this.newlineInControlStatementMode = true;
102 } else if (optionValue.equals(DO_NOT_INSERT)) {
103 this.newlineInControlStatementMode = false;
107 if (optionID.equals(OPTION_ClearAllBlankLines)) {
108 if (optionValue.equals(CLEAR_ALL)) {
109 this.clearAllBlankLinesMode = true;
110 } else if (optionValue.equals(PRESERVE_ONE)) {
111 this.clearAllBlankLinesMode = false;
115 // if (optionID.equals(OPTION_InsertNewLineBetweenElseAndIf)) {
116 // if (optionValue.equals(INSERT)) {
117 // this.compactElseIfMode = false;
118 // } else if (optionValue.equals(DO_NOT_INSERT)) {
119 // this.compactElseIfMode = true;
123 if (optionID.equals(OPTION_InsertNewLineInEmptyBlock)) {
124 if (optionValue.equals(INSERT)) {
125 this.newLineInEmptyBlockMode = true;
126 } else if (optionValue.equals(DO_NOT_INSERT)) {
127 this.newLineInEmptyBlockMode = false;
131 if (optionID.equals(OPTION_SplitLineExceedingLength)) {
133 int val = Integer.parseInt(optionValue);
135 this.maxLineLength = val;
136 } catch (NumberFormatException e) {
139 if (optionID.equals(OPTION_CompactAssignment)) {
140 if (optionValue.equals(COMPACT)) {
141 this.compactAssignmentMode = true;
142 } else if (optionValue.equals(NORMAL)) {
143 this.compactAssignmentMode = false;
147 if (optionID.equals(OPTION_TabulationChar)) {
148 if (optionValue.equals(TAB)) {
149 this.indentWithTab = true;
150 } else if (optionValue.equals(SPACE)) {
151 this.indentWithTab = false;
155 if (optionID.equals(OPTION_TabulationSize)) {
157 int val = Integer.parseInt(optionValue);
160 } catch (NumberFormatException e) {
170 public int getMaxLineLength() {
171 return maxLineLength;
173 public int getTabSize() {
176 public boolean isAddingNewLineBeforeOpeningBrace() {
177 return newLineBeforeOpeningBraceMode;
179 public boolean isAddingNewLineInControlStatement() {
180 return newlineInControlStatementMode;
182 public boolean isAddingNewLineInEmptyBlock() {
183 return newLineInEmptyBlockMode;
185 public boolean isClearingAllBlankLines() {
186 return clearAllBlankLinesMode;
188 public boolean isCompactingAssignment() {
189 return compactAssignmentMode;
191 public boolean isCompactingDereferencing() {
192 return compactDereferencingMode;
194 // public boolean isCompactingElseIf() {
195 // return compactElseIfMode;
197 public boolean isUsingTabForIndenting() {
198 return indentWithTab;
200 public void setLineSeparator(String lineSeparator) {
201 lineSeparatorSequence = lineSeparator.toCharArray();
204 * @deprecated - should use a Map when creating the options.
206 public void setMaxLineLength(int maxLineLength) {
207 this.maxLineLength = maxLineLength;
210 * @deprecated - should use a Map when creating the options.
212 // public void setCompactElseIfMode(boolean flag) {
213 // compactElseIfMode = flag;