Search in sources :

Example 31 with SmartList

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

the class BackwardReferenceIndexUtil method registerFile.

static void registerFile(String filePath, Collection<? extends JavacRef> refs, List<JavacDef> defs, final BackwardReferenceIndexWriter writer) {
    try {
        final int fileId = writer.enumeratePath(filePath);
        int funExprId = 0;
        final Map<LightRef, Void> definitions = new HashMap<>(defs.size());
        final Map<LightRef, Collection<LightRef>> backwardHierarchyMap = new HashMap<>();
        for (JavacDef def : defs) {
            if (def instanceof JavacDef.JavacClassDef) {
                JavacRef.JavacClass sym = (JavacRef.JavacClass) def.getDefinedElement();
                final LightRef.JavaLightClassRef aClass = writer.asClassUsage(sym);
                definitions.put(aClass, null);
                final JavacRef[] superClasses = ((JavacDef.JavacClassDef) def).getSuperClasses();
                for (JavacRef superClass : superClasses) {
                    LightRef.JavaLightClassRef superClassRef = writer.asClassUsage(superClass);
                    Collection<LightRef> children = backwardHierarchyMap.get(superClassRef);
                    if (children == null) {
                        backwardHierarchyMap.put(superClassRef, children = new SmartList<>());
                    }
                    children.add(aClass);
                }
            } else if (def instanceof JavacDef.JavacFunExprDef) {
                final LightRef.JavaLightClassRef functionalType = writer.asClassUsage(def.getDefinedElement());
                int id = funExprId++;
                LightRef.JavaLightFunExprDef result = new LightRef.JavaLightFunExprDef(id);
                definitions.put(result, null);
                ContainerUtil.getOrCreate(backwardHierarchyMap, functionalType, (Factory<Collection<LightRef>>) () -> new SmartList<>()).add(result);
            }
        }
        Map<LightRef, Void> convertedRefs = new HashMap<>(refs.size());
        for (JavacRef ref : refs) {
            LightRef key = writer.enumerateNames(ref);
            if (key != null) {
                convertedRefs.put(key, null);
            }
        }
        writer.writeData(fileId, new CompiledFileData(backwardHierarchyMap, convertedRefs, definitions));
    } catch (IOException e) {
        writer.setRebuildCause(e);
    }
}
Also used : JavacRef(org.jetbrains.jps.javac.ast.api.JavacRef) IOException(java.io.IOException) CompiledFileData(org.jetbrains.jps.backwardRefs.index.CompiledFileData) JavacDef(org.jetbrains.jps.javac.ast.api.JavacDef) SmartList(com.intellij.util.SmartList)

Example 32 with SmartList

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

the class DocumentCommitThread method commitUnderProgress.

// returns (finish commit Runnable (to be invoked later in EDT), null) on success or (null, failure reason) on failure
@NotNull
private Pair<Runnable, Object> commitUnderProgress(@NotNull final CommitTask task, final boolean synchronously) {
    if (synchronously) {
        assert !task.indicator.isCanceled();
    }
    final Document document = task.getDocument();
    final Project project = task.project;
    final PsiDocumentManagerBase documentManager = (PsiDocumentManagerBase) PsiDocumentManager.getInstance(project);
    final List<Processor<Document>> finishProcessors = new SmartList<>();
    Runnable runnable = () -> {
        myApplication.assertReadAccessAllowed();
        if (project.isDisposed())
            return;
        Lock lock = getDocumentLock(document);
        if (!lock.tryLock()) {
            task.cancel("Can't obtain document lock", this);
            return;
        }
        boolean canceled = false;
        try {
            if (documentManager.isCommitted(document))
                return;
            if (!task.isStillValid()) {
                canceled = true;
                return;
            }
            FileViewProvider viewProvider = documentManager.getCachedViewProvider(document);
            if (viewProvider == null) {
                finishProcessors.add(handleCommitWithoutPsi(documentManager, task));
                return;
            }
            for (Pair<PsiFileImpl, FileASTNode> pair : task.myOldFileNodes) {
                PsiFileImpl file = pair.first;
                if (file.isValid()) {
                    FileASTNode oldFileNode = pair.second;
                    Processor<Document> finishProcessor = doCommit(task, file, oldFileNode);
                    if (finishProcessor != null) {
                        finishProcessors.add(finishProcessor);
                    }
                } else {
                    // file became invalid while sitting in the queue
                    if (task.reason.equals(SYNC_COMMIT_REASON)) {
                        throw new PsiInvalidElementAccessException(file, "File " + file + " invalidated during sync commit");
                    }
                    commitAsynchronously(project, document, "File " + file + " invalidated during background commit; task: " + task, task.myCreationContext);
                }
            }
        } finally {
            lock.unlock();
            if (canceled) {
                task.cancel("Task invalidated", this);
            }
        }
    };
    if (synchronously) {
        runnable.run();
    } else if (!myApplication.tryRunReadAction(runnable)) {
        log(project, "Could not start read action", task, myApplication.isReadAccessAllowed(), Thread.currentThread());
        return new Pair<>(null, "Could not start read action");
    }
    boolean canceled = task.indicator.isCanceled();
    assert !synchronously || !canceled;
    if (canceled) {
        return new Pair<>(null, "Indicator was canceled");
    }
    Runnable result = createEdtRunnable(task, synchronously, finishProcessors);
    return Pair.create(result, null);
}
Also used : Processor(com.intellij.util.Processor) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) Document(com.intellij.openapi.editor.Document) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Project(com.intellij.openapi.project.Project) FileASTNode(com.intellij.lang.FileASTNode) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with SmartList

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

the class ToggleTaskActivationAction method getTasks.

@NotNull
private static List<TaskData> getTasks(AnActionEvent e) {
    final List<ExternalSystemNode> selectedNodes = ExternalSystemDataKeys.SELECTED_NODES.getData(e.getDataContext());
    if (selectedNodes == null)
        return Collections.emptyList();
    List<TaskData> tasks = new SmartList<>();
    for (ExternalSystemNode node : selectedNodes) {
        if (node instanceof TaskNode && !node.isIgnored()) {
            tasks.add((TaskData) node.getData());
        } else if (node instanceof RunConfigurationNode) {
            final RunnerAndConfigurationSettings configurationSettings = ((RunConfigurationNode) node).getSettings();
            final ExternalSystemRunConfiguration runConfiguration = (ExternalSystemRunConfiguration) configurationSettings.getConfiguration();
            final ExternalSystemTaskExecutionSettings taskExecutionSettings = runConfiguration.getSettings();
            tasks.add(new TaskData(taskExecutionSettings.getExternalSystemId(), RUN_CONFIGURATION_TASK_PREFIX + configurationSettings.getName(), taskExecutionSettings.getExternalProjectPath(), null));
        } else {
            return Collections.emptyList();
        }
    }
    return tasks;
}
Also used : TaskNode(com.intellij.openapi.externalSystem.view.TaskNode) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) ExternalSystemRunConfiguration(com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunConfiguration) ExternalSystemNode(com.intellij.openapi.externalSystem.view.ExternalSystemNode) SmartList(com.intellij.util.SmartList) RunConfigurationNode(com.intellij.openapi.externalSystem.view.RunConfigurationNode) TaskData(com.intellij.openapi.externalSystem.model.task.TaskData) ExternalSystemTaskExecutionSettings(com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with SmartList

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

the class PatternValidator method checkExpression.

private void checkExpression(PsiExpression expression, final PsiAnnotation[] annotations, ProblemsHolder holder) {
    if (annotations.length == 0)
        return;
    final PsiAnnotation psiAnnotation = annotations[0];
    // cache compiled pattern with annotation
    CachedValue<Pattern> p = psiAnnotation.getUserData(COMPLIED_PATTERN);
    if (p == null) {
        final CachedValueProvider<Pattern> provider = () -> {
            final String pattern = AnnotationUtilEx.calcAnnotationValue(psiAnnotation, "value");
            Pattern p1 = null;
            if (pattern != null) {
                try {
                    p1 = Pattern.compile(pattern);
                } catch (PatternSyntaxException e) {
                // pattern stays null
                }
            }
            return CachedValueProvider.Result.create(p1, (Object[]) annotations);
        };
        p = CachedValuesManager.getManager(expression.getProject()).createCachedValue(provider, false);
        psiAnnotation.putUserData(COMPLIED_PATTERN, p);
    }
    final Pattern pattern = p.getValue();
    if (pattern == null)
        return;
    List<PsiExpression> nonConstantElements = new SmartList<>();
    final Object result = new SubstitutedExpressionEvaluationHelper(expression.getProject()).computeExpression(expression, myConfiguration.getAdvancedConfiguration().getDfaOption(), false, nonConstantElements);
    final String o = result == null ? null : String.valueOf(result);
    if (o != null) {
        if (!pattern.matcher(o).matches()) {
            if (annotations.length > 1) {
                // the last element contains the element's actual annotation
                final String fqn = annotations[annotations.length - 1].getQualifiedName();
                assert fqn != null;
                final String name = StringUtil.getShortName(fqn);
                holder.registerProblem(expression, MessageFormat.format("Expression ''{0}'' doesn''t match ''{1}'' pattern: {2}", o, name, pattern.pattern()));
            } else {
                holder.registerProblem(expression, MessageFormat.format("Expression ''{0}'' doesn''t match pattern: {1}", o, pattern.pattern()));
            }
        }
    } else if (CHECK_NON_CONSTANT_VALUES) {
        for (PsiExpression expr : nonConstantElements) {
            final PsiElement e;
            if (expr instanceof PsiReferenceExpression) {
                e = ((PsiReferenceExpression) expr).resolve();
            } else if (expr instanceof PsiMethodCallExpression) {
                e = ((PsiMethodCallExpression) expr).getMethodExpression().resolve();
            } else {
                e = expr;
            }
            final PsiModifierListOwner owner = e instanceof PsiModifierListOwner ? (PsiModifierListOwner) e : null;
            LocalQuickFix quickFix;
            if (owner != null && PsiUtilEx.isLanguageAnnotationTarget(owner)) {
                PsiAnnotation[] resolvedAnnos = AnnotationUtilEx.getAnnotationFrom(owner, myConfiguration.getAdvancedConfiguration().getPatternAnnotationPair(), true);
                if (resolvedAnnos.length == 2 && annotations.length == 2 && Comparing.strEqual(resolvedAnnos[1].getQualifiedName(), annotations[1].getQualifiedName())) {
                    // both target and source annotated indirectly with the same anno
                    return;
                }
                final String classname = myConfiguration.getAdvancedConfiguration().getSubstAnnotationPair().first;
                quickFix = AnnotateFix.canApplyOn(owner) ? new AnnotateFix(classname) : new IntroduceVariableFix();
            } else {
                quickFix = new IntroduceVariableFix();
            }
            holder.registerProblem(expr, "Unsubstituted expression", quickFix);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) SubstitutedExpressionEvaluationHelper(org.intellij.plugins.intelliLang.util.SubstitutedExpressionEvaluationHelper) SmartList(com.intellij.util.SmartList) AnnotateFix(org.intellij.plugins.intelliLang.util.AnnotateFix) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 35 with SmartList

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

the class IPPTestCase method assertIntentionNotAvailable.

protected void assertIntentionNotAvailable(Class<? extends IntentionAction> intentionClass) {
    myFixture.configureByFile(getTestName(false) + ".java");
    final List<IntentionAction> result = new SmartList<>();
    for (final IntentionAction intention : myFixture.getAvailableIntentions()) {
        if (intentionClass.isInstance(intention)) {
            result.add(intention);
        } else if (intention instanceof IntentionActionWrapper) {
            final IntentionActionWrapper wrapper = (IntentionActionWrapper) intention;
            if (intentionClass.isInstance(wrapper.getDelegate())) {
                result.add(intention);
            }
        }
    }
    assertEmpty("Intention of class \'" + intentionClass + "\' is available but should not", result);
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction) SmartList(com.intellij.util.SmartList) IntentionActionWrapper(com.intellij.codeInsight.intention.impl.config.IntentionActionWrapper)

Aggregations

SmartList (com.intellij.util.SmartList)163 NotNull (org.jetbrains.annotations.NotNull)70 Nullable (org.jetbrains.annotations.Nullable)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)24 Module (com.intellij.openapi.module.Module)15 Project (com.intellij.openapi.project.Project)14 TextRange (com.intellij.openapi.util.TextRange)12 PsiElement (com.intellij.psi.PsiElement)12 List (java.util.List)12 Element (org.jdom.Element)12 File (java.io.File)11 THashSet (gnu.trove.THashSet)9 ContainerUtil (com.intellij.util.containers.ContainerUtil)8 ArrayList (java.util.ArrayList)8 Collection (java.util.Collection)8 Pair (com.intellij.openapi.util.Pair)7 PsiFile (com.intellij.psi.PsiFile)6 IOException (java.io.IOException)6 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)5 IElementType (com.intellij.psi.tree.IElementType)5