Move createSessionForLookupElement() to ImplementationViewSessionFactory; clarify code (IDEA-CR-41751)

This commit is contained in:
Dmitry Jemerov
2019-01-07 16:24:50 +01:00
parent 834f8a5c6a
commit c196e66f24
3 changed files with 36 additions and 24 deletions
@@ -1,6 +1,8 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.hint
import com.intellij.codeInsight.documentation.DocumentationManager
import com.intellij.codeInsight.hint.PsiImplementationViewSession.getSelfAndImplementations
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.extensions.ExtensionPointName
@@ -8,10 +10,17 @@ import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.presentation.java.SymbolPresentationUtil
import com.intellij.util.Processor
interface ImplementationViewSession {
val factory: ImplementationViewSessionFactory
val project: Project
/**
* The list of implementations which could be found synchronously. Additional implementations can be obtained by calling
* [searchImplementationsInBackground].
*/
val implementationElements: List<ImplementationViewElement>
val file: PsiFile?
@@ -19,8 +28,6 @@ interface ImplementationViewSession {
val text: String?
val editor: Editor?
fun createSessionForLookupElement(lookupItemObject: Any?, isSearchDeep: Boolean): ImplementationViewSession?
fun searchImplementationsInBackground(indicator: ProgressIndicator,
isSearchDeep: Boolean,
includeSelf: Boolean,
@@ -31,6 +38,7 @@ interface ImplementationViewSession {
interface ImplementationViewSessionFactory {
fun createSession(dataContext: DataContext, project: Project, invokedByShortcut: Boolean): ImplementationViewSession?
fun createSessionForLookupElement(project: Project, editor: Editor?, file: PsiFile?, lookupItemObject: Any?, isSearchDeep: Boolean): ImplementationViewSession?
companion object {
@JvmField val EP_NAME = ExtensionPointName.create<ImplementationViewSessionFactory>("com.intellij.implementationViewSessionFactory")
@@ -41,4 +49,20 @@ class PsiImplementationSessionViewFactory : ImplementationViewSessionFactory {
override fun createSession(dataContext: DataContext, project: Project, invokedByShortcut: Boolean): ImplementationViewSession? {
return PsiImplementationViewSession.create(dataContext, project, invokedByShortcut)
}
override fun createSessionForLookupElement(project: Project, editor: Editor?, file: PsiFile?, lookupItemObject: Any?, isSearchDeep: Boolean): ImplementationViewSession? {
val element = lookupItemObject as? PsiElement ?: DocumentationManager.getInstance(project).getElementFromLookup(editor, file)
var impls = arrayOf<PsiElement>()
var text = ""
if (element != null) {
// if (element instanceof PsiPackage) return;
val containingFile = element.containingFile
if (containingFile == null || !containingFile.viewProvider.isPhysical) return null
impls = getSelfAndImplementations(editor, element, PsiImplementationViewSession.createImplementationsSearcher(isSearchDeep))
text = SymbolPresentationUtil.getSymbolPresentableText(element)
}
return PsiImplementationViewSession(project, element, impls, text, editor, file)
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.hint;
import com.intellij.codeInsight.TargetElementUtil;
@@ -52,6 +52,12 @@ public class PsiImplementationViewSession implements ImplementationViewSession {
myFile = file;
}
@NotNull
@Override
public ImplementationViewSessionFactory getFactory() {
return ImplementationViewSessionFactory.EP_NAME.findExtensionOrFail(PsiImplementationSessionViewFactory.class);
}
@NotNull
public Project getProject() {
return myProject;
@@ -83,25 +89,6 @@ public class PsiImplementationViewSession implements ImplementationViewSession {
return myFile;
}
@Override
public PsiImplementationViewSession createSessionForLookupElement(Object lookupItemObject, boolean isSearchDeep) {
final PsiElement element = lookupItemObject instanceof PsiElement
? (PsiElement)lookupItemObject
: DocumentationManager.getInstance(myProject).getElementFromLookup(myEditor, myFile);
PsiElement[] impls = {};
String text = "";
if (element != null) {
// if (element instanceof PsiPackage) return;
PsiFile containingFile = element.getContainingFile();
if (containingFile == null || !containingFile.getViewProvider().isPhysical()) return null;
impls = getSelfAndImplementations(myEditor, element, PsiImplementationViewSession.createImplementationsSearcher(isSearchDeep));
text = SymbolPresentationUtil.getSymbolPresentableText(element);
}
return new PsiImplementationViewSession(myProject, element, impls, text, myEditor, myFile);
}
@Override
public boolean elementRequiresIncludeSelf() {
return !(myElement instanceof PomTargetPsiElement);
@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.hint.actions;
import com.intellij.codeInsight.CodeInsightBundle;
@@ -87,7 +87,8 @@ public class ShowImplementationsAction extends AnAction implements PopupAction {
}
private void updateElementImplementations(final Object lookupItemObject, ImplementationViewSession session) {
ImplementationViewSession newSession = session.createSessionForLookupElement(lookupItemObject, isSearchDeep());
ImplementationViewSession newSession = session.getFactory().createSessionForLookupElement(session.getProject(),
session.getEditor(), session.getFile(), lookupItemObject, isSearchDeep());
if (newSession != null) {
showImplementations(newSession, false, false);
}