Search in sources :

Example 16 with Computable

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

the class MatcherImpl method prepareMatching.

private CompiledPattern prepareMatching(final MatchResultSink sink, final MatchOptions options) {
    CompiledPattern savedPattern = null;
    if (matchContext.getOptions() == options && matchContext.getPattern() != null && matchContext.getOptions().hashCode() == matchContext.getPattern().getOptionsHashStamp()) {
        savedPattern = matchContext.getPattern();
    }
    matchContext.clear();
    matchContext.setSink(new DuplicateFilteringResultSink(sink));
    matchContext.setOptions(options);
    matchContext.setMatcher(visitor);
    visitor.setMatchContext(matchContext);
    CompiledPattern compiledPattern = savedPattern;
    if (compiledPattern == null) {
        synchronized (lastMatchDataLock) {
            final LastMatchData data = com.intellij.reference.SoftReference.dereference(lastMatchData);
            if (data != null && options == data.lastOptions) {
                compiledPattern = data.lastPattern;
            }
            lastMatchData = null;
        }
        if (compiledPattern == null) {
            compiledPattern = ApplicationManager.getApplication().runReadAction(new Computable<CompiledPattern>() {

                @Override
                public CompiledPattern compute() {
                    return PatternCompiler.compilePattern(project, options);
                }
            });
        }
    }
    cacheCompiledPattern(options, compiledPattern);
    return compiledPattern;
}
Also used : DuplicateFilteringResultSink(com.intellij.structuralsearch.plugin.util.DuplicateFilteringResultSink) Computable(com.intellij.openapi.util.Computable)

Example 17 with Computable

use of com.intellij.openapi.util.Computable in project android by JetBrains.

the class AndroidGotoRelatedProvider method getLazyItemsForClass.

@Nullable
static Computable<List<GotoRelatedItem>> getLazyItemsForClass(@NotNull PsiClass aClass, @NotNull AndroidFacet facet, boolean addDeclarationInManifest) {
    final GotoRelatedItem item = findDeclarationInManifest(aClass);
    final boolean isContextClass = isInheritorOfContextClass(aClass, facet.getModule());
    if (!isContextClass && item == null) {
        return null;
    }
    final List<GotoRelatedItem> items;
    if (isContextClass) {
        items = new ArrayList<GotoRelatedItem>(collectRelatedLayoutFiles(facet, aClass));
        if (addDeclarationInManifest) {
            if (item != null) {
                items.add(item);
            }
        }
        if (items.isEmpty()) {
            return null;
        }
    } else {
        items = Collections.singletonList(item);
    }
    return new Computable<List<GotoRelatedItem>>() {

        @Override
        public List<GotoRelatedItem> compute() {
            return items;
        }
    };
}
Also used : GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) Computable(com.intellij.openapi.util.Computable) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with Computable

use of com.intellij.openapi.util.Computable in project android by JetBrains.

the class LayoutlibCallbackImpl method getXmlFileParser.

@Nullable
@Override
public XmlPullParser getXmlFileParser(String fileName) {
    // layoutlib built-in directories
    if (fileName.contains(EXPLODED_AAR) || fileName.contains(FD_LAYOUTLIB)) {
        return null;
    }
    boolean token = RenderSecurityManager.enterSafeRegion(myCredential);
    try {
        VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(fileName);
        if (virtualFile != null) {
            PsiFile psiFile = AndroidPsiUtils.getPsiFileSafely(myModule.getProject(), virtualFile);
            if (psiFile != null) {
                try {
                    XmlPullParser parser = getParserFactory().createParser(fileName);
                    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
                    String psiText = ApplicationManager.getApplication().isReadAccessAllowed() ? psiFile.getText() : ApplicationManager.getApplication().runReadAction((Computable<String>) psiFile::getText);
                    parser.setInput(new StringReader(psiText));
                    return parser;
                } catch (XmlPullParserException e) {
                    LOG.warn("Could not create parser for " + fileName);
                }
            }
        }
        return null;
    } finally {
        RenderSecurityManager.exitSafeRegion(token);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlPullParser(org.xmlpull.v1.XmlPullParser) StringReader(java.io.StringReader) PsiFile(com.intellij.psi.PsiFile) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Computable(com.intellij.openapi.util.Computable) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with Computable

use of com.intellij.openapi.util.Computable in project android by JetBrains.

the class Template method render.

/**
   * Executes the template, rendering it to output files under the directory context.getModuleRoot()
   *
   * @return true if the template was rendered without finding any errors and there are no warnings
   * or the user selected to proceed with warnings.
   */
public boolean render(@NotNull final RenderingContext context) {
    final Project project = context.getProject();
    boolean success = runWriteCommandAction(project, context.getCommandName(), new Computable<Boolean>() {

        @Override
        public Boolean compute() {
            if (project.isInitialized()) {
                return doRender(context);
            } else {
                return PostprocessReformattingAspect.getInstance(project).disablePostprocessFormattingInside(new Computable<Boolean>() {

                    @Override
                    public Boolean compute() {
                        return doRender(context);
                    }
                });
            }
        }
    });
    String title = myMetadata.getTitle();
    if (title != null) {
        UsageTracker.getInstance().log(AndroidStudioEvent.newBuilder().setCategory(EventCategory.TEMPLATE).setKind(AndroidStudioEvent.EventKind.TEMPLATE_RENDER).setTemplateRenderer(titleToTemplateRenderer(title)));
    }
    if (context.shouldReformat()) {
        TemplateUtils.reformatAndRearrange(project, context.getTargetFiles());
    }
    return success;
}
Also used : Project(com.intellij.openapi.project.Project) Computable(com.intellij.openapi.util.Computable)

Example 20 with Computable

use of com.intellij.openapi.util.Computable in project android by JetBrains.

the class CreateResourceFileAction method createValidatorFactory.

// Or we could pass the action down, the dialog will set the root element, and then it can choose how to
// create the validator on its own. On the other hand this is symmetric with the other dialogs.
@NotNull
private CreateResourceFileDialogBase.ValidatorFactory createValidatorFactory(@NotNull final Project project) {
    return new CreateResourceFileDialogBase.ValidatorFactory() {

        @Override
        @NotNull
        public ElementCreatingValidator create(@NotNull final PsiDirectory resourceDirectory, @NotNull final String subdirName, @Nullable String rootElement) {
            PsiDirectory resSubdir = resourceDirectory.findSubdirectory(subdirName);
            if (resSubdir == null) {
                resSubdir = ApplicationManager.getApplication().runWriteAction((Computable<PsiDirectory>) () -> resourceDirectory.createSubdirectory(subdirName));
            }
            // Stash the chosen rootElement before action is asked to create() the elements.
            myRootElement = rootElement;
            return new MyInputValidator(project, resSubdir);
        }
    };
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable) Computable(com.intellij.openapi.util.Computable) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Computable (com.intellij.openapi.util.Computable)49 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 NotNull (org.jetbrains.annotations.NotNull)11 Nullable (org.jetbrains.annotations.Nullable)11 Project (com.intellij.openapi.project.Project)10 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)8 PsiFile (com.intellij.psi.PsiFile)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 Application (com.intellij.openapi.application.Application)5 Task (com.intellij.openapi.progress.Task)5 IOException (java.io.IOException)5 ApplicationManager (com.intellij.openapi.application.ApplicationManager)4 Editor (com.intellij.openapi.editor.Editor)4 Ref (com.intellij.openapi.util.Ref)4 Module (com.intellij.openapi.module.Module)3 PsiDirectory (com.intellij.psi.PsiDirectory)3 PsiElement (com.intellij.psi.PsiElement)3 UsageInfo (com.intellij.usageView.UsageInfo)3 Logger (com.intellij.openapi.diagnostic.Logger)2