use of com.intellij.util.Function in project intellij-community by JetBrains.
the class ModuleAwareProjectConfigurable method createComponent.
@Override
public JComponent createComponent() {
if (myProject.isDefault()) {
T configurable = createDefaultProjectConfigurable();
if (configurable != null) {
myModuleConfigurables.put(null, configurable);
return configurable.createComponent();
}
}
final List<Module> modules = ContainerUtil.filter(ModuleAttachProcessor.getSortedModules(myProject), module -> isSuitableForModule(module));
final T projectConfigurable = createProjectConfigurable();
if (modules.size() == 1 && projectConfigurable == null) {
Module module = modules.get(0);
final T configurable = createModuleConfigurable(module);
myModuleConfigurables.put(module, configurable);
return configurable.createComponent();
}
final Splitter splitter = new Splitter(false, 0.25f);
CollectionListModel<Module> listDataModel = new CollectionListModel<>(modules);
final JBList moduleList = new JBList(listDataModel);
new ListSpeedSearch(moduleList, (Function<Object, String>) o -> {
if (o == null) {
return getProjectConfigurableItemName();
} else if (o instanceof Module) {
return ((Module) o).getName();
}
return null;
});
moduleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
moduleList.setCellRenderer(new ModuleListCellRenderer() {
@Override
public void customize(JList list, Module module, int index, boolean selected, boolean hasFocus) {
if (module == null) {
setText(getProjectConfigurableItemName());
setIcon(getProjectConfigurableItemIcon());
} else {
super.customize(list, module, index, selected, hasFocus);
}
}
});
splitter.setFirstComponent(new JBScrollPane(moduleList));
final CardLayout layout = new CardLayout();
final JPanel cardPanel = new JPanel(layout);
splitter.setSecondComponent(cardPanel);
if (projectConfigurable != null) {
myModuleConfigurables.put(null, projectConfigurable);
final JComponent component = projectConfigurable.createComponent();
cardPanel.add(component, PROJECT_ITEM_KEY);
listDataModel.add(0, null);
}
for (Module module : modules) {
final T configurable = createModuleConfigurable(module);
myModuleConfigurables.put(module, configurable);
final JComponent component = configurable.createComponent();
cardPanel.add(component, module.getName());
}
moduleList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
final Module value = (Module) moduleList.getSelectedValue();
layout.show(cardPanel, value == null ? PROJECT_ITEM_KEY : value.getName());
}
});
if (moduleList.getItemsCount() > 0) {
moduleList.setSelectedIndex(0);
Module module = listDataModel.getElementAt(0);
layout.show(cardPanel, module == null ? PROJECT_ITEM_KEY : module.getName());
}
return splitter;
}
use of com.intellij.util.Function in project intellij-community by JetBrains.
the class RunLineMarkerProvider method getLineMarkerInfo.
@Nullable
@Override
public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) {
List<RunLineMarkerContributor> contributors = RunLineMarkerContributor.EXTENSION.allForLanguage(element.getLanguage());
Icon icon = null;
List<Info> infos = null;
for (RunLineMarkerContributor contributor : contributors) {
Info info = contributor.getInfo(element);
if (info == null) {
continue;
}
if (icon == null) {
icon = info.icon;
}
if (infos == null) {
infos = new SmartList<>();
}
infos.add(info);
}
if (icon == null)
return null;
if (infos.size() > 1) {
Collections.sort(infos, COMPARATOR);
final Info first = infos.get(0);
for (Iterator<Info> it = infos.iterator(); it.hasNext(); ) {
Info info = it.next();
if (info != first && first.shouldReplace(info)) {
it.remove();
}
}
}
final DefaultActionGroup actionGroup = new DefaultActionGroup();
for (Info info : infos) {
for (AnAction action : info.actions) {
actionGroup.add(new LineMarkerActionWrapper(element, action));
}
if (info != infos.get(infos.size() - 1)) {
actionGroup.add(new Separator());
}
}
List<Info> finalInfos = infos;
Function<PsiElement, String> tooltipProvider = element1 -> {
final StringBuilder tooltip = new StringBuilder();
for (Info info : finalInfos) {
if (info.tooltipProvider != null) {
String string = info.tooltipProvider.apply(element1);
if (string == null)
continue;
if (tooltip.length() != 0) {
tooltip.append("\n");
}
tooltip.append(string);
}
}
return tooltip.length() == 0 ? null : tooltip.toString();
};
return new LineMarkerInfo<PsiElement>(element, element.getTextRange(), icon, Pass.LINE_MARKERS, tooltipProvider, null, GutterIconRenderer.Alignment.CENTER) {
@Nullable
@Override
public GutterIconRenderer createGutterRenderer() {
return new LineMarkerGutterIconRenderer<PsiElement>(this) {
@Override
public AnAction getClickAction() {
return null;
}
@Override
public boolean isNavigateAction() {
return true;
}
@Nullable
@Override
public ActionGroup getPopupMenuActions() {
return actionGroup;
}
};
}
};
}
use of com.intellij.util.Function in project intellij-community by JetBrains.
the class RefResolveServiceImpl method initListeners.
private void initListeners(@NotNull MessageBus messageBus, @NotNull PsiManager psiManager) {
messageBus.connect().subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {
@Override
public void after(@NotNull List<? extends VFileEvent> events) {
fileCount.set(0);
List<VirtualFile> files = ContainerUtil.mapNotNull(events, new Function<VFileEvent, VirtualFile>() {
@Override
public VirtualFile fun(VFileEvent event) {
return event.getFile();
}
});
queue(files, "VFS events " + events.size());
}
});
psiManager.addPsiTreeChangeListener(new PsiTreeChangeAdapter() {
@Override
public void childrenChanged(@NotNull PsiTreeChangeEvent event) {
PsiFile file = event.getFile();
VirtualFile virtualFile = PsiUtilCore.getVirtualFile(file);
if (virtualFile != null) {
queue(Collections.singletonList(virtualFile), event);
}
}
@Override
public void propertyChanged(@NotNull PsiTreeChangeEvent event) {
childrenChanged(event);
}
});
messageBus.connect().subscribe(DumbService.DUMB_MODE, new DumbService.DumbModeListener() {
@Override
public void enteredDumbMode() {
disable();
}
@Override
public void exitDumbMode() {
enable();
}
});
messageBus.connect().subscribe(PowerSaveMode.TOPIC, new PowerSaveMode.Listener() {
@Override
public void powerSaveStateChanged() {
if (PowerSaveMode.isEnabled()) {
enable();
} else {
disable();
}
}
});
myApplication.addApplicationListener(new ApplicationAdapter() {
@Override
public void beforeWriteActionStart(@NotNull Object action) {
disable();
}
@Override
public void writeActionFinished(@NotNull Object action) {
enable();
}
@Override
public void applicationExiting() {
disable();
}
}, this);
VirtualFileManager.getInstance().addVirtualFileManagerListener(new VirtualFileManagerListener() {
@Override
public void beforeRefreshStart(boolean asynchronous) {
disable();
}
@Override
public void afterRefreshFinish(boolean asynchronous) {
enable();
}
}, this);
HeavyProcessLatch.INSTANCE.addListener(new HeavyProcessLatch.HeavyProcessListener() {
@Override
public void processStarted() {
}
@Override
public void processFinished() {
wakeUp();
}
}, this);
}
use of com.intellij.util.Function in project intellij-community by JetBrains.
the class JavaClassInheritorsSearcher method getOrComputeSubClasses.
@NotNull
private static Iterable<PsiClass> getOrComputeSubClasses(@NotNull Project project, @NotNull PsiClass baseClass, @NotNull SearchScope searchScopeForNonPhysical) {
ConcurrentMap<PsiClass, Iterable<PsiClass>> map = HighlightingCaches.getInstance(project).ALL_SUB_CLASSES;
Iterable<PsiClass> cached = map.get(baseClass);
if (cached == null) {
// returns lazy collection of subclasses. Each call to next() leads to calculation of next batch of subclasses.
Function<PsiAnchor, PsiClass> converter = anchor -> ReadAction.compute(() -> (PsiClass) anchor.retrieve());
Predicate<PsiClass> applicableFilter = candidate -> !(candidate instanceof PsiAnonymousClass) && candidate != null && !candidate.hasModifierProperty(PsiModifier.FINAL);
// for non-physical elements ignore the cache completely because non-physical elements created so often/unpredictably so I can't figure out when to clear caches in this case
boolean isPhysical = ReadAction.compute(baseClass::isPhysical);
SearchScope scopeToUse = isPhysical ? GlobalSearchScope.allScope(project) : searchScopeForNonPhysical;
LazyConcurrentCollection.MoreElementsGenerator<PsiAnchor, PsiClass> generator = (candidate, processor) -> DirectClassInheritorsSearch.search(candidate, scopeToUse).forEach(subClass -> {
ProgressManager.checkCanceled();
PsiAnchor pointer = ReadAction.compute(() -> PsiAnchor.create(subClass));
processor.consume(pointer);
return true;
});
PsiAnchor seed = ReadAction.compute(() -> PsiAnchor.create(baseClass));
// lazy collection: store underlying queue as PsiAnchors, generate new elements by running direct inheritors
Iterable<PsiClass> computed = new LazyConcurrentCollection<>(seed, converter, applicableFilter, generator);
// make sure concurrent calls of this method always return the same collection to avoid expensive duplicate work
cached = isPhysical ? ConcurrencyUtil.cacheOrGet(map, baseClass, computed) : computed;
}
return cached;
}
use of com.intellij.util.Function in project intellij-community by JetBrains.
the class AddMethodQualifierTest method doTest.
private void doTest(final String... candidatesNames) {
myFixture.configureByFile(getTestName(false) + ".java");
final AddMethodQualifierFix addMethodQualifierFix = getQuickFix();
if (candidatesNames.length == 0) {
assertNull(addMethodQualifierFix);
return;
}
assertNotNull(addMethodQualifierFix);
final Set<String> actualCandidatesNames = new TreeSet<>(ContainerUtil.map(addMethodQualifierFix.getCandidates(), new Function<PsiNamedElement, String>() {
@Override
public String fun(final PsiNamedElement psiNamedElement) {
final String name = psiNamedElement.getName();
assertNotNull(name);
return name;
}
}));
final Set<String> expectedCandidatesNames = new TreeSet<>(ContainerUtil.list(candidatesNames));
assertEquals(expectedCandidatesNames, actualCandidatesNames);
}
Aggregations