use of org.eclipse.text.edits.DeleteEdit in project AutoRefactor by JnRouvignac.
the class ASTCommentRewriter method deleteLineCommentAfterNode.
private void deleteLineCommentAfterNode(List<TextEdit> commentEdits, String source, LineComment lineComment) {
final int commentStart = lineComment.getStartPosition();
final int commentLength = lineComment.getLength();
final int nbWhiteSpaces = nbTrailingSpaces(source, commentStart);
commentEdits.add(new DeleteEdit(commentStart - nbWhiteSpaces, nbWhiteSpaces + commentLength));
}
use of org.eclipse.text.edits.DeleteEdit in project eclipse.jdt.ls by eclipse.
the class DocumentLifeCycleHandler method handleChanged.
public void handleChanged(DidChangeTextDocumentParams params) {
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
if (unit == null || !unit.isWorkingCopy() || params.getContentChanges().isEmpty()) {
return;
}
try {
if (unit.equals(sharedASTProvider.getActiveJavaElement())) {
sharedASTProvider.disposeAST();
}
List<TextDocumentContentChangeEvent> contentChanges = params.getContentChanges();
for (TextDocumentContentChangeEvent changeEvent : contentChanges) {
Range range = changeEvent.getRange();
int length;
if (range != null) {
length = changeEvent.getRangeLength().intValue();
} else {
// range is optional and if not given, the whole file content is replaced
length = unit.getSource().length();
range = JDTUtils.toRange(unit, 0, length);
}
int startOffset = JsonRpcHelpers.toOffset(unit.getBuffer(), range.getStart().getLine(), range.getStart().getCharacter());
TextEdit edit = null;
String text = changeEvent.getText();
if (length == 0) {
edit = new InsertEdit(startOffset, text);
} else if (text.isEmpty()) {
edit = new DeleteEdit(startOffset, length);
} else {
edit = new ReplaceEdit(startOffset, length, text);
}
IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
edit.apply(document, TextEdit.NONE);
}
triggerValidation(unit);
} catch (JavaModelException | MalformedTreeException | BadLocationException e) {
JavaLanguageServerPlugin.logException("Error while handling document change", e);
}
}
use of org.eclipse.text.edits.DeleteEdit in project titan.EclipsePlug-ins by eclipse.
the class ChangeCreator method createTextEdit.
private TextEdit[] createTextEdit(final IFile toVisit, final String fileContent, final Edit e, final Map<Edit, InsertEdit> editsDone) {
// check for multi-declaration statement
if (e.declSt.isMultiDeclaration()) {
final Location cutLoc = calculateMultiDeclarationCutLoc(fileContent, e.declSt);
final String moveContent = calculateMultiDeclarationMoveContent(fileContent, e.declSt);
// remove stmt from multi-declaration
e.declSt.removeFromMultiDeclaration();
// create remove edit
final int cutLen = cutLoc.getEndOffset() - cutLoc.getOffset();
final TextEdit cut = new DeleteEdit(cutLoc.getOffset(), cutLen);
// create insert edit
InsertEdit insert = null;
if (!e.isRemoveEdit()) {
// update insert location if the insertionPoint stmt was moved
int insertOffset = ((ILocateableNode) e.insertionPoint.getAstNode()).getLocation().getOffset();
for (Map.Entry<Edit, InsertEdit> ed : editsDone.entrySet()) {
if (ed.getKey().declSt.equals(e.insertionPoint)) {
insertOffset = ed.getValue().getOffset();
break;
}
}
//
insertOffset = findLineBeginningOffset(fileContent, insertOffset);
insert = new InsertEdit(insertOffset, moveContent);
editsDone.put(e, insert);
}
if (insert != null) {
return new TextEdit[] { insert, cut };
}
return new TextEdit[] { cut };
} else {
//
final Location cutLoc = findStatementLocation(fileContent, ((ILocateableNode) e.declSt.getAstNode()).getLocation(), true);
InsertEdit insert = null;
if (!e.isRemoveEdit()) {
final Location copyLoc = findStatementLocation(fileContent, ((ILocateableNode) e.declSt.getAstNode()).getLocation(), false);
// update insert location if the insertionPoint stmt was moved
final Location insPLoc = ((ILocateableNode) e.insertionPoint.getAstNode()).getLocation();
int insertOffset = insPLoc.getOffset();
for (Map.Entry<Edit, InsertEdit> ed : editsDone.entrySet()) {
if (ed.getKey().declSt.equals(e.insertionPoint)) {
insertOffset = ed.getValue().getOffset();
break;
}
}
//
final int prefixStartOffset = findLineBeginningOffset(fileContent, insertOffset);
final String insertText = fileContent.substring(copyLoc.getOffset(), copyLoc.getEndOffset()) + "\n";
String insertPrefix = fileContent.substring(prefixStartOffset, insertOffset);
// if prefix is not whitespace only, do not use the prefix
if (!insertPrefix.trim().equals("")) {
insertPrefix = "";
}
insert = new InsertEdit(prefixStartOffset, insertPrefix + insertText);
editsDone.put(e, insert);
}
final int cutLen = cutLoc.getEndOffset() - cutLoc.getOffset();
final TextEdit cut = new DeleteEdit(cutLoc.getOffset(), cutLen);
// System.err.println("DeleteEdit: " + fileContent.substring(cutLoc.getOffset(), cutLoc.getEndOffset()));
if (insert != null) {
return new TextEdit[] { insert, cut };
}
return new TextEdit[] { cut };
}
}
use of org.eclipse.text.edits.DeleteEdit in project titan.EclipsePlug-ins by eclipse.
the class ChangeCreator method createFileChange.
/**
* Creates the {@link #change} object, which contains all the inserted and edited texts
* in the selected resources.
*/
private Change createFileChange(final IFile toVisit) {
if (toVisit == null) {
return null;
}
final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(toVisit.getProject());
final Module module = sourceParser.containedModule(toVisit);
if (module == null) {
return null;
}
//
// collect functions
Set<Definition> funcs;
final FunctionCollector vis = new FunctionCollector();
if (defSelection == null) {
module.accept(vis);
funcs = vis.getResult();
} else {
if (defSelection instanceof Def_Function || defSelection instanceof Def_Testcase) {
// TODO any other possibilities for the type of 'defSelection'?
funcs = new HashSet<Definition>();
funcs.add(defSelection);
} else {
ErrorReporter.logError("Variable scope reduction called for " + defSelection.getIdentifier().getDisplayName() + ", but it is only supported for functions and testcases. ");
return null;
}
}
// create edits
final List<Edit> allEdits = new ArrayList<Edit>();
for (Definition def : funcs) {
final List<Edit> edits = analyzeFunction(def);
if (edits == null) {
continue;
}
allEdits.addAll(edits);
}
if (allEdits.isEmpty()) {
return null;
}
final String fileContents = loadFileContent(toVisit);
// create text edits
//
final TextFileChange tfc = new TextFileChange(toVisit.getName(), toVisit);
final MultiTextEdit rootEdit = new MultiTextEdit();
tfc.setEdit(rootEdit);
// TODO this is an O(n^2) algorithm
// merge overlapping DeleteEdits
// used, when removing all parts of a multi-declaration statement:
// the DeleteEdit for removing the last part covers all the DeleteEdits for the other parts
// WARNING merging edits might make debugging more difficult, since the overlapping edit errors are avoided
final List<TextEdit> allTes = new LinkedList<TextEdit>();
// collect processed (insert) edits with their created insert edit
final Map<Edit, InsertEdit> editsDone = new HashMap<Edit, InsertEdit>();
for (Edit e : allEdits) {
final TextEdit[] tes = createTextEdit(toVisit, fileContents, e, editsDone);
for (TextEdit te : tes) {
if (!(te instanceof DeleteEdit)) {
allTes.add(te);
// System.err.println("$ nonde added: " + te.getOffset() + "-" + te.getExclusiveEnd());
continue;
}
DeleteEdit dte = (DeleteEdit) te;
final ListIterator<TextEdit> it = allTes.listIterator();
while (it.hasNext()) {
final TextEdit currTe = it.next();
if (!(currTe instanceof DeleteEdit)) {
continue;
}
final DeleteEdit currDte = (DeleteEdit) currTe;
// if the new edit (dte) overlaps currDte, merge them
if (doesDeleteEditsOverlap(dte, currDte)) {
// System.err.println("$ de removed: " + currDte.getOffset() + "-" + currDte.getExclusiveEnd());
it.remove();
dte = mergeDeleteEdits(dte, currDte);
// System.err.println("$ merged des: " + dte.getOffset() + "-" + dte.getExclusiveEnd());
}
}
// System.err.println("$ de added: " + dte.getOffset() + "-" + dte.getExclusiveEnd());
allTes.add(dte);
}
}
Collections.reverse(allTes);
for (TextEdit te : allTes) {
rootEdit.addChild(te);
}
return tfc;
}
use of org.eclipse.text.edits.DeleteEdit in project webtools.sourceediting by eclipse.
the class DocumentChangeListenerToTextEdit method documentChanged.
public void documentChanged(DocumentEvent event) {
int length = event.getLength();
int offset = event.getOffset();
String text = event.getText();
if (length < 0) {
return;
}
if (length == 0) {
/* inserting text operation */
InsertEdit edit = new InsertEdit(offset, text);
textEdit.addChild(edit);
} else if (text == null || text.equals("")) {
// $NON-NLS-1$
/* delete operation */
DeleteEdit edit = new DeleteEdit(offset, length);
textEdit.addChild(edit);
} else if (length > 0) {
/* replace text operation */
ReplaceEdit edit = new ReplaceEdit(offset, length, text);
textEdit.addChild(edit);
}
}
Aggregations