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 
     20 import org.eclipse.jface.text.BadLocationException;
     21 import org.eclipse.jface.text.IDocument;
     22 import org.eclipse.jface.text.IRegion;
     23 import org.eclipse.jface.text.TypedPosition;
     24 import org.eclipse.jface.text.formatter.FormattingContext;
     25 import org.eclipse.jface.text.formatter.FormattingContextProperties;
     26 import org.eclipse.jface.text.formatter.IContentFormatter;
     27 import org.eclipse.jface.text.formatter.IContentFormatterExtension;
     28 import org.eclipse.jface.text.formatter.IFormattingContext;
     29 import org.eclipse.jface.text.formatter.IFormattingStrategy;
     30 import org.eclipse.wst.xml.core.text.IXMLPartitions;
     31 
     32 /**
     33  * Formatter which replaces the Eclipse formatter for the Android XML editors, and
     34  * delegates to it if the user has chosen to use the Eclipse formatter instead by turning
     35  * off {@link AdtPrefs#getUseCustomXmlFormatter()}
     36  */
     37 public class AndroidXmlFormatter implements IContentFormatter, IContentFormatterExtension {
     38     @Override
     39     public final void format(IDocument document, IRegion region) {
     40         /**
     41          * This method is probably not going to be called. It is part of the
     42          * {@link IContentFormatter} but since we also implement
     43          * {@link IContentFormatterExtension} Eclipse should /* be calling
     44          * {@link #format(IDocument,IFormattingContext)} instead. However, for
     45          * completeness (and because other implementations of {@link IContentFormatter}
     46          * also do this we might as well make the method behave correctly
     47          */
     48         FormattingContext context = new FormattingContext();
     49         context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE);
     50         context.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
     51 
     52         format(document, context);
     53     }
     54 
     55     @Override
     56     public IFormattingStrategy getFormattingStrategy(String contentType) {
     57         return new AndroidXmlFormattingStrategy();
     58     }
     59 
     60     @Override
     61     public void format(IDocument document, IFormattingContext context) {
     62         context.setProperty(FormattingContextProperties.CONTEXT_MEDIUM, document);
     63         formatMaster(context, document, 0, document.getLength());
     64     }
     65 
     66     protected void formatMaster(IFormattingContext context, IDocument document, int offset,
     67             int length) {
     68         try {
     69             final int delta= offset - document.getLineInformationOfOffset(offset).getOffset();
     70             offset -= delta;
     71             length += delta;
     72         } catch (BadLocationException exception) {
     73             // Do nothing
     74         }
     75 
     76         AndroidXmlFormattingStrategy strategy = new AndroidXmlFormattingStrategy();
     77         context.setProperty(FormattingContextProperties.CONTEXT_PARTITION,
     78                 new TypedPosition(offset, length, IXMLPartitions.XML_DEFAULT));
     79         strategy.formatterStarts(context);
     80         strategy.format();
     81         strategy.formatterStops();
     82     }
     83 }
     84