KotlinQuickDocumentationProvider: fix quick doc for elements from type hierarchy

Fix quick doc for light classes

 #KT-7964 Fixed
This commit is contained in:
Pavel V. Talanov
2015-06-08 18:25:31 +03:00
parent 220403b6f6
commit 54b1859aa7
4 changed files with 74 additions and 9 deletions
@@ -22,6 +22,7 @@ import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiManager
import org.jetbrains.kotlin.asJava.KotlinLightElement
import org.jetbrains.kotlin.asJava.KotlinLightMethod
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithSource
@@ -33,7 +34,6 @@ import org.jetbrains.kotlin.idea.kdoc.KDocRenderer
import org.jetbrains.kotlin.idea.kdoc.resolveKDocLink
import org.jetbrains.kotlin.psi.JetDeclaration
import org.jetbrains.kotlin.psi.JetElement
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.psi.JetReferenceExpression
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
@@ -50,7 +50,7 @@ public class KotlinQuickDocumentationProvider : AbstractDocumentationProvider()
return getText(element, originalElement, true)
}
override fun generateDoc(element: PsiElement, originalElement: PsiElement): String? {
override fun generateDoc(element: PsiElement, originalElement: PsiElement?): String? {
return getText(element, originalElement, false)
}
@@ -91,18 +91,17 @@ public class KotlinQuickDocumentationProvider : AbstractDocumentationProvider()
.build()
private fun getText(element: PsiElement, originalElement: PsiElement, quickNavigation: Boolean): String? {
private fun getText(element: PsiElement, originalElement: PsiElement?, quickNavigation: Boolean): String? {
if (element is JetDeclaration) {
return renderKotlinDeclaration(element, quickNavigation)
}
else if (element is KotlinLightMethod) {
val origin = element.getOrigin()
if (origin == null) return null
else if (element is KotlinLightElement<*, *>) {
val origin = element.getOrigin() ?: return null
return renderKotlinDeclaration(origin, quickNavigation)
}
if (quickNavigation) {
val referenceExpression = originalElement.getNonStrictParentOfType<JetReferenceExpression>()
val referenceExpression = originalElement?.getNonStrictParentOfType<JetReferenceExpression>()
if (referenceExpression != null) {
val context = referenceExpression.analyze(BodyResolveMode.PARTIAL)
val declarationDescriptor = context[BindingContext.REFERENCE_TARGET, referenceExpression]
@@ -139,7 +138,7 @@ public class KotlinQuickDocumentationProvider : AbstractDocumentationProvider()
return renderedDecl
}
private fun mixKotlinToJava(declarationDescriptor: DeclarationDescriptor, element: PsiElement, originalElement: PsiElement): String? {
private fun mixKotlinToJava(declarationDescriptor: DeclarationDescriptor, element: PsiElement, originalElement: PsiElement?): String? {
val originalInfo = JavaDocumentationProvider().getQuickNavigateInfo(element, originalElement)
if (originalInfo != null) {
val renderedDecl = DescriptorRenderer.HTML_NAMES_WITH_SHORT_TYPES.render(declarationDescriptor)
+2 -1
View File
@@ -6,4 +6,5 @@ class KotlinClassUsedFromJava {
}
}
//INFO: [light_idea_test_case] testing...
//INFO: <b>internal</b> <b>final</b> <b>class</b> Test <i>defined in</i> testing<br/><p>Some comment
//INFO: </p>
+8
View File
@@ -0,0 +1,8 @@
package a.b.c
/**
* Very special class
*/
class <caret>A {
}
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2015 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.kotlin.idea.editor.quickDoc
import com.intellij.codeInsight.CodeInsightTestCase
import com.intellij.ide.hierarchy.HierarchyBrowserBaseEx
import com.intellij.ide.hierarchy.LanguageTypeHierarchy
import com.intellij.ide.hierarchy.type.TypeHierarchyNodeDescriptor
import com.intellij.ide.hierarchy.type.TypeHierarchyTreeStructure
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.testFramework.MapDataContext
import junit.framework.TestCase
import org.jetbrains.kotlin.idea.JetLanguage
import org.jetbrains.kotlin.idea.KotlinQuickDocumentationProvider
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
public class QuickDocInHierarchyTest() : CodeInsightTestCase() {
override fun getTestDataPath(): String {
return PluginTestCaseBase.getTestDataPathBase() + "/kdoc/inTypeHierarchy/"
}
public fun testSimple() {
configureByFile(getTestName(true) + ".kt")
val context = MapDataContext()
context.put<Project>(CommonDataKeys.PROJECT, getProject())
context.put<Editor>(CommonDataKeys.EDITOR, getEditor())
val hierarchyTreeStructure = TypeHierarchyTreeStructure(
getProject(),
LanguageTypeHierarchy.INSTANCE.forLanguage(JetLanguage.INSTANCE).getTarget(context) as PsiClass?,
HierarchyBrowserBaseEx.SCOPE_PROJECT
)
val hierarchyNodeDescriptor = hierarchyTreeStructure.getBaseDescriptor() as TypeHierarchyNodeDescriptor
val doc = KotlinQuickDocumentationProvider().generateDoc(hierarchyNodeDescriptor.getPsiClass() as PsiElement, null)!!
TestCase.assertTrue("Invalid doc\n: $doc", doc.contains("Very special class"))
}
}