下面列出了怎么用com.intellij.psi.util.PsiModificationTracker的API类实例代码及写法,或者点击链接到github查看源代码。
@NotNull
private synchronized static Collection<String> getAllResourceKeys(@NotNull Project project) {
CachedValue<Collection<String>> userData = project.getUserData(RESOURCE_KEYS);
if (userData != null && userData.hasUpToDateValue()) {
return RESOURCE_KEYS_LOCAL_CACHE.getOrDefault(project, new ArrayList<>());
}
CachedValue<Collection<String>> cachedValue = CachedValuesManager.getManager(project).createCachedValue(() -> {
Collection<String> allKeys = FileBasedIndex.getInstance().getAllKeys(ResourcePathIndex.KEY, project);
if (RESOURCE_KEYS_LOCAL_CACHE.containsKey(project)) {
RESOURCE_KEYS_LOCAL_CACHE.replace(project, allKeys);
} else {
RESOURCE_KEYS_LOCAL_CACHE.put(project, allKeys);
}
return CachedValueProvider.Result.create(new ArrayList<>(), PsiModificationTracker.MODIFICATION_COUNT);
}, false);
project.putUserData(RESOURCE_KEYS, cachedValue);
return RESOURCE_KEYS_LOCAL_CACHE.getOrDefault(project, cachedValue.getValue());
}
@Nullable
private RunConfigurationContext findTestContext(ConfigurationContext context) {
if (!SmRunnerUtils.getSelectedSmRunnerTreeElements(context).isEmpty()) {
// handled by a different producer
return null;
}
ContextWrapper wrapper = new ContextWrapper(context);
PsiElement psi = context.getPsiLocation();
return psi == null
? null
: CachedValuesManager.getCachedValue(
psi,
cacheKey,
() ->
CachedValueProvider.Result.create(
doFindTestContext(wrapper.context),
PsiModificationTracker.MODIFICATION_COUNT,
BlazeSyncModificationTracker.getInstance(wrapper.context.getProject())));
}
@Nullable
private BinaryRunContext findRunContext(ConfigurationContext context) {
if (!SmRunnerUtils.getSelectedSmRunnerTreeElements(context).isEmpty()) {
// not a binary run context
return null;
}
ContextWrapper wrapper = new ContextWrapper(context);
PsiElement psi = context.getPsiLocation();
return psi == null
? null
: CachedValuesManager.getCachedValue(
psi,
() ->
CachedValueProvider.Result.create(
doFindRunContext(wrapper.context),
PsiModificationTracker.MODIFICATION_COUNT,
BlazeSyncModificationTracker.getInstance(wrapper.context.getProject())));
}
@Nullable
@Override
public RunConfigurationContext getTestContext(ConfigurationContext context) {
PsiElement psi = context.getPsiLocation();
if (!(psi instanceof PsiFileSystemItem) || !(psi instanceof FakePsiElement)) {
return null;
}
VirtualFile vf = ((PsiFileSystemItem) psi).getVirtualFile();
if (vf == null) {
return null;
}
WorkspacePath path = getWorkspacePath(context.getProject(), vf);
if (path == null) {
return null;
}
return CachedValuesManager.getCachedValue(
psi,
() ->
CachedValueProvider.Result.create(
doFindTestContext(context, vf, psi, path),
PsiModificationTracker.MODIFICATION_COUNT,
BlazeSyncModificationTracker.getInstance(context.getProject())));
}
/**
* Based on {@link JUnitUtil#isTestClass}. We don't use that directly because it returns true for
* all inner classes of a test class, regardless of whether they're also test classes.
*/
public static boolean isTestClass(PsiClass psiClass) {
if (psiClass.getQualifiedName() == null) {
return false;
}
if (JUnitUtil.isJUnit5(psiClass) && JUnitUtil.isJUnit5TestClass(psiClass, true)) {
return true;
}
if (!PsiClassUtil.isRunnableClass(psiClass, true, true)) {
return false;
}
if (isJUnit4Class(psiClass)) {
return true;
}
if (isTestCaseInheritor(psiClass)) {
return true;
}
return CachedValuesManager.getCachedValue(
psiClass,
() ->
CachedValueProvider.Result.create(
hasTestOrSuiteMethods(psiClass),
PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT));
}
@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();
}
@Nonnull
public static Unity3dManifest parse(@Nonnull Project project)
{
return CachedValuesManager.getManager(project).getCachedValue(project, () ->
{
Path projectPath = Paths.get(project.getBasePath());
Path manifestJson = projectPath.resolve(Paths.get("Packages", "manifest.json"));
if(Files.exists(manifestJson))
{
Gson gson = new Gson();
try (Reader reader = Files.newBufferedReader(manifestJson))
{
return CachedValueProvider.Result.create(gson.fromJson(reader, Unity3dManifest.class), PsiModificationTracker.MODIFICATION_COUNT);
}
catch(Exception e)
{
LOG.error(e);
}
}
return CachedValueProvider.Result.create(EMPTY, PsiModificationTracker.MODIFICATION_COUNT);
});
}
@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();
}
@Nonnull
@RequiredReadAction
public static EnumSet<CSharpModifier> getModifiersCached(@Nonnull CSharpModifierList modifierList)
{
if(!modifierList.isValid())
{
return emptySet;
}
return CachedValuesManager.getCachedValue(modifierList, () ->
{
Set<CSharpModifier> modifiers = new THashSet<>();
for(CSharpModifier modifier : CSharpModifier.values())
{
if(hasModifier(modifierList, modifier))
{
modifiers.add(modifier);
}
}
return CachedValueProvider.Result.create(modifiers.isEmpty() ? emptySet : EnumSet.copyOf(modifiers), PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
});
}
@RequiredReadAction
@Nonnull
@Override
public DotNetTypeRef[] getExtendTypeRefs()
{
return CachedValuesManager.getCachedValue(this, new CachedValueProvider<DotNetTypeRef[]>()
{
@Nullable
@Override
@RequiredReadAction
public Result<DotNetTypeRef[]> compute()
{
DotNetTypeRef[] extendTypeRefs = CSharpTypeDeclarationImplUtil.getExtendTypeRefs(CSharpTypeDeclarationImpl.this);
return Result.create(extendTypeRefs, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
}
});
}
@Nonnull
private Collection<CSharpElementGroup<CSharpMethodDeclaration>> getLocalMethods()
{
return CachedValuesManager.getCachedValue(this, () -> {
MultiMap<String, CSharpMethodDeclaration> multiMap = new MultiMap<>();
for(DotNetStatement statement : getStatements())
{
if(statement instanceof CSharpLocalMethodDeclarationStatementImpl)
{
CSharpMethodDeclaration method = ((CSharpLocalMethodDeclarationStatementImpl) statement).getMethod();
multiMap.putValue(method.getName(), method);
}
}
List<CSharpElementGroup<CSharpMethodDeclaration>> list = multiMap
.entrySet()
.stream()
.map(e -> new CSharpElementGroupImpl<>(getProject(), e.getKey(), e.getValue()))
.collect(Collectors.toList());
return CachedValueProvider.Result.create(list, PsiModificationTracker.MODIFICATION_COUNT);
});
}
@NotNull
@Override
public Collection<TwigPath> getNamespaces(@NotNull TwigNamespaceExtensionParameter parameter) {
Project project = parameter.getProject();
Collection<Pair<String, String>> cachedValue = CachedValuesManager.getManager(project).getCachedValue(
project,
CACHE,
() -> CachedValueProvider.Result.create(getTwigPaths(project), PsiModificationTracker.MODIFICATION_COUNT),
false
);
// TwigPath is not cache able as it right now; we need to build it here
return cachedValue.stream()
.map(p -> new TwigPath(p.getFirst(), p.getSecond(), TwigUtil.NamespaceType.ADD_PATH, true))
.collect(Collectors.toList());
}
private static boolean compare(@NotNull Project project, @NotNull String version, @NotNull Comparator comparator) {
Set<String> cache = CachedValuesManager.getManager(project).getCachedValue(
project,
CACHE,
() -> CachedValueProvider.Result.create(getVersions(project), PsiModificationTracker.MODIFICATION_COUNT),
false
);
for (String s : cache) {
if(comparator.accepts(s)) {
return true;
}
}
return false;
}
protected boolean isValid() {
if (isDumbMode() && !DumbService.isDumbAware(this)) {
return false;
}
if (PsiModificationTracker.SERVICE.getInstance(myProject).getModificationCount() != myInitialPsiStamp) {
return false;
}
if (myDocument != null) {
if (myDocument.getModificationStamp() != myInitialDocStamp) return false;
PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(myDocument);
return file != null && file.isValid();
}
return true;
}
@Override
protected long getTimeStamp(@Nonnull Object dependency) {
if (dependency instanceof PsiDirectory) {
return myManager.getModificationTracker().getOutOfCodeBlockModificationCount();
}
if (dependency instanceof PsiElement) {
PsiElement element = (PsiElement)dependency;
if (!element.isValid()) return -1;
PsiFile containingFile = element.getContainingFile();
if (containingFile != null) return containingFile.getModificationStamp();
}
if (dependency == PsiModificationTracker.MODIFICATION_COUNT || dependency == PSI_MOD_COUNT_OPTIMIZATION) {
return myManager.getModificationTracker().getModificationCount();
}
if (dependency == PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT) {
return myManager.getModificationTracker().getOutOfCodeBlockModificationCount();
}
if (dependency == PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT) {
return myManager.getModificationTracker().getJavaStructureModificationCount();
}
return super.getTimeStamp(dependency);
}
private void updateOnPsiChanges(@Nonnull LightweightHint hint, @Nonnull Info info, @Nonnull Consumer<? super String> textConsumer, @Nonnull String oldText, @Nonnull Editor editor) {
if (!hint.isVisible()) return;
Disposable hintDisposable = Disposable.newDisposable("CtrlMouseHandler.TooltipProvider.updateOnPsiChanges");
hint.addHintListener(__ -> Disposer.dispose(hintDisposable));
myProject.getMessageBus().connect(hintDisposable).subscribe(PsiModificationTracker.TOPIC, () -> ReadAction.nonBlocking(() -> {
try {
DocInfo newDocInfo = info.getInfo();
return (Runnable)() -> {
if (newDocInfo.text != null && !oldText.equals(newDocInfo.text)) {
updateText(newDocInfo.text, textConsumer, hint, editor);
}
};
}
catch (IndexNotReadyException e) {
showDumbModeNotification(myProject);
return createDisposalContinuation();
}
}).finishOnUiThread(ModalityState.defaultModalityState(), Runnable::run).withDocumentsCommitted(myProject).expireWith(hintDisposable).expireWhen(() -> !info.isValid(editor.getDocument()))
.coalesceBy(hint).submit(AppExecutorUtil.getAppExecutorService()));
}
/**
* Adds a listener that invalidates found refactoring opportunities if the structure of PSI is changed.
*/
private void registerPsiModificationListener() {
if (!isPreviewUsage) {
MessageBus projectMessageBus = scope.getProject().getMessageBus();
projectMessageBus.connect().subscribe(PsiModificationTracker.TOPIC, () -> ApplicationManager.getApplication().invokeLater(this::showRefreshingProposal));
}
}
@NotNull
public static Map<String, List<PsiFile>> getModuleMap(@NotNull Project project) {
return CachedValuesManager.getManager(project).getCachedValue(
project,
MODULE_MAP,
() -> CachedValueProvider.Result.create(doGetModuleMap(project), PsiModificationTracker.MODIFICATION_COUNT),
false
);
}
private static boolean unsyncedFileContainsGtestMacroCalls(OCFile file) {
return CachedValuesManager.getCachedValue(
file,
() ->
CachedValueProvider.Result.create(
computeUnsyncedFileContainsGtestMacroCalls(file),
PsiModificationTracker.MODIFICATION_COUNT));
}
@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();
}
@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();
}
@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();
}
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;
}
@Nullable
@Override
public Result<Collection<ServiceInterface>> compute() {
return Result.create(
ContainerUtil.map(SubscriberIndexUtil.getIndexedBootstrapResources(args.getProject(), BootstrapResource.INIT_RESOURCE), (Function<ServiceResource, ServiceInterface>) resource
-> new SerializableService(resource.getServiceName()).setClassName(SubscriberIndexUtil.getTypeForResource(args.getProject(), resource)))
, PsiModificationTracker.MODIFICATION_COUNT
);
}
@Nullable
@Override
public Result<Collection<String>> compute() {
return Result.create(
ContainerUtil.map(SubscriberIndexUtil.getIndexedBootstrapResources(args.getProject()), ServiceResource::getServiceName),
PsiModificationTracker.MODIFICATION_COUNT
);
}
public RoutesView(@NotNull Project project) {
myProject = project;
mainPanel = new MainPanel(project);
myConnection = myProject.getMessageBus().connect();
// Subscribe on files changes to update Route list regularly.
// We connect to project bus, as module bus don't work with this topic
MessageBusConnection conn = project.getMessageBus().connect();
conn.subscribe(PsiModificationTracker.TOPIC, new PSIModificationListener());
}
@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();
}
@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();
}
@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();
}
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;
}