use of org.eclipse.text.edits.TextEdit in project che by eclipse.
the class TextEditUtil method merge.
private static void merge(TextEdit edit1, TextEdit edit2, MultiTextEdit result) {
if (edit1 instanceof MultiTextEdit && edit2 instanceof MultiTextEdit) {
MultiTextEdit multiTextEdit1 = (MultiTextEdit) edit1;
if (!multiTextEdit1.hasChildren()) {
result.addChild(edit2);
return;
}
MultiTextEdit multiTextEdit2 = (MultiTextEdit) edit2;
if (!multiTextEdit2.hasChildren()) {
result.addChild(edit1);
return;
}
TextEdit[] children1 = multiTextEdit1.getChildren();
TextEdit[] children2 = multiTextEdit2.getChildren();
int i1 = 0;
int i2 = 0;
while (i1 < children1.length && i2 < children2.length) {
while (i1 < children1.length && children1[i1].getExclusiveEnd() < children2[i2].getOffset()) {
edit1.removeChild(0);
result.addChild(children1[i1]);
i1++;
}
if (i1 >= children1.length)
break;
while (i2 < children2.length && children2[i2].getExclusiveEnd() < children1[i1].getOffset()) {
edit2.removeChild(0);
result.addChild(children2[i2]);
i2++;
}
if (i2 >= children2.length)
break;
if (children1[i1].getExclusiveEnd() < children2[i2].getOffset())
continue;
edit1.removeChild(0);
edit2.removeChild(0);
merge(children1[i1], children2[i2], result);
i1++;
i2++;
}
while (i1 < children1.length) {
edit1.removeChild(0);
result.addChild(children1[i1]);
i1++;
}
while (i2 < children2.length) {
edit2.removeChild(0);
result.addChild(children2[i2]);
i2++;
}
} else if (edit1 instanceof MultiTextEdit) {
TextEdit[] children = edit1.getChildren();
int i = 0;
while (children[i].getExclusiveEnd() < edit2.getOffset()) {
edit1.removeChild(0);
result.addChild(children[i]);
i++;
if (i >= children.length) {
result.addChild(edit2);
return;
}
}
edit1.removeChild(0);
merge(children[i], edit2, result);
i++;
while (i < children.length) {
edit1.removeChild(0);
result.addChild(children[i]);
i++;
}
} else if (edit2 instanceof MultiTextEdit) {
TextEdit[] children = edit2.getChildren();
int i = 0;
while (children[i].getExclusiveEnd() < edit1.getOffset()) {
edit2.removeChild(0);
result.addChild(children[i]);
i++;
if (i >= children.length) {
result.addChild(edit1);
return;
}
}
edit2.removeChild(0);
merge(edit1, children[i], result);
i++;
while (i < children.length) {
edit2.removeChild(0);
result.addChild(children[i]);
i++;
}
} else {
if (edit1.getExclusiveEnd() < edit2.getOffset()) {
result.addChild(edit1);
result.addChild(edit2);
} else {
result.addChild(edit2);
result.addChild(edit1);
}
}
}
use of org.eclipse.text.edits.TextEdit in project che 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.TextEdit in project che by eclipse.
the class ExtractMethodRefactoring method createChange.
/* (non-Javadoc)
* Method declared in IRefactoring
*/
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
if (fMethodName == null)
return null;
//$NON-NLS-1$
pm.beginTask("", 2);
try {
fAnalyzer.aboutToCreateChange();
BodyDeclaration declaration = fAnalyzer.getEnclosingBodyDeclaration();
fRewriter = ASTRewrite.create(declaration.getAST());
final CompilationUnitChange result = new CompilationUnitChange(RefactoringCoreMessages.ExtractMethodRefactoring_change_name, fCUnit);
result.setSaveMode(TextFileChange.KEEP_SAVE_STATE);
result.setDescriptor(new RefactoringChangeDescriptor(getRefactoringDescriptor()));
MultiTextEdit root = new MultiTextEdit();
result.setEdit(root);
ASTNode[] selectedNodes = fAnalyzer.getSelectedNodes();
fRewriter.setTargetSourceRangeComputer(new SelectionAwareSourceRangeComputer(selectedNodes, fCUnit.getBuffer(), fSelectionStart, fSelectionLength));
TextEditGroup substituteDesc = new TextEditGroup(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_substitute_with_call, BasicElementLabels.getJavaElementName(fMethodName)));
result.addTextEditGroup(substituteDesc);
MethodDeclaration mm = createNewMethod(selectedNodes, fCUnit.findRecommendedLineSeparator(), substituteDesc);
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
typeGroup.addPosition(fRewriter.track(mm.getReturnType2()), false);
ITypeBinding typeBinding = fAnalyzer.getReturnTypeBinding();
if (typeBinding != null) {
ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(fAST, typeBinding);
for (int i = 0; i < relaxingTypes.length; i++) {
typeGroup.addProposal(relaxingTypes[i], fCUnit, relaxingTypes.length - i);
}
}
LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
nameGroup.addPosition(fRewriter.track(mm.getName()), false);
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, fRewriter, mm.modifiers(), false);
}
TextEditGroup insertDesc = new TextEditGroup(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_add_method, BasicElementLabels.getJavaElementName(fMethodName)));
result.addTextEditGroup(insertDesc);
if (fDestination == ASTResolving.findParentType(declaration.getParent())) {
ChildListPropertyDescriptor desc = (ChildListPropertyDescriptor) declaration.getLocationInParent();
ListRewrite container = fRewriter.getListRewrite(declaration.getParent(), desc);
container.insertAfter(mm, declaration, insertDesc);
} else {
BodyDeclarationRewrite container = BodyDeclarationRewrite.create(fRewriter, fDestination);
container.insert(mm, insertDesc);
}
replaceDuplicates(result, mm.getModifiers());
replaceBranches(result);
if (fImportRewriter.hasRecordedChanges()) {
TextEdit edit = fImportRewriter.rewriteImports(null);
root.addChild(edit);
result.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.ExtractMethodRefactoring_organize_imports, new TextEdit[] { edit }));
}
root.addChild(fRewriter.rewriteAST());
return result;
} finally {
pm.done();
}
}
use of org.eclipse.text.edits.TextEdit in project che by eclipse.
the class InlineMethodRefactoring method checkFinalConditions.
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
//$NON-NLS-1$
pm.beginTask("", 20);
fChangeManager = new TextChangeManager();
RefactoringStatus result = new RefactoringStatus();
fSourceProvider.initialize();
fTargetProvider.initialize();
pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_searching);
RefactoringStatus searchStatus = new RefactoringStatus();
String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getJavaElementName(fSourceProvider.getMethodName()));
ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
ICompilationUnit[] units = fTargetProvider.getAffectedCompilationUnits(searchStatus, binaryRefs, new SubProgressMonitor(pm, 1));
binaryRefs.addErrorIfNecessary(searchStatus);
if (searchStatus.hasFatalError()) {
result.merge(searchStatus);
return result;
}
IFile[] filesToBeModified = getFilesToBeModified(units);
result.merge(Checks.validateModifiesFiles(filesToBeModified, getValidationContext()));
if (result.hasFatalError())
return result;
result.merge(ResourceChangeChecker.checkFilesToBeChanged(filesToBeModified, new SubProgressMonitor(pm, 1)));
checkOverridden(result, new SubProgressMonitor(pm, 4));
IProgressMonitor sub = new SubProgressMonitor(pm, 15);
//$NON-NLS-1$
sub.beginTask("", units.length * 3);
for (int c = 0; c < units.length; c++) {
ICompilationUnit unit = units[c];
sub.subTask(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_processing, BasicElementLabels.getFileName(unit)));
CallInliner inliner = null;
try {
boolean added = false;
MultiTextEdit root = new MultiTextEdit();
CompilationUnitChange change = (CompilationUnitChange) fChangeManager.get(unit);
change.setEdit(root);
BodyDeclaration[] bodies = fTargetProvider.getAffectedBodyDeclarations(unit, new SubProgressMonitor(pm, 1));
if (bodies.length == 0)
continue;
inliner = new CallInliner(unit, (CompilationUnit) bodies[0].getRoot(), fSourceProvider);
for (int b = 0; b < bodies.length; b++) {
BodyDeclaration body = bodies[b];
inliner.initialize(body);
RefactoringStatus nestedInvocations = new RefactoringStatus();
ASTNode[] invocations = removeNestedCalls(nestedInvocations, unit, fTargetProvider.getInvocations(body, new SubProgressMonitor(sub, 2)));
for (int i = 0; i < invocations.length; i++) {
ASTNode invocation = invocations[i];
result.merge(inliner.initialize(invocation, fTargetProvider.getStatusSeverity()));
if (result.hasFatalError())
break;
if (result.getSeverity() < fTargetProvider.getStatusSeverity()) {
added = true;
TextEditGroup group = new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_inline);
change.addTextEditGroup(group);
result.merge(inliner.perform(group));
} else {
fDeleteSource = false;
}
}
// to generate the modifications.
if (!nestedInvocations.isOK()) {
result.merge(nestedInvocations);
fDeleteSource = false;
}
}
if (!added) {
fChangeManager.remove(unit);
} else {
root.addChild(inliner.getModifications());
ImportRewrite rewrite = inliner.getImportEdit();
if (rewrite.hasRecordedChanges()) {
TextEdit edit = rewrite.rewriteImports(null);
if (edit instanceof MultiTextEdit ? ((MultiTextEdit) edit).getChildrenSize() > 0 : true) {
root.addChild(edit);
change.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_import, new TextEdit[] { edit }));
}
}
}
} finally {
if (inliner != null)
inliner.dispose();
}
sub.worked(1);
if (sub.isCanceled())
throw new OperationCanceledException();
}
result.merge(searchStatus);
sub.done();
pm.done();
return result;
}
use of org.eclipse.text.edits.TextEdit in project che by eclipse.
the class TextChange method createTextEditProcessor.
private TextEditProcessor createTextEditProcessor(IDocument document, int flags, TextEditBasedChangeGroup[] changes) {
if (fEdit == null)
return new TextEditProcessor(document, new MultiTextEdit(0, 0), flags);
List includes = new ArrayList(0);
for (int c = 0; c < changes.length; c++) {
TextEditBasedChangeGroup change = changes[c];
Assert.isTrue(change.getTextEditChange() == this);
if (change.isEnabled()) {
includes.addAll(Arrays.asList(change.getTextEditGroup().getTextEdits()));
}
}
fCopier = new TextEditCopier(fEdit);
TextEdit copiedEdit = fCopier.perform();
boolean keep = getKeepPreviewEdits();
if (keep)
flags = flags | TextEdit.UPDATE_REGIONS;
LocalTextEditProcessor result = new LocalTextEditProcessor(document, copiedEdit, flags);
result.setIncludes(mapEdits((TextEdit[]) includes.toArray(new TextEdit[includes.size()]), fCopier));
if (!keep)
fCopier = null;
return result;
}
Aggregations