use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.
the class GenericInlineHandler method invoke.
public static boolean invoke(final PsiElement element, @Nullable Editor editor, final InlineHandler languageSpecific) {
final PsiReference invocationReference = editor != null ? TargetElementUtil.findReference(editor) : null;
final InlineHandler.Settings settings = languageSpecific.prepareInlineElement(element, editor, invocationReference != null);
if (settings == null || settings == InlineHandler.Settings.CANNOT_INLINE_SETTINGS) {
return settings != null;
}
final Collection<? extends PsiReference> allReferences;
if (settings.isOnlyOneReferenceToInline()) {
allReferences = Collections.singleton(invocationReference);
} else {
final Ref<Collection<? extends PsiReference>> usagesRef = new Ref<>();
ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> usagesRef.set(ReferencesSearch.search(element).findAll()), "Find Usages", false, element.getProject());
allReferences = usagesRef.get();
}
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
final Map<Language, InlineHandler.Inliner> inliners = initializeInliners(element, settings, allReferences);
for (PsiReference reference : allReferences) {
collectConflicts(reference, element, inliners, conflicts);
}
final Project project = element.getProject();
if (!conflicts.isEmpty()) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
} else {
final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts);
if (!conflictsDialog.showAndGet()) {
return true;
}
}
}
HashSet<PsiElement> elements = new HashSet<>();
for (PsiReference reference : allReferences) {
PsiElement refElement = reference.getElement();
if (refElement != null) {
elements.add(refElement);
}
}
if (!settings.isOnlyOneReferenceToInline()) {
elements.add(element);
}
if (!CommonRefactoringUtil.checkReadOnlyStatusRecursively(project, elements, true)) {
return true;
}
ApplicationManager.getApplication().runWriteAction(() -> {
final String subj = element instanceof PsiNamedElement ? ((PsiNamedElement) element).getName() : "element";
CommandProcessor.getInstance().executeCommand(project, () -> {
final PsiReference[] references = sortDepthFirstRightLeftOrder(allReferences);
final UsageInfo[] usages = new UsageInfo[references.length];
for (int i = 0; i < references.length; i++) {
usages[i] = new UsageInfo(references[i]);
}
for (UsageInfo usage : usages) {
inlineReference(usage, element, inliners);
}
if (!settings.isOnlyOneReferenceToInline()) {
languageSpecific.removeDefinition(element, settings);
}
}, RefactoringBundle.message("inline.command", StringUtil.notNullize(subj, "<nameless>")), null);
});
return true;
}
use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.
the class IntroduceParameterObjectProcessor method performRefactoring.
@Override
protected void performRefactoring(@NotNull UsageInfo[] usageInfos) {
final PsiElement aClass = myClassDescriptor.createClass(myMethod, myAccessors);
if (aClass != null) {
myClassDescriptor.setExistingClass(aClass);
super.performRefactoring(usageInfos);
List<UsageInfo> changeSignatureUsages = new ArrayList<>();
for (UsageInfo info : usageInfos) {
if (info instanceof ChangeSignatureUsageWrapper) {
changeSignatureUsages.add(((ChangeSignatureUsageWrapper) info).getInfo());
}
}
ChangeSignatureProcessorBase.doChangeSignature(myChangeInfo, changeSignatureUsages.toArray(new UsageInfo[changeSignatureUsages.size()]));
}
}
use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.
the class IntroduceParameterObjectProcessor method findUsages.
@Override
protected void findUsages(@NotNull List<FixableUsageInfo> usages) {
if (myClassDescriptor.isUseExistingClass()) {
myClassDescriptor.setExistingClassCompatibleConstructor(myClassDescriptor.findCompatibleConstructorInExistingClass(myMethod));
}
List<PsiNamedElement> methodHierarchy = new ArrayList<>();
methodHierarchy.add(myMethod);
for (UsageInfo info : ChangeSignatureProcessorBase.findUsages(myChangeInfo)) {
if (info instanceof OverriderMethodUsageInfo) {
methodHierarchy.add(((OverriderMethodUsageInfo) info).getOverridingMethod());
}
usages.add(new ChangeSignatureUsageWrapper(info));
}
final P[] paramsToMerge = myClassDescriptor.getParamsToMerge();
for (PsiElement element : methodHierarchy) {
final IntroduceParameterObjectDelegate delegate = IntroduceParameterObjectDelegate.findDelegate(element);
if (delegate != null) {
for (int i = 0; i < paramsToMerge.length; i++) {
ReadWriteAccessDetector.Access access = delegate.collectInternalUsages(usages, (PsiNamedElement) element, myClassDescriptor, paramsToMerge[i], myMergedParameterInfo.getName());
if (myAccessors[i] == null || access == ReadWriteAccessDetector.Access.Write) {
myAccessors[i] = access;
}
}
}
}
myDelegate.collectUsagesToGenerateMissedFieldAccessors(usages, myMethod, myClassDescriptor, myAccessors);
myDelegate.collectAdditionalFixes(usages, myMethod, myClassDescriptor);
}
use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.
the class MoveFilesOrDirectoriesProcessor method findUsages.
@Override
@NotNull
protected UsageInfo[] findUsages() {
ArrayList<UsageInfo> result = new ArrayList<>();
for (int i = 0; i < myElementsToMove.length; i++) {
PsiElement element = myElementsToMove[i];
if (mySearchForReferences) {
for (PsiReference reference : ReferencesSearch.search(element, GlobalSearchScope.projectScope(myProject))) {
result.add(new MyUsageInfo(reference.getElement(), i, reference));
}
}
findElementUsages(result, element);
}
return result.toArray(new UsageInfo[result.size()]);
}
use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.
the class MoveFilesOrDirectoriesProcessor method retargetUsages.
protected void retargetUsages(UsageInfo[] usages, Map<PsiElement, PsiElement> oldToNewMap) {
final List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
for (UsageInfo usageInfo : usages) {
if (usageInfo instanceof MyUsageInfo) {
final MyUsageInfo info = (MyUsageInfo) usageInfo;
final PsiElement element = myElementsToMove[info.myIndex];
if (info.getReference() instanceof FileReference || info.getReference() instanceof PsiDynaReference) {
final PsiElement usageElement = info.getElement();
if (usageElement != null) {
final PsiFile usageFile = usageElement.getContainingFile();
final PsiFile psiFile = usageFile.getViewProvider().getPsi(usageFile.getViewProvider().getBaseLanguage());
if (psiFile != null && psiFile.equals(element)) {
// already processed in MoveFilesOrDirectoriesUtil.doMoveFile
continue;
}
}
}
final PsiElement refElement = info.myReference.getElement();
if (refElement != null && refElement.isValid()) {
info.myReference.bindToElement(element);
}
} else if (usageInfo instanceof NonCodeUsageInfo) {
nonCodeUsages.add((NonCodeUsageInfo) usageInfo);
}
}
for (PsiFile movedFile : myFoundUsages.keySet()) {
MoveFileHandler.forElement(movedFile).retargetUsages(myFoundUsages.get(movedFile), oldToNewMap);
}
myNonCodeUsages = nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
}
Aggregations