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;
}
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());
}
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)));
}
}
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;
}
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());
}
Aggregations