use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method createAddImportChange.
static CompilationUnitChange createAddImportChange(ICompilationUnit cu, Name name, String fullyQualifiedName) throws CoreException {
String[] args = { BasicElementLabels.getJavaElementName(Signature.getSimpleName(fullyQualifiedName)), BasicElementLabels.getJavaElementName(Signature.getQualifier(fullyQualifiedName)) };
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importtype_description, args);
CompilationUnitChange cuChange = new CompilationUnitChange(label, cu);
ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite((CompilationUnit) name.getRoot(), true);
importRewrite.addImport(fullyQualifiedName);
cuChange.setEdit(importRewrite.rewriteImports(null));
return cuChange;
}
use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.
the class SurroundWithTryCatchRefactoring method createChange.
/* non Java-doc
* @see IRefactoring#createChange(IProgressMonitor)
*/
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
// $NON-NLS-1$
final String NN = "";
if (pm == null) {
pm = new NullProgressMonitor();
}
pm.beginTask(NN, 2);
try {
final CompilationUnitChange result = new CompilationUnitChange(getName(), fCUnit);
if (fLeaveDirty) {
result.setSaveMode(TextFileChange.LEAVE_DIRTY);
}
MultiTextEdit root = new MultiTextEdit();
result.setEdit(root);
fRewriter = ASTRewrite.create(fAnalyzer.getEnclosingBodyDeclaration().getAST());
fRewriter.setTargetSourceRangeComputer(new SelectionAwareSourceRangeComputer(fAnalyzer.getSelectedNodes(), fCUnit.getBuffer(), fSelection.getOffset(), fSelection.getLength()));
fImportRewrite = CodeStyleConfiguration.createImportRewrite(fRootNode, true);
fLinkedProposalModel = new LinkedProposalModelCore();
fScope = CodeScopeBuilder.perform(fAnalyzer.getEnclosingBodyDeclaration(), fSelection).findScope(fSelection.getOffset(), fSelection.getLength());
fScope.setCursor(fSelection.getOffset());
fSelectedNodes = fAnalyzer.getSelectedNodes();
createTryCatchStatement(fCUnit.getBuffer(), fCUnit.findRecommendedLineSeparator());
if (fImportRewrite.hasRecordedChanges()) {
TextEdit edit = fImportRewrite.rewriteImports(null);
root.addChild(edit);
result.addTextEditGroup(new TextEditGroup(NN, new TextEdit[] { edit }));
}
TextEdit change = fRewriter.rewriteAST();
root.addChild(change);
result.addTextEditGroup(new TextEditGroup(NN, new TextEdit[] { change }));
return result;
} finally {
pm.done();
}
}
use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.
the class RenameFieldProcessor method addDelegates.
private RefactoringStatus addDelegates() throws JavaModelException, CoreException {
RefactoringStatus status = new RefactoringStatus();
CompilationUnitRewrite rewrite = new CompilationUnitRewrite(fField.getCompilationUnit());
rewrite.setResolveBindings(true);
// add delegate for the field
if (RefactoringAvailabilityTester.isDelegateCreationAvailable(fField)) {
FieldDeclaration fieldDeclaration = ASTNodeSearchUtil.getFieldDeclarationNode(fField, rewrite.getRoot());
if (fieldDeclaration.fragments().size() > 1) {
status.addWarning(Messages.format(RefactoringCoreMessages.DelegateCreator_cannot_create_field_delegate_more_than_one_fragment, BasicElementLabels.getJavaElementName(fField.getElementName())), JavaStatusContext.create(fField));
} else if (((VariableDeclarationFragment) fieldDeclaration.fragments().get(0)).getInitializer() == null) {
status.addWarning(Messages.format(RefactoringCoreMessages.DelegateCreator_cannot_create_field_delegate_no_initializer, BasicElementLabels.getJavaElementName(fField.getElementName())), JavaStatusContext.create(fField));
} else {
DelegateFieldCreator creator = new DelegateFieldCreator();
creator.setDeclareDeprecated(fDelegateDeprecation);
creator.setDeclaration(fieldDeclaration);
creator.setNewElementName(getNewElementName());
creator.setSourceRewrite(rewrite);
creator.prepareDelegate();
creator.createEdit();
}
}
// there may be getters even if the field is static final
if (getGetter() != null && fRenameGetter) {
addMethodDelegate(getGetter(), getNewGetterName(), rewrite);
}
if (getSetter() != null && fRenameSetter) {
addMethodDelegate(getSetter(), getNewSetterName(), rewrite);
}
final CompilationUnitChange change = rewrite.createChange(true);
if (change != null) {
change.setKeepPreviewEdits(true);
fChangeManager.manage(fField.getCompilationUnit(), change);
}
return status;
}
use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project xtext-eclipse by eclipse.
the class TextChangeCombiner method createTextChange.
protected TextChange createTextChange(Object key, String textType) {
TextChange change = null;
if (key instanceof ICompilationUnit)
change = new CompilationUnitChange("Combined CompilationUnitChange", (ICompilationUnit) key);
else if (key instanceof IFile)
change = new TextFileChange("Combined TextFileChange", (IFile) key);
else if (key instanceof ITextEditor)
change = new EditorDocumentChange("Combined DocumentChange", (ITextEditor) key, false);
else
LOG.error("Error creating change for " + key.getClass().getName());
if (change != null) {
MultiTextEdit edits = new MultiTextEdit();
change.setEdit(edits);
change.setTextType(textType);
}
return change;
}
use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project che by eclipse.
the class TextEditFix method createChange.
/**
* {@inheritDoc}
*/
public CompilationUnitChange createChange(IProgressMonitor progressMonitor) throws CoreException {
String label = fChangeDescription;
CompilationUnitChange result = new CompilationUnitChange(label, fUnit);
result.setEdit(fEdit);
result.addTextEditGroup(new CategorizedTextEditGroup(label, new GroupCategorySet(new GroupCategory(label, label, label))));
return result;
}
Aggregations