use of com.intellij.util.containers.HashSet 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.util.containers.HashSet in project intellij-community by JetBrains.
the class BaseRefactoringProcessor method createPresentation.
@NotNull
private static UsageViewPresentation createPresentation(@NotNull UsageViewDescriptor descriptor, @NotNull Usage[] usages) {
UsageViewPresentation presentation = new UsageViewPresentation();
presentation.setTabText(RefactoringBundle.message("usageView.tabText"));
presentation.setTargetsNodeText(descriptor.getProcessedElementsHeader());
presentation.setShowReadOnlyStatusAsRed(true);
presentation.setShowCancelButton(true);
presentation.setUsagesString(RefactoringBundle.message("usageView.usagesText"));
int codeUsageCount = 0;
int nonCodeUsageCount = 0;
int dynamicUsagesCount = 0;
Set<PsiFile> codeFiles = new HashSet<>();
Set<PsiFile> nonCodeFiles = new HashSet<>();
Set<PsiFile> dynamicUsagesCodeFiles = new HashSet<>();
for (Usage usage : usages) {
if (usage instanceof PsiElementUsage) {
final PsiElementUsage elementUsage = (PsiElementUsage) usage;
final PsiElement element = elementUsage.getElement();
if (element == null)
continue;
final PsiFile containingFile = element.getContainingFile();
if (elementUsage.isNonCodeUsage()) {
nonCodeUsageCount++;
nonCodeFiles.add(containingFile);
} else {
codeUsageCount++;
codeFiles.add(containingFile);
}
if (usage instanceof UsageInfo2UsageAdapter) {
final UsageInfo usageInfo = ((UsageInfo2UsageAdapter) usage).getUsageInfo();
if (usageInfo instanceof MoveRenameUsageInfo && usageInfo.isDynamicUsage()) {
dynamicUsagesCount++;
dynamicUsagesCodeFiles.add(containingFile);
}
}
}
}
codeFiles.remove(null);
nonCodeFiles.remove(null);
dynamicUsagesCodeFiles.remove(null);
String codeReferencesText = descriptor.getCodeReferencesText(codeUsageCount, codeFiles.size());
presentation.setCodeUsagesString(codeReferencesText);
final String commentReferencesText = descriptor.getCommentReferencesText(nonCodeUsageCount, nonCodeFiles.size());
if (commentReferencesText != null) {
presentation.setNonCodeUsagesString(commentReferencesText);
}
presentation.setDynamicUsagesString("Dynamic " + StringUtil.decapitalize(descriptor.getCodeReferencesText(dynamicUsagesCount, dynamicUsagesCodeFiles.size())));
String generatedCodeString;
if (codeReferencesText.contains("in code")) {
generatedCodeString = StringUtil.replace(codeReferencesText, "in code", "in generated code");
} else {
generatedCodeString = codeReferencesText + " in generated code";
}
presentation.setUsagesInGeneratedCodeString(generatedCodeString);
return presentation;
}
use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.
the class GotoCustomRegionAction method getCustomFoldingDescriptors.
@NotNull
private static Collection<FoldingDescriptor> getCustomFoldingDescriptors(@NotNull Editor editor, @NotNull Project project) {
Set<FoldingDescriptor> foldingDescriptors = new HashSet<>();
final Document document = editor.getDocument();
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
PsiFile file = documentManager != null ? documentManager.getPsiFile(document) : null;
if (file != null) {
final FileViewProvider viewProvider = file.getViewProvider();
for (final Language language : viewProvider.getLanguages()) {
final PsiFile psi = viewProvider.getPsi(language);
final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language);
if (psi != null) {
for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, false)) {
CustomFoldingBuilder customFoldingBuilder = getCustomFoldingBuilder(foldingBuilder, descriptor);
if (customFoldingBuilder != null) {
if (customFoldingBuilder.isCustomRegionStart(descriptor.getElement())) {
foldingDescriptors.add(descriptor);
}
}
}
}
}
}
return foldingDescriptors;
}
use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.
the class JavaTestFinder method collectTests.
private boolean collectTests(PsiClass klass, Processor<Pair<? extends PsiNamedElement, Integer>> processor) {
GlobalSearchScope scope = getSearchScope(klass, false);
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(klass.getProject());
String klassName = klass.getName();
Pattern pattern = Pattern.compile(".*" + StringUtil.escapeToRegexp(klassName) + ".*", Pattern.CASE_INSENSITIVE);
HashSet<String> names = new HashSet<>();
cache.getAllClassNames(names);
for (String eachName : names) {
if (pattern.matcher(eachName).matches()) {
for (PsiClass eachClass : cache.getClassesByName(eachName, scope)) {
if (isTestClass(eachClass, klass)) {
if (!processor.process(Pair.create(eachClass, TestFinderHelper.calcTestNameProximity(klassName, eachName)))) {
return true;
}
}
}
}
}
return false;
}
use of com.intellij.util.containers.HashSet in project intellij-community by JetBrains.
the class JspContextManager method getRootContextFile.
@NotNull
public BaseJspFile getRootContextFile(@NotNull BaseJspFile file) {
BaseJspFile rootContext = file;
HashSet<BaseJspFile> recursionPreventer = new HashSet<>();
do {
recursionPreventer.add(rootContext);
BaseJspFile context = getContextFile(rootContext);
if (context == null || recursionPreventer.contains(context))
break;
rootContext = context;
} while (true);
return rootContext;
}
Aggregations