Search in sources :

Example 26 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project che by eclipse.

the class QualifiedNameSearchResult method getSingleChange.

public Change getSingleChange(IFile[] alreadyTouchedFiles) {
    Collection<TextChange> values = fChanges.values();
    if (values.size() == 0)
        return null;
    CompositeChange result = new CompositeChange(RefactoringCoreMessages.QualifiedNameSearchResult_change_name);
    result.markAsSynthetic();
    List<IFile> files = Arrays.asList(alreadyTouchedFiles);
    for (Iterator<TextChange> iter = values.iterator(); iter.hasNext(); ) {
        TextFileChange change = (TextFileChange) iter.next();
        if (!files.contains(change.getFile())) {
            result.add(change);
        }
    }
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) TextChange(org.eclipse.ltk.core.refactoring.TextChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange)

Example 27 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project che by eclipse.

the class RefactoringSession method apply.

/**
     * @return instance of {@link org.eclipse.che.ide.ext.java.shared.dto.refactoring.RefactoringStatus}.
     * That describes status of the refactoring operation.
     */
public RefactoringResult apply() {
    PerformChangeOperation operation = new PerformChangeOperation(change);
    FinishResult result = internalPerformFinish(operation);
    if (result.isException()) {
        return DtoConverter.toRefactoringResultDto(RefactoringStatus.createErrorStatus("Refactoring failed with Exception."));
    }
    CompositeChange operationChange = (CompositeChange) operation.getUndoChange();
    Change[] changes = operationChange.getChildren();
    RefactoringStatus validationStatus = operation.getValidationStatus();
    if (validationStatus != null) {
        List<ChangeInfo> changesInfo = new ArrayList<>();
        prepareChangesInfo(changes, changesInfo);
        RefactoringResult status = DtoConverter.toRefactoringResultDto(validationStatus);
        status.setChanges(changesInfo);
        return status;
    }
    return DtoConverter.toRefactoringResultDto(new RefactoringStatus());
}
Also used : PerformChangeOperation(org.eclipse.ltk.core.refactoring.PerformChangeOperation) FinishResult(org.eclipse.ltk.internal.ui.refactoring.FinishResult) ChangeInfo(org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo) RefactoringResult(org.eclipse.che.ide.ext.java.shared.dto.refactoring.RefactoringResult) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) MoveCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange) UndoTextFileChange(org.eclipse.ltk.core.refactoring.UndoTextFileChange) Change(org.eclipse.ltk.core.refactoring.Change) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange) RenameCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange) RenamePackageChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenamePackageChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange)

Example 28 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project che by eclipse.

the class ReorgCorrectionsSubProcessor method getWrongPackageDeclNameProposals.

public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    boolean isLinked = cu.getResource().isLinked();
    // correct package declaration
    // bug 38357
    int relevance = cu.getPackageDeclarations().length == 0 ? IProposalRelevance.MISSING_PACKAGE_DECLARATION : IProposalRelevance.CORRECT_PACKAGE_DECLARATION;
    proposals.add(new CorrectPackageDeclarationProposal(cu, problem, relevance));
    // move to package
    IPackageDeclaration[] packDecls = cu.getPackageDeclarations();
    //$NON-NLS-1$
    String newPackName = packDecls.length > 0 ? packDecls[0].getElementName() : "";
    IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(cu);
    IPackageFragment newPack = root.getPackageFragment(newPackName);
    ICompilationUnit newCU = newPack.getCompilationUnit(cu.getElementName());
    if (!newCU.exists() && !isLinked) {
        String label;
        if (newPack.isDefaultPackage()) {
            label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_default_description, BasicElementLabels.getFileName(cu));
        } else {
            String packageLabel = JavaElementLabels.getElementLabel(newPack, JavaElementLabels.ALL_DEFAULT);
            label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_description, new Object[] { BasicElementLabels.getFileName(cu), packageLabel });
        }
        CompositeChange composite = new CompositeChange(label);
        composite.add(new CreatePackageChange(newPack));
        composite.add(new MoveCompilationUnitChange(cu, newPack));
        proposals.add(new ChangeCorrectionProposal(label, composite, IProposalRelevance.MOVE_CU_TO_PACKAGE, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_MOVE)));
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CorrectPackageDeclarationProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) CreatePackageChange(org.eclipse.jdt.internal.corext.refactoring.changes.CreatePackageChange) MoveCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration)

Example 29 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project eclipse.platform.text by eclipse.

the class ReplaceRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    if (fReplaceString == null) {
        return RefactoringStatus.createFatalErrorStatus(SearchMessages.ReplaceRefactoring_error_no_replace_string);
    }
    Pattern pattern = null;
    FileSearchQuery query = getQuery();
    if (query.isRegexSearch()) {
        pattern = createSearchPattern(query);
    }
    RefactoringStatus resultingStatus = new RefactoringStatus();
    Collection<IFile> allFilesSet = fMatches.keySet();
    IFile[] allFiles = allFilesSet.toArray(new IFile[allFilesSet.size()]);
    Arrays.sort(allFiles, new Comparator<IFile>() {

        private Collator fCollator = Collator.getInstance();

        @Override
        public int compare(IFile o1, IFile o2) {
            String p1 = o1.getFullPath().toString();
            String p2 = o2.getFullPath().toString();
            return fCollator.compare(p1, p2);
        }
    });
    checkFilesToBeChanged(allFiles, resultingStatus);
    if (resultingStatus.hasFatalError()) {
        return resultingStatus;
    }
    CompositeChange compositeChange = new CompositeChange(SearchMessages.ReplaceRefactoring_composite_change_name);
    compositeChange.markAsSynthetic();
    ArrayList<MatchGroup> matchGroups = new ArrayList<>();
    boolean hasChanges = false;
    try {
        for (IFile file : allFiles) {
            Set<FileMatch> bucket = fMatches.get(file);
            if (!bucket.isEmpty()) {
                try {
                    TextChange change = createFileChange(file, pattern, bucket, resultingStatus, matchGroups);
                    if (change != null) {
                        compositeChange.add(change);
                        hasChanges = true;
                    }
                } catch (CoreException e) {
                    String message = Messages.format(SearchMessages.ReplaceRefactoring_error_access_file, new Object[] { file.getName(), e.getLocalizedMessage() });
                    return RefactoringStatus.createFatalErrorStatus(message);
                }
            }
        }
    } catch (PatternSyntaxException e) {
        String message = Messages.format(SearchMessages.ReplaceRefactoring_error_replacement_expression, e.getLocalizedMessage());
        return RefactoringStatus.createFatalErrorStatus(message);
    }
    if (!hasChanges && resultingStatus.isOK()) {
        return RefactoringStatus.createFatalErrorStatus(SearchMessages.ReplaceRefactoring_error_no_changes);
    }
    compositeChange.add(new SearchResultUpdateChange(fResult, matchGroups.toArray(new MatchGroup[matchGroups.size()]), fIgnoredMatches));
    fChange = compositeChange;
    return resultingStatus;
}
Also used : Pattern(java.util.regex.Pattern) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) TextChange(org.eclipse.ltk.core.refactoring.TextChange) Collator(com.ibm.icu.text.Collator) CoreException(org.eclipse.core.runtime.CoreException) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 30 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project xtext-eclipse by eclipse.

the class UpdateAcceptorTest method testAddTextEdit.

@Test
public void testAddTextEdit() throws Exception {
    updateAcceptor.accept(resourceURI0, new ReplaceEdit(0, 1, "foo"));
    updateAcceptor.accept(resourceURI1, new ReplaceEdit(1, 2, "bar"));
    updateAcceptor.accept(resourceURI0, new ReplaceEdit(3, 4, "baz"));
    Change change = updateAcceptor.createCompositeChange(CHANGE_NAME, new NullProgressMonitor());
    assertTrue(change instanceof CompositeChange);
    Change[] children = ((CompositeChange) change).getChildren();
    assertEquals(2, children.length);
    assertTrue(children[0] instanceof MockChange);
    MockChange change0 = (MockChange) children[0];
    assertEquals(CHANGE_NAME, change0.getName());
    assertTrue(children[1] instanceof MockChange);
    MockChange change1 = (MockChange) children[1];
    assertEquals(CHANGE_NAME, change1.getName());
    assertTrue(change0.getTextEdit() instanceof MultiTextEdit);
    assertTrue(change1.getTextEdit() instanceof MultiTextEdit);
    assertEquals(3, ((MultiTextEdit) change0.getTextEdit()).getChildrenSize() + ((MultiTextEdit) change1.getTextEdit()).getChildrenSize());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) MockChange(org.eclipse.xtext.ui.tests.refactoring.MockRefactoringDocument.MockChange) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) MockChange(org.eclipse.xtext.ui.tests.refactoring.MockRefactoringDocument.MockChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) Change(org.eclipse.ltk.core.refactoring.Change) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Test(org.junit.Test)

Aggregations

CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)49 Change (org.eclipse.ltk.core.refactoring.Change)22 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)14 IFile (org.eclipse.core.resources.IFile)10 IResource (org.eclipse.core.resources.IResource)9 TextChange (org.eclipse.ltk.core.refactoring.TextChange)9 IResourceVisitor (org.eclipse.core.resources.IResourceVisitor)7 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)6 EditorDocumentChange (org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)6 CoreException (org.eclipse.core.runtime.CoreException)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 TextEditBasedChange (org.eclipse.ltk.core.refactoring.TextEditBasedChange)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)3 TextEdit (org.eclipse.text.edits.TextEdit)3 IOException (java.io.IOException)2