Search in sources :

Example 16 with CompilationUnitChange

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();
    }
}
Also used : SelectionAwareSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.SelectionAwareSourceRangeComputer) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LinkedProposalModel(org.eclipse.jdt.internal.corext.fix.LinkedProposalModel) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 17 with CompilationUnitChange

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;
}
Also used : TextChange(org.eclipse.ltk.core.refactoring.TextChange) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 18 with CompilationUnitChange

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DocumentChange(org.eclipse.ltk.core.refactoring.DocumentChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IDocument(org.eclipse.jface.text.IDocument) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 19 with CompilationUnitChange

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;
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 20 with CompilationUnitChange

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);
}
Also used : LocalAnalyzePackage(org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil.LocalAnalyzePackage) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) LocalAnalyzePackage(org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil.LocalAnalyzePackage) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Aggregations

CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)24 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)9 TextEdit (org.eclipse.text.edits.TextEdit)9 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)6 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)5 TextEditGroup (org.eclipse.text.edits.TextEditGroup)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)3 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)3 RefactoringChangeDescriptor (org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor)3 TextChange (org.eclipse.ltk.core.refactoring.TextChange)3 HashMap (java.util.HashMap)2 IFile (org.eclipse.core.resources.IFile)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2