KotlinFindUsagesHandler: J2K

This commit is contained in:
Dmitry Jemerov
2015-07-13 20:35:14 +02:00
parent 3966db3430
commit 6b2ea56b76
4 changed files with 68 additions and 104 deletions
@@ -57,7 +57,7 @@ public class KotlinFindClassUsagesHandler(
): AbstractFindUsagesDialog { ): AbstractFindUsagesDialog {
return KotlinFindClassUsagesDialog(getElement(), return KotlinFindClassUsagesDialog(getElement(),
getProject(), getProject(),
getFactory().findClassOptions, factory.findClassOptions,
toShowInNewTab, toShowInNewTab,
mustOpenInNewTab, mustOpenInNewTab,
isSingleFile, isSingleFile,
@@ -131,6 +131,6 @@ public class KotlinFindClassUsagesHandler(
} }
public override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions { public override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions {
return getFactory().findClassOptions return factory.findClassOptions
} }
} }
@@ -157,7 +157,7 @@ public abstract class KotlinFindMemberUsagesHandler<T extends JetNamedDeclaratio
new CommonProcessors.UniqueProcessor<UsageInfo>(processor); new CommonProcessors.UniqueProcessor<UsageInfo>(processor);
for (PsiReference ref : UsagesSearch.INSTANCE$.search(request)) { for (PsiReference ref : UsagesSearch.INSTANCE$.search(request)) {
processUsage(uniqueProcessor, ref); Companion.processUsage(uniqueProcessor, ref);
} }
PsiMethod psiMethod = PsiMethod psiMethod =
@@ -168,7 +168,7 @@ public abstract class KotlinFindMemberUsagesHandler<T extends JetNamedDeclaratio
: null; : null;
if (psiMethod != null) { if (psiMethod != null) {
for (PsiReference ref : MethodReferencesSearch.search(psiMethod, options.searchScope, true)) { for (PsiReference ref : MethodReferencesSearch.search(psiMethod, options.searchScope, true)) {
processUsage(uniqueProcessor, ref); Companion.processUsage(uniqueProcessor, ref);
} }
} }
@@ -180,7 +180,7 @@ public abstract class KotlinFindMemberUsagesHandler<T extends JetNamedDeclaratio
new PsiElementProcessor<PsiMethod>() { new PsiElementProcessor<PsiMethod>() {
@Override @Override
public boolean execute(@NotNull PsiMethod method) { public boolean execute(@NotNull PsiMethod method) {
return processUsage(uniqueProcessor, method.getNavigationElement()); return Companion.processUsage(uniqueProcessor, method.getNavigationElement());
} }
} }
) )
@@ -14,127 +14,91 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.idea.findUsages.handlers; package org.jetbrains.kotlin.idea.findUsages.handlers
import com.intellij.find.findUsages.FindUsagesHandler; import com.intellij.find.findUsages.FindUsagesHandler
import com.intellij.find.findUsages.FindUsagesOptions; import com.intellij.find.findUsages.FindUsagesOptions
import com.intellij.psi.PsiElement; import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiReference; import com.intellij.psi.PsiElement
import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.PsiReference
import com.intellij.psi.search.SearchScope; import com.intellij.psi.search.GlobalSearchScope
import com.intellij.usageView.UsageInfo; import com.intellij.psi.search.SearchScope
import com.intellij.util.Processor; import com.intellij.usageView.UsageInfo
import org.jetbrains.annotations.NotNull; import com.intellij.util.Processor
import org.jetbrains.kotlin.idea.findUsages.KotlinFindUsagesHandlerFactory; import org.jetbrains.kotlin.idea.findUsages.KotlinFindUsagesHandlerFactory
import org.jetbrains.kotlin.idea.findUsages.KotlinReferenceUsageInfo;
import java.util.ArrayList; import java.util.ArrayList
import java.util.Collection; import java.util.Collections
import java.util.Collections;
import java.util.List;
public abstract class KotlinFindUsagesHandler<T extends PsiElement> extends FindUsagesHandler { public abstract class KotlinFindUsagesHandler<T : PsiElement>(psiElement: T, private val elementsToSearch: Collection<PsiElement>, public val factory: KotlinFindUsagesHandlerFactory) : FindUsagesHandler(psiElement) {
private final KotlinFindUsagesHandlerFactory factory;
private final Collection<? extends PsiElement> elementsToSearch;
public KotlinFindUsagesHandler( @suppress("UNCHECKED_CAST")
@NotNull T psiElement, public fun getElement(): T {
@NotNull Collection<? extends PsiElement> elementsToSearch, return getPsiElement() as T
@NotNull KotlinFindUsagesHandlerFactory factory
) {
super(psiElement);
this.factory = factory;
this.elementsToSearch = elementsToSearch;
} }
@SuppressWarnings("unchecked") public constructor(psiElement: T, factory: KotlinFindUsagesHandlerFactory) : this(psiElement, emptyList<PsiElement>(), factory) {
@NotNull
public T getElement() {
return (T)getPsiElement();
} }
public KotlinFindUsagesHandler(@NotNull T psiElement, @NotNull KotlinFindUsagesHandlerFactory factory) { override fun getPrimaryElements(): Array<PsiElement> {
this(psiElement, Collections.<PsiElement>emptyList(), factory); return if (elementsToSearch.isEmpty())
arrayOf(getPsiElement())
else
elementsToSearch.toTypedArray()
} }
@NotNull protected fun searchTextOccurrences(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
public final KotlinFindUsagesHandlerFactory getFactory() { val scope = options.searchScope
return factory;
}
protected static boolean processUsage(@NotNull Processor<UsageInfo> processor, @NotNull PsiReference ref) { val searchText = options.isSearchForTextOccurrences && scope is GlobalSearchScope
return processor.process(new KotlinReferenceUsageInfo(ref));
}
protected static boolean processUsage(@NotNull Processor<UsageInfo> processor, @NotNull PsiElement element) {
return processor.process(new UsageInfo(element));
}
@NotNull
@Override
public PsiElement[] getPrimaryElements() {
return elementsToSearch.isEmpty()
? new PsiElement[] {getPsiElement()}
: elementsToSearch.toArray(new PsiElement[elementsToSearch.size()]);
}
protected boolean searchTextOccurrences(
@NotNull final PsiElement element,
@NotNull final Processor<UsageInfo> processor,
@NotNull FindUsagesOptions options
) {
final SearchScope scope = options.searchScope;
boolean searchText = options.isSearchForTextOccurrences && scope instanceof GlobalSearchScope;
if (searchText) { if (searchText) {
if (options.fastTrack != null) { if (options.fastTrack != null) {
options.fastTrack.searchCustom(new Processor<Processor<PsiReference>>() { options.fastTrack.searchCustom(object : Processor<Processor<PsiReference>> {
@Override override fun process(consumer: Processor<PsiReference>): Boolean {
public boolean process(Processor<PsiReference> consumer) { return processUsagesInText(element, processor, scope as GlobalSearchScope)
return processUsagesInText(element, processor, (GlobalSearchScope)scope);
} }
}); })
} }
else { else {
return processUsagesInText(element, processor, (GlobalSearchScope)scope); return processUsagesInText(element, processor, scope as GlobalSearchScope)
} }
} }
return true; return true
} }
@Override override fun processElementUsages(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean {
public boolean processElementUsages( return searchReferences(element, processor, options) && searchTextOccurrences(element, processor, options)
@NotNull PsiElement element,
@NotNull Processor<UsageInfo> processor,
@NotNull FindUsagesOptions options
) {
return searchReferences(element, processor, options) && searchTextOccurrences(element, processor, options);
} }
protected abstract boolean searchReferences( protected abstract fun searchReferences(element: PsiElement, processor: Processor<UsageInfo>, options: FindUsagesOptions): Boolean
@NotNull PsiElement element, @NotNull Processor<UsageInfo> processor, @NotNull FindUsagesOptions options
);
@NotNull override fun findReferencesToHighlight(target: PsiElement, searchScope: SearchScope): Collection<PsiReference> {
@Override val results = ArrayList<PsiReference>()
public Collection<PsiReference> findReferencesToHighlight(@NotNull PsiElement target, @NotNull SearchScope searchScope val options = getFindUsagesOptions()
) { options.searchScope = searchScope
final List<PsiReference> results = new ArrayList<PsiReference>(); searchReferences(target, object : Processor<UsageInfo> {
FindUsagesOptions options = getFindUsagesOptions(); override fun process(info: UsageInfo): Boolean {
options.searchScope = searchScope; val reference = info.getReference()
searchReferences(target, if (reference != null) {
new Processor<UsageInfo>() { results.add(reference)
@Override }
public boolean process(UsageInfo info) { return true
PsiReference reference = info.getReference(); }
if (reference != null) { }, options)
results.add(reference); return results
} }
return true;
} companion object {
},
options); protected fun processUsage(processor: Processor<UsageInfo>, ref: PsiReference?): Boolean {
return results; if (ref == null) return true
val rangeInElement = ref.getRangeInElement()
return processor.process(UsageInfo(ref.getElement(), rangeInElement.getStartOffset(), rangeInElement.getEndOffset(), false))
}
protected fun processUsage(processor: Processor<UsageInfo>, element: PsiElement): Boolean {
return processor.process(UsageInfo(element))
}
} }
} }
@@ -51,6 +51,6 @@ public class KotlinTypeParameterFindUsagesHandler(
} }
public override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions { public override fun getFindUsagesOptions(dataContext: DataContext?): FindUsagesOptions {
return getFactory().defaultOptions return factory.defaultOptions
} }
} }