use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.
the class OrganizeImportsOperation method createTextEdit.
public TextEdit createTextEdit(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
try {
fNumberOfImportsAdded = 0;
fNumberOfImportsRemoved = 0;
monitor.beginTask(Messages.format(CodeGenerationMessages.OrganizeImportsOperation_description, BasicElementLabels.getFileName(fCompilationUnit)), 9);
CompilationUnit astRoot = fASTRoot;
if (astRoot == null) {
astRoot = SharedASTProvider.getAST(fCompilationUnit, SharedASTProvider.WAIT_YES, new SubProgressMonitor(monitor, 2));
if (monitor.isCanceled())
throw new OperationCanceledException();
} else {
monitor.worked(2);
}
ImportRewrite importsRewrite = StubUtility.createImportRewrite(astRoot, false);
Set<String> oldSingleImports = new HashSet<>();
Set<String> oldDemandImports = new HashSet<>();
List<SimpleName> typeReferences = new ArrayList<>();
List<SimpleName> staticReferences = new ArrayList<>();
if (!collectReferences(astRoot, typeReferences, staticReferences, oldSingleImports, oldDemandImports))
return null;
monitor.worked(1);
processor = new TypeReferenceProcessor(oldSingleImports, oldDemandImports, astRoot, importsRewrite, fIgnoreLowerCaseNames);
Iterator<SimpleName> refIterator = typeReferences.iterator();
while (refIterator.hasNext()) {
SimpleName typeRef = refIterator.next();
processor.add(typeRef);
}
hasOpenChoices = processor.process(new SubProgressMonitor(monitor, 3));
addStaticImports(staticReferences, importsRewrite);
if (hasOpenChoices) {
choices = processor.getChoices();
ISourceRange[] ranges = processor.getChoicesSourceRanges();
if (fChooseImportQuery != null) {
TypeNameMatch[] chosen = fChooseImportQuery.chooseImports(choices, ranges);
for (int i = 0; i < chosen.length; i++) {
TypeNameMatch typeInfo = chosen[i];
importsRewrite.addImport(typeInfo.getFullyQualifiedName());
}
} else if (chosenFQN != null) {
chosenFQN.forEach(importsRewrite::addImport);
}
}
TextEdit result = importsRewrite.rewriteImports(new SubProgressMonitor(monitor, 3));
determineImportDifferences(importsRewrite, oldSingleImports, oldDemandImports);
return result;
} finally {
monitor.done();
}
}
use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.
the class RenameTypeProcessor method analyseEnclosedTypes.
private RefactoringStatus analyseEnclosedTypes() throws CoreException {
final ISourceRange typeRange = fType.getSourceRange();
final RefactoringStatus result = new RefactoringStatus();
CompilationUnit cuNode = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(fType.getCompilationUnit(), false);
cuNode.accept(new ASTVisitor() {
@Override
public boolean visit(TypeDeclaration node) {
// enums and annotations can't be local
if (node.getStartPosition() <= typeRange.getOffset())
return true;
if (node.getStartPosition() > typeRange.getOffset() + typeRange.getLength())
return true;
if (getNewElementName().equals(node.getName().getIdentifier())) {
RefactoringStatusContext context = JavaStatusContext.create(fType.getCompilationUnit(), node);
String msg = null;
if (node.isLocalTypeDeclaration()) {
msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_local_type, new String[] { JavaElementUtil.createSignature(fType), getNewElementLabel() });
} else if (node.isMemberTypeDeclaration()) {
msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_member_type, new String[] { JavaElementUtil.createSignature(fType), getNewElementLabel() });
}
if (msg != null)
result.addError(msg, context);
}
MethodDeclaration[] methods = node.getMethods();
for (int i = 0; i < methods.length; i++) {
if (Modifier.isNative(methods[i].getModifiers())) {
RefactoringStatusContext context = JavaStatusContext.create(fType.getCompilationUnit(), methods[i]);
String msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_enclosed_type_native, BasicElementLabels.getJavaElementName(node.getName().getIdentifier()));
result.addWarning(msg, context);
}
}
return true;
}
});
return result;
}
use of org.eclipse.jdt.core.ISourceRange in project che 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);
}
use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.
the class RenameAnalyzeUtil method addReferenceShadowedError.
private static void addReferenceShadowedError(ICompilationUnit cu, SearchMatch newMatch, String newElementName, RefactoringStatus result) {
//TODO: should not have to filter declarations:
if (newMatch instanceof MethodDeclarationMatch || newMatch instanceof FieldDeclarationMatch)
return;
ISourceRange range = getOldSourceRange(newMatch);
RefactoringStatusContext context = JavaStatusContext.create(cu, range);
String message = Messages.format(RefactoringCoreMessages.RenameAnalyzeUtil_reference_shadowed, new String[] { BasicElementLabels.getFileName(cu), BasicElementLabels.getJavaElementName(newElementName) });
result.addError(message, context);
}
use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.
the class RenameAnalyzeUtil method addShadowsError.
private static void addShadowsError(ICompilationUnit cu, SearchMatch oldMatch, RefactoringStatus result) {
//TODO: should not have to filter declarations:
if (oldMatch instanceof MethodDeclarationMatch || oldMatch instanceof FieldDeclarationMatch)
return;
ISourceRange range = new SourceRange(oldMatch.getOffset(), oldMatch.getLength());
RefactoringStatusContext context = JavaStatusContext.create(cu, range);
String message = Messages.format(RefactoringCoreMessages.RenameAnalyzeUtil_shadows, BasicElementLabels.getFileName(cu));
result.addError(message, context);
}
Aggregations