Search in sources :

Example 11 with Application

use of com.intellij.openapi.application.Application in project intellij-community by JetBrains.

the class PyPackagesUpdater method runActivity.

@Override
public void runActivity(@NotNull final Project project) {
    final Application application = ApplicationManager.getApplication();
    if (application.isUnitTestMode()) {
        return;
    }
    final PyPackageService service = PyPackageService.getInstance();
    if (checkNeeded(project, service)) {
        application.executeOnPooledThread(() -> {
            try {
                PyPIPackageUtil.INSTANCE.updatePyPICache(service);
                service.LAST_TIME_CHECKED = System.currentTimeMillis();
            } catch (IOException e) {
                LOG.warn(e.getMessage());
            }
        });
    }
    if (checkCondaUpdateNeeded(project)) {
        application.executeOnPooledThread(() -> PyCondaPackageService.getInstance().updatePackagesCache());
    }
}
Also used : IOException(java.io.IOException) Application(com.intellij.openapi.application.Application)

Example 12 with Application

use of com.intellij.openapi.application.Application in project intellij-community by JetBrains.

the class SvnCompatibilityChecker method reportNoRoots.

public void reportNoRoots(final List<VirtualFile> result) {
    synchronized (myLock) {
        if (myInvocationCounter >= ourInvocationMax)
            return;
        ++myCounter;
        --myDownStartCounter;
        if ((myCounter > ourFrequency) || (myDownStartCounter >= 0)) {
            myCounter = 0;
            ++myInvocationCounter;
            final Application application = ApplicationManager.getApplication();
            application.executeOnPooledThread(() -> {
                final List<VirtualFile> suspicious = new ArrayList<>();
                for (VirtualFile vf : result) {
                    if (SvnUtil.seemsLikeVersionedDir(vf)) {
                        suspicious.add(vf);
                    }
                }
                if (!suspicious.isEmpty()) {
                    final String message = (suspicious.size() == 1) ? "Root '" + suspicious.get(0).getPresentableName() + "' is likely to be of unsupported Subversion format" : "Some roots are likely to be of unsupported Subversion format";
                    application.invokeLater(() -> new VcsBalloonProblemNotifier(myProject, message, MessageType.WARNING).run(), ModalityState.NON_MODAL, o -> (!myProject.isOpen()) || myProject.isDisposed());
                }
            });
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VcsBalloonProblemNotifier(com.intellij.openapi.vcs.ui.VcsBalloonProblemNotifier) ArrayList(java.util.ArrayList) Application(com.intellij.openapi.application.Application)

Example 13 with Application

use of com.intellij.openapi.application.Application in project intellij-community by JetBrains.

the class ExclusiveBackgroundVcsAction method run.

public static void run(final Project project, final Runnable action) {
    final ProjectLevelVcsManager plVcsManager = ProjectLevelVcsManager.getInstance(project);
    plVcsManager.startBackgroundVcsOperation();
    try {
        action.run();
    } finally {
        final Application application = ApplicationManager.getApplication();
        if (application.isDispatchThread()) {
            application.executeOnPooledThread(() -> plVcsManager.stopBackgroundVcsOperation());
        } else {
            plVcsManager.stopBackgroundVcsOperation();
        }
    }
}
Also used : ProjectLevelVcsManager(com.intellij.openapi.vcs.ProjectLevelVcsManager) Application(com.intellij.openapi.application.Application)

Example 14 with Application

use of com.intellij.openapi.application.Application in project intellij-community by JetBrains.

the class YouTrackCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull CompletionResultSet result) {
    if (LOG.isDebugEnabled()) {
        LOG.debug(DebugUtil.psiToString(parameters.getOriginalFile(), true));
    }
    super.fillCompletionVariants(parameters, result);
    PsiFile file = parameters.getOriginalFile();
    final YouTrackIntellisense intellisense = file.getUserData(YouTrackIntellisense.INTELLISENSE_KEY);
    if (intellisense == null) {
        return;
    }
    final Application application = ApplicationManager.getApplication();
    Future<List<CompletionItem>> future = application.executeOnPooledThread(() -> intellisense.fetchCompletion(parameters.getOriginalFile().getText(), parameters.getOffset()));
    try {
        final List<CompletionItem> suggestions = future.get(TIMEOUT, TimeUnit.MILLISECONDS);
        // actually backed by original CompletionResultSet
        result = result.withPrefixMatcher(extractPrefix(parameters)).caseInsensitive();
        result.addAllElements(ContainerUtil.map(suggestions, (Function<CompletionItem, LookupElement>) item -> LookupElementBuilder.create(item, item.getOption()).withTypeText(item.getDescription(), true).withInsertHandler(INSERT_HANDLER).withBoldness(item.getStyleClass().equals("keyword"))));
    } catch (Exception ignored) {
        //noinspection InstanceofCatchParameter
        if (ignored instanceof TimeoutException) {
            LOG.debug(String.format("YouTrack request took more than %d ms to complete", TIMEOUT));
        }
        LOG.debug(ignored);
    }
}
Also used : Function(com.intellij.util.Function) YouTrackIntellisense(com.intellij.tasks.youtrack.YouTrackIntellisense) CompletionItem(com.intellij.tasks.youtrack.YouTrackIntellisense.CompletionItem) PsiFile(com.intellij.psi.PsiFile) List(java.util.List) Application(com.intellij.openapi.application.Application) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 15 with Application

use of com.intellij.openapi.application.Application in project intellij-community by JetBrains.

the class ImportToggleAliasIntention method doInvoke.

@Override
public void doInvoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    // sanity check: isAvailable must have set it.
    final IntentionState state = IntentionState.fromContext(editor, file);
    //
    // we set in in the source
    final String target_name;
    // we replace it in the source
    final String remove_name;
    PyReferenceExpression reference = sure(state.myImportElement.getImportReferenceExpression());
    // search for references to us with the right name
    try {
        String imported_name = PyPsiUtils.toPath(reference);
        if (state.myAlias != null) {
            // have to remove alias, rename everything to original
            target_name = imported_name;
            remove_name = state.myAlias;
        } else {
            // ask for and add alias
            Application application = ApplicationManager.getApplication();
            if (application != null && !application.isUnitTestMode()) {
                String alias = Messages.showInputDialog(project, PyBundle.message("INTN.alias.for.$0.dialog.title", imported_name), "Add Alias", Messages.getQuestionIcon(), "", new InputValidator() {

                    @Override
                    public boolean checkInput(String inputString) {
                        return PyNames.isIdentifier(inputString);
                    }

                    @Override
                    public boolean canClose(String inputString) {
                        return PyNames.isIdentifier(inputString);
                    }
                });
                if (alias == null) {
                    return;
                }
                target_name = alias;
            } else {
                // test mode
                target_name = "alias";
            }
            remove_name = imported_name;
        }
        final PsiElement referee = reference.getReference().resolve();
        if (referee != null && imported_name != null) {
            final Collection<PsiReference> references = new ArrayList<>();
            final ScopeOwner scope = PsiTreeUtil.getParentOfType(state.myImportElement, ScopeOwner.class);
            PsiTreeUtil.processElements(scope, new PsiElementProcessor() {

                public boolean execute(@NotNull PsiElement element) {
                    getReferences(element);
                    if (element instanceof PyStringLiteralExpression) {
                        final PsiLanguageInjectionHost host = (PsiLanguageInjectionHost) element;
                        final List<Pair<PsiElement, TextRange>> files = InjectedLanguageManager.getInstance(project).getInjectedPsiFiles(host);
                        if (files != null) {
                            for (Pair<PsiElement, TextRange> pair : files) {
                                final PsiElement first = pair.getFirst();
                                if (first instanceof ScopeOwner) {
                                    final ScopeOwner scopeOwner = (ScopeOwner) first;
                                    PsiTreeUtil.processElements(scopeOwner, new PsiElementProcessor() {

                                        public boolean execute(@NotNull PsiElement element) {
                                            getReferences(element);
                                            return true;
                                        }
                                    });
                                }
                            }
                        }
                    }
                    return true;
                }

                private void getReferences(PsiElement element) {
                    if (element instanceof PyReferenceExpression && PsiTreeUtil.getParentOfType(element, PyImportElement.class) == null) {
                        PyReferenceExpression ref = (PyReferenceExpression) element;
                        if (remove_name.equals(PyPsiUtils.toPath(ref))) {
                            // filter out other names that might resolve to our target
                            PsiElement resolved = ref.getReference().resolve();
                            if (resolved == referee)
                                references.add(ref.getReference());
                        }
                    }
                }
            });
            // no references here is OK by us.
            if (showConflicts(project, findDefinitions(target_name, references, Collections.<PsiElement>emptySet()), target_name, null)) {
                // got conflicts
                return;
            }
            // alter the import element
            PyElementGenerator generator = PyElementGenerator.getInstance(project);
            final LanguageLevel languageLevel = LanguageLevel.forElement(state.myImportElement);
            if (state.myAlias != null) {
                // remove alias
                ASTNode node = sure(state.myImportElement.getNode());
                ASTNode parent = sure(node.getTreeParent());
                // this is the reference
                node = sure(node.getFirstChildNode());
                // things past the reference: space, 'as', and alias
                node = sure(node.getTreeNext());
                parent.removeRange(node, null);
            } else {
                // add alias
                ASTNode my_ielt_node = sure(state.myImportElement.getNode());
                PyImportElement fountain = generator.createFromText(languageLevel, PyImportElement.class, "import foo as " + target_name, new int[] { 0, 2 });
                // at import elt
                ASTNode graft_node = sure(fountain.getNode());
                // at ref
                graft_node = sure(graft_node.getFirstChildNode());
                // space
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // 'as'
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // space
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // alias
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
            }
            // alter references
            for (PsiReference ref : references) {
                ASTNode ref_name_node = sure(sure(ref.getElement()).getNode());
                ASTNode parent = sure(ref_name_node.getTreeParent());
                ASTNode new_name_node = generator.createExpressionFromText(languageLevel, target_name).getNode();
                assert new_name_node != null;
                parent.replaceChild(ref_name_node, new_name_node);
            }
        }
    } catch (IncorrectOperationException ignored) {
        PyUtil.showBalloon(project, PyBundle.message("QFIX.action.failed"), MessageType.WARNING);
    }
}
Also used : ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) InputValidator(com.intellij.openapi.ui.InputValidator) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) ASTNode(com.intellij.lang.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Application(com.intellij.openapi.application.Application) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Aggregations

Application (com.intellij.openapi.application.Application)188 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)21 IOException (java.io.IOException)14 Project (com.intellij.openapi.project.Project)13 Nullable (org.jetbrains.annotations.Nullable)12 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)9 ModalityState (com.intellij.openapi.application.ModalityState)8 Ref (com.intellij.openapi.util.Ref)7 ArrayList (java.util.ArrayList)7 List (java.util.List)6 ApplicationImpl (com.intellij.openapi.application.impl.ApplicationImpl)5 ApplicationManager (com.intellij.openapi.application.ApplicationManager)4 ApplicationEx (com.intellij.openapi.application.ex.ApplicationEx)4 Document (com.intellij.openapi.editor.Document)4 Module (com.intellij.openapi.module.Module)4 Computable (com.intellij.openapi.util.Computable)4 PsiFile (com.intellij.psi.PsiFile)4 File (java.io.File)4 Editor (com.intellij.openapi.editor.Editor)3