Search in sources :

Example 21 with Ref

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

the class LiveProvider method getEarliestBunchInIntervalImpl.

private Fragment getEarliestBunchInIntervalImpl(long earliestRevision, final long oldestRevision, final int desirableSize, final boolean includeYoungest, final boolean includeOldest, final long earliestToTake) throws VcsException {
    if ((myEarliestRevisionWasAccessed) || ((oldestRevision == myYoungestRevision) && ((!includeYoungest) || (!includeOldest)))) {
        return null;
    }
    final SVNRevision youngRevision = (earliestRevision == -1) ? SVNRevision.HEAD : SVNRevision.create(earliestRevision);
    final Ref<List<CommittedChangeList>> refToList = new Ref<>();
    final Ref<VcsException> exceptionRef = new Ref<>();
    final Runnable loader = () -> {
        try {
            refToList.set(myLoader.loadInterval(youngRevision, SVNRevision.create(oldestRevision), desirableSize, includeYoungest, includeOldest));
        } catch (VcsException e) {
            exceptionRef.set(e);
        }
    };
    final Application application = ApplicationManager.getApplication();
    if (application.isUnitTestMode() || !application.isDispatchThread()) {
        loader.run();
    } else {
        ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
            final ProgressIndicator ind = ProgressManager.getInstance().getProgressIndicator();
            if (ind != null) {
                ind.setText(SvnBundle.message("progress.live.provider.loading.revisions.details.text"));
            }
            loader.run();
        }, SvnBundle.message("progress.live.provider.loading.revisions.text"), false, myVcs.getProject());
    }
    if (!exceptionRef.isNull()) {
        final VcsException e = exceptionRef.get();
        if (isElementNotFound(e)) {
            // occurs when target URL is deleted in repository
            // try to find latest existent revision. expensive ...
            final LatestExistentSearcher searcher = new LatestExistentSearcher(oldestRevision, myYoungestRevision, (oldestRevision != 0), myVcs, myLocation.toSvnUrl(), myRepositoryUrl);
            final long existent = searcher.getLatestExistent();
            if ((existent == -1) || (existent == earliestRevision)) {
                myEarliestRevisionWasAccessed = true;
                return null;
            }
            return getEarliestBunchInIntervalImpl(existent, oldestRevision, includeYoungest ? desirableSize : (desirableSize + 1), true, includeOldest, Math.min(existent, earliestRevision));
        }
        throw e;
    }
    final List<CommittedChangeList> list = refToList.get();
    if (list.isEmpty()) {
        myEarliestRevisionWasAccessed = (oldestRevision == 0);
        return null;
    }
    if (earliestToTake > 0) {
        for (Iterator<CommittedChangeList> iterator = list.iterator(); iterator.hasNext(); ) {
            final CommittedChangeList changeList = iterator.next();
            if (changeList.getNumber() > earliestToTake)
                iterator.remove();
        }
    }
    myEarliestRevisionWasAccessed = (oldestRevision == 0) && ((list.size() + ((!includeOldest) ? 1 : 0) + ((!includeYoungest) ? 1 : 0)) < desirableSize);
    return new Fragment(Origin.LIVE, list, true, true, null);
}
Also used : CommittedChangeList(com.intellij.openapi.vcs.versionBrowser.CommittedChangeList) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VcsException(com.intellij.openapi.vcs.VcsException) List(java.util.List) CommittedChangeList(com.intellij.openapi.vcs.versionBrowser.CommittedChangeList) SVNRevision(org.tmatesoft.svn.core.wc.SVNRevision) Application(com.intellij.openapi.application.Application)

Example 22 with Ref

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

the class CreateSnapShotAction method runSnapShooterSession.

private static void runSnapShooterSession(final SnapShotClient client, final Project project, final PsiDirectory dir, final IdeView view) {
    try {
        client.suspendSwing();
    } catch (IOException e1) {
        Messages.showMessageDialog(project, UIDesignerBundle.message("snapshot.connection.error"), UIDesignerBundle.message("snapshot.title"), Messages.getInformationIcon());
        return;
    }
    final MyDialog dlg = new MyDialog(project, client, dir);
    dlg.show();
    if (dlg.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
        final int id = dlg.getSelectedComponentId();
        final Ref<Object> result = new Ref<>();
        ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
            try {
                result.set(client.createSnapshot(id));
            } catch (Exception ex) {
                result.set(ex);
            }
        }, UIDesignerBundle.message("progress.creating.snapshot"), false, project);
        String snapshot = null;
        if (result.get() instanceof String) {
            snapshot = (String) result.get();
        } else {
            Exception ex = (Exception) result.get();
            Messages.showMessageDialog(project, UIDesignerBundle.message("snapshot.create.error", ex.getMessage()), UIDesignerBundle.message("snapshot.title"), Messages.getErrorIcon());
        }
        if (snapshot != null) {
            final String snapshot1 = snapshot;
            ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(project, () -> {
                try {
                    PsiFile formFile = PsiFileFactory.getInstance(dir.getProject()).createFileFromText(dlg.getFormName() + GuiFormFileType.DOT_DEFAULT_EXTENSION, snapshot1);
                    formFile = (PsiFile) dir.add(formFile);
                    formFile.getVirtualFile().setCharset(CharsetToolkit.UTF8_CHARSET);
                    formFile.getViewProvider().getDocument().setText(snapshot1);
                    view.selectElement(formFile);
                } catch (IncorrectOperationException ex) {
                    Messages.showMessageDialog(project, UIDesignerBundle.message("snapshot.save.error", ex.getMessage()), UIDesignerBundle.message("snapshot.title"), Messages.getErrorIcon());
                }
            }, "", null));
        }
    }
    try {
        client.resumeSwing();
    } catch (IOException ex) {
        Messages.showErrorDialog(project, UIDesignerBundle.message("snapshot.connection.broken"), UIDesignerBundle.message("snapshot.title"));
    }
    client.dispose();
}
Also used : Ref(com.intellij.openapi.util.Ref) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ExecutionException(com.intellij.execution.ExecutionException) IOException(java.io.IOException)

Example 23 with Ref

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

the class PyNamedParameterImpl method getParametersByCallArgument.

@NotNull
private List<PyNamedParameter> getParametersByCallArgument(@NotNull PsiElement element, @NotNull TypeEvalContext context) {
    final PyArgumentList argumentList = PsiTreeUtil.getParentOfType(element, PyArgumentList.class);
    if (argumentList != null) {
        boolean elementIsArgument = false;
        for (PyExpression argument : argumentList.getArgumentExpressions()) {
            if (PyPsiUtils.flattenParens(argument) == element) {
                elementIsArgument = true;
                break;
            }
        }
        final PyCallExpression callExpression = argumentList.getCallExpression();
        if (elementIsArgument && callExpression != null) {
            final PyExpression callee = callExpression.getCallee();
            if (callee instanceof PyReferenceExpression) {
                final PyReferenceExpression calleeReferenceExpr = (PyReferenceExpression) callee;
                final PyExpression firstQualifier = PyPsiUtils.getFirstQualifier(calleeReferenceExpr);
                if (firstQualifier != null) {
                    final PsiReference ref = firstQualifier.getReference();
                    if (ref != null && ref.isReferenceTo(this)) {
                        return Collections.emptyList();
                    }
                }
            }
            final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
            return callExpression.multiMapArguments(resolveContext).stream().flatMap(mapping -> mapping.getMappedParameters().entrySet().stream()).filter(entry -> entry.getKey() == element).map(Map.Entry::getValue).collect(Collectors.toList());
        }
    }
    return Collections.emptyList();
}
Also used : PyNames(com.jetbrains.python.PyNames) java.util(java.util) PythonDocumentationProvider(com.jetbrains.python.documentation.PythonDocumentationProvider) ScopeUtil(com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) PyElementTypes(com.jetbrains.python.PyElementTypes) PythonDialectsTokenSetProvider(com.jetbrains.python.PythonDialectsTokenSetProvider) PyNamedParameterStub(com.jetbrains.python.psi.stubs.PyNamedParameterStub) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ItemPresentation(com.intellij.navigation.ItemPresentation) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) com.jetbrains.python.psi(com.jetbrains.python.psi) com.jetbrains.python.psi.types(com.jetbrains.python.psi.types) PyTypingTypeProvider(com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider) PlatformIcons(com.intellij.util.PlatformIcons) Extensions(com.intellij.openapi.extensions.Extensions) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IStubElementType(com.intellij.psi.stubs.IStubElementType) PyTokenTypes(com.jetbrains.python.PyTokenTypes) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) Collectors(java.util.stream.Collectors) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) StreamEx(one.util.streamex.StreamEx) Processor(com.intellij.util.Processor) Pair(com.intellij.openapi.util.Pair) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) javax.swing(javax.swing) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with Ref

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

the class PyNamedParameterImpl method collectUsedAttributes.

@NotNull
private Set<String> collectUsedAttributes(@NotNull final TypeEvalContext context) {
    final Set<String> result = new LinkedHashSet<>();
    final ScopeOwner owner = ScopeUtil.getScopeOwner(this);
    final String name = getName();
    if (owner != null && name != null) {
        owner.accept(new PyRecursiveElementVisitor() {

            @Override
            public void visitPyElement(PyElement node) {
                if (node instanceof ScopeOwner && node != owner) {
                    return;
                }
                if (node instanceof PyQualifiedExpression) {
                    final PyQualifiedExpression expr = (PyQualifiedExpression) node;
                    final PyExpression qualifier = expr.getQualifier();
                    if (qualifier != null) {
                        final String attributeName = expr.getReferencedName();
                        final PyExpression referencedExpr = node instanceof PyBinaryExpression && PyNames.isRightOperatorName(attributeName) ? ((PyBinaryExpression) node).getRightExpression() : qualifier;
                        if (referencedExpr != null) {
                            final PsiReference ref = referencedExpr.getReference();
                            if (ref != null && ref.isReferenceTo(PyNamedParameterImpl.this)) {
                                if (attributeName != null && !result.contains(attributeName)) {
                                    result.add(attributeName);
                                }
                            }
                        }
                    } else {
                        final PsiReference ref = expr.getReference();
                        if (ref != null && ref.isReferenceTo(PyNamedParameterImpl.this)) {
                            StreamEx.of(getParametersByCallArgument(expr, context)).nonNull().map(context::getType).select(PyStructuralType.class).forEach(type -> result.addAll(type.getAttributeNames()));
                        }
                    }
                }
                super.visitPyElement(node);
            }

            @Override
            public void visitPyIfStatement(PyIfStatement node) {
                final PyExpression ifCondition = node.getIfPart().getCondition();
                if (ifCondition != null) {
                    ifCondition.accept(this);
                }
                for (PyIfPart part : node.getElifParts()) {
                    final PyExpression elseIfCondition = part.getCondition();
                    if (elseIfCondition != null) {
                        elseIfCondition.accept(this);
                    }
                }
            }

            @Override
            public void visitPyCallExpression(PyCallExpression node) {
                Optional.ofNullable(node.getCallee()).filter(callee -> "len".equals(callee.getName())).map(PyExpression::getReference).map(PsiReference::resolve).filter(element -> PyBuiltinCache.getInstance(element).isBuiltin(element)).ifPresent(callable -> {
                    final PyReferenceExpression argument = node.getArgument(0, PyReferenceExpression.class);
                    if (argument != null && argument.getReference().isReferenceTo(PyNamedParameterImpl.this)) {
                        result.add(PyNames.LEN);
                    }
                });
                super.visitPyCallExpression(node);
            }

            @Override
            public void visitPyForStatement(PyForStatement node) {
                Optional.of(node.getForPart()).map(PyForPart::getSource).map(PyExpression::getReference).filter(reference -> reference.isReferenceTo(PyNamedParameterImpl.this)).ifPresent(reference -> result.add(PyNames.ITER));
                super.visitPyForStatement(node);
            }
        });
    }
    return result;
}
Also used : PyNames(com.jetbrains.python.PyNames) java.util(java.util) PythonDocumentationProvider(com.jetbrains.python.documentation.PythonDocumentationProvider) ScopeUtil(com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) PyElementTypes(com.jetbrains.python.PyElementTypes) PythonDialectsTokenSetProvider(com.jetbrains.python.PythonDialectsTokenSetProvider) PyNamedParameterStub(com.jetbrains.python.psi.stubs.PyNamedParameterStub) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ItemPresentation(com.intellij.navigation.ItemPresentation) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) com.jetbrains.python.psi(com.jetbrains.python.psi) com.jetbrains.python.psi.types(com.jetbrains.python.psi.types) PyTypingTypeProvider(com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider) PlatformIcons(com.intellij.util.PlatformIcons) Extensions(com.intellij.openapi.extensions.Extensions) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IStubElementType(com.intellij.psi.stubs.IStubElementType) PyTokenTypes(com.jetbrains.python.PyTokenTypes) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) Collectors(java.util.stream.Collectors) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) StreamEx(one.util.streamex.StreamEx) Processor(com.intellij.util.Processor) Pair(com.intellij.openapi.util.Pair) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) javax.swing(javax.swing) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with Ref

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

the class XmlBuilderDriver method processPrologNode.

private void processPrologNode(PsiBuilder psiBuilder, XmlBuilder builder, FlyweightCapableTreeStructure<LighterASTNode> structure, LighterASTNode prolog) {
    final Ref<LighterASTNode[]> prologChildren = new Ref<>(null);
    final int prologChildrenCount = structure.getChildren(structure.prepareForGetChildren(prolog), prologChildren);
    for (int i = 0; i < prologChildrenCount; i++) {
        LighterASTNode node = prologChildren.get()[i];
        IElementType type = node.getTokenType();
        if (type == XmlElementType.XML_DOCTYPE) {
            processDoctypeNode(builder, structure, node);
            break;
        }
        if (type == TokenType.ERROR_ELEMENT) {
            processErrorNode(psiBuilder, node, builder);
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Ref(com.intellij.openapi.util.Ref)

Aggregations

Ref (com.intellij.openapi.util.Ref)253 Nullable (org.jetbrains.annotations.Nullable)82 NotNull (org.jetbrains.annotations.NotNull)70 VirtualFile (com.intellij.openapi.vfs.VirtualFile)48 Project (com.intellij.openapi.project.Project)38 List (java.util.List)30 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)26 PsiElement (com.intellij.psi.PsiElement)26 IOException (java.io.IOException)24 ArrayList (java.util.ArrayList)22 PsiFile (com.intellij.psi.PsiFile)20 Module (com.intellij.openapi.module.Module)18 Editor (com.intellij.openapi.editor.Editor)16 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)16 File (java.io.File)16 Task (com.intellij.openapi.progress.Task)15 Pair (com.intellij.openapi.util.Pair)14 VcsException (com.intellij.openapi.vcs.VcsException)14 IncorrectOperationException (com.intellij.util.IncorrectOperationException)14 Document (com.intellij.openapi.editor.Document)13