use of org.eclipse.text.edits.ReplaceEdit in project flux by eclipse.
the class ReplaceCorrectionProposal method addEdits.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jface.text.IDocument)
*/
@Override
protected void addEdits(IDocument doc, TextEdit rootEdit) throws CoreException {
super.addEdits(doc, rootEdit);
TextEdit edit = new ReplaceEdit(fOffset, fLength, fReplacementString);
rootEdit.addChild(edit);
}
use of org.eclipse.text.edits.ReplaceEdit 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.ReplaceEdit in project AutoRefactor by JnRouvignac.
the class ASTCommentRewriter method addReplacementEdits.
private void addReplacementEdits(final List<TextEdit> commentEdits) {
if (this.replacements.isEmpty()) {
return;
}
for (Pair<Comment, String> pair : this.replacements) {
Comment node = pair.getFirst();
int start = node.getStartPosition();
int length = node.getLength();
commentEdits.add(new ReplaceEdit(start, length, pair.getSecond()));
}
}
use of org.eclipse.text.edits.ReplaceEdit in project AutoRefactor by JnRouvignac.
the class ASTCommentRewriter method addSingleLineCommentToJavadocEdits.
private void addSingleLineCommentToJavadocEdits(final List<TextEdit> commentEdits, final ASTNode nextNode, final List<LineComment> lineComments, final String source, final TreeSet<Integer> lineStarts) {
int nodeStart = nextNode.getStartPosition();
LineComment lineComment = lineComments.get(0);
// TODO JNR how to obey configured indentation?
// TODO JNR how to obey configured line length?
int commentStart = lineComment.getStartPosition();
if (commentStart < nodeStart) {
// Assume comment is situated exactly before target node for javadoc
String spaceAtStart = getSpaceAtStart(source, lineComment);
// $NON-NLS-1$ //$NON-NLS-2$
commentEdits.add(new ReplaceEdit(commentStart, "//".length(), "/**" + spaceAtStart));
// $NON-NLS-1$
commentEdits.add(new InsertEdit(SourceLocation.getEndPosition(lineComment), getSpaceAtEnd(source, lineComment) + "*/"));
replaceEndsOfBlockCommentFromCommentText(commentEdits, lineComment, source);
} else {
// Assume comment is situated exactly after target node for javadoc
// $NON-NLS-1$
StringBuilder newJavadoc = new StringBuilder("/**").append(getSpaceAtStart(source, lineComment));
appendCommentTextReplaceEndsOfBlockComment(newJavadoc, lineComment, source);
SourceLocation indent = getIndent(nextNode, lineStarts);
// $NON-NLS-1$
newJavadoc.append(getSpaceAtEnd(source, lineComment)).append("*/").append(lineSeparator).append(// $NON-NLS-1$
source, indent.getStartPosition(), indent.getEndPosition());
commentEdits.add(new InsertEdit(nodeStart, newJavadoc.toString()));
deleteLineCommentAfterNode(commentEdits, source, lineComment);
}
}
use of org.eclipse.text.edits.ReplaceEdit in project eclipse.jdt.ls by eclipse.
the class DeletePackageFragmentRootChange method doDelete.
@Override
protected Change doDelete(IProgressMonitor pm) throws CoreException {
if (!confirmDeleteIfReferenced()) {
return new NullChange();
}
int resourceUpdateFlags = IResource.KEEP_HISTORY;
int jCoreUpdateFlags = IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH | IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH;
// $NON-NLS-1$
pm.beginTask("", 2);
IPackageFragmentRoot root = getRoot();
IResource rootResource = root.getResource();
CompositeChange result = new CompositeChange(getName());
IJavaProject[] referencingProjects = JavaElementUtil.getReferencingProjects(root);
HashMap<IFile, String> classpathFilesContents = new HashMap<>();
for (int i = 0; i < referencingProjects.length; i++) {
IJavaProject javaProject = referencingProjects[i];
// $NON-NLS-1$
IFile classpathFile = javaProject.getProject().getFile(".classpath");
if (classpathFile.exists()) {
classpathFilesContents.put(classpathFile, getFileContents(classpathFile));
}
}
root.delete(resourceUpdateFlags, jCoreUpdateFlags, new SubProgressMonitor(pm, 1));
// rootDescription.recordStateFromHistory(rootResource, new SubProgressMonitor(pm, 1));
for (Iterator<Entry<IFile, String>> iterator = classpathFilesContents.entrySet().iterator(); iterator.hasNext(); ) {
Entry<IFile, String> entry = iterator.next();
IFile file = entry.getKey();
String contents = entry.getValue();
// Restore time stamps? This should probably be some sort of UndoTextFileChange.
TextFileChange classpathUndo = new TextFileChange(Messages.format(RefactoringCoreMessages.DeletePackageFragmentRootChange_restore_file, BasicElementLabels.getPathLabel(file.getFullPath(), true)), file);
classpathUndo.setEdit(new ReplaceEdit(0, getFileLength(file), contents));
result.add(classpathUndo);
}
// result.add(new UndoDeleteResourceChange(rootDescription));
pm.done();
return result;
}
Aggregations