String javaText = getJavaText();
for (int i = 0; i < deltas.length; i++)
System.out.println("pos[" + deltas[i].preOffset + ":" + deltas[i].preLength + "]" + javaText.substring(deltas[i].preOffset, deltas[i].preOffset + deltas[i].preLength)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println("==============================================="); //$NON-NLS-1$
}
UndoEdit undo = null;
// apply the edit to the java document
try {
undo = javaEdit.apply(getJavaDocument());
}
catch (MalformedTreeException e) {
Logger.logException(e);
}
catch (BadLocationException e) {
Logger.logException(e);
}
// now at this point Java positions are unreliable since they were
// updated after applying java edit.
String newJavaText = getJavaDocument().get();
if (DEBUG)
System.out.println("java post format text:\n" + newJavaText); //$NON-NLS-1$
// record post edit data
for (int i = 0; i < javaPositions.length; i++)
deltas[i].setPostEditData(javaPositions[i].offset, javaPositions[i].length, javaPositions[i].isDeleted);
// create appropriate text edits for deltas
Position jspPos = null;
String replaceText = ""; //$NON-NLS-1$
for (int i = 0; i < deltas.length; i++) {
jspPos = jspPositions[i];
// can be null if it's an indirect mapping position
// or if something was added into java that was not originally in
// JSP (like a new import...)
if (jspPos != null) {
if (deltas[i].isDeleted) {
jspEdits.add(new DeleteEdit(jspPos.offset, jspPos.length));
}
else {
replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
// get rid of pre and post white space or fine tuned
// adjustment later.
// fix text here...
replaceText = fixJspReplaceText(replaceText, jspPos);
if (!(replaceText.length() == 0 && jspPos.length == 0))//Unwanted TextEdit can lead to MalformedTreeException.See: Bug 321977
jspEdits.add(new ReplaceEdit(jspPos.offset, jspPos.length, replaceText));
}
if (DEBUG)
debugReplace(deltas, jspPos, replaceText, i);
}
else {
// the new Java text has no corresponding JSP position
// possible new import?
if (isImport(javaPositions[i].getOffset()) && replaceText.lastIndexOf("import ") != -1) { //$NON-NLS-1$
replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
String importText = replaceText.substring(replaceText.lastIndexOf("import "), replaceText.indexOf(";")); //$NON-NLS-1$ //$NON-NLS-2$
// evenutally need to check if it's XML-JSP
importText = "<%@page import=\"" + importText + "\" %>\n"; //$NON-NLS-1$ //$NON-NLS-2$
jspEdits.add(new InsertEdit(0, importText));
}
}
}
TextEdit allJspEdits = createMultiTextEdit((TextEdit[]) jspEdits.toArray(new TextEdit[jspEdits.size()]));
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=105632
// undo the java edit
// (so the underlying Java document still represents what's in the
// editor)
if (undo != null) {
try {
undo.apply(getJavaDocument());
}
catch (MalformedTreeException e) {
Logger.logException(e);
}
catch (BadLocationException e) {