use of org.eclipse.text.edits.DeleteEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testDelete1.
@Test
public void testDelete1() throws Exception {
TextEdit e1 = new DeleteEdit(3, 1);
fRoot.addChild(e1);
UndoEdit undo = fRoot.apply(fDocument);
assertEquals(fRoot, 3, 0);
assertEquals(e1, 3, 0);
Assert.assertEquals("Buffer content", "012456789", fDocument.get());
doUndoRedo(undo, "012456789");
}
use of org.eclipse.text.edits.DeleteEdit in project eclipse.platform.text by eclipse.
the class RemoveTrailingWhitespaceOperation method computeTextEdit.
@Override
protected MultiTextEditWithProgress computeTextEdit(ITextFileBuffer fileBuffer, IProgressMonitor progressMonitor) throws CoreException {
IDocument document = fileBuffer.getDocument();
int lineCount = document.getNumberOfLines();
SubMonitor subMonitor = SubMonitor.convert(progressMonitor, FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_generatingChanges, lineCount);
try {
MultiTextEditWithProgress multiEdit = new MultiTextEditWithProgress(FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_applyingChanges);
for (int i = 0; i < lineCount; i++) {
IRegion region = document.getLineInformation(i);
if (region.getLength() == 0)
continue;
int lineStart = region.getOffset();
int lineExclusiveEnd = lineStart + region.getLength();
int j = lineExclusiveEnd - 1;
while (j >= lineStart && Character.isWhitespace(document.getChar(j))) --j;
++j;
if (j < lineExclusiveEnd)
multiEdit.addChild(new DeleteEdit(j, lineExclusiveEnd - j));
subMonitor.split(1);
}
return multiEdit.getChildrenSize() <= 0 ? null : multiEdit;
} catch (BadLocationException x) {
// $NON-NLS-1$
throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, "", x));
}
}
use of org.eclipse.text.edits.DeleteEdit in project erlide_eclipse by erlang.
the class ChangesetMaker method createReplaceEdit.
/**
* Creates a <code>ReplaceEdit</code> object from the given parameters and
* the stored input/output strings.
*
* @param addedStart
* @param addedEnd
* @param deletedStart
* @param deletedEnd
* @return
*/
private static TextEdit createReplaceEdit(final int addedStart, final int addedEnd, final int deletedStart, final int deletedEnd) {
final TextEdit result = new MultiTextEdit();
final int addedLength = addedEnd - addedStart + 1;
final int deletedLength = deletedEnd - deletedStart + 1;
final int minLength = Math.min(addedLength, deletedLength);
if (deletedLength < addedLength) {
result.addChild(new InsertEdit(deletedStart + minLength, ChangesetMaker.getString(addedStart + minLength, addedEnd)));
}
result.addChild(new ReplaceEdit(deletedStart, minLength, ChangesetMaker.getString(addedStart, addedStart + minLength - 1)));
if (addedLength < deletedLength) {
result.addChild(new DeleteEdit(deletedStart + minLength, deletedLength - minLength));
}
return result;
}
use of org.eclipse.text.edits.DeleteEdit in project webtools.sourceediting by eclipse.
the class JSPTranslationExtension method getJspEdit.
/**
* Returns a corresponding TextEdit for the JSP file given a TextEdit for
* a Java file.
*
* @param javaEdit
* @return the corresponding JSP edits (not applied to the document yet)
*/
public TextEdit getJspEdit(TextEdit javaEdit) {
if (javaEdit == null)
return null;
List jspEdits = new ArrayList();
int offset = javaEdit.getOffset();
int length = javaEdit.getLength();
if (javaEdit instanceof MultiTextEdit && javaEdit.getChildren().length > 0) {
IRegion r = TextEdit.getCoverage(getAllEdits(javaEdit));
offset = r.getOffset();
length = r.getLength();
}
// get java ranges that will be affected by the edit
Position[] javaPositions = getJavaRanges(offset, length);
// record position data before the change
Position[] jspPositions = new Position[javaPositions.length];
PositionDelta[] deltas = new PositionDelta[javaPositions.length];
for (int i = 0; i < javaPositions.length; i++) {
deltas[i] = new PositionDelta(javaPositions[i].offset, javaPositions[i].length);
// mapping from java <-> jsp (eg. an import statement)
if (!isIndirect(javaPositions[i].offset))
jspPositions[i] = (Position) getJava2JspMap().get(javaPositions[i]);
}
if (DEBUG) {
// $NON-NLS-1$
System.out.println("================================================");
// $NON-NLS-1$
System.out.println("deltas:");
String javaText = getJavaText();
for (int i = 0; i < deltas.length; i++) // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println("pos[" + deltas[i].preOffset + ":" + deltas[i].preLength + "]" + javaText.substring(deltas[i].preOffset, deltas[i].preOffset + deltas[i].preLength));
// $NON-NLS-1$
System.out.println("===============================================");
}
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)
// $NON-NLS-1$
System.out.println("java post format text:\n" + newJavaText);
// 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;
// $NON-NLS-1$
String replaceText = "";
for (int i = 0; i < deltas.length; i++) {
jspPos = jspPositions[i];
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 (// Unwanted TextEdit can lead to MalformedTreeException.See: Bug 321977
!(replaceText.length() == 0 && jspPos.length == 0))
jspEdits.add(new ReplaceEdit(jspPos.offset, jspPos.length, replaceText));
}
if (DEBUG)
debugReplace(deltas, jspPos, replaceText, i);
} else {
// 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);
// $NON-NLS-1$ //$NON-NLS-2$
String importText = replaceText.substring(replaceText.lastIndexOf("import "), replaceText.indexOf(";"));
// evenutally need to check if it's XML-JSP
// $NON-NLS-1$ //$NON-NLS-2$
importText = "<%@page import=\"" + importText + "\" %>\n";
jspEdits.add(new InsertEdit(0, importText));
}
}
}
TextEdit allJspEdits = createMultiTextEdit((TextEdit[]) jspEdits.toArray(new TextEdit[jspEdits.size()]));
// editor)
if (undo != null) {
try {
undo.apply(getJavaDocument());
} catch (MalformedTreeException e) {
Logger.logException(e);
} catch (BadLocationException e) {
Logger.logException(e);
}
}
return allJspEdits;
}
use of org.eclipse.text.edits.DeleteEdit in project eclipse.jdt.ls by eclipse.
the class CorrectPackageDeclarationProposal method addEdits.
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
super.addEdits(doc, root);
ICompilationUnit cu = getCompilationUnit();
IPackageFragment parentPack = (IPackageFragment) cu.getParent();
IPackageDeclaration[] decls = cu.getPackageDeclarations();
if (parentPack.isDefaultPackage() && decls.length > 0) {
for (int i = 0; i < decls.length; i++) {
ISourceRange range = decls[i].getSourceRange();
root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
}
return;
}
if (!parentPack.isDefaultPackage() && decls.length == 0) {
String lineDelim = "\n";
// $NON-NLS-1$
String str = "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim;
root.addChild(new InsertEdit(0, str));
return;
}
root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
Aggregations