Introduce parent class for "find usage" handlers

This commit is contained in:
Alexey Sedunov
2013-08-02 16:21:05 +04:00
parent 4ee9783101
commit 76d8e8b95d
4 changed files with 83 additions and 16 deletions
@@ -22,20 +22,23 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.psi.JetFunction;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.plugin.findUsages.handlers.KotlinFindClassUsagesHandler;
import org.jetbrains.jet.plugin.findUsages.handlers.KotlinFindFunctionUsagesHandler;
public class KotlinFindUsagesHandlerFactory extends FindUsagesHandlerFactory {
@Override
public boolean canFindUsages(@NotNull PsiElement element) {
return element instanceof JetClass || element instanceof JetFunction;
return element instanceof JetClass || element instanceof JetNamedFunction;
}
@Override
public FindUsagesHandler createFindUsagesHandler(@NotNull PsiElement element, boolean forHighlightUsages) {
if (element instanceof JetClass) {
return new KotlinFindClassUsagesHandler(element);
return new KotlinFindClassUsagesHandler((JetClass) element, this);
}
if (element instanceof JetFunction) {
return new KotlinFindFunctionUsagesHandler(element);
if (element instanceof JetNamedFunction) {
return new KotlinFindFunctionUsagesHandler((JetNamedFunction) element, this);
}
throw new IllegalArgumentException("unexpected element type: " + element);
}
@@ -14,14 +14,14 @@
* limitations under the License.
*/
package org.jetbrains.jet.plugin.findUsages;
package org.jetbrains.jet.plugin.findUsages.handlers;
import com.intellij.find.findUsages.FindUsagesHandler;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.plugin.findUsages.KotlinFindUsagesHandlerFactory;
public class KotlinFindClassUsagesHandler extends FindUsagesHandler {
public KotlinFindClassUsagesHandler(@NotNull PsiElement psiElement) {
super(psiElement);
public class KotlinFindClassUsagesHandler extends KotlinFindUsagesHandler<JetClass> {
public KotlinFindClassUsagesHandler(@NotNull JetClass jetClass, @NotNull KotlinFindUsagesHandlerFactory factory) {
super(jetClass, factory);
}
}
@@ -14,14 +14,14 @@
* limitations under the License.
*/
package org.jetbrains.jet.plugin.findUsages;
package org.jetbrains.jet.plugin.findUsages.handlers;
import com.intellij.find.findUsages.FindUsagesHandler;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.plugin.findUsages.KotlinFindUsagesHandlerFactory;
public class KotlinFindFunctionUsagesHandler extends FindUsagesHandler {
protected KotlinFindFunctionUsagesHandler(@NotNull PsiElement psiElement) {
super(psiElement);
public class KotlinFindFunctionUsagesHandler extends KotlinFindUsagesHandler<JetNamedFunction> {
public KotlinFindFunctionUsagesHandler(@NotNull JetNamedFunction function, @NotNull KotlinFindUsagesHandlerFactory factory) {
super(function, factory);
}
}
@@ -0,0 +1,64 @@
/*
* 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.findUsages.handlers;
import com.intellij.find.findUsages.FindUsagesHandler;
import com.intellij.find.findUsages.JavaFindUsagesHandlerFactory;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.plugin.findUsages.KotlinFindUsagesHandlerFactory;
import java.util.Collection;
import java.util.Collections;
public abstract class KotlinFindUsagesHandler<T extends PsiElement> extends FindUsagesHandler {
private final KotlinFindUsagesHandlerFactory factory;
private final Collection<? extends PsiElement> elementsToSearch;
public KotlinFindUsagesHandler(
@NotNull T psiElement,
@NotNull Collection<? extends PsiElement> elementsToSearch,
@NotNull KotlinFindUsagesHandlerFactory factory
) {
super(psiElement);
this.factory = factory;
this.elementsToSearch = elementsToSearch;
}
@SuppressWarnings("unchecked")
@NotNull
public T getElement() {
return (T)getPsiElement();
}
public KotlinFindUsagesHandler(@NotNull T psiElement, @NotNull KotlinFindUsagesHandlerFactory factory) {
this(psiElement, Collections.<PsiElement>emptyList(), factory);
}
public final KotlinFindUsagesHandlerFactory getFactory() {
return factory;
}
@NotNull
@Override
public PsiElement[] getPrimaryElements() {
return elementsToSearch.isEmpty()
? new PsiElement[] {getPsiElement()}
: elementsToSearch.toArray(new PsiElement[elementsToSearch.size()]);
}
}