X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/util/CodeFormatterUtil.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/util/CodeFormatterUtil.java index bd39503..5013241 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/util/CodeFormatterUtil.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/util/CodeFormatterUtil.java @@ -10,24 +10,26 @@ *******************************************************************************/ package net.sourceforge.phpdt.internal.corext.util; -import java.util.Map; +//import java.util.Map; import net.sourceforge.phpdt.core.ToolFactory; import net.sourceforge.phpdt.core.formatter.DefaultCodeFormatterConstants; -import net.sourceforge.phpdt.internal.corext.Assert; +//incastrix +//import net.sourceforge.phpdt.internal.corext.Assert; +//import org.eclipse.core.runtime.Assert; import net.sourceforge.phpeclipse.PHPeclipsePlugin; import org.eclipse.core.runtime.Preferences; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.BadPositionCategoryException; -import org.eclipse.jface.text.DefaultPositionUpdater; -import org.eclipse.jface.text.Document; -import org.eclipse.jface.text.Position; -import org.eclipse.text.edits.DeleteEdit; -import org.eclipse.text.edits.InsertEdit; -import org.eclipse.text.edits.MultiTextEdit; -import org.eclipse.text.edits.ReplaceEdit; -import org.eclipse.text.edits.TextEdit; +//import org.eclipse.jface.text.BadLocationException; +//import org.eclipse.jface.text.BadPositionCategoryException; +//import org.eclipse.jface.text.DefaultPositionUpdater; +//import org.eclipse.jface.text.Document; +//import org.eclipse.jface.text.Position; +//import org.eclipse.text.edits.DeleteEdit; +//import org.eclipse.text.edits.InsertEdit; +//import org.eclipse.text.edits.MultiTextEdit; +//import org.eclipse.text.edits.ReplaceEdit; +//import org.eclipse.text.edits.TextEdit; public class CodeFormatterUtil { @@ -107,27 +109,27 @@ public class CodeFormatterUtil { // return getOldAPICompatibleResult(string, edit, indentationLevel, // positions, lineSeparator, options); // } - private static String getOldAPICompatibleResult(String string, - TextEdit edit, int indentationLevel, int[] positions, - String lineSeparator, Map options) { - Position[] p = null; - - if (positions != null) { - p = new Position[positions.length]; - for (int i = 0; i < positions.length; i++) { - p[i] = new Position(positions[i], 0); - } - } - String res = evaluateFormatterEdit(string, edit, p); - - if (positions != null) { - for (int i = 0; i < positions.length; i++) { - Position curr = p[i]; - positions[i] = curr.getOffset(); - } - } - return res; - } +// private static String getOldAPICompatibleResult(String string, +// TextEdit edit, int indentationLevel, int[] positions, +// String lineSeparator, Map options) { +// Position[] p = null; +// +// if (positions != null) { +// p = new Position[positions.length]; +// for (int i = 0; i < positions.length; i++) { +// p[i] = new Position(positions[i], 0); +// } +// } +// String res = evaluateFormatterEdit(string, edit, p); +// +// if (positions != null) { +// for (int i = 0; i < positions.length; i++) { +// Position curr = p[i]; +// positions[i] = curr.getOffset(); +// } +// } +// return res; +// } /** * Evaluates the edit on the given string. @@ -136,27 +138,27 @@ public class CodeFormatterUtil { * If the positions are not inside the string, a * IllegalArgumentException is thrown. */ - public static String evaluateFormatterEdit(String string, TextEdit edit, - Position[] positions) { - try { - Document doc = createDocument(string, positions); - edit.apply(doc, 0); - if (positions != null) { - for (int i = 0; i < positions.length; i++) { - Assert.isTrue(!positions[i].isDeleted, - "Position got deleted"); //$NON-NLS-1$ - } - } - return doc.get(); - } catch (BadLocationException e) { - PHPeclipsePlugin.log(e); // bug in the formatter - Assert - .isTrue( - false, - "Fromatter created edits with wrong positions: " + e.getMessage()); //$NON-NLS-1$ - } - return null; - } +// public static String evaluateFormatterEdit(String string, TextEdit edit, +// Position[] positions) { +// try { +// Document doc = createDocument(string, positions); +// edit.apply(doc, 0); +// if (positions != null) { +// for (int i = 0; i < positions.length; i++) { +// Assert.isTrue(!positions[i].isDeleted, +// "Position got deleted"); //$NON-NLS-1$ +// } +// } +// return doc.get(); +// } catch (BadLocationException e) { +// PHPeclipsePlugin.log(e); // bug in the formatter +// Assert +// .isTrue( +// false, +// "Fromatter created edits with wrong positions: " + e.getMessage()); //$NON-NLS-1$ +// } +// return null; +// } /** * Creates edits that describe how to format the given string. Returns @@ -275,73 +277,73 @@ public class CodeFormatterUtil { // } // return edit; // } - private static TextEdit shifEdit(TextEdit oldEdit, int diff) { - TextEdit newEdit; - if (oldEdit instanceof ReplaceEdit) { - ReplaceEdit edit = (ReplaceEdit) oldEdit; - newEdit = new ReplaceEdit(edit.getOffset() - diff, - edit.getLength(), edit.getText()); - } else if (oldEdit instanceof InsertEdit) { - InsertEdit edit = (InsertEdit) oldEdit; - newEdit = new InsertEdit(edit.getOffset() - diff, edit.getText()); - } else if (oldEdit instanceof DeleteEdit) { - DeleteEdit edit = (DeleteEdit) oldEdit; - newEdit = new DeleteEdit(edit.getOffset() - diff, edit.getLength()); - } else if (oldEdit instanceof MultiTextEdit) { - newEdit = new MultiTextEdit(); - } else { - return null; // not supported - } - TextEdit[] children = oldEdit.getChildren(); - for (int i = 0; i < children.length; i++) { - TextEdit shifted = shifEdit(children[i], diff); - if (shifted != null) { - newEdit.addChild(shifted); - } - } - return newEdit; - } - - private static Document createDocument(String string, Position[] positions) - throws IllegalArgumentException { - Document doc = new Document(string); - try { - if (positions != null) { - final String POS_CATEGORY = "myCategory"; //$NON-NLS-1$ +// private static TextEdit shifEdit(TextEdit oldEdit, int diff) { +// TextEdit newEdit; +// if (oldEdit instanceof ReplaceEdit) { +// ReplaceEdit edit = (ReplaceEdit) oldEdit; +// newEdit = new ReplaceEdit(edit.getOffset() - diff, +// edit.getLength(), edit.getText()); +// } else if (oldEdit instanceof InsertEdit) { +// InsertEdit edit = (InsertEdit) oldEdit; +// newEdit = new InsertEdit(edit.getOffset() - diff, edit.getText()); +// } else if (oldEdit instanceof DeleteEdit) { +// DeleteEdit edit = (DeleteEdit) oldEdit; +// newEdit = new DeleteEdit(edit.getOffset() - diff, edit.getLength()); +// } else if (oldEdit instanceof MultiTextEdit) { +// newEdit = new MultiTextEdit(); +// } else { +// return null; // not supported +// } +// TextEdit[] children = oldEdit.getChildren(); +// for (int i = 0; i < children.length; i++) { +// TextEdit shifted = shifEdit(children[i], diff); +// if (shifted != null) { +// newEdit.addChild(shifted); +// } +// } +// return newEdit; +// } - doc.addPositionCategory(POS_CATEGORY); - doc - .addPositionUpdater(new DefaultPositionUpdater( - POS_CATEGORY) { - protected boolean notDeleted() { - if (fOffset < fPosition.offset - && (fPosition.offset + fPosition.length < fOffset - + fLength)) { - fPosition.offset = fOffset + fLength; // deleted - // positions: - // set - // to - // end - // of - // remove - return false; - } - return true; - } - }); - for (int i = 0; i < positions.length; i++) { - try { - doc.addPosition(POS_CATEGORY, positions[i]); - } catch (BadLocationException e) { - throw new IllegalArgumentException( - "Position outside of string. offset: " + positions[i].offset + ", length: " + positions[i].length + ", string size: " + string.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ - } - } - } - } catch (BadPositionCategoryException cannotHappen) { - // can not happen: category is correctly set up - } - return doc; - } +// private static Document createDocument(String string, Position[] positions) +// throws IllegalArgumentException { +// Document doc = new Document(string); +// try { +// if (positions != null) { +// final String POS_CATEGORY = "myCategory"; //$NON-NLS-1$ +// +// doc.addPositionCategory(POS_CATEGORY); +// doc +// .addPositionUpdater(new DefaultPositionUpdater( +// POS_CATEGORY) { +// protected boolean notDeleted() { +// if (fOffset < fPosition.offset +// && (fPosition.offset + fPosition.length < fOffset +// + fLength)) { +// fPosition.offset = fOffset + fLength; // deleted +// // positions: +// // set +// // to +// // end +// // of +// // remove +// return false; +// } +// return true; +// } +// }); +// for (int i = 0; i < positions.length; i++) { +// try { +// doc.addPosition(POS_CATEGORY, positions[i]); +// } catch (BadLocationException e) { +// throw new IllegalArgumentException( +// "Position outside of string. offset: " + positions[i].offset + ", length: " + positions[i].length + ", string size: " + string.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ +// } +// } +// } +// } catch (BadPositionCategoryException cannotHappen) { +// // can not happen: category is correctly set up +// } +// return doc; +// } }