From bf917992e787f87d631e9b351e8d587517c6d92f Mon Sep 17 00:00:00 2001 From: "Gregory.Shrago" Date: Mon, 2 Feb 2015 20:53:25 +0300 Subject: [PATCH] Scratches: directory index (performance!) & drop project service --- .../ide/scratch/LightDirectoryIndex.java | 134 +++++++++ .../com/intellij/ide/scratch/RootType.java | 5 +- .../ide/scratch/ScratchFileService.java | 4 - .../ide/scratch/ScratchFileServiceImpl.java | 257 ++++++++---------- .../ide/scratch/ScratchProjectViewPane.java | 2 +- .../src/META-INF/LangExtensions.xml | 4 +- 6 files changed, 243 insertions(+), 163 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/ide/scratch/LightDirectoryIndex.java diff --git a/platform/lang-impl/src/com/intellij/ide/scratch/LightDirectoryIndex.java b/platform/lang-impl/src/com/intellij/ide/scratch/LightDirectoryIndex.java new file mode 100644 index 000000000000..525e2fb27498 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/ide/scratch/LightDirectoryIndex.java @@ -0,0 +1,134 @@ +/* + * Copyright 2000-2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.ide.scratch; + +import com.intellij.openapi.fileTypes.FileTypeEvent; +import com.intellij.openapi.fileTypes.FileTypeListener; +import com.intellij.openapi.fileTypes.FileTypeManager; +import com.intellij.openapi.vfs.VirtualFile; +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.containers.ConcurrentIntObjectMap; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.messages.MessageBusConnection; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +/** + * This is a light version of DirectoryIndexImpl. + * + * @author gregsh + */ +abstract class LightDirectoryIndex { + private final ConcurrentIntObjectMap myInfoCache = ContainerUtil.createConcurrentIntObjectMap(); + private final T myDefValue; + + public LightDirectoryIndex(@NotNull MessageBusConnection connection, @NotNull T defValue) { + myDefValue = defValue; + reinitRoots(); + connection.subscribe(FileTypeManager.TOPIC, new FileTypeListener.Adapter() { + @Override + public void fileTypesChanged(@NotNull FileTypeEvent event) { + reinitRoots(); + } + }); + connection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() { + @Override + public void before(@NotNull List events) { + } + + @Override + public void after(@NotNull List events) { + for (VFileEvent event : events) { + VirtualFile file = event.getFile(); + if (file == null || file.isDirectory()) { + reinitRoots(); + break; + } + } + } + }); + } + + public void reinitRoots() { + myInfoCache.clear(); + collectRoots(new PairConsumer() { + @Override + public void consume(VirtualFile file, T info) { + cacheInfo(file, info); + } + }); + } + + protected abstract void collectRoots(@NotNull PairConsumer consumer); + + @NotNull + public T getInfoForFile(@NotNull VirtualFile file) { + VirtualFile dir; + if (!file.isDirectory()) { + T info = getCachedInfo(file); + if (info != null) { + return info; + } + dir = file.getParent(); + } + else { + dir = file; + } + + int count = 0; + for (VirtualFile root = dir; root != null; root = root.getParent()) { + if (++count > 1000) { + throw new IllegalStateException("Possible loop in tree, started at " + dir.getName()); + } + T info = getCachedInfo(root); + if (info != null) { + if (!dir.equals(root)) { + cacheInfos(dir, root, info); + } + return info; + } + } + + return cacheInfos(dir, null, myDefValue); + } + + @NotNull + private T cacheInfos(VirtualFile dir, @Nullable VirtualFile stopAt, @NotNull T info) { + while (dir != null) { + cacheInfo(dir, info); + if (dir.equals(stopAt)) { + break; + } + dir = dir.getParent(); + } + return info; + } + + private void cacheInfo(VirtualFile file, T info) { + myInfoCache.put(((VirtualFileWithId)file).getId(), info); + } + + private T getCachedInfo(VirtualFile file) { + return myInfoCache.get(((VirtualFileWithId)file).getId()); + } + +} diff --git a/platform/lang-impl/src/com/intellij/ide/scratch/RootType.java b/platform/lang-impl/src/com/intellij/ide/scratch/RootType.java index 39ade95c7fe1..11d3de6668a5 100644 --- a/platform/lang-impl/src/com/intellij/ide/scratch/RootType.java +++ b/platform/lang-impl/src/com/intellij/ide/scratch/RootType.java @@ -78,8 +78,6 @@ public abstract class RootType { return StringUtil.isEmpty(myDisplayName); } - public boolean canBeProject() { return false; } - @Nullable public Language substituteLanguage(@NotNull Project project, @NotNull VirtualFile file) { return null; @@ -98,8 +96,7 @@ public abstract class RootType { } public VirtualFile findFile(@Nullable Project project, @NotNull String pathName, ScratchFileService.Option option) throws IOException { - ScratchFileService fileService = project == null ? ScratchFileService.getInstance() : ScratchFileService.getInstance(project); - return fileService.findFile(this, pathName, option); + return ScratchFileService.getInstance().findFile(this, pathName, option); } public void fileOpened(@NotNull VirtualFile file, @NotNull FileEditorManager source) { diff --git a/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileService.java b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileService.java index 7e6d95a43704..ae956174f8f8 100644 --- a/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileService.java +++ b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileService.java @@ -29,10 +29,6 @@ public abstract class ScratchFileService { public enum Option { existing_only, create_if_missing, create_new_always } - public static ScratchFileService getInstance(@NotNull Project project) { - return ServiceManager.getService(project, ScratchFileService.class); - } - public static ScratchFileService getInstance() { return ServiceManager.getService(ScratchFileService.class); } diff --git a/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileServiceImpl.java b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileServiceImpl.java index f792078c5a54..4ea7efd8e4e4 100644 --- a/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileServiceImpl.java +++ b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileServiceImpl.java @@ -42,7 +42,6 @@ import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VfsUtil; -import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.wm.IdeFrame; import com.intellij.openapi.wm.StatusBar; @@ -52,6 +51,7 @@ import com.intellij.psi.LanguageSubstitutor; import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiUtilCore; import com.intellij.ui.UIBundle; +import com.intellij.util.PairConsumer; import com.intellij.util.PathUtil; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.messages.MessageBus; @@ -66,85 +66,119 @@ import java.util.List; import java.util.Set; -public abstract class ScratchFileServiceImpl extends ScratchFileService { +@State( + name = "ScratchFileService", + storages = { + @Storage(file = StoragePathMacros.APP_CONFIG + "/scratches.xml") + }) +public class ScratchFileServiceImpl extends ScratchFileService implements PersistentStateComponent{ - @State( - name = "ScratchFileService", - storages = { - @Storage(file = StoragePathMacros.APP_CONFIG + "/scratches.xml") - }) - public static class App extends ScratchFileServiceImpl implements PersistentStateComponent { + private static final RootType NULL_TYPE = new RootType("", null) {}; - private final MyLanguages myScratchMapping = new MyLanguages(); + private final LightDirectoryIndex myIndex; + private final MyLanguages myScratchMapping = new MyLanguages(); - public App(WindowManager windowManager, MessageBus messageBus) { - final WindowManagerListener windowListener = new WindowManagerListener() { - @Override - public void frameCreated(IdeFrame frame) { - Project project = frame.getProject(); - StatusBar statusBar = frame.getStatusBar(); - if (project == null || statusBar == null || statusBar.getWidget(ScratchWidget.WIDGET_ID) != null) return; - ScratchWidget widget = new ScratchWidget(project); - statusBar.addWidget(widget, "before Encoding", project); - statusBar.updateWidget(ScratchWidget.WIDGET_ID); - } + protected ScratchFileServiceImpl(WindowManager windowManager, MessageBus messageBus) { + myIndex = new LightDirectoryIndex(messageBus.connect(), NULL_TYPE) { - @Override - public void beforeFrameReleased(IdeFrame frame) { - } - }; - for (IdeFrame frame : windowManager.getAllProjectFrames()) { - windowListener.frameCreated(frame); - } - windowManager.addListener(windowListener); - - final FileEditorManagerAdapter editorListener = new FileEditorManagerAdapter() { - @Override - public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) { - RootType rootType = getRootType(file); - if (rootType != null) { - rootType.fileOpened(file, source); + @Override + protected void collectRoots(@NotNull PairConsumer 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); } } - }; - ProjectManagerAdapter projectListener = new ProjectManagerAdapter() { - @Override - public void projectOpened(Project project) { - project.getMessageBus().connect(project).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, editorListener); - FileEditorManager editorManager = FileEditorManager.getInstance(project); - for (VirtualFile virtualFile : editorManager.getOpenFiles()) { - editorListener.fileOpened(editorManager, virtualFile); - } - } - }; - for (Project project : ProjectManager.getInstance().getOpenProjects()) { - projectListener.projectOpened(project); } - messageBus.connect().subscribe(ProjectManager.TOPIC, projectListener); - } + }; + initScratchWidget(windowManager); + initFileOpenedListener(messageBus); + } - @NotNull - @Override - protected String getRootPath() { - return FileUtil.toSystemIndependentName(PathManager.getConfigPath()); - } + @NotNull + @Override + public String getRootPath(@NotNull RootType rootId) { + return getRootPath() + "/" + rootId.getId(); + } - @NotNull - @Override - public PerFileMappings getScratchesMapping() { - return myScratchMapping; - } + @Nullable + @Override + public RootType getRootType(@NotNull VirtualFile file) { + if (!file.isValid()) return null; + RootType result = myIndex.getInfoForFile(file); + return result == NULL_TYPE ? null : result; + } - @Nullable - @Override - public Element getState() { - return myScratchMapping.getState(); + private void initFileOpenedListener(MessageBus messageBus) { + final FileEditorManagerAdapter editorListener = new FileEditorManagerAdapter() { + @Override + public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) { + RootType rootType = getRootType(file); + if (rootType != null) { + rootType.fileOpened(file, source); + } + } + }; + ProjectManagerAdapter projectListener = new ProjectManagerAdapter() { + @Override + public void projectOpened(Project project) { + project.getMessageBus().connect(project).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, editorListener); + FileEditorManager editorManager = FileEditorManager.getInstance(project); + for (VirtualFile virtualFile : editorManager.getOpenFiles()) { + editorListener.fileOpened(editorManager, virtualFile); + } + } + }; + for (Project project : ProjectManager.getInstance().getOpenProjects()) { + projectListener.projectOpened(project); } + messageBus.connect().subscribe(ProjectManager.TOPIC, projectListener); + } - @Override - public void loadState(Element state) { - myScratchMapping.loadState(state); + private static void initScratchWidget(WindowManager windowManager) { + final WindowManagerListener windowListener = new WindowManagerListener() { + @Override + public void frameCreated(IdeFrame frame) { + Project project = frame.getProject(); + StatusBar statusBar = frame.getStatusBar(); + if (project == null || statusBar == null || statusBar.getWidget(ScratchWidget.WIDGET_ID) != null) return; + ScratchWidget widget = new ScratchWidget(project); + statusBar.addWidget(widget, "before Encoding", project); + statusBar.updateWidget(ScratchWidget.WIDGET_ID); + } + + @Override + public void beforeFrameReleased(IdeFrame frame) { + } + }; + for (IdeFrame frame : windowManager.getAllProjectFrames()) { + windowListener.frameCreated(frame); } + windowManager.addListener(windowListener); + } + + @NotNull + protected String getRootPath() { + return FileUtil.toSystemIndependentName(PathManager.getConfigPath()); + } + + @NotNull + @Override + public PerFileMappings getScratchesMapping() { + return myScratchMapping; + } + + @Nullable + @Override + public Element getState() { + return myScratchMapping.getState(); + } + + @Override + public void loadState(Element state) { + myScratchMapping.loadState(state); } private static class MyLanguages extends PerFileMappingsBase { @@ -178,57 +212,6 @@ public abstract class ScratchFileServiceImpl extends ScratchFileService { } } - - public static class Prj extends ScratchFileServiceImpl { - - private final ScratchFileServiceImpl myParentService; - private final Project myProject; - - public Prj(@NotNull Project project, App parentService) { - myProject = project; - myParentService = parentService; - } - - @NotNull - protected Project getProject() { - return myProject; - } - - @NotNull - @Override - protected String getRootPath() { - return FileUtil.toSystemIndependentName(StringUtil.notNullize(PathUtil.getParentPath(myProject.getProjectFilePath()))); - } - - @NotNull - @Override - public String getRootPath(@NotNull RootType rootType) { - if (!rootType.canBeProject()) { - return myParentService.getRootPath(rootType); - } - return super.getRootPath(rootType); - } - - @Override - public RootType getRootType(@NotNull VirtualFile file) { - RootType rootType = super.getRootType(file); - if (rootType != null) return rootType; - return myParentService.getRootType(file); - } - - @Nullable - @Override - public VirtualFile createScratchFile(@NotNull Project project, @NotNull Language language, @NotNull String initialContent) { - return myParentService.createScratchFile(project, language, initialContent); - } - - @NotNull - @Override - public PerFileMappings getScratchesMapping() { - return ScratchFileService.getInstance().getScratchesMapping(); - } - } - public static class TypeFactory extends FileTypeFactory { @Override @@ -241,7 +224,7 @@ public abstract class ScratchFileServiceImpl extends ScratchFileService { @Nullable @Override public Language getLanguage(@NotNull VirtualFile file, @NotNull Project project) { - RootType rootType = ScratchFileService.getInstance(project).getRootType(file); + RootType rootType = ScratchFileService.getInstance().getRootType(file); if (rootType == null) return null; return rootType.substituteLanguage(project, file); } @@ -252,7 +235,7 @@ public abstract class ScratchFileServiceImpl extends ScratchFileService { @Nullable public SyntaxHighlighter create(@NotNull FileType fileType, @Nullable Project project, @Nullable VirtualFile file) { if (project == null || file == null) return null; - RootType rootType = ScratchFileService.getInstance(project).getRootType(file); + RootType rootType = ScratchFileService.getInstance().getRootType(file); if (rootType == null) return null; Language language = rootType.substituteLanguage(project, file); return language == null ? null : SyntaxHighlighterFactory.getSyntaxHighlighter(language, project, file); @@ -265,7 +248,7 @@ public abstract class ScratchFileServiceImpl extends ScratchFileService { @Override public Icon getIcon(@NotNull VirtualFile file, @Iconable.IconFlags int flags, @Nullable Project project) { if (project == null || file.isDirectory()) return null; - RootType rootType = ScratchFileService.getInstance(project).getRootType(file); + RootType rootType = ScratchFileService.getInstance().getRootType(file); if (rootType == null) return null; return rootType.substituteIcon(project, file); } @@ -273,7 +256,7 @@ public abstract class ScratchFileServiceImpl extends ScratchFileService { @Nullable @Override public String getEditorTabTitle(@NotNull Project project, @NotNull VirtualFile file) { - RootType rootType = ScratchFileService.getInstance(project).getRootType(file); + RootType rootType = ScratchFileService.getInstance().getRootType(file); if (rootType == null) return null; return rootType.substituteName(project, file); } @@ -296,7 +279,7 @@ public abstract class ScratchFileServiceImpl extends ScratchFileService { Project project = ((PsiElement)object).getProject(); VirtualFile virtualFile = PsiUtilCore.getVirtualFile((PsiElement)object); if (virtualFile == null || !virtualFile.isValid()) return null; - RootType rootType = ScratchFileService.getInstance(project).getRootType(virtualFile); + RootType rootType = ScratchFileService.getInstance().getRootType(virtualFile); if (rootType == null) return null; if (virtualFile.isDirectory()) { return additionalRoots(project).contains(virtualFile) ? rootType.getDisplayName() : null; @@ -310,26 +293,13 @@ public abstract class ScratchFileServiceImpl extends ScratchFileService { Set result = ContainerUtil.newLinkedHashSet(); LocalFileSystem fileSystem = LocalFileSystem.getInstance(); ScratchFileService app = ScratchFileService.getInstance(); - ScratchFileService prj = ScratchFileService.getInstance(project); for (RootType r : RootType.getAllRootIds()) { - if (r.canBeProject()) { - ContainerUtil.addIfNotNull(result, fileSystem.findFileByPath(prj.getRootPath(r))); - } ContainerUtil.addIfNotNull(result, fileSystem.findFileByPath(app.getRootPath(r))); } return result; } } - @NotNull - protected abstract String getRootPath(); - - @NotNull - @Override - public String getRootPath(@NotNull RootType rootId) { - return getRootPath() + "/" + rootId.getId(); - } - @Nullable @Override @@ -354,20 +324,6 @@ public abstract class ScratchFileServiceImpl extends ScratchFileService { return result.getResultObject(); } - @Override - public RootType getRootType(@NotNull VirtualFile file) { - if (!file.isInLocalFileSystem()) return null; - LocalFileSystem fileSystem = LocalFileSystem.getInstance(); - VirtualFile rootRoot = fileSystem.findFileByPath(getRootPath()); - if (rootRoot == null || !VfsUtilCore.isAncestor(rootRoot, file, true)) return null; - for (RootType r : RootType.getAllRootIds()) { - String root = getRootPath(r); - VirtualFile rootFile = fileSystem.findFileByPath(root); - if (rootFile != null && VfsUtilCore.isAncestor(rootFile, file, false)) return r; - } - return null; - } - @Override public VirtualFile findFile(@NotNull final RootType rootType, @NotNull final String pathName, Option option) throws IOException { String fullPath = getRootPath(rootType) + "/" + pathName; @@ -393,5 +349,4 @@ public abstract class ScratchFileServiceImpl extends ScratchFileService { token.finish(); } } - } \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/ide/scratch/ScratchProjectViewPane.java b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchProjectViewPane.java index f02c24b12f63..87015782f224 100644 --- a/platform/lang-impl/src/com/intellij/ide/scratch/ScratchProjectViewPane.java +++ b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchProjectViewPane.java @@ -93,7 +93,7 @@ public class ScratchProjectViewPane extends ProjectViewPane { if (!super.canSelect(file)) return false; final VirtualFile vFile = file.getVirtualFile(); if (vFile == null || !vFile.isValid()) return false; - return ScratchFileService.getInstance(myProject).getRootType(file.getVirtualFile()) != null; + return ScratchFileService.getInstance().getRootType(vFile) != null; } @Override diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index 1ce3d821a60f..65f8821d530a 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -330,9 +330,7 @@ - + serviceImplementation="com.intellij.ide.scratch.ScratchFileServiceImpl"/>