package net.sourceforge.phpeclipse.phpeditor.html;
import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.formatter.IFormattingStrategy;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.ui.texteditor.ITextEditor;
/**
* @author chrisl
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>ObfuscatorIgnores.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class HTMLFormattingStrategy implements IFormattingStrategy, IHTMLConstants {
////////////////////////////////////////////////////////////////////////
private PHPSourceViewerConfiguration fConfig;
private ISourceViewer fViewer;
//
private ITextEditor fEditor;
private int fTabWidth;
////////////////////////////////////////////////////////////////////////
public HTMLFormattingStrategy(PHPSourceViewerConfiguration cf, ISourceViewer viewer) {
fConfig = cf;
fViewer = viewer;
//
fEditor = fConfig.getEditor();
initPreferences();
}
public void initPreferences() {
fTabWidth=fConfig.getTabWidth(fViewer);
}
////////////////////////////////////////////////////////////////////////
/**
* @see IFormattingStrategy#formatterStarts(String)
*/
public void formatterStarts(String initialIndentation) {
}
/**
* @see IFormattingStrategy#formatterStops()
*/
public void formatterStops() {
}
/**
* @see IFormattingStrategy#format(String, boolean, String, int[])
*/
public String format(String content, boolean isLineStart, String indentation, int[] positions) {
HTMLFormatter formatter = new HTMLFormatter(fConfig, (SourceViewer) fViewer);
IDocument doc = fViewer.getDocument();
String lineDelimiter = getLineDelimiterFor(doc);
int indent = 0;
if (indentation != null) {
indent = computeIndent(indentation, fTabWidth);
}
return formatter.format(content, indent, positions, lineDelimiter, fEditor.getEditorInput().getName());
}
public static String getLineDelimiterFor(IDocument doc) {
String lineDelim = null;
try {
lineDelim = doc.getLineDelimiter(0);
} catch (BadLocationException e) {
}
if (lineDelim == null) {
String systemDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
String[] lineDelims = doc.getLegalLineDelimiters();
for (int i = 0; i < lineDelims.length; i++) {
if (lineDelims[i].equals(systemDelimiter)) {
lineDelim = systemDelimiter;
break;
}
}
if (lineDelim == null) {
lineDelim = lineDelims.length > 0 ? lineDelims[0] : systemDelimiter;
}
}
return lineDelim;
}
/**
* Returns the indent of the given string.
*
* @param line the text line
* @param tabWidth the width of the '\t' character.
*
* @see org.eclipse.jdt.internal.corext.util.Strings.computeIndent(String,int)
*/
public static int computeIndent(String line, int tabWidth) {
int result = 0;
int blanks = 0;
int size = line.length();
for (int i = 0; i < size; i++) {
char c = line.charAt(i);
if (c == '\t') {
result++;
blanks = 0;
} else if (Character.isSpaceChar(c)) {
blanks++;
if (blanks == tabWidth) {
result++;
blanks = 0;
}
} else {
return result;
}
}
return result;
}
////////////////////////////////////////////////////////////////////////
}