Search in sources :

Example 1 with NullableFunction

use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.

the class WorkingContextManager method getContextHistory.

private synchronized List<ContextInfo> getContextHistory(String zipPostfix) {
    JBZipFile archive = null;
    try {
        archive = getTasksArchive(zipPostfix);
        List<JBZipEntry> entries = archive.getEntries();
        return ContainerUtil.mapNotNull(entries, (NullableFunction<JBZipEntry, ContextInfo>) entry -> entry.getName().startsWith("/context") ? new ContextInfo(entry.getName(), entry.getTime(), entry.getComment()) : null);
    } catch (IOException e) {
        LOG.error(e);
        return Collections.emptyList();
    } finally {
        closeArchive(archive);
    }
}
Also used : Arrays(java.util.Arrays) CharsetToolkit(com.intellij.openapi.vfs.CharsetToolkit) PathManager(com.intellij.openapi.application.PathManager) NonNls(org.jetbrains.annotations.NonNls) XMLOutputter(org.jdom.output.XMLOutputter) InvalidDataException(com.intellij.openapi.util.InvalidDataException) ContainerUtil(com.intellij.util.containers.ContainerUtil) JBZipFile(com.intellij.util.io.zip.JBZipFile) JBZipEntry(com.intellij.util.io.zip.JBZipEntry) Project(com.intellij.openapi.project.Project) FileUtil(com.intellij.openapi.util.io.FileUtil) Logger(com.intellij.openapi.diagnostic.Logger) Notifications(com.intellij.notification.Notifications) Extensions(com.intellij.openapi.extensions.Extensions) StringUtil(com.intellij.openapi.util.text.StringUtil) NullableFunction(com.intellij.util.NullableFunction) IOException(java.io.IOException) File(java.io.File) NotificationType(com.intellij.notification.NotificationType) Task(com.intellij.tasks.Task) TestOnly(org.jetbrains.annotations.TestOnly) Notification(com.intellij.notification.Notification) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ServiceManager(com.intellij.openapi.components.ServiceManager) JdomKt(com.intellij.util.JdomKt) WriteExternalException(com.intellij.openapi.util.WriteExternalException) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) Element(org.jdom.Element) Collections(java.util.Collections) JBZipFile(com.intellij.util.io.zip.JBZipFile) IOException(java.io.IOException) JBZipEntry(com.intellij.util.io.zip.JBZipEntry)

Example 2 with NullableFunction

use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.

the class ExpectedHighlightingData method composeText.

public static String composeText(final Map<String, ExpectedHighlightingSet> types, Collection<HighlightInfo> infos, String text) {
    // filter highlighting data and map each highlighting to a tag name
    List<Pair<String, HighlightInfo>> list = ContainerUtil.mapNotNull(infos, (NullableFunction<HighlightInfo, Pair<String, HighlightInfo>>) info -> {
        for (Map.Entry<String, ExpectedHighlightingSet> entry : types.entrySet()) {
            final ExpectedHighlightingSet set = entry.getValue();
            if (set.enabled && set.severity == info.getSeverity() && set.endOfLine == info.isAfterEndOfLine()) {
                return Pair.create(entry.getKey(), info);
            }
        }
        return null;
    });
    boolean showAttributesKeys = false;
    for (ExpectedHighlightingSet eachSet : types.values()) {
        for (HighlightInfo eachInfo : eachSet.infos) {
            if (eachInfo.forcedTextAttributesKey != null) {
                showAttributesKeys = true;
                break;
            }
        }
    }
    // sort filtered highlighting data by end offset in descending order
    Collections.sort(list, (o1, o2) -> {
        HighlightInfo i1 = o1.second;
        HighlightInfo i2 = o2.second;
        int byEnds = i2.endOffset - i1.endOffset;
        if (byEnds != 0)
            return byEnds;
        if (!i1.isAfterEndOfLine() && !i2.isAfterEndOfLine()) {
            int byStarts = i1.startOffset - i2.startOffset;
            if (byStarts != 0)
                return byStarts;
        } else {
            int byEOL = Comparing.compare(i2.isAfterEndOfLine(), i1.isAfterEndOfLine());
            if (byEOL != 0)
                return byEOL;
        }
        int bySeverity = i2.getSeverity().compareTo(i1.getSeverity());
        if (bySeverity != 0)
            return bySeverity;
        return Comparing.compare(i1.getDescription(), i2.getDescription());
    });
    // combine highlighting data with original text
    StringBuilder sb = new StringBuilder();
    Couple<Integer> result = composeText(sb, list, 0, text, text.length(), 0, showAttributesKeys);
    sb.insert(0, text.substring(0, result.second));
    return sb.toString();
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) GutterIconRenderer(com.intellij.openapi.editor.markup.GutterIconRenderer) FileComparisonFailure(com.intellij.rt.execution.junit.FileComparisonFailure) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) CodeInsightTestFixture(com.intellij.testFramework.fixtures.CodeInsightTestFixture) THashSet(gnu.trove.THashSet) SeveritiesProvider(com.intellij.codeInsight.daemon.impl.SeveritiesProvider) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) Matcher(java.util.regex.Matcher) CommonBundle(com.intellij.CommonBundle) EffectType(com.intellij.openapi.editor.markup.EffectType) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Logger(com.intellij.openapi.diagnostic.Logger) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) RangeMarker(com.intellij.openapi.editor.RangeMarker) Extensions(com.intellij.openapi.extensions.Extensions) ConstantFunction(com.intellij.util.ConstantFunction) StringUtil(com.intellij.openapi.util.text.StringUtil) NullableFunction(com.intellij.util.NullableFunction) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) HighlighterColors(com.intellij.openapi.editor.HighlighterColors) Field(java.lang.reflect.Field) Pass(com.intellij.codeHighlighting.Pass) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) List(java.util.List) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Result(com.intellij.openapi.application.Result) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 3 with NullableFunction

use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.

the class NonProjectFileAccessTest method typeAndCheck.

private void typeAndCheck(VirtualFile file, @Nullable final NonProjectFileWritingAccessProvider.UnlockOption option, boolean fileHasBeenChanged) {
    Editor editor = getEditor(file);
    NullableFunction<List<VirtualFile>, NonProjectFileWritingAccessProvider.UnlockOption> unlocker = files -> option;
    NonProjectFileWritingAccessProvider.setCustomUnlocker(unlocker);
    String before = editor.getDocument().getText();
    typeInChar(editor, 'a');
    if (fileHasBeenChanged) {
        assertEquals("Text should be changed", 'a' + before, editor.getDocument().getText());
    } else {
        assertEquals("Text should not be changed", before, editor.getDocument().getText());
    }
}
Also used : FileEditorManagerEx(com.intellij.openapi.fileEditor.ex.FileEditorManagerEx) java.util(java.util) WriteAction(com.intellij.openapi.application.WriteAction) DataContext(com.intellij.openapi.actionSystem.DataContext) EditorNotifications(com.intellij.ui.EditorNotifications) ModuleManager(com.intellij.openapi.module.ModuleManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) THashSet(gnu.trove.THashSet) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) FileUtil(com.intellij.openapi.util.io.FileUtil) EditorActionManager(com.intellij.openapi.editor.actionSystem.EditorActionManager) Module(com.intellij.openapi.module.Module) Extensions(com.intellij.openapi.extensions.Extensions) PlatformTestUtil(com.intellij.testFramework.PlatformTestUtil) EmptyModuleType(com.intellij.openapi.module.EmptyModuleType) ProjectManagerEx(com.intellij.openapi.project.ex.ProjectManagerEx) NullableFunction(com.intellij.util.NullableFunction) IOException(java.io.IOException) Editor(com.intellij.openapi.editor.Editor) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) NonProjectFileWritingAccessExtension(com.intellij.openapi.fileEditor.impl.NonProjectFileWritingAccessExtension) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) File(java.io.File) NonProjectFileWritingAccessProvider(com.intellij.openapi.fileEditor.impl.NonProjectFileWritingAccessProvider) EditorTestUtil(com.intellij.testFramework.EditorTestUtil) Nullable(org.jetbrains.annotations.Nullable) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Result(com.intellij.openapi.application.Result) ApplicationManager(com.intellij.openapi.application.ApplicationManager) EditorNotificationsImpl(com.intellij.ui.EditorNotificationsImpl) ComponentManagerImpl(com.intellij.openapi.components.impl.ComponentManagerImpl) NotNull(org.jetbrains.annotations.NotNull) WritingAccessProvider(com.intellij.openapi.vfs.WritingAccessProvider) Editor(com.intellij.openapi.editor.Editor)

Example 4 with NullableFunction

use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.

the class GppExpectedTypesContributor method addExpectedConstructorParameters.

private static List<TypeConstraint> addExpectedConstructorParameters(GrListOrMap list, GrExpression[] args, GrExpression arg) {
    PsiType[] argTypes = ContainerUtil.map2Array(args, PsiType.class, (NullableFunction<GrExpression, PsiType>) grExpression -> grExpression.getType());
    final ArrayList<TypeConstraint> result = new ArrayList<>();
    for (PsiType type : GroovyExpectedTypesProvider.getDefaultExpectedTypes(list)) {
        if (type instanceof PsiClassType) {
            for (GroovyResolveResult resolveResult : PsiUtil.getConstructorCandidates((PsiClassType) type, argTypes, list)) {
                final PsiElement method = resolveResult.getElement();
                if (method instanceof PsiMethod && ((PsiMethod) method).isConstructor()) {
                    final Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(resolveResult, list, false, true, GrNamedArgument.EMPTY_ARRAY, args, GrClosableBlock.EMPTY_ARRAY);
                    if (map != null) {
                        final Pair<PsiParameter, PsiType> pair = map.get(arg);
                        if (pair != null) {
                            result.add(SubtypeConstraint.create(pair.second));
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) GroovyExpectedTypesContributor(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.GroovyExpectedTypesContributor) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) NullableFunction(com.intellij.util.NullableFunction) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) ArrayList(java.util.ArrayList) List(java.util.List) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) Map(java.util.Map) Pair(com.intellij.openapi.util.Pair) SubtypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SubtypeConstraint) com.intellij.psi(com.intellij.psi) GroovyExpectedTypesProvider(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.GroovyExpectedTypesProvider) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) Pair(com.intellij.openapi.util.Pair)

Example 5 with NullableFunction

use of com.intellij.util.NullableFunction in project intellij-community by JetBrains.

the class HierarchyBrowserBaseEx method createTree.

protected final JTree createTree(boolean dndAware) {
    final Tree tree;
    final NullableFunction<Object, PsiElement> toPsiConverter = o -> {
        if (o instanceof HierarchyNodeDescriptor) {
            return ((HierarchyNodeDescriptor) o).getContainingFile();
        }
        return null;
    };
    if (dndAware) {
        //noinspection Duplicates
        tree = new DnDAwareTree(new DefaultTreeModel(new DefaultMutableTreeNode(""))) {

            @Override
            public void removeNotify() {
                super.removeNotify();
                if (ScreenUtil.isStandardAddRemoveNotify(this))
                    myRefreshAction.unregisterCustomShortcutSet(this);
            }

            @Override
            public boolean isFileColorsEnabled() {
                return ProjectViewTree.isFileColorsEnabledFor(this);
            }

            @Override
            public Color getFileColorFor(Object object) {
                return ProjectViewTree.getColorForObject(object, myProject, toPsiConverter);
            }
        };
        if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
            DnDManager.getInstance().registerSource(new DnDSource() {

                @Override
                public boolean canStartDragging(final DnDAction action, final Point dragOrigin) {
                    return getSelectedElements().length > 0;
                }

                @Override
                public DnDDragStartBean startDragging(final DnDAction action, final Point dragOrigin) {
                    return new DnDDragStartBean(new TransferableWrapper() {

                        @Override
                        public TreeNode[] getTreeNodes() {
                            return tree.getSelectedNodes(TreeNode.class, null);
                        }

                        @Override
                        public PsiElement[] getPsiElements() {
                            return getSelectedElements();
                        }

                        @Override
                        public List<File> asFileList() {
                            return PsiCopyPasteManager.asFileList(getPsiElements());
                        }
                    });
                }

                @Override
                public Pair<Image, Point> createDraggedImage(final DnDAction action, final Point dragOrigin) {
                    return null;
                }

                @Override
                public void dragDropEnd() {
                }

                @Override
                public void dropActionChanged(final int gestureModifiers) {
                }
            }, tree);
        }
    } else {
        //noinspection Duplicates
        tree = new Tree(new DefaultTreeModel(new DefaultMutableTreeNode(""))) {

            @Override
            public void removeNotify() {
                super.removeNotify();
                if (ScreenUtil.isStandardAddRemoveNotify(this))
                    myRefreshAction.unregisterCustomShortcutSet(this);
            }

            @Override
            public boolean isFileColorsEnabled() {
                return ProjectViewTree.isFileColorsEnabledFor(this);
            }

            @Override
            public Color getFileColorFor(Object object) {
                return ProjectViewTree.getColorForObject(object, myProject, toPsiConverter);
            }
        };
    }
    configureTree(tree);
    EditSourceOnDoubleClickHandler.install(tree);
    EditSourceOnEnterKeyHandler.install(tree);
    myRefreshAction.registerShortcutOn(tree);
    return tree;
}
Also used : EditScopesDialog(com.intellij.ide.util.scopeChooser.EditScopesDialog) java.util(java.util) AllIcons(com.intellij.icons.AllIcons) TreeNode(javax.swing.tree.TreeNode) MessageFormat(java.text.MessageFormat) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) EditSourceOnDoubleClickHandler(com.intellij.util.EditSourceOnDoubleClickHandler) NamedScope(com.intellij.psi.search.scope.packageSet.NamedScope) JBUI(com.intellij.util.ui.JBUI) ProjectViewTree(com.intellij.ide.projectView.impl.ProjectViewTree) Disposer(com.intellij.openapi.util.Disposer) Project(com.intellij.openapi.project.Project) Tree(com.intellij.ui.treeStructure.Tree) com.intellij.ide.dnd(com.intellij.ide.dnd) ComboBoxAction(com.intellij.openapi.actionSystem.ex.ComboBoxAction) Logger(com.intellij.openapi.diagnostic.Logger) OccurenceNavigator(com.intellij.ide.OccurenceNavigator) PsiElementNavigatable(com.intellij.openapi.fileEditor.PsiElementNavigatable) EditSourceOnEnterKeyHandler(com.intellij.util.EditSourceOnEnterKeyHandler) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) NamedScopesHolder(com.intellij.psi.search.scope.packageSet.NamedScopesHolder) BrowseHierarchyActionBase(com.intellij.ide.hierarchy.actions.BrowseHierarchyActionBase) LanguageExtension(com.intellij.lang.LanguageExtension) ScreenUtil(com.intellij.ui.ScreenUtil) NullableFunction(com.intellij.util.NullableFunction) DnDAwareTree(com.intellij.ide.dnd.aware.DnDAwareTree) File(java.io.File) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) java.awt(java.awt) IdeBundle(com.intellij.ide.IdeBundle) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) Pair(com.intellij.openapi.util.Pair) ApplicationManager(com.intellij.openapi.application.ApplicationManager) OccurenceNavigatorSupport(com.intellij.ide.OccurenceNavigatorSupport) Navigatable(com.intellij.pom.Navigatable) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) ScrollPaneFactory(com.intellij.ui.ScrollPaneFactory) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ProjectViewTree(com.intellij.ide.projectView.impl.ProjectViewTree) Tree(com.intellij.ui.treeStructure.Tree) DnDAwareTree(com.intellij.ide.dnd.aware.DnDAwareTree) DnDAwareTree(com.intellij.ide.dnd.aware.DnDAwareTree) File(java.io.File) Pair(com.intellij.openapi.util.Pair)

Aggregations

NullableFunction (com.intellij.util.NullableFunction)19 NotNull (org.jetbrains.annotations.NotNull)17 ContainerUtil (com.intellij.util.containers.ContainerUtil)15 Nullable (org.jetbrains.annotations.Nullable)13 List (java.util.List)12 Project (com.intellij.openapi.project.Project)11 StringUtil (com.intellij.openapi.util.text.StringUtil)7 Logger (com.intellij.openapi.diagnostic.Logger)6 java.util (java.util)6 Collections (java.util.Collections)6 Comparing (com.intellij.openapi.util.Comparing)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 com.intellij.psi (com.intellij.psi)5 java.awt (java.awt)5 javax.swing (javax.swing)5 ArrayList (java.util.ArrayList)4 Extensions (com.intellij.openapi.extensions.Extensions)3 Ref (com.intellij.openapi.util.Ref)3 PsiFile (com.intellij.psi.PsiFile)3 ActionEvent (java.awt.event.ActionEvent)3