From 499e281ded0ff59b5170c257d57af412ffd56dd0 Mon Sep 17 00:00:00 2001 From: Gregory Shrago Date: Sat, 26 Jun 2010 23:25:06 +0400 Subject: [PATCH] reference provider EP --- .../psi/PsiReferenceContributorImpl.java | 44 +++++++++ .../psi/PsiReferenceProviderBean.java | 89 +++++++++++++++++++ .../src/META-INF/LangExtensionPoints.xml | 1 + .../src/META-INF/LangExtensions.xml | 2 + 4 files changed, 136 insertions(+) create mode 100644 platform/lang-impl/src/com/intellij/psi/PsiReferenceContributorImpl.java create mode 100644 platform/lang-impl/src/com/intellij/psi/PsiReferenceProviderBean.java diff --git a/platform/lang-impl/src/com/intellij/psi/PsiReferenceContributorImpl.java b/platform/lang-impl/src/com/intellij/psi/PsiReferenceContributorImpl.java new file mode 100644 index 000000000000..c937b32861c9 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/psi/PsiReferenceContributorImpl.java @@ -0,0 +1,44 @@ +/* + * 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 PSI_REFERENCE_PROVIDERS_EP = + new ExtensionPointName("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 pattern = providerBean.createElementPattern(); + if (pattern != null) { + final PsiReferenceProvider provider = providerBean.instantiate(); + if (provider != null) { + registrar.registerReferenceProvider(pattern, provider); + } + } + } +} diff --git a/platform/lang-impl/src/com/intellij/psi/PsiReferenceProviderBean.java b/platform/lang-impl/src/com/intellij/psi/PsiReferenceProviderBean.java new file mode 100644 index 000000000000..58092e97b38d --- /dev/null +++ b/platform/lang-impl/src/com/intellij/psi/PsiReferenceProviderBean.java @@ -0,0 +1,89 @@ +/* + * 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.application.ApplicationManager; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.extensions.CustomLoadingExtensionPointBean; +import com.intellij.patterns.ElementPattern; +import com.intellij.patterns.StandardPatterns; +import com.intellij.patterns.compiler.PatternCompiler; +import com.intellij.patterns.compiler.PatternCompilerFactory; +import com.intellij.util.xmlb.annotations.AbstractCollection; +import com.intellij.util.xmlb.annotations.Property; +import com.intellij.util.xmlb.annotations.Tag; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +public class PsiReferenceProviderBean extends CustomLoadingExtensionPointBean { + private static final Logger LOG = Logger.getInstance("#com.intellij.psi.PsiReferenceProviderBean"); + @Tag("className") + public String className; + @Tag("description") + public String description; + @Property(surroundWithTag = false) + @AbstractCollection(surroundWithTag = false, elementTag = "patternClass", elementValueAttribute = "") + public List patternClasses = new ArrayList(); + @Property(surroundWithTag = false) + @AbstractCollection(surroundWithTag = false, elementTag = "pattern", elementValueAttribute = "") + public List patterns = new ArrayList(); + + public String getDescription() { + return description; + } + + public PsiReferenceProvider instantiate() { + try { + return (PsiReferenceProvider)instantiateExtension(className, ApplicationManager.getApplication().getPicoContainer()); + } + catch (ClassNotFoundException e) { + LOG.error(e); + } + return null; + } + + @Nullable + public ElementPattern createElementPattern() { + final ArrayList classes = new ArrayList(); + for (String patternClass : patternClasses) { + try { + classes.add(Class.forName(patternClass, true, getLoaderForClass())); + } + catch (ClassNotFoundException e) { + LOG.error(e); + } + } + final PatternCompiler compiler = + PatternCompilerFactory.getFactory().getPatternCompiler(classes.toArray(new Class[classes.size()])); + if (patterns.size() > 1) { + final ElementPattern[] patterns = new ElementPattern[this.patterns.size()]; + for (int i = 0, len = this.patterns.size(); i < len; i++) { + patterns[i] = compiler.compileElementPattern(this.patterns.get(i)); + } + return StandardPatterns.or(patterns); + } + else if (!patterns.isEmpty()) { + return compiler.compileElementPattern(patterns.get(0)); + } + else { + LOG.error("At least one pattern should be specified"); + return null; + } + } +} diff --git a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml index b1cecb26e0fd..140130775d16 100644 --- a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml +++ b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml @@ -257,6 +257,7 @@ + diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index 86876ac4b28f..9bb837c6bc89 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -366,6 +366,8 @@ + +