use of com.intellij.lang.refactoring.InlineHandler in project intellij-community by JetBrains.
the class GenericInlineHandler method initializeInliners.
public static Map<Language, InlineHandler.Inliner> initializeInliners(PsiElement element, InlineHandler.Settings settings, Collection<? extends PsiReference> allReferences) {
final Map<Language, InlineHandler.Inliner> inliners = new HashMap<>();
for (PsiReference ref : allReferences) {
if (ref == null) {
LOG.error("element: " + element.getClass() + ", allReferences contains null!");
continue;
}
PsiElement refElement = ref.getElement();
LOG.assertTrue(refElement != null, ref.getClass().getName());
final Language language = refElement.getLanguage();
if (inliners.containsKey(language))
continue;
final List<InlineHandler> handlers = InlineHandlers.getInlineHandlers(language);
for (InlineHandler handler : handlers) {
InlineHandler.Inliner inliner = handler.createInliner(element, settings);
if (inliner != null) {
inliners.put(language, inliner);
break;
}
}
}
return inliners;
}
use of com.intellij.lang.refactoring.InlineHandler 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.lang.refactoring.InlineHandler in project intellij-community by JetBrains.
the class InlineAction method getHandler.
@Override
protected RefactoringActionHandler getHandler(@NotNull Language language, PsiElement element) {
RefactoringActionHandler handler = super.getHandler(language, element);
if (handler != null)
return handler;
List<InlineHandler> handlers = InlineHandlers.getInlineHandlers(language);
return handlers.isEmpty() ? null : new InlineRefactoringActionHandler();
}
Aggregations