Search in sources :

Example 36 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class CompilationUnitRewrite method createChange.

/**
 * Creates a compilation unit change based on the events recorded by this compilation unit rewrite.
 * @param name the name of the change to create
 * @param generateGroups <code>true</code> to generate text edit groups, <code>false</code> otherwise
 * @param monitor the progress monitor or <code>null</code>
 * @return a {@link CompilationUnitChange}, or <code>null</code> for an empty change
 * @throws CoreException when text buffer acquisition or import rewrite text edit creation fails
 * @throws IllegalArgumentException when the AST rewrite encounters problems
 */
public CompilationUnitChange createChange(String name, boolean generateGroups, IProgressMonitor monitor) throws CoreException {
    CompilationUnitChange cuChange = new CompilationUnitChange(name, fCu);
    MultiTextEdit multiEdit = new MultiTextEdit();
    cuChange.setEdit(multiEdit);
    return attachChange(cuChange, generateGroups, monitor);
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 37 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class LocalCorrectionsSubProcessor method addUnusedMemberProposal.

public static void addUnusedMemberProposal(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
    int problemId = problem.getProblemId();
    UnusedCodeFix fix = UnusedCodeFix.createUnusedMemberFix(context.getASTRoot(), problem, false);
    if (fix != null) {
        try {
            CompilationUnitChange change = fix.createChange(null);
            CUCorrectionProposal proposal = new CUCorrectionProposal(change.getName(), change.getCompilationUnit(), change, IProposalRelevance.UNUSED_MEMBER);
            proposals.add(proposal);
        } catch (CoreException e) {
            JavaLanguageServerPlugin.log(e);
        }
    }
    if (problemId == IProblem.ArgumentIsNeverUsed) {
        JavadocTagsSubProcessor.getUnusedAndUndocumentedParameterOrExceptionProposals(context, problem, proposals);
    }
    if (problemId == IProblem.UnusedPrivateField) {
        GetterSetterCorrectionSubProcessor.addGetterSetterProposal(context, problem, proposals);
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) UnusedCodeFix(org.eclipse.jdt.ls.core.internal.corext.fix.UnusedCodeFix) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 38 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project bndtools by bndtools.

the class PackageInfoBaselineQuickFixProcessor method getCorrections.

@Override
public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IProblemLocation[] locations) throws CoreException {
    ICompilationUnit compUnit = context.getCompilationUnit();
    IResource resource = compUnit.getResource();
    IMarker[] markers = resource.findMarkers(BndtoolsConstants.MARKER_JAVA_BASELINE, false, 1);
    for (IProblemLocation location : locations) {
        for (IMarker marker : markers) {
            int markerStart = marker.getAttribute(IMarker.CHAR_START, -1);
            int markerEnd = marker.getAttribute(IMarker.CHAR_END, -1);
            int markerLength = markerEnd - markerStart;
            if (location.getOffset() <= markerStart && markerStart < location.getOffset() + location.getLength()) {
                String newVersion = marker.getAttribute("suggestedVersion", null);
                if (newVersion != null) {
                    StringBuilder quotedVersion = new StringBuilder(newVersion.trim());
                    if (quotedVersion.charAt(0) != '"')
                        quotedVersion.insert(0, '"');
                    if (quotedVersion.charAt(quotedVersion.length() - 1) != '"')
                        quotedVersion.append('"');
                    CompilationUnitChange change = new CompilationUnitChange("Change package-info.java", compUnit);
                    change.setEdit(new ReplaceEdit(markerStart, markerLength, quotedVersion.toString()));
                    return new IJavaCompletionProposal[] { new ChangeCorrectionProposal("Change package version to " + newVersion, change, 1000) };
                }
            }
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IMarker(org.eclipse.core.resources.IMarker) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) IResource(org.eclipse.core.resources.IResource) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 39 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class RenameNonVirtualMethodProcessor method addDeclarationUpdate.

private void addDeclarationUpdate(TextChangeManager manager) throws CoreException {
    if (getDelegateUpdating()) {
        // create the delegate
        CompilationUnitRewrite rewrite = new CompilationUnitRewrite(getDeclaringCU());
        rewrite.setResolveBindings(true);
        MethodDeclaration methodDeclaration = ASTNodeSearchUtil.getMethodDeclarationNode(getMethod(), rewrite.getRoot());
        DelegateMethodCreator creator = new DelegateMethodCreator();
        creator.setDeclaration(methodDeclaration);
        creator.setDeclareDeprecated(getDeprecateDelegates());
        creator.setSourceRewrite(rewrite);
        creator.setCopy(true);
        creator.setNewElementName(getNewElementName());
        creator.prepareDelegate();
        creator.createEdit();
        CompilationUnitChange cuChange = rewrite.createChange(true);
        if (cuChange != null) {
            cuChange.setKeepPreviewEdits(true);
            manager.manage(getDeclaringCU(), cuChange);
        }
    }
    String editName = RefactoringCoreMessages.RenameMethodRefactoring_update_declaration;
    ISourceRange nameRange = getMethod().getNameRange();
    ReplaceEdit replaceEdit = new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), getNewElementName());
    addTextEdit(manager.get(getDeclaringCU()), editName, replaceEdit);
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) DelegateMethodCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateMethodCreator) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 40 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls 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) RenameAnalyzeUtil(org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil) 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)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