1 package net.sourceforge.phpeclipse.phpeditor.html;
 
   3 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
 
   4 import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration;
 
   6 import org.eclipse.jface.text.BadLocationException;
 
   7 import org.eclipse.jface.text.IDocument;
 
   8 import org.eclipse.jface.text.formatter.IFormattingStrategy;
 
   9 import org.eclipse.jface.text.source.ISourceViewer;
 
  10 import org.eclipse.jface.text.source.SourceViewer;
 
  15  * To change this generated comment edit the template variable "typecomment":
 
  16  * Window>Preferences>Java>Templates.
 
  17  * To enable and disable the creation of type comments go to
 
  18  * Window>Preferences>Java>Code Generation.
 
  20 public class HTMLFormattingStrategy implements IFormattingStrategy, IHTMLConstants {
 
  22         ////////////////////////////////////////////////////////////////////////
 
  24         private PHPSourceViewerConfiguration fConfig;
 
  25         private ISourceViewer fViewer;
 
  27         private PHPEditor fEditor;
 
  28         private int fTabWidth;
 
  30         ////////////////////////////////////////////////////////////////////////
 
  32         public HTMLFormattingStrategy(PHPSourceViewerConfiguration cf, ISourceViewer viewer) {
 
  36                 fEditor = fConfig.getEditor();
 
  40         public void initPreferences() {
 
  41                 fTabWidth=fConfig.getTabWidth(fViewer);
 
  44         ////////////////////////////////////////////////////////////////////////
 
  47          * @see IFormattingStrategy#formatterStarts(String)
 
  49         public void formatterStarts(String initialIndentation) {
 
  53          * @see IFormattingStrategy#formatterStops()
 
  55         public void formatterStops() {
 
  59          * @see IFormattingStrategy#format(String, boolean, String, int[])
 
  61         public String format(String content, boolean isLineStart, String indentation, int[] positions) {
 
  62                 HTMLFormatter formatter = new HTMLFormatter(fConfig, (SourceViewer) fViewer);
 
  63                 IDocument doc = fViewer.getDocument();
 
  64                 String lineDelimiter = getLineDelimiterFor(doc);
 
  66                 if (indentation != null) {
 
  67                         indent = computeIndent(indentation, fTabWidth);
 
  69                 return formatter.format(content, indent, positions, lineDelimiter, fEditor.getEditorInput().getName());
 
  72         public static String getLineDelimiterFor(IDocument doc) {
 
  73                 // new for: 1GF5UU0: ITPJUI:WIN2000 - "Organize Imports" in java editor inserts lines in wrong format
 
  74                 String lineDelim = null;
 
  76                         lineDelim = doc.getLineDelimiter(0);
 
  77                 } catch (BadLocationException e) {
 
  79                 if (lineDelim == null) {
 
  80                         String systemDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
 
  81                         String[] lineDelims = doc.getLegalLineDelimiters();
 
  82                         for (int i = 0; i < lineDelims.length; i++) {
 
  83                                 if (lineDelims[i].equals(systemDelimiter)) {
 
  84                                         lineDelim = systemDelimiter;
 
  88                         if (lineDelim == null) {
 
  89                                 lineDelim = lineDelims.length > 0 ? lineDelims[0] : systemDelimiter;
 
  96          * Returns the indent of the given string.
 
  98          * @param line the text line
 
  99          * @param tabWidth the width of the '\t' character.
 
 101          * @see org.eclipse.jdt.internal.corext.util.Strings.computeIndent(String,int)
 
 103         public static int computeIndent(String line, int tabWidth) {
 
 106                 int size = line.length();
 
 107                 for (int i = 0; i < size; i++) {
 
 108                         char c = line.charAt(i);
 
 112                         } else if (Character.isSpaceChar(c)) {
 
 114                                 if (blanks == tabWidth) {
 
 125         ////////////////////////////////////////////////////////////////////////