• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ide.eclipse.adt.internal.editors.formatting;
17 
18 import com.android.annotations.VisibleForTesting;
19 import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs;
20 import com.android.ide.eclipse.adt.internal.preferences.AttributeSortOrder;
21 
22 import org.eclipse.core.runtime.Preferences;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.ui.internal.editors.text.EditorsPlugin;
25 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
26 import org.eclipse.wst.xml.core.internal.XMLCorePlugin;
27 import org.eclipse.wst.xml.core.internal.preferences.XMLCorePreferenceNames;
28 
29 /**
30  * Formatting preferences used by the Android XML formatter.
31  */
32 public class XmlFormatPreferences {
33     /** Use the Eclipse indent (tab/space, indent size) settings? */
34     public boolean useEclipseIndent = false;
35 
36     /** Remove empty lines in all cases? */
37     public boolean removeEmptyLines = false;
38 
39     /** Reformat the text and comment blocks? */
40     public boolean reflowText = false;
41 
42     /** Join lines when reformatting text and comment blocks? */
43     public boolean joinLines = false;
44 
45     /** Can attributes appear on the same line as the opening line if there is just one of them? */
46     public boolean oneAttributeOnFirstLine = true;
47 
48     /** The sorting order to use when formatting */
49     public AttributeSortOrder sortAttributes = AttributeSortOrder.LOGICAL;
50 
51     /** Should there be a space before the closing > or /> ? */
52     public boolean spaceBeforeClose = true;
53 
54     /** The string to insert for each indentation level */
55     private String mOneIndentUnit = "    "; //$NON-NLS-1$
56 
57     /** Tab width (number of spaces to display for a tab) */
58     private int mTabWidth = -1; // -1: uninitialized
59 
60     @VisibleForTesting
XmlFormatPreferences()61     protected XmlFormatPreferences() {
62     }
63 
64     /**
65      * Creates a new {@link XmlFormatPreferences} based on the current settings
66      * in {@link AdtPrefs}
67      *
68      * @return an {@link XmlFormatPreferences} object
69      */
create()70     public static XmlFormatPreferences create() {
71         XmlFormatPreferences p = new XmlFormatPreferences();
72         AdtPrefs prefs = AdtPrefs.getPrefs();
73 
74         p.useEclipseIndent = prefs.isUseEclipseIndent();
75         p.removeEmptyLines = prefs.isRemoveEmptyLines();
76         p.oneAttributeOnFirstLine = prefs.isOneAttributeOnFirstLine();
77         p.sortAttributes = prefs.getAttributeSort();
78         p.spaceBeforeClose = prefs.isSpaceBeforeClose();
79 
80         return p;
81     }
82 
83     // The XML module settings do not have a public API. We should replace this with JDT
84     // settings anyway since that's more likely what users have configured and want applied
85     // to their XML files
86     /**
87      * Returns the string to use to indent one indentation level
88      *
89      * @return the string used to indent one indentation level
90      */
91     @SuppressWarnings({
92             "restriction", "deprecation"
93     })
getOneIndentUnit()94     public String getOneIndentUnit() {
95         if (useEclipseIndent) {
96             // Look up Eclipse indent preferences
97             // TODO: Use the JDT preferences instead, which make more sense
98             Preferences preferences = XMLCorePlugin.getDefault().getPluginPreferences();
99             int indentationWidth = preferences.getInt(XMLCorePreferenceNames.INDENTATION_SIZE);
100             String indentCharPref = preferences.getString(XMLCorePreferenceNames.INDENTATION_CHAR);
101             boolean useSpaces = XMLCorePreferenceNames.SPACE.equals(indentCharPref);
102 
103             StringBuilder indentString = new StringBuilder();
104             for (int j = 0; j < indentationWidth; j++) {
105                 if (useSpaces) {
106                     indentString.append(' ');
107                 } else {
108                     indentString.append('\t');
109                 }
110             }
111             mOneIndentUnit = indentString.toString();
112         }
113 
114         return mOneIndentUnit;
115     }
116 
117     /**
118      * Returns the number of spaces used to display a single tab character
119      *
120      * @return the number of spaces used to display a single tab character
121      */
122     @SuppressWarnings("restriction") // Editor settings
getTabWidth()123     public int getTabWidth() {
124         if (mTabWidth == -1) {
125             String key = AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH;
126             try {
127                 IPreferenceStore prefs = EditorsPlugin.getDefault().getPreferenceStore();
128                 mTabWidth = prefs.getInt(key);
129             } catch (Throwable t) {
130                 // Pass: We'll pick a suitable default instead below
131             }
132             if (mTabWidth <= 0) {
133                 mTabWidth = 4;
134             }
135         }
136 
137         return mTabWidth;
138     }
139 }
140