Java Code Examples for com.intellij.psi.util.CachedValue#getValue()

The following examples show how to use com.intellij.psi.util.CachedValue#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlNSNamespaceProvider.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
private synchronized Collection<FluidNamespace> doProvide(@NotNull PsiElement element) {
    FileViewProvider viewProvider = element.getContainingFile().getViewProvider();
    if (!viewProvider.getLanguages().contains(HTMLLanguage.INSTANCE)) {
        return ContainerUtil.emptyList();
    }

    PsiFile htmlFile = viewProvider.getPsi(HTMLLanguage.INSTANCE);
    CachedValue userData = htmlFile.getUserData(HTML_NS_KEY);
    if (userData != null) {
        return (Collection<FluidNamespace>) userData.getValue();
    }

    CachedValue<Collection<FluidNamespace>> cachedValue = CachedValuesManager.getManager(element.getProject()).createCachedValue(() -> {
        HtmlNSVisitor visitor = new HtmlNSVisitor();
        htmlFile.accept(visitor);

        return CachedValueProvider.Result.createSingleDependency(visitor.namespaces, htmlFile);
    }, false);

    htmlFile.putUserData(HTML_NS_KEY, cachedValue);

    return cachedValue.getValue();
}
 
Example 2
Source File: SubscriberIndexUtil.java    From idea-php-shopware-plugin with MIT License 6 votes vote down vote up
@NotNull
public static Collection<ServiceResource> getIndexedBootstrapResources(@NotNull Project project) {

    // cache
    CachedValue<Collection<ServiceResource>> cache = project.getUserData(SERVICE_RESOURCE);
    if (cache == null) {
        cache = CachedValuesManager.getManager(project).createCachedValue(() -> CachedValueProvider.Result.create(
            getIndexedBootstrapResources(project, BootstrapResource.INIT_RESOURCE, BootstrapResource.AFTER_INIT_RESOURCE, BootstrapResource.AFTER_REGISTER_RESOURCE),
            PsiModificationTracker.MODIFICATION_COUNT
        ), false);

        project.putUserData(SERVICE_RESOURCE, cache);
    }

    return cache.getValue();
}
 
Example 3
Source File: GlobalNamespaceLoader.java    From idea-php-drupal-symfony2-bridge with MIT License 6 votes vote down vote up
@Override
@NotNull
public Collection<String> getGlobalNamespaces(@NotNull AnnotationGlobalNamespacesLoaderParameter parameter) {
    Project project = parameter.getProject();

    CachedValue<Collection<String>> cache = project.getUserData(CACHE);

    if(cache == null) {
        cache = CachedValuesManager.getManager(project).createCachedValue(() ->
            CachedValueProvider.Result.create(getGlobalNamespacesInner(project), PsiModificationTracker.MODIFICATION_COUNT), false
        );

        project.putUserData(CACHE, cache);
    }

    return cache.getValue();
}
 
Example 4
Source File: ExtensionProviderUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@NotNull
synchronized public static Collection<PhpToolboxProviderInterface> getProviders(final @NotNull Project project) {
    CachedValue<Collection<PhpToolboxProviderInterface>> cache = project.getUserData(PROVIDER_CACHE);

    if(cache == null) {
        cache = CachedValuesManager.getManager(project).createCachedValue(() -> CachedValueProvider.Result.create(getProvidersInner(project), PsiModificationTracker.MODIFICATION_COUNT), false);

        project.putUserData(PROVIDER_CACHE, cache);
    }

    return cache.getValue();
}
 
Example 5
Source File: ExtensionProviderUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@NotNull
synchronized public static Collection<JsonRegistrar> getRegistrar(final @NotNull Project project, final @NotNull PhpToolboxApplicationService phpToolboxApplicationService) {
    CachedValue<Collection<JsonRegistrar>> cache = project.getUserData(REGISTRAR_CACHE);

    if(cache == null) {
        cache = CachedValuesManager.getManager(project).createCachedValue(() -> CachedValueProvider.Result.create(getRegistrarInner(project, phpToolboxApplicationService), PsiModificationTracker.MODIFICATION_COUNT), false);

        project.putUserData(REGISTRAR_CACHE, cache);
    }

    return cache.getValue();
}
 
Example 6
Source File: ExtensionProviderUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@NotNull
synchronized public static Collection<JsonRegistrar> getTypes(final @NotNull Project project) {
    CachedValue<Collection<JsonRegistrar>> cache = project.getUserData(TYPE_CACHE);

    if(cache == null) {
        cache = CachedValuesManager.getManager(project).createCachedValue(() -> CachedValueProvider.Result.create(getTypesInner(project), PsiModificationTracker.MODIFICATION_COUNT), false);

        project.putUserData(TYPE_CACHE, cache);
    }

    return cache.getValue();
}
 
Example 7
Source File: ExtensionProviderUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
synchronized public static Collection<JsonConfigFile> getJsonConfigs(final @NotNull Project project, final @NotNull PhpToolboxApplicationService phpToolboxApplicationService) {
    CachedValue<Collection<JsonConfigFile>> cache = project.getUserData(CONFIGS_CACHE);

    if(cache == null) {
        cache = CachedValuesManager.getManager(project).createCachedValue(() -> CachedValueProvider.Result.create(getJsonConfigsInner(project, phpToolboxApplicationService), PsiModificationTracker.MODIFICATION_COUNT), false);
        project.putUserData(CONFIGS_CACHE, cache);
    }

    Collection<JsonConfigFile> jsonConfigFiles = new ArrayList<>(cache.getValue());

    // prevent reindex issues
    if (!DumbService.getInstance(project).isDumb()) {
        CachedValue<Collection<JsonConfigFile>> indexCache = project.getUserData(CONFIGS_CACHE_INDEX);

        if (indexCache == null) {
            indexCache = CachedValuesManager.getManager(project).createCachedValue(() -> {
                Collection<JsonConfigFile> jsonConfigFiles1 = new ArrayList<>();

                for (final PsiFile psiFile : FilenameIndex.getFilesByName(project, ".ide-toolbox.metadata.json", GlobalSearchScope.allScope(project))) {
                    JsonConfigFile cachedValue = CachedValuesManager.getCachedValue(psiFile, () -> new CachedValueProvider.Result<>(
                        JsonParseUtil.getDeserializeConfig(psiFile.getText()),
                        psiFile,
                        psiFile.getVirtualFile()
                    ));

                    if(cachedValue != null) {
                        jsonConfigFiles1.add(cachedValue);
                    }
                }

                return CachedValueProvider.Result.create(jsonConfigFiles1, PsiModificationTracker.MODIFICATION_COUNT);
            }, false);
        }

        project.putUserData(CONFIGS_CACHE_INDEX, indexCache);
        jsonConfigFiles.addAll(indexCache.getValue());
    }

    return jsonConfigFiles;
}
 
Example 8
Source File: ExtensionProviderUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@NotNull
synchronized public static Collection<PhpToolboxProviderInterface> getProviders(final @NotNull Project project) {
    CachedValue<Collection<PhpToolboxProviderInterface>> cache = project.getUserData(PROVIDER_CACHE);

    if(cache == null) {
        cache = CachedValuesManager.getManager(project).createCachedValue(() -> CachedValueProvider.Result.create(getProvidersInner(project), PsiModificationTracker.MODIFICATION_COUNT), false);

        project.putUserData(PROVIDER_CACHE, cache);
    }

    return cache.getValue();
}
 
Example 9
Source File: ExtensionProviderUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@NotNull
synchronized public static Collection<JsonRegistrar> getRegistrar(final @NotNull Project project, final @NotNull PhpToolboxApplicationService phpToolboxApplicationService) {
    CachedValue<Collection<JsonRegistrar>> cache = project.getUserData(REGISTRAR_CACHE);

    if(cache == null) {
        cache = CachedValuesManager.getManager(project).createCachedValue(() -> CachedValueProvider.Result.create(getRegistrarInner(project, phpToolboxApplicationService), PsiModificationTracker.MODIFICATION_COUNT), false);

        project.putUserData(REGISTRAR_CACHE, cache);
    }

    return cache.getValue();
}
 
Example 10
Source File: ExtensionProviderUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@NotNull
synchronized public static Collection<JsonRegistrar> getTypes(final @NotNull Project project) {
    CachedValue<Collection<JsonRegistrar>> cache = project.getUserData(TYPE_CACHE);

    if(cache == null) {
        cache = CachedValuesManager.getManager(project).createCachedValue(() -> CachedValueProvider.Result.create(getTypesInner(project), PsiModificationTracker.MODIFICATION_COUNT), false);

        project.putUserData(TYPE_CACHE, cache);
    }

    return cache.getValue();
}
 
Example 11
Source File: ExtensionProviderUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
synchronized public static Collection<JsonConfigFile> getJsonConfigs(final @NotNull Project project, final @NotNull PhpToolboxApplicationService phpToolboxApplicationService) {
    CachedValue<Collection<JsonConfigFile>> cache = project.getUserData(CONFIGS_CACHE);

    if(cache == null) {
        cache = CachedValuesManager.getManager(project).createCachedValue(() -> CachedValueProvider.Result.create(getJsonConfigsInner(project, phpToolboxApplicationService), PsiModificationTracker.MODIFICATION_COUNT), false);
        project.putUserData(CONFIGS_CACHE, cache);
    }

    Collection<JsonConfigFile> jsonConfigFiles = new ArrayList<>(cache.getValue());

    // prevent reindex issues
    if (!DumbService.getInstance(project).isDumb()) {
        CachedValue<Collection<JsonConfigFile>> indexCache = project.getUserData(CONFIGS_CACHE_INDEX);

        if (indexCache == null) {
            indexCache = CachedValuesManager.getManager(project).createCachedValue(() -> {
                Collection<JsonConfigFile> jsonConfigFiles1 = new ArrayList<>();

                for (final PsiFile psiFile : FilenameIndex.getFilesByName(project, ".ide-toolbox.metadata.json", GlobalSearchScope.allScope(project))) {
                    JsonConfigFile cachedValue = CachedValuesManager.getCachedValue(psiFile, () -> new CachedValueProvider.Result<>(
                        JsonParseUtil.getDeserializeConfig(psiFile.getText()),
                        psiFile,
                        psiFile.getVirtualFile()
                    ));

                    if(cachedValue != null) {
                        jsonConfigFiles1.add(cachedValue);
                    }
                }

                return CachedValueProvider.Result.create(jsonConfigFiles1, PsiModificationTracker.MODIFICATION_COUNT);
            }, false);
        }

        project.putUserData(CONFIGS_CACHE_INDEX, indexCache);
        jsonConfigFiles.addAll(indexCache.getValue());
    }

    return jsonConfigFiles;
}
 
Example 12
Source File: CSharpResolveContextUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nonnull
@RequiredReadAction
private static CSharpResolveContext cacheTypeContextImpl(@Nonnull DotNetGenericExtractor genericExtractor,
														 @Nonnull final CSharpTypeDeclaration typeDeclaration,
														 @Nullable final Set<PsiElement> recursiveGuardSet)
{
	if(genericExtractor == DotNetGenericExtractor.EMPTY && (recursiveGuardSet == null || recursiveGuardSet.size() == 1 && recursiveGuardSet.contains(typeDeclaration)))
	{
		CachedValue<CSharpResolveContext> provider = typeDeclaration.getUserData(RESOLVE_CONTEXT);
		if(provider != null)
		{
			return provider.getValue();
		}

		CachedValue<CSharpResolveContext> cachedValue = CachedValuesManager.getManager(typeDeclaration.getProject()).createCachedValue(new CachedValueProvider<CSharpResolveContext>()
		{
			@Nullable
			@Override
			@RequiredReadAction
			public Result<CSharpResolveContext> compute()
			{
				return Result.<CSharpResolveContext>create(new CSharpTypeResolveContext(typeDeclaration, DotNetGenericExtractor.EMPTY, null), PsiModificationTracker.MODIFICATION_COUNT);
			}
		}, false);
		typeDeclaration.putUserData(RESOLVE_CONTEXT, cachedValue);
		return cachedValue.getValue();
	}
	else
	{
		return new CSharpTypeResolveContext(typeDeclaration, genericExtractor, recursiveGuardSet);
	}
}
 
Example 13
Source File: CSharpResolveContextUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static <T extends PsiElement> CSharpResolveContext cacheSimple(@Nonnull final T element, final NotNullFunction<T, CSharpResolveContext> fun)
{
	CachedValue<CSharpResolveContext> provider = element.getUserData(RESOLVE_CONTEXT);
	if(provider != null)
	{
		return provider.getValue();
	}

	CachedValue<CSharpResolveContext> cachedValue = CachedValuesManager.getManager(element.getProject()).createCachedValue(() -> CachedValueProvider.Result.create(fun.fun(element),
			PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT), false);
	element.putUserData(RESOLVE_CONTEXT, cachedValue);
	return cachedValue.getValue();
}
 
Example 14
Source File: MakefileIdentifierReference.java    From CppTools with Apache License 2.0 5 votes vote down vote up
@NotNull
public Object[] getVariants() {
  if (isSelfReference()) {
    return new Object[0];
  }
  CachedValue<Map<String, Object>> mapCachedValue = getDeclarationsMap();
  Map<String, Object> declMap = mapCachedValue.getValue();
  return declMap.keySet().toArray();
}
 
Example 15
Source File: CppStructureViewBuilder.java    From CppTools with Apache License 2.0 5 votes vote down vote up
@NotNull
public StructureViewTreeElement getRoot() {
  CachedValue<StructureViewTreeElement> value = myFile.getUserData(ourCachedKey);
  if (value == null) {
    value = CachedValuesManager.getManager(myFile.getManager().getProject()).createCachedValue(new CachedValueProvider<StructureViewTreeElement>() {
      public Result<StructureViewTreeElement> compute() {
        final OutlineCommand outlineCommand = new OutlineCommand(myFile.getVirtualFile().getPath());
        outlineCommand.post(myFile.getProject());

        final Communicator communicator = Communicator.getInstance(myFile.getProject());

        if (outlineCommand.hasReadyResult()) {
          final OutlineData first = outlineCommand.outlineDatumStack.getFirst();
          return new Result<StructureViewTreeElement>(
            new CppStructureViewTreeElement((CppFile) myFile, first),
            communicator.getServerRestartTracker(),
            communicator.getModificationTracker()
          );
        }

        return new Result<StructureViewTreeElement>(new CppStructureViewTreeElement((CppFile) myFile, null),
          communicator.getServerRestartTracker(),
          communicator.getModificationTracker()
        );
      }
    }, false);

    myFile.putUserData(ourCachedKey, value);
  }

  return value.getValue();
}
 
Example 16
Source File: CppIconProvider.java    From CppTools with Apache License 2.0 4 votes vote down vote up
public Icon getIcon(@NotNull PsiElement psiElement, int flags) {
    if (psiElement instanceof CppParserDefinition.CppFile) {
      final CppParserDefinition.CppFile cppFile = (CppParserDefinition.CppFile) psiElement;
      CachedValue<Icon> value = cppFile.getUserData(ourFileIconKey);

      if (value == null) {
        value = CachedValuesManager.getManager(cppFile.getManager().getProject()).createCachedValue(new CachedValueProvider<Icon>() {
          public Result<Icon> compute() {
            final VirtualFile virtualFile = cppFile.getVirtualFile();

            final boolean underSource = CppSupportLoader.isInSource(
              virtualFile, ProjectRootManager.getInstance(cppFile.getProject()).getFileIndex());

            return new Result<Icon>(
              underSource ?
                Communicator.isHeaderFile(virtualFile) ?
                  CppSupportLoader.ourIncludeIcon :
                  CppSupportLoader.ourCppIcon :
                iconForSkippedFile,
              ProjectRootManager.EVER_CHANGED
            );

//            final BlockingStringCommand stringCommand = new BlockingStringCommand(
//              "file-in-project "+ BuildingCommandHelper.quote(
//                BuildingCommandHelper.fixVirtualFileName(virtualFile.getPresentableUrl()),
//                true
//              )
//            );
//            stringCommand.post(cppFile.getProject());
//            String s1 = stringCommand.getCommandResult();
//
//            return new Result<Icon>( "false".equals(s1) ? iconForSkippedFile :null, serverRestartTracker);
          }
        }, false);
        cppFile.putUserData(ourFileIconKey, value);
      }

      return value.getValue();
    }
    return null;
  }
 
Example 17
Source File: UniqueVFilePathBuilderImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static String getUniqueVirtualFilePath(final Project project, VirtualFile file, final boolean skipNonOpenedFiles, GlobalSearchScope scope) {
  Key<CachedValue<Map<GlobalSearchScope, Map<String, UniqueNameBuilder<VirtualFile>>>>> key =
          skipNonOpenedFiles ?  ourShortNameOpenedBuilderCacheKey:ourShortNameBuilderCacheKey;
  CachedValue<Map<GlobalSearchScope, Map<String, UniqueNameBuilder<VirtualFile>>>> data = project.getUserData(key);
  if (data == null) {
    project.putUserData(key, data = CachedValuesManager.getManager(project).createCachedValue(
            () -> new CachedValueProvider.Result<Map<GlobalSearchScope, Map<String, UniqueNameBuilder<VirtualFile>>>>(
                    new ConcurrentHashMap<>(2),
                    PsiModificationTracker.MODIFICATION_COUNT,
                    //ProjectRootModificationTracker.getInstance(project),
                    //VirtualFileManager.VFS_STRUCTURE_MODIFICATIONS,
                    FileEditorManagerImpl.OPEN_FILE_SET_MODIFICATION_COUNT
            ), false));
  }

  ConcurrentMap<GlobalSearchScope, Map<String, UniqueNameBuilder<VirtualFile>>> scope2ValueMap =
          (ConcurrentMap<GlobalSearchScope, Map<String,UniqueNameBuilder<VirtualFile>>>)data.getValue();
  Map<String, UniqueNameBuilder<VirtualFile>> valueMap = scope2ValueMap.get(scope);
  if (valueMap == null) {
    valueMap = ConcurrencyUtil.cacheOrGet(scope2ValueMap, scope, ContainerUtil.createConcurrentSoftValueMap());
  }

  final String fileName = file.getName();
  UniqueNameBuilder<VirtualFile> uniqueNameBuilderForShortName = valueMap.get(fileName);

  if (uniqueNameBuilderForShortName == null) {
    final UniqueNameBuilder<VirtualFile> builder = filesWithTheSameName(
            fileName,
            project,
            skipNonOpenedFiles,
            scope
    );
    valueMap.put(fileName, builder != null ? builder:ourEmptyBuilder);
    uniqueNameBuilderForShortName = builder;
  } else if (uniqueNameBuilderForShortName == ourEmptyBuilder) {
    uniqueNameBuilderForShortName = null;
  }

  if (uniqueNameBuilderForShortName != null && uniqueNameBuilderForShortName.contains(file)) {
    if (file instanceof VirtualFilePathWrapper) {
      return ((VirtualFilePathWrapper)file).getPresentablePath();
    }
    return uniqueNameBuilderForShortName.getShortPath(file);
  }
  return file.getName();
}
 
Example 18
Source File: PhpViewHelpersProvider.java    From idea-php-typo3-plugin with MIT License 4 votes vote down vote up
@NotNull
private synchronized Map<String, ViewHelper> doProvide(@NotNull Project project, @NotNull String namespace) {
    HashMap<String, ViewHelper> collectedViewHelpers = new HashMap<>();
    String fqnPart = namespace.replace("/", "\\");
    PhpIndex phpIndex = PhpIndex.getInstance(project);

    Collection<PhpClass> viewHelperClasses = phpIndex.getAllSubclasses("TYPO3Fluid\\Fluid\\Core\\ViewHelper\\ViewHelperInterface");
    for (PhpClass viewHelperPhpClass : viewHelperClasses) {

        String fqn = viewHelperPhpClass.getPresentableFQN();
        if (fqn.startsWith(fqnPart) && !viewHelperPhpClass.isAbstract()) {
            CachedValue cachedViewHelper = viewHelperPhpClass.getUserData(VIEWHELPER_DEFINITION_KEY);
            if (cachedViewHelper != null) {
                ViewHelper value = (ViewHelper) cachedViewHelper.getValue();
                collectedViewHelpers.put(value.name, value);

                continue;
            }

            try {
                CachedValue<ViewHelper> cachedValue = CachedValuesManager.getManager(project).createCachedValue(() -> {
                    ViewHelperVisitor visitor = new ViewHelperVisitor();
                    viewHelperPhpClass.accept(visitor);

                    ViewHelper viewHelper = new ViewHelper(convertFqnToViewHelperName(fqn.substring(fqnPart.length() + 1)));
                    viewHelper.setFqn(fqn);

                    viewHelper.arguments.putAll(visitor.arguments);

                    return CachedValueProvider.Result.createSingleDependency(viewHelper, viewHelperPhpClass);
                }, false);

                viewHelperPhpClass.putUserData(VIEWHELPER_DEFINITION_KEY, cachedValue);

                collectedViewHelpers.put(cachedValue.getValue().name, cachedValue.getValue());
            } catch (IllegalArgumentException e) {
                e.getMessage();
            }
        }
    }

    return collectedViewHelpers;
}