move the code for loading the stub tree from FileBasedIndex from StubTree to a separa...
authorDmitry Jemerov <yole@jetbrains.com>
Fri, 2 Sep 2011 13:55:18 +0000 (15:55 +0200)
committerDmitry Jemerov <yole@jetbrains.com>
Mon, 5 Sep 2011 10:09:12 +0000 (12:09 +0200)
java/java-impl/src/com/intellij/psi/impl/compiled/ClsFileImpl.java
platform/lang-impl/src/com/intellij/psi/impl/source/PsiFileImpl.java
platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java
platform/lang-impl/src/com/intellij/psi/stubs/StubTree.java
platform/lang-impl/src/com/intellij/psi/stubs/StubTreeLoader.java [new file with mode: 0644]
platform/lang-impl/src/com/intellij/psi/stubs/StubTreeLoaderImpl.java [new file with mode: 0644]
platform/platform-resources/src/META-INF/LangExtensions.xml

index 201e66489d24d5be22f1c147e8c19c23dbb3e814..48f66e1668ff70747bea33b8a089cb8aeeb15150 100644 (file)
@@ -382,7 +382,7 @@ public class ClsFileImpl extends ClsRepositoryPsiElement<PsiClassHolderFileStub>
     final StubTree derefd = derefStub();
     if (derefd != null) return derefd;
 
-    StubTree stubHolder = StubTree.readOrBuild(getProject(), getVirtualFile());
+    StubTree stubHolder = StubTreeLoader.getInstance().readOrBuild(getProject(), getVirtualFile());
     if (stubHolder == null) {
       // Must be corrupted classfile
       LOG.info("Class file is corrupted: " + getVirtualFile().getPresentableUrl());
index 1a2cd59be9075e0c3140bcec72dbcf1b8284b997..0efa84fa7b024dfdb61b7e1d70a838f1d52be896 100644 (file)
@@ -603,7 +603,7 @@ public abstract class PsiFileImpl extends ElementBase implements PsiFileEx, PsiF
     final VirtualFile vFile = getVirtualFile();
     if (!(vFile instanceof VirtualFileWithId)) return null;
 
-    StubTree stubHolder = StubTree.readOrBuild(getProject(), vFile);
+    StubTree stubHolder = StubTreeLoader.getInstance().readOrBuild(getProject(), vFile);
     if (stubHolder == null) return null;
     
     final IElementType contentElementType = getContentElementType();
index 453220e66a757d16f4d2ed244759db765f2584d6..a895e6a6b4f085fa0bc6ed9589893de1dfeafcdc 100644 (file)
@@ -206,7 +206,7 @@ public class StubIndexImpl extends StubIndex implements ApplicationComponent, Pe
 
               if (stubTree != null || psiFile != null) {
                 if (stubTree == null) {
-                  stubTree = StubTree.readFromVFile(project, file);
+                  stubTree = StubTreeLoader.getInstance().readFromVFile(project, file);
                   if (stubTree != null) {
                     final List<StubElement<?>> plained = stubTree.getPlainList();
                     for (int i = 0; i < value.size(); i++) {
index b45f7df533975a768a14e07c488e0fb3994679b2..5f559cdd669392dc5af50101ea6145a29d4cc667 100644 (file)
  */
 package com.intellij.psi.stubs;
 
-import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.openapi.application.ModalityState;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.editor.Document;
-import com.intellij.openapi.fileEditor.FileDocumentManager;
-import com.intellij.openapi.project.DumbService;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.psi.search.GlobalSearchScopes;
-import com.intellij.util.indexing.FileBasedIndex;
-import com.intellij.util.indexing.FileContent;
-import com.intellij.util.indexing.FileContentImpl;
 import gnu.trove.TIntArrayList;
 import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 public class StubTree {
-  private static final Logger LOG = Logger.getInstance("#com.intellij.psi.stubs.StubTree");
-
   private final PsiFileStub myRoot;
   private final List<StubElement<?>> myPlainList = new ArrayList<StubElement<?>>();
 
@@ -98,65 +82,4 @@ public class StubTree {
 
     return result;
   }
-
-  @Nullable
-  public static StubTree readOrBuild(Project project, final VirtualFile vFile) {
-    final StubTree fromIndices = readFromVFile(project, vFile);
-    if (fromIndices != null) {
-      return fromIndices;
-    }
-
-    if (!StubUpdatingIndex.canHaveStub(vFile)) {
-      return null;
-    }
-
-    try {
-      final FileContent fc = new FileContentImpl(vFile, vFile.contentsToByteArray());
-      fc.putUserData(FileBasedIndex.PROJECT, project);
-      final StubElement element = StubUpdatingIndex.buildStubTree(fc);
-      if (element instanceof PsiFileStub) {
-        return new StubTree((PsiFileStub)element);
-      }
-    }
-    catch (IOException e) {
-      throw new RuntimeException(e);
-    }
-
-    return null;
-  }
-
-  @Nullable
-  public static StubTree readFromVFile(Project project, final VirtualFile vFile) {
-    if (DumbService.getInstance(project).isDumb()) {
-      return null;
-    }
-
-    final int id = Math.abs(FileBasedIndex.getFileId(vFile));
-    if (id > 0) {
-      final List<SerializedStubTree> datas = FileBasedIndex.getInstance().getValues(StubUpdatingIndex.INDEX_ID, id, GlobalSearchScopes
-          .fileScope(project, vFile));
-      final int size = datas.size();
-
-      if (size == 1) {
-        StubElement stub = datas.get(0).getStub();
-        return new StubTree((PsiFileStub)stub);
-      }
-      else if (size != 0) {
-        LOG.error("Twin stubs: " + vFile.getPresentableUrl() + " has " + size + " stub versions. Should only have one. id=" + id);
-
-        ApplicationManager.getApplication().invokeLater(new Runnable() {
-          public void run() {
-            final Document doc = FileDocumentManager.getInstance().getCachedDocument(vFile);
-            if (doc != null) {
-              FileDocumentManager.getInstance().saveDocument(doc);
-            }
-          }
-        }, ModalityState.NON_MODAL);
-
-        FileBasedIndex.getInstance().requestReindex(vFile);
-      }
-    }
-
-    return null;
-  }
 }
diff --git a/platform/lang-impl/src/com/intellij/psi/stubs/StubTreeLoader.java b/platform/lang-impl/src/com/intellij/psi/stubs/StubTreeLoader.java
new file mode 100644 (file)
index 0000000..190d907
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2000-2011 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.psi.stubs;
+
+import com.intellij.openapi.components.ServiceManager;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vfs.VirtualFile;
+
+/**
+ * @author yole
+ */
+public abstract class StubTreeLoader {
+  public static StubTreeLoader getInstance() {
+    return ServiceManager.getService(StubTreeLoader.class);
+  }
+
+  public abstract StubTree readOrBuild(Project project, final VirtualFile vFile);
+
+  public abstract StubTree readFromVFile(Project project, final VirtualFile vFile);
+}
diff --git a/platform/lang-impl/src/com/intellij/psi/stubs/StubTreeLoaderImpl.java b/platform/lang-impl/src/com/intellij/psi/stubs/StubTreeLoaderImpl.java
new file mode 100644 (file)
index 0000000..634bc44
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2000-2011 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.psi.stubs;
+
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.application.ModalityState;
+import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.fileEditor.FileDocumentManager;
+import com.intellij.openapi.project.DumbService;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.psi.search.GlobalSearchScopes;
+import com.intellij.util.indexing.FileBasedIndex;
+import com.intellij.util.indexing.FileContent;
+import com.intellij.util.indexing.FileContentImpl;
+import org.jetbrains.annotations.Nullable;
+
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * @author yole
+ */
+public class StubTreeLoaderImpl extends StubTreeLoader {
+  private static final Logger LOG = Logger.getInstance("#com.intellij.psi.stubs.StubTreeLoaderImpl");
+
+  @Nullable
+  public StubTree readOrBuild(Project project, final VirtualFile vFile) {
+    final StubTree fromIndices = readFromVFile(project, vFile);
+    if (fromIndices != null) {
+      return fromIndices;
+    }
+
+    if (!StubUpdatingIndex.canHaveStub(vFile)) {
+      return null;
+    }
+
+    try {
+      final FileContent fc = new FileContentImpl(vFile, vFile.contentsToByteArray());
+      fc.putUserData(FileBasedIndex.PROJECT, project);
+      final StubElement element = StubUpdatingIndex.buildStubTree(fc);
+      if (element instanceof PsiFileStub) {
+        return new StubTree((PsiFileStub)element);
+      }
+    }
+    catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+
+    return null;
+  }
+
+  @Nullable
+  public StubTree readFromVFile(Project project, final VirtualFile vFile) {
+    if (DumbService.getInstance(project).isDumb()) {
+      return null;
+    }
+
+    final int id = Math.abs(FileBasedIndex.getFileId(vFile));
+    if (id > 0) {
+      final List<SerializedStubTree> datas = FileBasedIndex.getInstance().getValues(StubUpdatingIndex.INDEX_ID, id, GlobalSearchScopes
+          .fileScope(project, vFile));
+      final int size = datas.size();
+
+      if (size == 1) {
+        StubElement stub = datas.get(0).getStub();
+        return new StubTree((PsiFileStub)stub);
+      }
+      else if (size != 0) {
+        LOG.error("Twin stubs: " + vFile.getPresentableUrl() + " has " + size + " stub versions. Should only have one. id=" + id);
+
+        ApplicationManager.getApplication().invokeLater(new Runnable() {
+          public void run() {
+            final Document doc = FileDocumentManager.getInstance().getCachedDocument(vFile);
+            if (doc != null) {
+              FileDocumentManager.getInstance().saveDocument(doc);
+            }
+          }
+        }, ModalityState.NON_MODAL);
+
+        FileBasedIndex.getInstance().requestReindex(vFile);
+      }
+    }
+
+    return null;
+  }
+}
index 46eca23f7fbd6b0bfe7974a8355f0536a74539f1..f00542aa4242add2a454e03a7ca7307afc1947c5 100644 (file)
   <applicationService serviceInterface="com.intellij.psi.impl.DocumentCommitThread"
       serviceImplementation="com.intellij.psi.impl.DocumentCommitThread"/>
 
+  <applicationService serviceInterface="com.intellij.psi.stubs.StubTreeLoader"
+                      serviceImplementation="com.intellij.psi.stubs.StubTreeLoaderImpl"/>
+
   <projectService serviceInterface="com.intellij.ui.EditorTextFieldProvider"
                   serviceImplementation="com.intellij.ui.EditorTextFieldProviderImpl"/>