Index classes refactored: keys are properly references, ensuring that a keys doesn't get confused across classes

This commit is contained in:
Andrey Breslav
2012-12-18 18:26:25 +04:00
parent 5f3fc5190f
commit 4a6fd1676b
17 changed files with 93 additions and 82 deletions
@@ -35,7 +35,7 @@ import org.jetbrains.jet.plugin.JetLanguage;
import java.io.IOException;
public class JetFileElementType extends IStubFileElementType<PsiJetFileStub> {
public static final int STUB_VERSION = 18;
public static final int STUB_VERSION = 19;
public JetFileElementType() {
super("jet.FILE", JetLanguage.INSTANCE);
@@ -25,7 +25,8 @@ import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.stubs.StubIndex;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.plugin.stubindex.JetIndexKeys;
import org.jetbrains.jet.plugin.stubindex.JetAllShortFunctionNameIndex;
import org.jetbrains.jet.plugin.stubindex.JetShortPropertiesNameIndex;
import java.util.ArrayList;
import java.util.Collection;
@@ -35,8 +36,8 @@ public class JetGotoSymbolContributor implements ChooseByNameContributor {
@NotNull
@Override
public String[] getNames(Project project, boolean includeNonProjectItems) {
Collection<String> items = StubIndex.getInstance().getAllKeys(JetIndexKeys.FUNCTIONS_SHORT_NAME_KEY, project);
items.addAll(StubIndex.getInstance().getAllKeys(JetIndexKeys.PROPERTIES_SHORT_NAME_KEY, project));
Collection<String> items = StubIndex.getInstance().getAllKeys(JetAllShortFunctionNameIndex.getInstance().getKey(), project);
items.addAll(StubIndex.getInstance().getAllKeys(JetShortPropertiesNameIndex.getInstance().getKey(), project));
return ArrayUtil.toStringArray(items);
}
@@ -47,10 +48,10 @@ public class JetGotoSymbolContributor implements ChooseByNameContributor {
GlobalSearchScope scope = includeNonProjectItems ? GlobalSearchScope.allScope(project) : GlobalSearchScope.projectScope(project);
Collection<? extends NavigationItem> functions = StubIndex.getInstance().get(
JetIndexKeys.FUNCTIONS_SHORT_NAME_KEY, name, project, scope);
JetAllShortFunctionNameIndex.getInstance().getKey(), name, project, scope);
Collection<? extends NavigationItem> properties = StubIndex.getInstance().get(
JetIndexKeys.PROPERTIES_SHORT_NAME_KEY, name, project, scope);
JetShortPropertiesNameIndex.getInstance().getKey(), name, project, scope);
final List<NavigationItem> items = new ArrayList<NavigationItem>(Collections2.filter(functions, Predicates.notNull()));
items.addAll(properties);
@@ -26,6 +26,8 @@ import org.jetbrains.jet.lang.psi.JetNamedFunction;
import java.util.Collection;
public class JetAllShortFunctionNameIndex extends StringStubIndexExtension<JetNamedFunction> {
private static final StubIndexKey<String, JetNamedFunction> KEY = KotlinIndexUtil.createIndexKey(JetAllShortFunctionNameIndex.class);
private static final JetShortClassNameIndex ourInstance = new JetShortClassNameIndex();
public static JetShortClassNameIndex getInstance() {
@@ -35,7 +37,7 @@ public class JetAllShortFunctionNameIndex extends StringStubIndexExtension<JetNa
@NotNull
@Override
public StubIndexKey<String, JetNamedFunction> getKey() {
return JetIndexKeys.FUNCTIONS_SHORT_NAME_KEY;
return KEY;
}
@Override
@@ -26,6 +26,8 @@ import org.jetbrains.jet.lang.psi.JetAnnotationEntry;
import java.util.Collection;
public class JetAnnotationsIndex extends StringStubIndexExtension<JetAnnotationEntry> {
private static final StubIndexKey<String, JetAnnotationEntry> KEY = KotlinIndexUtil.createIndexKey(JetAnnotationsIndex.class);
private static final JetAnnotationsIndex ourInstance = new JetAnnotationsIndex();
public static JetAnnotationsIndex getInstance() {
@@ -35,7 +37,7 @@ public class JetAnnotationsIndex extends StringStubIndexExtension<JetAnnotationE
@NotNull
@Override
public StubIndexKey<String, JetAnnotationEntry> getKey() {
return JetIndexKeys.ANNOTATIONS_KEY;
return KEY;
}
@Override
@@ -22,6 +22,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
public class JetExtensionFunctionNameIndex extends StringStubIndexExtension<JetNamedFunction> {
private static final StubIndexKey<String, JetNamedFunction> KEY = KotlinIndexUtil.createIndexKey(JetExtensionFunctionNameIndex.class);
private static final JetExtensionFunctionNameIndex instance = new JetExtensionFunctionNameIndex();
public static JetExtensionFunctionNameIndex getInstance() {
@@ -31,6 +33,6 @@ public class JetExtensionFunctionNameIndex extends StringStubIndexExtension<JetN
@NotNull
@Override
public StubIndexKey<String, JetNamedFunction> getKey() {
return JetIndexKeys.TOP_LEVEL_EXTENSION_FUNCTION_SHORT_NAME_KEY;
return KEY;
}
}
@@ -26,6 +26,8 @@ import org.jetbrains.jet.lang.psi.JetClassOrObject;
import java.util.Collection;
public class JetFullClassNameIndex extends StringStubIndexExtension<JetClassOrObject> {
private static final StubIndexKey<String, JetClassOrObject> KEY = KotlinIndexUtil.createIndexKey(JetFullClassNameIndex.class);
private static final JetFullClassNameIndex ourInstance = new JetFullClassNameIndex();
public static JetFullClassNameIndex getInstance() {
@@ -35,7 +37,7 @@ public class JetFullClassNameIndex extends StringStubIndexExtension<JetClassOrOb
@NotNull
@Override
public StubIndexKey<String, JetClassOrObject> getKey() {
return JetIndexKeys.CLASS_OR_OBJECT_FQN_KEY;
return KEY;
}
@Override
@@ -1,50 +0,0 @@
/*
* Copyright 2010-2012 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 org.jetbrains.jet.plugin.stubindex;
import com.intellij.psi.stubs.StubIndexKey;
import org.jetbrains.jet.lang.psi.*;
public interface JetIndexKeys {
StubIndexKey<String, JetFile> PACKAGE_DECLARATION_KEY = StubIndexKey.createIndexKey("jet.file.package.declaration");
StubIndexKey<String, JetClassOrObject> SHORT_NAME_KEY = StubIndexKey.createIndexKey("jet.class.shortName");
StubIndexKey<String, JetClassOrObject> SUPERCLASS_NAME_KEY = StubIndexKey.createIndexKey("jet.class.superClassName");
StubIndexKey<String, JetClassOrObject> CLASS_OR_OBJECT_FQN_KEY = StubIndexKey.createIndexKey("jet.fqn");
StubIndexKey<String, JetObjectDeclaration> TOP_LEVEL_OBJECT_SHORT_NAME_KEY =
StubIndexKey.createIndexKey("jet.top.level.object.short.name");
StubIndexKey<String, JetNamedFunction> TOP_LEVEL_FUNCTION_SHORT_NAME_KEY =
StubIndexKey.createIndexKey("jet.top.level.function.short.name");
StubIndexKey<String, JetNamedFunction> TOP_LEVEL_EXTENSION_FUNCTION_SHORT_NAME_KEY =
StubIndexKey.createIndexKey("jet.top.level.extension.function.short.name");
/** Stores package top level function (both extension and non-extension) full qualified names. */
StubIndexKey<String, JetNamedFunction> TOP_LEVEL_FUNCTIONS_FQN_NAME_KEY =
StubIndexKey.createIndexKey("jet.top.level.functions.fqn.name");
StubIndexKey<String, JetProperty> TOP_LEVEL_PROPERTY_FQN_NAME_KEY =
StubIndexKey.createIndexKey("jet.top.level.property.fqn.name");
StubIndexKey<String, JetNamedFunction> FUNCTIONS_SHORT_NAME_KEY = StubIndexKey.createIndexKey("jet.functions.short.name");
StubIndexKey<String, JetProperty> PROPERTIES_SHORT_NAME_KEY = StubIndexKey.createIndexKey("jet.properties.short.name");
StubIndexKey<String, JetAnnotationEntry> ANNOTATIONS_KEY = StubIndexKey.createIndexKey("jet.annotations");
}
@@ -26,6 +26,8 @@ import org.jetbrains.jet.lang.psi.JetFile;
import java.util.Collection;
public class JetPackageDeclarationIndex extends StringStubIndexExtension<JetFile> {
private static final StubIndexKey<String, JetFile> KEY = KotlinIndexUtil.createIndexKey(JetPackageDeclarationIndex.class);
private static final JetPackageDeclarationIndex ourInstance = new JetPackageDeclarationIndex();
public static JetPackageDeclarationIndex getInstance() {
@@ -35,7 +37,7 @@ public class JetPackageDeclarationIndex extends StringStubIndexExtension<JetFile
@NotNull
@Override
public StubIndexKey<String, JetFile> getKey() {
return JetIndexKeys.PACKAGE_DECLARATION_KEY;
return KEY;
}
@Override
@@ -26,6 +26,8 @@ import org.jetbrains.jet.lang.psi.JetClassOrObject;
import java.util.Collection;
public class JetShortClassNameIndex extends StringStubIndexExtension<JetClassOrObject> {
private static final StubIndexKey<String, JetClassOrObject> KEY = KotlinIndexUtil.createIndexKey(JetShortClassNameIndex.class);
private static final JetShortClassNameIndex ourInstance = new JetShortClassNameIndex();
public static JetShortClassNameIndex getInstance() {
@@ -35,7 +37,7 @@ public class JetShortClassNameIndex extends StringStubIndexExtension<JetClassOrO
@NotNull
@Override
public StubIndexKey<String, JetClassOrObject> getKey() {
return JetIndexKeys.SHORT_NAME_KEY;
return KEY;
}
@Override
@@ -26,6 +26,8 @@ import org.jetbrains.jet.lang.psi.JetNamedFunction;
import java.util.Collection;
public class JetShortFunctionNameIndex extends StringStubIndexExtension<JetNamedFunction> {
private static final StubIndexKey<String, JetNamedFunction> KEY = KotlinIndexUtil.createIndexKey(JetShortFunctionNameIndex.class);
private static final JetShortFunctionNameIndex ourInstance = new JetShortFunctionNameIndex();
public static JetShortFunctionNameIndex getInstance() {
@@ -35,7 +37,7 @@ public class JetShortFunctionNameIndex extends StringStubIndexExtension<JetNamed
@NotNull
@Override
public StubIndexKey<String, JetNamedFunction> getKey() {
return JetIndexKeys.TOP_LEVEL_FUNCTION_SHORT_NAME_KEY;
return KEY;
}
@Override
@@ -26,6 +26,8 @@ import org.jetbrains.jet.lang.psi.JetProperty;
import java.util.Collection;
public class JetShortPropertiesNameIndex extends StringStubIndexExtension<JetProperty> {
private static final StubIndexKey<String, JetProperty> KEY = KotlinIndexUtil.createIndexKey(JetShortPropertiesNameIndex.class);
private static final JetShortPropertiesNameIndex ourInstance = new JetShortPropertiesNameIndex();
public static JetShortPropertiesNameIndex getInstance() {
@@ -35,7 +37,7 @@ public class JetShortPropertiesNameIndex extends StringStubIndexExtension<JetPro
@NotNull
@Override
public StubIndexKey<String, JetProperty> getKey() {
return JetIndexKeys.PROPERTIES_SHORT_NAME_KEY;
return KEY;
}
@Override
@@ -26,7 +26,10 @@ import org.jetbrains.jet.lang.psi.JetClassOrObject;
import java.util.Collection;
public class JetSuperClassIndex extends StringStubIndexExtension<JetClassOrObject> {
private static final StubIndexKey<String, JetClassOrObject> KEY = KotlinIndexUtil.createIndexKey(JetSuperClassIndex.class);
private static final JetSuperClassIndex ourInstance = new JetSuperClassIndex();
public static JetSuperClassIndex getInstance() {
return ourInstance;
}
@@ -34,7 +37,7 @@ public class JetSuperClassIndex extends StringStubIndexExtension<JetClassOrObjec
@NotNull
@Override
public StubIndexKey<String, JetClassOrObject> getKey() {
return JetIndexKeys.SUPERCLASS_NAME_KEY;
return KEY;
}
@Override
@@ -25,7 +25,12 @@ import org.jetbrains.jet.lang.psi.JetNamedFunction;
import java.util.Collection;
/**
* Stores package top level function (both extension and non-extension) full qualified names.
*/
public class JetTopLevelFunctionsFqnNameIndex extends StringStubIndexExtension<JetNamedFunction> {
private static final StubIndexKey<String, JetNamedFunction> KEY = KotlinIndexUtil.createIndexKey(JetTopLevelFunctionsFqnNameIndex.class);
private static final JetTopLevelFunctionsFqnNameIndex INSTANCE = new JetTopLevelFunctionsFqnNameIndex();
public static JetTopLevelFunctionsFqnNameIndex getInstance() {
@@ -35,7 +40,7 @@ public class JetTopLevelFunctionsFqnNameIndex extends StringStubIndexExtension<J
@NotNull
@Override
public StubIndexKey<String, JetNamedFunction> getKey() {
return JetIndexKeys.TOP_LEVEL_FUNCTIONS_FQN_NAME_KEY;
return KEY;
}
@Override
@@ -26,6 +26,8 @@ import org.jetbrains.jet.lang.psi.JetProperty;
import java.util.Collection;
public class JetTopLevelPropertiesFqnNameIndex extends StringStubIndexExtension<JetProperty> {
private static final StubIndexKey<String, JetProperty> KEY = KotlinIndexUtil.createIndexKey(JetTopLevelPropertiesFqnNameIndex.class);
private static final JetTopLevelPropertiesFqnNameIndex INSTANCE = new JetTopLevelPropertiesFqnNameIndex();
public static JetTopLevelPropertiesFqnNameIndex getInstance() {
@@ -35,7 +37,7 @@ public class JetTopLevelPropertiesFqnNameIndex extends StringStubIndexExtension<
@NotNull
@Override
public StubIndexKey<String, JetProperty> getKey() {
return JetIndexKeys.TOP_LEVEL_PROPERTY_FQN_NAME_KEY;
return KEY;
}
@Override
@@ -26,6 +26,8 @@ import org.jetbrains.jet.lang.psi.JetObjectDeclaration;
import java.util.Collection;
public class JetTopLevelShortObjectNameIndex extends StringStubIndexExtension<JetObjectDeclaration> {
private static final StubIndexKey<String, JetObjectDeclaration> KEY = KotlinIndexUtil.createIndexKey(JetTopLevelShortObjectNameIndex.class);
private static final JetTopLevelShortObjectNameIndex ourInstance = new JetTopLevelShortObjectNameIndex();
public static JetTopLevelShortObjectNameIndex getInstance() {
@@ -35,7 +37,7 @@ public class JetTopLevelShortObjectNameIndex extends StringStubIndexExtension<Je
@NotNull
@Override
public StubIndexKey<String, JetObjectDeclaration> getKey() {
return JetIndexKeys.TOP_LEVEL_OBJECT_SHORT_NAME_KEY;
return KEY;
}
@Override
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2012 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 org.jetbrains.jet.plugin.stubindex;
import com.intellij.psi.PsiElement;
import com.intellij.psi.stubs.StubIndexExtension;
import com.intellij.psi.stubs.StubIndexKey;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class KotlinIndexUtil {
@NonNls
public static <K, Psi extends PsiElement> StubIndexKey<K, Psi> createIndexKey(@NotNull Class<? extends StubIndexExtension<K, Psi>> indexClass) {
return StubIndexKey.createIndexKey(indexClass.getCanonicalName());
}
private KotlinIndexUtil() {}
}
@@ -26,23 +26,23 @@ public class StubIndexServiceImpl implements StubIndexService {
@Override
public void indexFile(PsiJetFileStub stub, IndexSink sink) {
String packageName = stub.getPackageName();
sink.occurrence(JetIndexKeys.PACKAGE_DECLARATION_KEY, packageName == null ? "" : packageName);
sink.occurrence(JetPackageDeclarationIndex.getInstance().getKey(), packageName == null ? "" : packageName);
}
@Override
public void indexClass(PsiJetClassStub stub, IndexSink sink) {
String name = stub.getName();
if (name != null) {
sink.occurrence(JetIndexKeys.SHORT_NAME_KEY, name);
sink.occurrence(JetShortClassNameIndex.getInstance().getKey(), name);
}
String fqn = stub.getQualifiedName();
if (fqn != null) {
sink.occurrence(JetIndexKeys.CLASS_OR_OBJECT_FQN_KEY, fqn);
sink.occurrence(JetFullClassNameIndex.getInstance().getKey(), fqn);
}
for (String superName : stub.getSuperNames()) {
sink.occurrence(JetIndexKeys.SUPERCLASS_NAME_KEY, superName);
sink.occurrence(JetSuperClassIndex.getInstance().getKey(), superName);
}
}
@@ -51,15 +51,15 @@ public class StubIndexServiceImpl implements StubIndexService {
String name = stub.getName();
assert name != null;
sink.occurrence(JetIndexKeys.SHORT_NAME_KEY, name);
sink.occurrence(JetShortClassNameIndex.getInstance().getKey(), name);
if (stub.isTopLevel()) {
sink.occurrence(JetIndexKeys.TOP_LEVEL_OBJECT_SHORT_NAME_KEY, name);
sink.occurrence(JetTopLevelShortObjectNameIndex.getInstance().getKey(), name);
}
FqName fqName = stub.getFQName();
if (fqName != null) {
sink.occurrence(JetIndexKeys.CLASS_OR_OBJECT_FQN_KEY, fqName.toString());
sink.occurrence(JetFullClassNameIndex.getInstance().getKey(), fqName.toString());
}
}
@@ -70,19 +70,19 @@ public class StubIndexServiceImpl implements StubIndexService {
if (stub.isTopLevel()) {
// Collection only top level functions as only they are expected in completion without explicit import
if (!stub.isExtension()) {
sink.occurrence(JetIndexKeys.TOP_LEVEL_FUNCTION_SHORT_NAME_KEY, name);
sink.occurrence(JetShortFunctionNameIndex.getInstance().getKey(), name);
}
else {
sink.occurrence(JetIndexKeys.TOP_LEVEL_EXTENSION_FUNCTION_SHORT_NAME_KEY, name);
sink.occurrence(JetExtensionFunctionNameIndex.getInstance().getKey(), name);
}
FqName topFQName = stub.getTopFQName();
if (topFQName != null) {
sink.occurrence(JetIndexKeys.TOP_LEVEL_FUNCTIONS_FQN_NAME_KEY, topFQName.toString());
sink.occurrence(JetTopLevelFunctionsFqnNameIndex.getInstance().getKey(), topFQName.toString());
}
}
sink.occurrence(JetIndexKeys.FUNCTIONS_SHORT_NAME_KEY, name);
sink.occurrence(JetAllShortFunctionNameIndex.getInstance().getKey(), name);
}
}
@@ -93,16 +93,16 @@ public class StubIndexServiceImpl implements StubIndexService {
if (stub.isTopLevel()) {
FqName topFQName = stub.getTopFQName();
if (topFQName != null) {
sink.occurrence(JetIndexKeys.TOP_LEVEL_PROPERTY_FQN_NAME_KEY, topFQName.toString());
sink.occurrence(JetTopLevelPropertiesFqnNameIndex.getInstance().getKey(), topFQName.toString());
}
}
sink.occurrence(JetIndexKeys.PROPERTIES_SHORT_NAME_KEY, propertyName);
sink.occurrence(JetShortPropertiesNameIndex.getInstance().getKey(), propertyName);
}
}
@Override
public void indexAnnotation(PsiJetAnnotationStub stub, IndexSink sink) {
sink.occurrence(JetIndexKeys.ANNOTATIONS_KEY, stub.getShortName());
sink.occurrence(JetAnnotationsIndex.getInstance().getKey(), stub.getShortName());
}
}