Refactor: rewrite JetReferenceContributor to kotlin

Add additional utilities to track what psi elements are capable of holding references
This commit is contained in:
Pavel V. Talanov
2014-01-30 16:38:48 +04:00
parent 41f9dcba91
commit 2f55959578
2 changed files with 62 additions and 89 deletions
@@ -1,89 +0,0 @@
/*
* Copyright 2010-2013 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.references;
import com.intellij.psi.*;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
import static com.intellij.patterns.PlatformPatterns.psiElement;
public class JetReferenceContributor extends PsiReferenceContributor {
@Override
public void registerReferenceProviders(PsiReferenceRegistrar registrar) {
registrar.registerReferenceProvider(psiElement(JetSimpleNameExpression.class),
new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) {
return toArray(new JetSimpleNameReference((JetSimpleNameExpression) element));
}
});
registrar.registerReferenceProvider(psiElement(JetThisReferenceExpression.class),
new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) {
return toArray(new JetThisReference((JetThisReferenceExpression) element));
}
});
registrar.registerReferenceProvider(psiElement(JetArrayAccessExpression.class),
new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) {
return toArray(new JetArrayAccessReference((JetArrayAccessExpression) element));
}
});
registrar.registerReferenceProvider(psiElement(JetCallExpression.class),
new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) {
return toArray(new JetInvokeFunctionReference((JetCallExpression) element));
}
});
registrar.registerReferenceProvider(psiElement(JetPropertyDelegate.class),
new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) {
return toArray(
new JetPropertyDelegationMethodsReference((JetPropertyDelegate) element));
}
});
registrar.registerReferenceProvider(psiElement(JetForExpression.class),
new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext) {
return toArray(new JetForLoopInReference((JetForExpression) element));
}
});
}
@NotNull
private static JetReference[] toArray(@NotNull JetReference reference) {
return new JetReference[] {reference};
}
}
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2014 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.references
import com.intellij.psi.*
import org.jetbrains.jet.lang.psi.*
import com.intellij.util.ProcessingContext
import com.intellij.patterns.PlatformPatterns
public class JetReferenceContributor() : PsiReferenceContributor() {
public override fun registerReferenceProviders(registrar: PsiReferenceRegistrar?) {
if (registrar == null) {
return
}
with(registrar) {
registerProvider(javaClass<JetSimpleNameExpression>()) {
JetSimpleNameReference(it)
}
registerProvider(javaClass<JetThisReferenceExpression>()) {
JetThisReference(it)
}
registerProvider(javaClass<JetCallExpression>()) {
JetInvokeFunctionReference(it)
}
registerProvider(javaClass<JetArrayAccessExpression>()) {
JetArrayAccessReference(it)
}
registerProvider(javaClass<JetForExpression>()) {
JetForLoopInReference(it)
}
registerProvider(javaClass<JetPropertyDelegate>()) {
JetPropertyDelegationMethodsReference(it)
}
}
}
private fun <E : JetElement, R : AbstractJetReference<E>> PsiReferenceRegistrar.registerProvider(
elementClass: Class<E>,
factory: (E) -> R
) {
registerReferenceProvider(PlatformPatterns.psiElement(elementClass)!!, object: PsiReferenceProvider() {
override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array<PsiReference> {
[suppress("UNCHECKED_CAST")]
return array(factory(element as E))
}
})
}
}