Home | History | Annotate | Download | only in formatting
      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.ide.eclipse.adt.internal.preferences.AdtPrefs;
     19 import com.android.ide.eclipse.adt.internal.preferences.AttributeSortOrder;
     20 
     21 import org.eclipse.core.runtime.Preferences;
     22 import org.eclipse.jface.preference.IPreferenceStore;
     23 import org.eclipse.ui.internal.editors.text.EditorsPlugin;
     24 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
     25 import org.eclipse.wst.xml.core.internal.XMLCorePlugin;
     26 import org.eclipse.wst.xml.core.internal.preferences.XMLCorePreferenceNames;
     27 
     28 /**
     29  * Formatting preferences used by the Android XML formatter.
     30  */
     31 public class XmlFormatPreferences {
     32     /** Use the Eclipse indent (tab/space, indent size) settings? */
     33     public boolean useEclipseIndent = false;
     34 
     35     /** Remove empty lines in all cases? */
     36     public boolean removeEmptyLines = false;
     37 
     38     /** Reformat the text and comment blocks? */
     39     public boolean reflowText = false;
     40 
     41     /** Join lines when reformatting text and comment blocks? */
     42     public boolean joinLines = false;
     43 
     44     /** Can attributes appear on the same line as the opening line if there is just one of them? */
     45     public boolean oneAttributeOnFirstLine = true;
     46 
     47     /** The sorting order to use when formatting */
     48     public AttributeSortOrder sortAttributes = AttributeSortOrder.LOGICAL;
     49 
     50     /** Should there be a space before the closing > or /> ? */
     51     public boolean spaceBeforeClose = true;
     52 
     53     /** The string to insert for each indentation level */
     54     private String mOneIndentUnit = "    "; //$NON-NLS-1$
     55 
     56     /** Tab width (number of spaces to display for a tab) */
     57     private int mTabWidth = -1; // -1: uninitialized
     58 
     59     private XmlFormatPreferences() {
     60     }
     61 
     62     /**
     63      * Creates a new {@link XmlFormatPreferences} based on the current settings
     64      * in {@link AdtPrefs}
     65      *
     66      * @return an {@link XmlFormatPreferences} object
     67      */
     68     public static XmlFormatPreferences create() {
     69         XmlFormatPreferences p = new XmlFormatPreferences();
     70         AdtPrefs prefs = AdtPrefs.getPrefs();
     71 
     72         p.useEclipseIndent = prefs.isUseEclipseIndent();
     73         p.removeEmptyLines = prefs.isRemoveEmptyLines();
     74         p.oneAttributeOnFirstLine = prefs.isOneAttributeOnFirstLine();
     75         p.sortAttributes = prefs.getAttributeSort();
     76         p.spaceBeforeClose = prefs.isSpaceBeforeClose();
     77 
     78         return p;
     79     }
     80 
     81     // The XML module settings do not have a public API. We should replace this with JDT
     82     // settings anyway since that's more likely what users have configured and want applied
     83     // to their XML files
     84     /**
     85      * Returns the string to use to indent one indentation level
     86      *
     87      * @return the string used to indent one indentation level
     88      */
     89     @SuppressWarnings({
     90             "restriction", "deprecation"
     91     })
     92     public String getOneIndentUnit() {
     93         if (useEclipseIndent) {
     94             // Look up Eclipse indent preferences
     95             // TODO: Use the JDT preferences instead, which make more sense
     96             Preferences preferences = XMLCorePlugin.getDefault().getPluginPreferences();
     97             int indentationWidth = preferences.getInt(XMLCorePreferenceNames.INDENTATION_SIZE);
     98             String indentCharPref = preferences.getString(XMLCorePreferenceNames.INDENTATION_CHAR);
     99             boolean useSpaces = XMLCorePreferenceNames.SPACE.equals(indentCharPref);
    100 
    101             StringBuilder indentString = new StringBuilder();
    102             for (int j = 0; j < indentationWidth; j++) {
    103                 if (useSpaces) {
    104                     indentString.append(' ');
    105                 } else {
    106                     indentString.append('\t');
    107                 }
    108             }
    109             mOneIndentUnit = indentString.toString();
    110         }
    111 
    112         return mOneIndentUnit;
    113     }
    114 
    115     /**
    116      * Returns the number of spaces used to display a single tab character
    117      *
    118      * @return the number of spaces used to display a single tab character
    119      */
    120     @SuppressWarnings("restriction") // Editor settings
    121     public int getTabWidth() {
    122         if (mTabWidth == -1) {
    123             String key = AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH;
    124             try {
    125                 IPreferenceStore prefs = EditorsPlugin.getDefault().getPreferenceStore();
    126                 mTabWidth = prefs.getInt(key);
    127             } catch (Throwable t) {
    128                 // Pass: We'll pick a suitable default instead below
    129             }
    130             if (mTabWidth <= 0) {
    131                 mTabWidth = 4;
    132             }
    133         }
    134 
    135         return mTabWidth;
    136     }
    137 }
    138