Search in sources :

Example 26 with CompilationUnitChange

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

Example 27 with CompilationUnitChange

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();
    }
}
Also used : SelectionAwareSourceRangeComputer(org.eclipse.jdt.ls.core.internal.corext.refactoring.util.SelectionAwareSourceRangeComputer) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LinkedProposalModelCore(org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore) 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 28 with CompilationUnitChange

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;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) DelegateFieldCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateFieldCreator) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 29 with CompilationUnitChange

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 30 with CompilationUnitChange

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

Aggregations

CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)45 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)14 TextEdit (org.eclipse.text.edits.TextEdit)14 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)13 CoreException (org.eclipse.core.runtime.CoreException)9 CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)8 TextEditGroup (org.eclipse.text.edits.TextEditGroup)8 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)4 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)4 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)4 IDocument (org.eclipse.jface.text.IDocument)4 TextChange (org.eclipse.ltk.core.refactoring.TextChange)4 HashMap (java.util.HashMap)3 IFile (org.eclipse.core.resources.IFile)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3