use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project che 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 = StubUtility.createImportRewrite(fRootNode, true);
fLinkedProposalModel = new LinkedProposalModel();
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 che by eclipse.
the class TextChangeManager method get.
/**
* Returns the <code>TextChange</code> associated with the given compilation unit.
* If the manager does not already manage an association it creates a one.
*
* @param cu the compilation unit for which the text buffer change is requested
* @return the text change associated with the given compilation unit.
*/
public TextChange get(ICompilationUnit cu) {
TextChange result = fMap.get(cu);
if (result == null) {
result = new CompilationUnitChange(cu.getElementName(), cu);
result.setKeepPreviewEdits(fKeepExecutedTextEdits);
fMap.put(cu, result);
}
return result;
}
use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project flux by eclipse.
the class CUCorrectionProposal method createTextChange.
protected TextChange createTextChange() throws CoreException {
ICompilationUnit cu = getCompilationUnit();
String name = getName();
TextChange change;
if (!cu.getResource().exists()) {
String source;
try {
source = cu.getSource();
} catch (JavaModelException e) {
JavaPlugin.log(e);
// empty
source = new String();
}
Document document = new Document(source);
document.setInitialLineDelimiter(StubUtility.getLineDelimiterUsed(cu));
change = new DocumentChange(name, document);
} else {
CompilationUnitChange cuChange = new CompilationUnitChange(name, cu);
cuChange.setSaveMode(TextFileChange.LEAVE_DIRTY);
change = cuChange;
}
TextEdit rootEdit = new MultiTextEdit();
change.setEdit(rootEdit);
// initialize text change
IDocument document = change.getCurrentDocument(new NullProgressMonitor());
addEdits(document, rootEdit);
return change;
}
use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project che by eclipse.
the class UnresolvedElementsSubProcessor method createAddImportChange.
// private static void addCopyAnnotationsJarProposal(final ICompilationUnit cu, final Name name, final String fullyQualifiedName, Bundle annotationsBundle, Collection<ICommandAccess> proposals) {
// final IJavaProject javaProject= cu.getJavaProject();
// final File bundleFile;
// try {
// bundleFile= FileLocator.getBundleFile(annotationsBundle);
// } catch (IOException e) {
// JavaPlugin.log(e);
// return;
// }
// if (!bundleFile.isFile() || !bundleFile.canRead())
// return; // we only support a JAR'd bundle, so this won't work in the runtime if you have org.eclipse.jdt.annotation in source.
//
// final String changeName= CorrectionMessages.UnresolvedElementsSubProcessor_copy_annotation_jar_description;
// ChangeCorrectionProposal proposal= new ChangeCorrectionProposal(changeName, null, IProposalRelevance.COPY_ANNOTATION_JAR) {
// @Override
// protected Change createChange() throws CoreException {
// final IFile file= javaProject.getProject().getFile(bundleFile.getName());
// ResourceChange copyFileChange= new ResourceChange() {
// @Override
// public Change perform(IProgressMonitor pm) throws CoreException {
// try {
// if (file.exists())
// file.delete(false, pm);
// file.create(new BufferedInputStream(new FileInputStream(bundleFile)), false, pm);
// return new DeleteResourceChange(file.getFullPath(), false);
// } catch (FileNotFoundException e) {
// throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), e.getMessage()));
// }
// }
// @Override
// public String getName() {
// return changeName;
// }
// @Override
// protected IResource getModifiedResource() {
// return javaProject.getProject();
// }
// };
// ClasspathChange addEntryChange= ClasspathChange.addEntryChange(javaProject, JavaCore
// .newLibraryEntry(file.getFullPath(), null, null));
// CompilationUnitChange addImportChange= createAddImportChange(cu, name, fullyQualifiedName);
// return new CompositeChange(changeName, new Change[] { copyFileChange, addEntryChange, addImportChange});
// }
//
// @Override
// public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
// return CorrectionMessages.UnresolvedElementsSubProcessor_copy_annotation_jar_info;
// }
// };
// proposals.add(proposal);
// }
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 = StubUtility.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 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