use of org.eclipse.text.edits.MultiTextEdit in project eclipse.platform.text by eclipse.
the class LinkedPositionGroup method handleEvent.
/**
* Creates an edition of a document change that will forward any
* modification in one position to all linked siblings. The return value is
* a map from <code>IDocument</code> to <code>TextEdit</code>.
*
* @param event the document event to check
* @return a map of edits, grouped by edited document, or <code>null</code>
* if there are no edits
*/
Map<IDocument, TextEdit> handleEvent(DocumentEvent event) {
if (fLastPosition != null) {
Map<IDocument, List<ReplaceEdit>> map = new HashMap<>();
int relativeOffset = event.getOffset() - fLastRegion.getOffset();
if (relativeOffset < 0) {
relativeOffset = 0;
}
int eventEnd = event.getOffset() + event.getLength();
int lastEnd = fLastRegion.getOffset() + fLastRegion.getLength();
int length;
if (eventEnd > lastEnd)
length = lastEnd - relativeOffset - fLastRegion.getOffset();
else
length = eventEnd - relativeOffset - fLastRegion.getOffset();
String text = event.getText();
if (text == null)
// $NON-NLS-1$
text = "";
for (LinkedPosition p : fPositions) {
if (p == fLastPosition || p.isDeleted())
// don't re-update the origin of the change
continue;
List<ReplaceEdit> edits = map.get(p.getDocument());
if (edits == null) {
edits = new ArrayList<>();
map.put(p.getDocument(), edits);
}
if (fMustEnforceEqualContents) {
try {
edits.add(new ReplaceEdit(p.getOffset(), p.getLength(), fLastPosition.getContent()));
} catch (BadLocationException e) {
// should not happen
throw new RuntimeException(e);
}
} else {
edits.add(new ReplaceEdit(p.getOffset() + relativeOffset, length, text));
}
}
fMustEnforceEqualContents = false;
try {
Map<IDocument, TextEdit> result = new HashMap<>();
for (IDocument d : map.keySet()) {
TextEdit edit = new MultiTextEdit(0, d.getLength());
edit.addChildren(map.get(d).toArray(new TextEdit[0]));
result.put(d, edit);
}
return result;
} catch (MalformedTreeException x) {
// manager
return null;
}
}
return null;
}
use of org.eclipse.text.edits.MultiTextEdit in project AutoRefactor by JnRouvignac.
the class ASTCommentRewriter method overlaps.
private boolean overlaps(TextEdit edits, TextEdit edit2) {
final SourceLocation range = toSourceLoc(edit2);
final AtomicBoolean overlaps = new AtomicBoolean();
edits.accept(new TextEditVisitor() {
@Override
public boolean visit(MultiTextEdit edit) {
// move on there is nothing to check here
return VISIT_SUBTREE;
}
@Override
public boolean visitNode(TextEdit edit) {
if (!overlaps.get()) {
overlaps.set(range.overlapsWith(toSourceLoc(edit)));
}
return !overlaps.get();
}
});
return overlaps.get();
}
use of org.eclipse.text.edits.MultiTextEdit in project che by eclipse.
the class UnimplementedCodeFix method createAddUnimplementedMethodsFix.
public static IProposableFix createAddUnimplementedMethodsFix(final CompilationUnit root, IProblemLocation problem) {
ASTNode typeNode = getSelectedTypeNode(root, problem);
if (typeNode == null)
return null;
if (isTypeBindingNull(typeNode))
return null;
AddUnimplementedMethodsOperation operation = new AddUnimplementedMethodsOperation(typeNode);
if (operation.getMethodsToImplement().length > 0) {
return new UnimplementedCodeFix(CorrectionMessages.UnimplementedMethodsCorrectionProposal_description, root, new CompilationUnitRewriteOperation[] { operation });
} else {
return new IProposableFix() {
public CompilationUnitChange createChange(IProgressMonitor progressMonitor) throws CoreException {
CompilationUnitChange change = new CompilationUnitChange(CorrectionMessages.UnimplementedMethodsCorrectionProposal_description, (ICompilationUnit) root.getJavaElement()) {
@Override
public Change perform(IProgressMonitor pm) throws CoreException {
//TODO
return new NullChange();
}
};
change.setEdit(new MultiTextEdit());
return change;
}
public String getAdditionalProposalInfo() {
return new String();
}
public String getDisplayString() {
return CorrectionMessages.UnimplementedMethodsCorrectionProposal_description;
}
public IStatus getStatus() {
return new Status(IStatus.ERROR, JavaPlugin.ID_PLUGIN, CorrectionMessages.UnimplementedCodeFix_DependenciesStatusMessage);
}
};
}
}
use of org.eclipse.text.edits.MultiTextEdit in project che by eclipse.
the class TextChangeCompatibility method addTextEdit.
public static void addTextEdit(TextChange change, String name, TextEdit edit, GroupCategorySet groupCategories) throws MalformedTreeException {
Assert.isNotNull(change);
Assert.isNotNull(name);
Assert.isNotNull(edit);
TextEdit root = change.getEdit();
if (root == null) {
root = new MultiTextEdit();
change.setEdit(root);
}
insert(root, edit);
change.addTextEditChangeGroup(new TextEditChangeGroup(change, new CategorizedTextEditGroup(name, edit, groupCategories)));
}
use of org.eclipse.text.edits.MultiTextEdit in project che by eclipse.
the class RenameLocalVariableProcessor method createEdits.
private void createEdits() {
TextEdit declarationEdit = createRenameEdit(fTempDeclarationNode.getName().getStartPosition());
TextEdit[] allRenameEdits = getAllRenameEdits(declarationEdit);
TextEdit[] allUnparentedRenameEdits = new TextEdit[allRenameEdits.length];
TextEdit unparentedDeclarationEdit = null;
fChange = new CompilationUnitChange(RefactoringCoreMessages.RenameTempRefactoring_rename, fCu);
MultiTextEdit rootEdit = new MultiTextEdit();
fChange.setEdit(rootEdit);
fChange.setKeepPreviewEdits(true);
for (int i = 0; i < allRenameEdits.length; i++) {
if (fIsComposite) {
// Add a copy of the text edit (text edit may only have one
// parent) to keep problem reporting code clean
TextChangeCompatibility.addTextEdit(fChangeManager.get(fCu), RefactoringCoreMessages.RenameTempRefactoring_changeName, allRenameEdits[i].copy(), fCategorySet);
// Add a separate copy for problem reporting
allUnparentedRenameEdits[i] = allRenameEdits[i].copy();
if (allRenameEdits[i].equals(declarationEdit))
unparentedDeclarationEdit = allUnparentedRenameEdits[i];
}
rootEdit.addChild(allRenameEdits[i]);
fChange.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.RenameTempRefactoring_changeName, allRenameEdits[i]));
}
// store information for analysis
if (fIsComposite) {
fLocalAnalyzePackage = new RenameAnalyzeUtil.LocalAnalyzePackage(unparentedDeclarationEdit, allUnparentedRenameEdits);
} else
fLocalAnalyzePackage = new RenameAnalyzeUtil.LocalAnalyzePackage(declarationEdit, allRenameEdits);
}
Aggregations