ElementPatternBean extracted & lazy PsiReferenceProviderBean

This commit is contained in:
Dmitry Avdeev
2011-05-12 10:15:36 +04:00
parent 45040495e7
commit ab3650db7a
8 changed files with 114 additions and 71 deletions
@@ -71,7 +71,7 @@ public class WordCompletionTest extends CompletionTestCase {
}
};
PsiReferenceRegistrarImpl registrar =
(PsiReferenceRegistrarImpl)ReferenceProvidersRegistry.getInstance().getRegistrar(StdLanguages.JAVA);
ReferenceProvidersRegistry.getInstance().getRegistrar(StdLanguages.JAVA);
try {
registrar.registerReferenceProvider(PsiLiteralExpression.class, softProvider);
registrar.registerReferenceProvider(PsiLiteralExpression.class, hardProvider);
@@ -0,0 +1,38 @@
/*
* 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.patterns;
import com.intellij.patterns.compiler.PatternCompilerFactory;
import com.intellij.psi.PsiElement;
import com.intellij.util.xmlb.annotations.Attribute;
import com.intellij.util.xmlb.annotations.Tag;
import com.intellij.util.xmlb.annotations.Text;
import org.jetbrains.annotations.Nullable;
@Tag("pattern")
public class ElementPatternBean {
@Attribute("type")
public String type;
@Text
public String text;
@Nullable
public ElementPattern<PsiElement> compilePattern() {
return PatternCompilerFactory.getFactory().<PsiElement>getPatternCompiler(type).compileElementPattern(text);
}
}
@@ -15,7 +15,10 @@ import com.intellij.openapi.extensions.ExtensionPointName;
* Some elements return them from {@link PsiElement#getReferences()} directly though, but one should not rely on that
* behavior since it may be changed in the future.
*
* The alternative way to register {@link PsiReferenceProvider} is by using {@link PsiReferenceProviderBean}.
*
* @author peter
* @see PsiReferenceProviderBean
*/
public abstract class PsiReferenceContributor implements Disposable {
public static final ExtensionPointName<PsiReferenceContributor> EP_NAME = ExtensionPointName.create("com.intellij.psi.referenceContributor");
@@ -20,6 +20,8 @@ import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;
/**
* Register it via {@link PsiReferenceContributor} or {@link PsiReferenceProviderBean#EP_NAME}
*
* @author ik
*/
public abstract class PsiReferenceProvider {
@@ -19,27 +19,41 @@ package com.intellij.psi;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.AbstractExtensionPointBean;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.ElementPatternBean;
import com.intellij.patterns.StandardPatterns;
import com.intellij.patterns.compiler.PatternCompilerFactory;
import com.intellij.util.xmlb.annotations.*;
import com.intellij.util.NullableFunction;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xmlb.annotations.AbstractCollection;
import com.intellij.util.xmlb.annotations.Attribute;
import com.intellij.util.xmlb.annotations.Property;
import com.intellij.util.xmlb.annotations.Tag;
import org.jetbrains.annotations.Nullable;
/**
* Registers a {@link PsiReferenceProvider} in plugin.xml
*/
public class PsiReferenceProviderBean extends AbstractExtensionPointBean {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.PsiReferenceProviderBean");
public static final ExtensionPointName<PsiReferenceProviderBean> EP_NAME =
new ExtensionPointName<PsiReferenceProviderBean>("com.intellij.psi.referenceProvider");
@Attribute("providerClass")
public String className;
@Tag("description")
public String description;
@Property(surroundWithTag = false)
@AbstractCollection(surroundWithTag = false)
public Info[] patterns;
public ElementPatternBean[] patterns;
public String getDescription() {
return description;
}
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.PsiReferenceProviderBean");
public PsiReferenceProvider instantiate() {
try {
return (PsiReferenceProvider)instantiate(className, ApplicationManager.getApplication().getPicoContainer());
@@ -50,30 +64,25 @@ public class PsiReferenceProviderBean extends AbstractExtensionPointBean {
return null;
}
private static final NullableFunction<ElementPatternBean,ElementPattern<? extends PsiElement>> PATTERN_NULLABLE_FUNCTION = new NullableFunction<ElementPatternBean, ElementPattern<? extends PsiElement>>() {
@Override
public ElementPattern<? extends PsiElement> fun(ElementPatternBean elementPatternBean) {
return elementPatternBean.compilePattern();
}
};
@Nullable
public ElementPattern<PsiElement> createElementPattern() {
final PatternCompilerFactory factory = PatternCompilerFactory.getFactory();
if (patterns.length > 1) {
final ElementPattern[] result = new ElementPattern[this.patterns.length];
for (int i = 0, len = this.patterns.length; i < len; i++) {
result[i] = factory.getPatternCompiler(patterns[i].type).compileElementPattern(patterns[i].text);
}
return StandardPatterns.or(result);
return StandardPatterns.or(ContainerUtil.mapNotNull(patterns,
PATTERN_NULLABLE_FUNCTION).toArray(new ElementPattern[0]));
}
else if (patterns.length == 1) {
return factory.<PsiElement>getPatternCompiler(patterns[0].type).compileElementPattern(patterns[0].text);
return patterns[0].compilePattern();
}
else {
LOG.error("At least one pattern should be specified");
return null;
}
}
@Tag("pattern")
public static class Info {
@Attribute("type")
public String type;
@Text
public String text;
}
}
@@ -1,44 +0,0 @@
/*
* Copyright 2000-2010 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;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.patterns.ElementPattern;
/**
* @author Gregory.Shrago
*/
public class PsiReferenceContributorImpl extends PsiReferenceContributor {
public static final ExtensionPointName<PsiReferenceProviderBean> PSI_REFERENCE_PROVIDERS_EP =
new ExtensionPointName<PsiReferenceProviderBean>("com.intellij.psi.referenceProvider");
@Override
public void registerReferenceProviders(PsiReferenceRegistrar registrar) {
for (PsiReferenceProviderBean providerBean : PSI_REFERENCE_PROVIDERS_EP.getExtensions()) {
registerReferenceProvider(registrar, providerBean);
}
}
private static void registerReferenceProvider(PsiReferenceRegistrar registrar, PsiReferenceProviderBean providerBean) {
final ElementPattern<PsiElement> pattern = providerBean.createElementPattern();
if (pattern != null) {
final PsiReferenceProvider provider = providerBean.instantiate();
if (provider != null) {
registrar.registerReferenceProvider(pattern, provider);
}
}
}
}
@@ -22,6 +22,7 @@ import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.IndexNotReadyException;
import com.intellij.openapi.util.Trinity;
import com.intellij.patterns.ElementPattern;
import com.intellij.psi.*;
import com.intellij.util.ProcessingContext;
import com.intellij.util.SmartList;
@@ -52,9 +53,16 @@ public class ReferenceProvidersRegistry {
return o2.getThird().compareTo(o1.getThird());
}
};
public final static PsiReferenceProvider NULL_REFERENCE_PROVIDER = new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
return PsiReference.EMPTY_ARRAY;
}
};
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
private final static Map<Language, PsiReferenceRegistrarImpl> ourRegistrars = new FactoryMap<Language, PsiReferenceRegistrarImpl>() {
private final Map<Language, PsiReferenceRegistrarImpl> myRegistrars = new FactoryMap<Language, PsiReferenceRegistrarImpl>() {
@Override
protected PsiReferenceRegistrarImpl create(Language language) {
PsiReferenceRegistrarImpl registrar = new PsiReferenceRegistrarImpl();
@@ -70,8 +78,36 @@ public class ReferenceProvidersRegistry {
return ServiceManager.getService(ReferenceProvidersRegistry.class);
}
public PsiReferenceRegistrar getRegistrar(Language language) {
return ourRegistrars.get(language);
public ReferenceProvidersRegistry() {
PsiReferenceRegistrarImpl registrar = getRegistrar(Language.ANY);
for (final PsiReferenceProviderBean providerBean : PsiReferenceProviderBean.EP_NAME.getExtensions()) {
final ElementPattern<PsiElement> pattern = providerBean.createElementPattern();
if (pattern != null) {
registrar.registerReferenceProvider(pattern, new PsiReferenceProvider() {
PsiReferenceProvider myProvider;
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
if (myProvider == null) {
myProvider = providerBean.instantiate();
if (myProvider == null) {
myProvider = NULL_REFERENCE_PROVIDER;
}
}
return myProvider.getReferencesByElement(element, context);
}
});
}
}
}
public PsiReferenceRegistrarImpl getRegistrar(Language language) {
return myRegistrars.get(language);
}
@Deprecated
@@ -83,10 +119,11 @@ public class ReferenceProvidersRegistry {
ProgressManager.checkCanceled();
assert context.isValid() : "Invalid context: " + context;
PsiReferenceRegistrarImpl registrar = ourRegistrars.get(context.getLanguage());
ReferenceProvidersRegistry registry = getInstance();
PsiReferenceRegistrarImpl registrar = registry.getRegistrar(context.getLanguage());
SmartList<Trinity<PsiReferenceProvider, ProcessingContext, Double>> providers = new SmartList<Trinity<PsiReferenceProvider, ProcessingContext, Double>>();
providers.addAll(registrar.getPairsByElement(context, hints));
providers.addAll(ourRegistrars.get(Language.ANY).getPairsByElement(context, hints));
providers.addAll(registry.getRegistrar(Language.ANY).getPairsByElement(context, hints));
if (providers.isEmpty()) {
return PsiReference.EMPTY_ARRAY;
}
@@ -397,8 +397,6 @@
<applicationService serviceInterface="com.intellij.execution.console.ConsoleFoldingSettings" serviceImplementation="com.intellij.execution.console.ConsoleFoldingSettings"/>
<console.folding implementation="com.intellij.execution.console.SubstringConsoleFolding"/>
<psi.referenceContributor implementation="com.intellij.psi.PsiReferenceContributorImpl"/>
<lookup.charFilter implementation="com.intellij.codeInsight.template.impl.LiveTemplateCharFilter" order="first" id="liveTemplate"/>
<lookup.charFilter implementation="com.intellij.codeInsight.completion.CompletionCharFilter" order="last" id="completion"/>
<lookup.charFilter implementation="com.intellij.refactoring.IdentifierCharFilter" id="identifier"/>