use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.
the class FunctionalInterfaceSuggester method suggestFunctionalInterfaces.
private static <T extends PsiElement> Collection<? extends PsiType> suggestFunctionalInterfaces(@NotNull final T element, final NullableFunction<PsiClass, PsiType> acceptanceChecker) {
final Project project = element.getProject();
final Set<PsiType> types = new HashSet<>();
final Processor<PsiMember> consumer = member -> {
if (member instanceof PsiClass && Java15APIUsageInspectionBase.getLastIncompatibleLanguageLevel(member, PsiUtil.getLanguageLevel(element)) == null) {
if (!JavaResolveUtil.isAccessible(member, null, member.getModifierList(), element, null, null)) {
return true;
}
ContainerUtil.addIfNotNull(types, acceptanceChecker.fun((PsiClass) member));
}
return true;
};
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final GlobalSearchScope allScope = GlobalSearchScope.allScope(project);
final PsiClass functionalInterfaceClass = psiFacade.findClass(CommonClassNames.JAVA_LANG_FUNCTIONAL_INTERFACE, allScope);
if (functionalInterfaceClass != null) {
AnnotatedMembersSearch.search(functionalInterfaceClass, element.getResolveScope()).forEach(consumer);
}
for (String functionalInterface : FUNCTIONAL_INTERFACES) {
final PsiClass aClass = psiFacade.findClass(functionalInterface, allScope);
if (aClass != null) {
consumer.process(aClass);
}
}
final ArrayList<PsiType> typesToSuggest = new ArrayList<>(types);
Collections.sort(typesToSuggest, Comparator.comparing(PsiType::getCanonicalText));
return typesToSuggest;
}
use of com.intellij.util.NullableFunction in project intellij-plugins by JetBrains.
the class AllInDirectoryRunSettingsSection method getConfigsInDir.
@NotNull
private static List<String> getConfigsInDir(@NotNull final Project project, @NotNull String dirPath) {
List<String> result = Collections.emptyList();
File dir = new File(dirPath);
if (!StringUtil.isEmpty(dirPath) && dir.isDirectory() && dir.isAbsolute()) {
VirtualFile directoryVFile = LocalFileSystem.getInstance().findFileByIoFile(dir);
if (directoryVFile != null) {
List<VirtualFile> configs = JstdSettingsUtil.collectJstdConfigFilesInDirectory(project, directoryVFile);
result = ContainerUtil.filter(ContainerUtil.map(configs, (NullableFunction<VirtualFile, String>) virtualFile -> ProjectRootUtils.getRootRelativePath(project, virtualFile.getPath())), Condition.NOT_NULL);
Collections.sort(result);
}
}
return result;
}
use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.
the class GutterIconsConfigurable method createComponent.
@Nullable
@Override
public JComponent createComponent() {
ExtensionPoint<LineMarkerProvider> point = Extensions.getRootArea().getExtensionPoint(LineMarkerProviders.EP_NAME);
@SuppressWarnings("unchecked") LanguageExtensionPoint<LineMarkerProvider>[] extensions = (LanguageExtensionPoint<LineMarkerProvider>[]) point.getExtensions();
NullableFunction<LanguageExtensionPoint<LineMarkerProvider>, PluginDescriptor> function = point1 -> {
LineMarkerProvider instance = point1.getInstance();
return instance instanceof LineMarkerProviderDescriptor && ((LineMarkerProviderDescriptor) instance).getName() != null ? point1.getPluginDescriptor() : null;
};
MultiMap<PluginDescriptor, LanguageExtensionPoint<LineMarkerProvider>> map = ContainerUtil.groupBy(Arrays.asList(extensions), function);
Map<GutterIconDescriptor, PluginDescriptor> pluginDescriptorMap = ContainerUtil.newHashMap();
Set<String> ids = new HashSet<>();
myDescriptors = new ArrayList<>();
for (final PluginDescriptor descriptor : map.keySet()) {
Collection<LanguageExtensionPoint<LineMarkerProvider>> points = map.get(descriptor);
for (LanguageExtensionPoint<LineMarkerProvider> extensionPoint : points) {
GutterIconDescriptor instance = (GutterIconDescriptor) extensionPoint.getInstance();
if (instance.getOptions().length > 0) {
for (GutterIconDescriptor option : instance.getOptions()) {
if (ids.add(option.getId())) {
myDescriptors.add(option);
}
pluginDescriptorMap.put(option, descriptor);
}
} else {
if (ids.add(instance.getId())) {
myDescriptors.add(instance);
}
pluginDescriptorMap.put(instance, descriptor);
}
}
}
/*
List<GutterIconDescriptor> options = new ArrayList<GutterIconDescriptor>();
for (Iterator<GutterIconDescriptor> iterator = myDescriptors.iterator(); iterator.hasNext(); ) {
GutterIconDescriptor descriptor = iterator.next();
if (descriptor.getOptions().length > 0) {
options.addAll(Arrays.asList(descriptor.getOptions()));
iterator.remove();
}
}
myDescriptors.addAll(options);
*/
myDescriptors.sort((o1, o2) -> {
if (pluginDescriptorMap.get(o1) != pluginDescriptorMap.get(o2))
return 0;
return Comparing.compare(o1.getName(), o2.getName());
});
PluginDescriptor current = null;
for (GutterIconDescriptor descriptor : myDescriptors) {
PluginDescriptor pluginDescriptor = pluginDescriptorMap.get(descriptor);
if (pluginDescriptor != current) {
myFirstDescriptors.put(descriptor, pluginDescriptor);
current = pluginDescriptor;
}
}
myList.setItems(myDescriptors, GutterIconDescriptor::getName);
myShowGutterIconsJBCheckBox.addChangeListener(e -> myList.setEnabled(myShowGutterIconsJBCheckBox.isSelected()));
return myPanel;
}
use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.
the class DfaPsiUtil method getVariableAssignmentsInFile.
@NotNull
public static Collection<PsiExpression> getVariableAssignmentsInFile(@NotNull PsiVariable psiVariable, final boolean literalsOnly, final PsiElement place) {
Ref<Boolean> modificationRef = Ref.create(Boolean.FALSE);
PsiElement codeBlock = place == null ? null : getTopmostBlockInSameClass(place);
int placeOffset = codeBlock != null ? place.getTextRange().getStartOffset() : 0;
PsiFile containingFile = psiVariable.getContainingFile();
LocalSearchScope scope = new LocalSearchScope(new PsiElement[] { containingFile }, null, true);
Collection<PsiReference> references = ReferencesSearch.search(psiVariable, scope).findAll();
List<PsiExpression> list = ContainerUtil.mapNotNull(references, (NullableFunction<PsiReference, PsiExpression>) psiReference -> {
if (modificationRef.get())
return null;
final PsiElement parent = psiReference.getElement().getParent();
if (parent instanceof PsiAssignmentExpression) {
final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) parent;
final IElementType operation = assignmentExpression.getOperationTokenType();
if (assignmentExpression.getLExpression() == psiReference) {
if (JavaTokenType.EQ.equals(operation)) {
final PsiExpression rValue = assignmentExpression.getRExpression();
if (!literalsOnly || allOperandsAreLiterals(rValue)) {
if (codeBlock != null && PsiTreeUtil.isAncestor(codeBlock, parent, true) && placeOffset < parent.getTextRange().getStartOffset()) {
return null;
}
return rValue;
} else {
modificationRef.set(Boolean.TRUE);
}
} else if (JavaTokenType.PLUSEQ.equals(operation)) {
modificationRef.set(Boolean.TRUE);
}
}
}
return null;
});
if (modificationRef.get())
return Collections.emptyList();
PsiExpression initializer = psiVariable.getInitializer();
if (initializer != null && (!literalsOnly || allOperandsAreLiterals(initializer))) {
list = ContainerUtil.concat(list, Collections.singletonList(initializer));
}
return list;
}
use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.
the class LoadContextAction method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
final Project project = getProject(e);
assert project != null;
DefaultActionGroup group = new DefaultActionGroup();
final WorkingContextManager manager = WorkingContextManager.getInstance(project);
List<ContextInfo> history = manager.getContextHistory();
List<ContextHolder> infos = new ArrayList<>(ContainerUtil.map2List(history, (Function<ContextInfo, ContextHolder>) info -> new ContextHolder() {
@Override
void load(final boolean clear) {
LoadContextUndoableAction undoableAction = LoadContextUndoableAction.createAction(manager, clear, info.name);
UndoableCommand.execute(project, undoableAction, "Load context " + info.comment, "Context");
}
@Override
void remove() {
manager.removeContext(info.name);
}
@Override
Date getDate() {
return new Date(info.date);
}
@Override
String getComment() {
return info.comment;
}
@Override
Icon getIcon() {
return TasksIcons.SavedContext;
}
}));
final TaskManager taskManager = TaskManager.getManager(project);
List<LocalTask> tasks = taskManager.getLocalTasks();
infos.addAll(ContainerUtil.mapNotNull(tasks, (NullableFunction<LocalTask, ContextHolder>) task -> {
if (task.isActive()) {
return null;
}
return new ContextHolder() {
@Override
void load(boolean clear) {
LoadContextUndoableAction undoableAction = LoadContextUndoableAction.createAction(manager, clear, task);
UndoableCommand.execute(project, undoableAction, "Load context " + TaskUtil.getTrimmedSummary(task), "Context");
}
@Override
void remove() {
SwitchTaskAction.removeTask(project, task, taskManager);
}
@Override
Date getDate() {
return task.getUpdated();
}
@Override
String getComment() {
return TaskUtil.getTrimmedSummary(task);
}
@Override
Icon getIcon() {
return task.getIcon();
}
};
}));
Collections.sort(infos, (o1, o2) -> o2.getDate().compareTo(o1.getDate()));
final Ref<Boolean> shiftPressed = Ref.create(false);
boolean today = true;
Calendar now = Calendar.getInstance();
for (int i = 0, historySize = Math.min(MAX_ROW_COUNT, infos.size()); i < historySize; i++) {
final ContextHolder info = infos.get(i);
Calendar calendar = Calendar.getInstance();
calendar.setTime(info.getDate());
if (today && (calendar.get(Calendar.YEAR) != now.get(Calendar.YEAR) || calendar.get(Calendar.DAY_OF_YEAR) != now.get(Calendar.DAY_OF_YEAR))) {
group.addSeparator();
today = false;
}
group.add(createItem(info, shiftPressed));
}
final ListPopupImpl popup = (ListPopupImpl) JBPopupFactory.getInstance().createActionGroupPopup("Load Context", group, e.getDataContext(), false, null, MAX_ROW_COUNT);
popup.setAdText("Press SHIFT to merge with current context");
popup.registerAction("shiftPressed", KeyStroke.getKeyStroke("shift pressed SHIFT"), new AbstractAction() {
public void actionPerformed(ActionEvent e) {
shiftPressed.set(true);
popup.setCaption("Merge with Current Context");
}
});
popup.registerAction("shiftReleased", KeyStroke.getKeyStroke("released SHIFT"), new AbstractAction() {
public void actionPerformed(ActionEvent e) {
shiftPressed.set(false);
popup.setCaption("Load Context");
}
});
popup.registerAction("invoke", KeyStroke.getKeyStroke("shift ENTER"), new AbstractAction() {
public void actionPerformed(ActionEvent e) {
popup.handleSelect(true);
}
});
popup.addPopupListener(new JBPopupAdapter() {
@Override
public void onClosed(LightweightWindowEvent event) {
}
});
popup.showCenteredInCurrentWindow(project);
}
Aggregations