use of org.eclipse.text.edits.InsertEdit in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method collapseSpaces.
private int collapseSpaces(TextEdit textEdit, int spaceStartOffset, int availableLineWidth, String whitespaceRun) {
// prefer to use use existing whitespace
int existingWhitespaceOffset = whitespaceRun.indexOf(' ');
if (existingWhitespaceOffset > -1) {
// delete whitespaces before and after existing whitespace
if (existingWhitespaceOffset > 0) {
DeleteEdit deleteEdit = new DeleteEdit(spaceStartOffset, existingWhitespaceOffset);
textEdit.addChild(deleteEdit);
}
if (existingWhitespaceOffset < whitespaceRun.length() - 1) {
int nextOffset = existingWhitespaceOffset + 1;
DeleteEdit deleteEdit = new DeleteEdit(spaceStartOffset + nextOffset, whitespaceRun.length() - nextOffset);
textEdit.addChild(deleteEdit);
}
} else {
// delete all whitespace and insert new one
// collapse whitespace by deleting whitespace
DeleteEdit deleteEdit = new DeleteEdit(spaceStartOffset, whitespaceRun.length());
textEdit.addChild(deleteEdit);
// then insert one space
InsertEdit insertEdit = new InsertEdit(spaceStartOffset, SPACE);
textEdit.addChild(insertEdit);
}
// remember to account for space added
--availableLineWidth;
return availableLineWidth;
}
use of org.eclipse.text.edits.InsertEdit in project flux by eclipse.
the class Utils method editsToJsonArray.
public static JSONArray editsToJsonArray(TextEdit edit) {
final LinkedList<JSONObject> list = new LinkedList<JSONObject>();
edit.accept(new TextEditVisitor() {
@Override
public boolean visit(DeleteEdit delete) {
try {
JSONObject json = new JSONObject();
json.put("offset", delete.getOffset());
json.put("length", delete.getLength());
json.put("text", "");
list.addFirst(json);
} catch (JSONException e) {
e.printStackTrace();
}
return super.visit(delete);
}
@Override
public boolean visit(InsertEdit insert) {
try {
JSONObject json = new JSONObject();
json.put("offset", insert.getOffset());
json.put("length", 0);
json.put("text", insert.getText());
list.addFirst(json);
} catch (JSONException e) {
e.printStackTrace();
}
return super.visit(insert);
}
@Override
public boolean visit(ReplaceEdit replace) {
try {
JSONObject json = new JSONObject();
json.put("offset", replace.getOffset());
json.put("length", replace.getLength());
json.put("text", replace.getText());
list.addFirst(json);
} catch (JSONException e) {
e.printStackTrace();
}
return super.visit(replace);
}
});
return new JSONArray(list);
}
use of org.eclipse.text.edits.InsertEdit in project che by eclipse.
the class RefactoringCorrectionProposal method createTextChange.
@Override
protected TextChange createTextChange() throws CoreException {
init(fRefactoring);
fRefactoringStatus = fRefactoring.checkFinalConditions(new NullProgressMonitor());
if (fRefactoringStatus.hasFatalError()) {
//$NON-NLS-1$
TextFileChange dummyChange = new TextFileChange("fatal error", (IFile) getCompilationUnit().getResource());
//$NON-NLS-1$
dummyChange.setEdit(new InsertEdit(0, ""));
return dummyChange;
}
return (TextChange) fRefactoring.createChange(new NullProgressMonitor());
}
use of org.eclipse.text.edits.InsertEdit in project che by eclipse.
the class CorrectPackageDeclarationProposal method addEdits.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jdt.internal.corext.textmanipulation
* .TextBuffer)
*/
@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 = StubUtility.getLineDelimiterUsed(cu);
//$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()));
}
use of org.eclipse.text.edits.InsertEdit in project AutoRefactor by JnRouvignac.
the class ASTCommentRewriter method replaceLineCommentBeforeJavaElement.
private void replaceLineCommentBeforeJavaElement(List<TextEdit> commentEdits, LineComment lineComment, List<LineComment> lineComments, int i, String source, TreeSet<Integer> lineStarts) {
final int replaceLength = "//".length();
final boolean isFirst = i == 0;
String replacementText;
final SourceLocation indentLoc = getIndentForJavadoc(lineComment, source, lineStarts);
if (isFirst) {
// TODO JNR how to obey configured indentation?
replacementText = "/**" + lineSeparator + indentLoc.substring(source) + " *";
} else {
replacementText = " *";
}
final boolean commentStartsWithSlash = source.charAt(lineComment.getStartPosition() + replaceLength) == '/';
if (commentStartsWithSlash) {
replacementText += " ";
}
commentEdits.add(new ReplaceEdit(lineComment.getStartPosition(), replaceLength, replacementText));
replaceEndsOfBlockCommentFromCommentText(commentEdits, lineComment, source);
final boolean isLast = i == lineComments.size() - 1;
if (isLast) {
// TODO JNR how to obey configured indentation?
final int position = getEndPosition(lineComment);
commentEdits.add(new InsertEdit(position, lineSeparator + indentLoc.substring(source) + " */"));
}
}
Aggregations