mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extract LightDirectoryIndex
This commit is contained in:
@@ -22,6 +22,7 @@ import com.intellij.lang.LanguageUtil;
|
||||
import com.intellij.lang.PerFileMappings;
|
||||
import com.intellij.lang.PerFileMappingsBase;
|
||||
import com.intellij.openapi.application.AccessToken;
|
||||
import com.intellij.openapi.application.Application;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import com.intellij.openapi.components.PersistentStateComponent;
|
||||
@@ -44,15 +45,14 @@ import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileWithId;
|
||||
import com.intellij.psi.LanguageSubstitutor;
|
||||
import com.intellij.psi.LanguageSubstitutors;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import com.intellij.util.PairConsumer;
|
||||
import com.intellij.util.PathUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.indexing.IndexableSetContributor;
|
||||
import com.intellij.util.indexing.LightDirectoryIndex;
|
||||
import com.intellij.util.messages.MessageBus;
|
||||
import org.jdom.Element;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -71,22 +71,14 @@ public class ScratchFileServiceImpl extends ScratchFileService implements Persis
|
||||
private final LightDirectoryIndex<RootType> myIndex;
|
||||
private final MyLanguages myScratchMapping = new MyLanguages();
|
||||
|
||||
protected ScratchFileServiceImpl(MessageBus messageBus) {
|
||||
myIndex = new LightDirectoryIndex<RootType>(messageBus.connect(), NULL_TYPE) {
|
||||
|
||||
@Override
|
||||
protected void collectRoots(@NotNull PairConsumer<VirtualFile, RootType> consumer) {
|
||||
LocalFileSystem fileSystem = LocalFileSystem.getInstance();
|
||||
for (RootType r : RootType.getAllRootIds()) {
|
||||
String root = getRootPath(r);
|
||||
VirtualFile rootFile = fileSystem.findFileByPath(root);
|
||||
if (rootFile != null) {
|
||||
consumer.consume(rootFile, r);
|
||||
}
|
||||
}
|
||||
protected ScratchFileServiceImpl(Application application) {
|
||||
myIndex = new LightDirectoryIndex<RootType>(application, NULL_TYPE, index -> {
|
||||
LocalFileSystem fileSystem = LocalFileSystem.getInstance();
|
||||
for (RootType r : RootType.getAllRootIds()) {
|
||||
index.putInfo(fileSystem.findFileByPath(getRootPath(r)), r);
|
||||
}
|
||||
};
|
||||
initFileOpenedListener(messageBus);
|
||||
});
|
||||
initFileOpenedListener(application.getMessageBus());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -100,7 +92,6 @@ public class ScratchFileServiceImpl extends ScratchFileService implements Persis
|
||||
public RootType getRootType(@Nullable VirtualFile file) {
|
||||
if (file == null) return null;
|
||||
VirtualFile directory = file.isDirectory() ? file : file.getParent();
|
||||
if (!(directory instanceof VirtualFileWithId)) return null;
|
||||
RootType result = myIndex.getInfoForFile(directory);
|
||||
return result == NULL_TYPE ? null : result;
|
||||
}
|
||||
|
||||
+24
-13
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,8 +13,10 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.ide.scratch;
|
||||
package com.intellij.util.indexing;
|
||||
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.fileTypes.FileTypeEvent;
|
||||
import com.intellij.openapi.fileTypes.FileTypeListener;
|
||||
import com.intellij.openapi.fileTypes.FileTypeManager;
|
||||
@@ -23,7 +25,7 @@ import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
import com.intellij.openapi.vfs.VirtualFileWithId;
|
||||
import com.intellij.openapi.vfs.newvfs.BulkFileListener;
|
||||
import com.intellij.openapi.vfs.newvfs.events.VFileEvent;
|
||||
import com.intellij.util.PairConsumer;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.containers.ConcurrentIntObjectMap;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.messages.MessageBusConnection;
|
||||
@@ -33,23 +35,27 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This is a light version of DirectoryIndexImpl.
|
||||
* This is a light version of DirectoryIndexImpl
|
||||
*
|
||||
* @author gregsh
|
||||
*/
|
||||
abstract class LightDirectoryIndex<T> {
|
||||
public final class LightDirectoryIndex<T> {
|
||||
private final ConcurrentIntObjectMap<T> myInfoCache = ContainerUtil.createConcurrentIntObjectMap();
|
||||
private final T myDefValue;
|
||||
private final Consumer<LightDirectoryIndex<T>> myInitializer;
|
||||
|
||||
public LightDirectoryIndex(@NotNull MessageBusConnection connection, @NotNull T defValue) {
|
||||
public LightDirectoryIndex(@NotNull Disposable parentDisposable, @NotNull T defValue, @NotNull Consumer<LightDirectoryIndex<T>> initializer) {
|
||||
myDefValue = defValue;
|
||||
reinitRoots();
|
||||
myInitializer = initializer;
|
||||
resetIndex();
|
||||
MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect(parentDisposable);
|
||||
connection.subscribe(FileTypeManager.TOPIC, new FileTypeListener.Adapter() {
|
||||
@Override
|
||||
public void fileTypesChanged(@NotNull FileTypeEvent event) {
|
||||
reinitRoots();
|
||||
resetIndex();
|
||||
}
|
||||
});
|
||||
|
||||
connection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {
|
||||
@Override
|
||||
public void before(@NotNull List<? extends VFileEvent> events) {
|
||||
@@ -60,7 +66,7 @@ abstract class LightDirectoryIndex<T> {
|
||||
for (VFileEvent event : events) {
|
||||
VirtualFile file = event.getFile();
|
||||
if (file == null || file.isDirectory()) {
|
||||
reinitRoots();
|
||||
resetIndex();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -68,15 +74,20 @@ abstract class LightDirectoryIndex<T> {
|
||||
});
|
||||
}
|
||||
|
||||
public void reinitRoots() {
|
||||
public void resetIndex() {
|
||||
myInfoCache.clear();
|
||||
collectRoots((file, info) -> cacheInfo(file, info));
|
||||
myInitializer.consume(this);
|
||||
}
|
||||
|
||||
protected abstract void collectRoots(@NotNull PairConsumer<VirtualFile, T> consumer);
|
||||
public void putInfo(@Nullable VirtualFile file, @NotNull T value) {
|
||||
if (!(file instanceof VirtualFileWithId)) return;
|
||||
cacheInfo(file, value);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public T getInfoForFile(@NotNull VirtualFile file) {
|
||||
public T getInfoForFile(@Nullable VirtualFile file) {
|
||||
if (!(file instanceof VirtualFileWithId)) return myDefValue;
|
||||
|
||||
VirtualFile dir;
|
||||
if (!file.isDirectory()) {
|
||||
T info = getCachedInfo(file);
|
||||
Reference in New Issue
Block a user