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>ObfuscatorIgnores.
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 String lineDelim = null;
75 lineDelim = doc.getLineDelimiter(0);
76 } catch (BadLocationException e) {
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;
87 if (lineDelim == null) {
88 lineDelim = lineDelims.length > 0 ? lineDelims[0] : systemDelimiter;
95 * Returns the indent of the given string.
97 * @param line the text line
98 * @param tabWidth the width of the '\t' character.
100 * @see org.eclipse.jdt.internal.corext.util.Strings.computeIndent(String,int)
102 public static int computeIndent(String line, int tabWidth) {
105 int size = line.length();
106 for (int i = 0; i < size; i++) {
107 char c = line.charAt(i);
111 } else if (Character.isSpaceChar(c)) {
113 if (blanks == tabWidth) {
124 ////////////////////////////////////////////////////////////////////////