e3d00d40da62580cf70949d662925362343eb90b
[phpeclipse.git] /
1 package net.sourceforge.phpeclipse.phpeditor.html;
2
3 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
4 import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration;
5
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;
11
12 /**
13  * @author chrisl
14  *
15  * To change this generated comment edit the template variable "typecomment":
16  * Window>Preferences>Java>ObfuscatorIgnores.
17  * To enable and disable the creation of type comments go to
18  * Window>Preferences>Java>Code Generation.
19  */
20 public class HTMLFormattingStrategy implements IFormattingStrategy, IHTMLConstants {
21
22         ////////////////////////////////////////////////////////////////////////
23
24         private PHPSourceViewerConfiguration fConfig;
25         private ISourceViewer fViewer;
26         //
27         private PHPEditor fEditor;
28         private int fTabWidth;
29
30         ////////////////////////////////////////////////////////////////////////
31
32         public HTMLFormattingStrategy(PHPSourceViewerConfiguration cf, ISourceViewer viewer) {
33                 fConfig = cf;
34                 fViewer = viewer;
35                 //
36                 fEditor = fConfig.getEditor();
37                 initPreferences();
38         }
39
40         public void initPreferences() {
41                 fTabWidth=fConfig.getTabWidth(fViewer);
42         }
43         
44         ////////////////////////////////////////////////////////////////////////
45
46         /**
47          * @see IFormattingStrategy#formatterStarts(String)
48          */
49         public void formatterStarts(String initialIndentation) {
50         }
51
52         /**
53          * @see IFormattingStrategy#formatterStops()
54          */
55         public void formatterStops() {
56         }
57
58         /**
59          * @see IFormattingStrategy#format(String, boolean, String, int[])
60          */
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);
65                 int indent = 0;
66                 if (indentation != null) {
67                         indent = computeIndent(indentation, fTabWidth);
68                 }
69                 return formatter.format(content, indent, positions, lineDelimiter, fEditor.getEditorInput().getName());
70         }
71
72         public static String getLineDelimiterFor(IDocument doc) {
73                 String lineDelim = null;
74                 try {
75                         lineDelim = doc.getLineDelimiter(0);
76                 } catch (BadLocationException e) {
77                 }
78                 if (lineDelim == null) {
79                         String systemDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
80                         String[] lineDelims = doc.getLegalLineDelimiters();
81                         for (int i = 0; i < lineDelims.length; i++) {
82                                 if (lineDelims[i].equals(systemDelimiter)) {
83                                         lineDelim = systemDelimiter;
84                                         break;
85                                 }
86                         }
87                         if (lineDelim == null) {
88                                 lineDelim = lineDelims.length > 0 ? lineDelims[0] : systemDelimiter;
89                         }
90                 }
91                 return lineDelim;
92         }
93
94         /**
95          * Returns the indent of the given string.
96          * 
97          * @param line the text line
98          * @param tabWidth the width of the '\t' character.
99          * 
100          * @see org.eclipse.jdt.internal.corext.util.Strings.computeIndent(String,int)
101          */
102         public static int computeIndent(String line, int tabWidth) {
103                 int result = 0;
104                 int blanks = 0;
105                 int size = line.length();
106                 for (int i = 0; i < size; i++) {
107                         char c = line.charAt(i);
108                         if (c == '\t') {
109                                 result++;
110                                 blanks = 0;
111                         } else if (Character.isSpaceChar(c)) {
112                                 blanks++;
113                                 if (blanks == tabWidth) {
114                                         result++;
115                                         blanks = 0;
116                                 }
117                         } else {
118                                 return result;
119                         }
120                 }
121                 return result;
122         }
123
124         ////////////////////////////////////////////////////////////////////////
125
126 }