Remastering of documentation provider
- Remove whole file resolve - Take already resolved element - Get kotlin element behind the wrapper directly - Remove outdated isKotlinDeclaration method - More tests
This commit is contained in:
@@ -444,7 +444,7 @@ public class GenerateTests {
|
||||
"idea/tests/",
|
||||
"JetQuickDocProviderTestGenerated",
|
||||
AbstractJetQuickDocProviderTest.class,
|
||||
testModel("idea/testData/editor/quickDoc", "doTest")
|
||||
testModelWithPattern("idea/testData/editor/quickDoc", "^([^_]+)\\.[^\\.]*$", "doTest")
|
||||
);
|
||||
|
||||
generateTest(
|
||||
|
||||
@@ -20,27 +20,25 @@ import com.google.common.base.Predicate;
|
||||
import com.intellij.lang.documentation.AbstractDocumentationProvider;
|
||||
import com.intellij.lang.java.JavaDocumentationProvider;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiWhiteSpace;
|
||||
import com.intellij.psi.impl.compiled.ClsClassImpl;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.asJava.KotlinLightClass;
|
||||
import org.jetbrains.jet.asJava.KotlinLightClassForExplicitDeclaration;
|
||||
import org.jetbrains.jet.asJava.KotlinLightClassForPackage;
|
||||
import org.jetbrains.jet.kdoc.lexer.KDocTokens;
|
||||
import org.jetbrains.jet.kdoc.psi.api.KDoc;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
|
||||
import org.jetbrains.jet.plugin.libraries.DecompiledUtils;
|
||||
import org.jetbrains.jet.lang.resolve.java.jetAsJava.KotlinLightMethod;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
|
||||
import org.jetbrains.jet.plugin.references.BuiltInsReferenceResolver;
|
||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class JetQuickDocumentationProvider extends AbstractDocumentationProvider {
|
||||
private static final Predicate<PsiElement> SKIP_WHITESPACE_AND_EMPTY_NAMESPACE = new Predicate<PsiElement>() {
|
||||
@Override
|
||||
@@ -51,78 +49,6 @@ public class JetQuickDocumentationProvider extends AbstractDocumentationProvider
|
||||
}
|
||||
};
|
||||
|
||||
private static String getText(PsiElement element, PsiElement originalElement, boolean mergeKotlinAndJava) {
|
||||
JetReferenceExpression ref = PsiTreeUtil.getParentOfType(originalElement, JetReferenceExpression.class, false);
|
||||
|
||||
PsiElement declarationPsiElement = PsiTreeUtil.getParentOfType(originalElement, JetDeclaration.class);
|
||||
if (ref != null || declarationPsiElement != null) {
|
||||
BindingContext bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) originalElement.getContainingFile())
|
||||
.getBindingContext();
|
||||
|
||||
if (ref != null) {
|
||||
DeclarationDescriptor declarationDescriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, ref);
|
||||
if (declarationDescriptor != null) {
|
||||
return render(declarationDescriptor, bindingContext, element, originalElement, mergeKotlinAndJava);
|
||||
}
|
||||
if (declarationPsiElement != null) {
|
||||
declarationPsiElement = BindingContextUtils.resolveToDeclarationPsiElement(bindingContext, ref);
|
||||
}
|
||||
}
|
||||
|
||||
if (declarationPsiElement != null) {
|
||||
DeclarationDescriptor declarationDescriptor = bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR,
|
||||
declarationPsiElement);
|
||||
|
||||
if (declarationDescriptor != null) {
|
||||
return render(declarationDescriptor, bindingContext, element, originalElement, mergeKotlinAndJava);
|
||||
}
|
||||
}
|
||||
return "Unresolved";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String render(
|
||||
@NotNull DeclarationDescriptor declarationDescriptor, @NotNull BindingContext bindingContext,
|
||||
PsiElement element, PsiElement originalElement, boolean mergeKotlinAndJava) {
|
||||
String renderedDecl = DescriptorRenderer.HTML.render(declarationDescriptor);
|
||||
if (isKotlinDeclaration(declarationDescriptor, bindingContext, element)) {
|
||||
KDoc comment = findElementKDoc(element);
|
||||
if (comment != null) {
|
||||
renderedDecl = renderedDecl + "<br/>" + kDocToHtml(comment);
|
||||
}
|
||||
|
||||
return renderedDecl;
|
||||
}
|
||||
else if (mergeKotlinAndJava) {
|
||||
String originalInfo = new JavaDocumentationProvider().getQuickNavigateInfo(element, originalElement);
|
||||
if (originalInfo != null) {
|
||||
return renderedDecl + "<br/>Java declaration:<br/>" + originalInfo;
|
||||
}
|
||||
return renderedDecl;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean isKotlinDeclaration(
|
||||
DeclarationDescriptor descriptor,
|
||||
BindingContext bindingContext,
|
||||
PsiElement element
|
||||
) {
|
||||
if (JetLanguage.INSTANCE == element.getLanguage()) return true;
|
||||
PsiElement declaration = BindingContextUtils.descriptorToDeclaration(bindingContext, descriptor);
|
||||
if (declaration == null) {
|
||||
BuiltInsReferenceResolver libraryReferenceResolver = element.getProject().getComponent(BuiltInsReferenceResolver.class);
|
||||
Collection<PsiElement> elements = libraryReferenceResolver.resolveBuiltInSymbol(descriptor);
|
||||
return !elements.isEmpty();
|
||||
}
|
||||
|
||||
ClsClassImpl clsClass = PsiTreeUtil.getParentOfType(declaration, ClsClassImpl.class);
|
||||
if (clsClass == null) return false;
|
||||
VirtualFile file = clsClass.getContainingFile().getVirtualFile();
|
||||
return file != null && DecompiledUtils.isKotlinCompiledFile(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) {
|
||||
return getText(element, originalElement, true);
|
||||
@@ -133,6 +59,58 @@ public class JetQuickDocumentationProvider extends AbstractDocumentationProvider
|
||||
return getText(element, originalElement, false);
|
||||
}
|
||||
|
||||
private static String getText(PsiElement element, PsiElement originalElement, boolean mergeKotlinAndJava) {
|
||||
if (element instanceof JetDeclaration) {
|
||||
return renderKotlinDeclaration((JetDeclaration) element);
|
||||
}
|
||||
else if (element instanceof KotlinLightMethod) {
|
||||
return renderKotlinDeclaration(((KotlinLightMethod) element).getOrigin());
|
||||
}
|
||||
|
||||
if (mergeKotlinAndJava) {
|
||||
JetReferenceExpression referenceExpression = PsiTreeUtil.getParentOfType(originalElement, JetReferenceExpression.class, false);
|
||||
if (referenceExpression != null) {
|
||||
BindingContext context = AnalyzerFacadeWithCache.getContextForElement(referenceExpression);
|
||||
DeclarationDescriptor declarationDescriptor = context.get(BindingContext.REFERENCE_TARGET, referenceExpression);
|
||||
if (declarationDescriptor != null) {
|
||||
return mixKotlinToJava(declarationDescriptor, element, originalElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// This element was resolved to non-kotlin element, it will be rendered with own provider
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String renderKotlinDeclaration(JetDeclaration declaration) {
|
||||
BindingContext context = AnalyzerFacadeWithCache.getContextForElement(declaration);
|
||||
DeclarationDescriptor declarationDescriptor = context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, declaration);
|
||||
|
||||
assert declarationDescriptor != null;
|
||||
|
||||
String renderedDecl = DescriptorRenderer.HTML.render(declarationDescriptor);
|
||||
KDoc comment = findElementKDoc(declaration);
|
||||
if (comment != null) {
|
||||
renderedDecl = renderedDecl + "<br/>" + kDocToHtml(comment);
|
||||
}
|
||||
|
||||
return renderedDecl;
|
||||
}
|
||||
|
||||
private static String mixKotlinToJava(@NotNull DeclarationDescriptor declarationDescriptor,
|
||||
PsiElement element, PsiElement originalElement
|
||||
) {
|
||||
String originalInfo = new JavaDocumentationProvider().getQuickNavigateInfo(element, originalElement);
|
||||
if (originalInfo != null) {
|
||||
String renderedDecl = DescriptorRenderer.HTML.render(declarationDescriptor);
|
||||
return renderedDecl + "<br/>Java declaration:<br/>" + originalInfo;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String getKDocContent(@NotNull KDoc kDoc) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
@@ -161,10 +139,8 @@ public class JetQuickDocumentationProvider extends AbstractDocumentationProvider
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static KDoc findElementKDoc(@NotNull PsiElement element) {
|
||||
PsiElement navigateElement = (element instanceof JetElement) ? element : element.getNavigationElement();
|
||||
PsiElement comment = JetPsiUtil.skipSiblingsBackwardByPredicate(navigateElement, SKIP_WHITESPACE_AND_EMPTY_NAMESPACE);
|
||||
|
||||
private static KDoc findElementKDoc(@NotNull JetElement element) {
|
||||
PsiElement comment = JetPsiUtil.skipSiblingsBackwardByPredicate(element, SKIP_WHITESPACE_AND_EMPTY_NAMESPACE);
|
||||
return comment instanceof KDoc ? (KDoc) comment : null;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fun some(<caret>f: (Int) -> String) : String? = null
|
||||
|
||||
//INFO: <b>value-parameter</b> <b>val</b> f: (jet.Int) → jet.String <i>defined in</i> some
|
||||
@@ -0,0 +1,5 @@
|
||||
trait Base
|
||||
|
||||
class Some<<caret>T: Base>
|
||||
|
||||
//INFO: <T : Base> <i>defined in</i> Some
|
||||
@@ -0,0 +1,8 @@
|
||||
fun some() : String? = null
|
||||
|
||||
fun test() {
|
||||
val <caret>test = some()
|
||||
}
|
||||
|
||||
|
||||
//INFO: <b>val</b> test: jet.String? <i>defined in</i> test
|
||||
@@ -0,0 +1,5 @@
|
||||
fun testing() {
|
||||
<caret>SomeClass<List<String>>()
|
||||
}
|
||||
|
||||
// INFO: <b>public</b> <b>constructor</b> SomeClass<T : jet.List<jet.Any?>?>() <i>defined in</i> SomeClass<br/>Java declaration:<br/>[light_idea_test_case] public class SomeClass<T extends java.util.List> extends Object
|
||||
@@ -0,0 +1,9 @@
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Some Java Class
|
||||
* @param <T>
|
||||
*/
|
||||
public class SomeClass<T extends List> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun ktTest() {
|
||||
Test.<caret>foo("SomeTest")
|
||||
}
|
||||
|
||||
//INFO: <b>public</b> <b>open</b> <b>fun</b> foo(param: jet.String?): jet.Array<jet.Any>? <i>defined in</i> Test<br/>Java declaration:<br/>Test...
|
||||
@@ -0,0 +1,9 @@
|
||||
class Test {
|
||||
/**
|
||||
* Java Method
|
||||
*/
|
||||
@KotlinSignature("fun foo(param: String): Array<out Any>")
|
||||
public static Object[] foo(String param) {
|
||||
return new Object[0];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import testing.Test
|
||||
|
||||
class KotlinClassUsedFromJava {
|
||||
void test() {
|
||||
<caret>Test()
|
||||
}
|
||||
}
|
||||
|
||||
//INFO: [light_idea_test_case] testing...
|
||||
@@ -0,0 +1,6 @@
|
||||
package testing
|
||||
|
||||
/**
|
||||
* Some comment
|
||||
*/
|
||||
class Test
|
||||
@@ -0,0 +1,9 @@
|
||||
import testing.TestingPackage
|
||||
|
||||
class KotlinClassUsedFromJava {
|
||||
void test() {
|
||||
<caret>TestingPackage.foo()
|
||||
}
|
||||
}
|
||||
|
||||
// INFO: [light_idea_test_case] testing...
|
||||
@@ -0,0 +1,4 @@
|
||||
package testing
|
||||
|
||||
fun foo() {
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package server
|
||||
|
||||
import some.SomePackage
|
||||
|
||||
class Testing {
|
||||
void test() {
|
||||
SomePackage.<caret>foo(12);
|
||||
}
|
||||
}
|
||||
|
||||
// INFO: <b>internal</b> <b>fun</b> foo(bar: jet.Int): jet.Unit <i>defined in</i> some<br/><p>KDoc foo<br/></p>
|
||||
@@ -0,0 +1,7 @@
|
||||
package some
|
||||
|
||||
/**
|
||||
* KDoc foo
|
||||
*/
|
||||
fun foo(bar: Int) {
|
||||
}
|
||||
@@ -16,20 +16,32 @@
|
||||
|
||||
package org.jetbrains.jet.editor.quickDoc;
|
||||
|
||||
import com.beust.jcommander.internal.Lists;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.Collections2;
|
||||
import com.intellij.codeInsight.documentation.DocumentationManager;
|
||||
import com.intellij.codeInsight.navigation.CtrlMouseHandler;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.rt.execution.junit.FileComparisonFailure;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.ComparisonFailure;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.InTextDirectivesUtils;
|
||||
import org.jetbrains.jet.plugin.ProjectDescriptorWithStdlibSources;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class AbstractJetQuickDocProviderTest extends LightCodeInsightFixtureTestCase {
|
||||
public void doTest(@NotNull String path) throws Exception {
|
||||
myFixture.configureByFile(path);
|
||||
myFixture.configureByFiles(ArrayUtil.toStringArray(getTestFiles(path)));
|
||||
|
||||
PsiElement element = myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset());
|
||||
assertNotNull("Can't find element at caret in file: " + path, element);
|
||||
@@ -37,10 +49,35 @@ public abstract class AbstractJetQuickDocProviderTest extends LightCodeInsightFi
|
||||
DocumentationManager documentationManager = DocumentationManager.getInstance(myFixture.getProject());
|
||||
PsiElement targetElement = documentationManager.findTargetElement(myFixture.getEditor(), myFixture.getFile());
|
||||
|
||||
List<String> directives = InTextDirectivesUtils.findLinesWithPrefixesRemoved(myFixture.getFile().getText(), "INFO:");
|
||||
assertTrue("Documentation to check should be added to test file with // INFO: directive " + path, 1 == directives.size());
|
||||
String info = CtrlMouseHandler.getInfo(targetElement, element);
|
||||
|
||||
assertEquals(directives.get(0), CtrlMouseHandler.getInfo(targetElement, element));
|
||||
File testDataFile = new File(path);
|
||||
String textData = FileUtil.loadFile(testDataFile);
|
||||
List<String> directives = InTextDirectivesUtils.findLinesWithPrefixesRemoved(textData, "INFO:");
|
||||
|
||||
if (directives.isEmpty()) {
|
||||
throw new FileComparisonFailure(
|
||||
"'// INFO:' directive was expected",
|
||||
textData,
|
||||
textData + "\n\n//INFO: " + info,
|
||||
testDataFile.getAbsolutePath());
|
||||
}
|
||||
else if (directives.size() == 1) {
|
||||
String expectedInfo = directives.get(0);
|
||||
|
||||
// We can avoid testing for too long comments with \n character by placing '...' in test data
|
||||
if (info != null && expectedInfo.endsWith("...")) {
|
||||
if (!info.startsWith(StringUtil.trimEnd(expectedInfo, "..."))) {
|
||||
throw new ComparisonFailure(null, expectedInfo, info);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Assert.assertEquals(expectedInfo, info);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Assert.fail("Too many '// INFO:' directives in file " + path);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -48,4 +85,29 @@ public abstract class AbstractJetQuickDocProviderTest extends LightCodeInsightFi
|
||||
protected LightProjectDescriptor getProjectDescriptor() {
|
||||
return ProjectDescriptorWithStdlibSources.INSTANCE;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Collection<String> getTestFiles(@NotNull String path) {
|
||||
File testFile = new File(path);
|
||||
String testFileName = FileUtil.getNameWithoutExtension(testFile);
|
||||
|
||||
List<String> filePaths = Lists.newArrayList();
|
||||
|
||||
filePaths.add(path);
|
||||
|
||||
filePaths.add(checkDataFileWithSuffix(testFile, testFileName, "_Data.kt"));
|
||||
filePaths.add(checkDataFileWithSuffix(testFile, testFileName, "_Data.java"));
|
||||
|
||||
return Collections2.filter(filePaths, Predicates.notNull());
|
||||
}
|
||||
|
||||
private static String checkDataFileWithSuffix(File testFile, String testFileName, String dataFileSuffix) {
|
||||
String ktDataFileName = testFileName + dataFileSuffix;
|
||||
File ktDataFile = new File(testFile.getParent(), ktDataFileName);
|
||||
if (ktDataFile.exists()) {
|
||||
return FileUtil.normalize(ktDataFile.getPath());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,42 @@ import org.jetbrains.jet.editor.quickDoc.AbstractJetQuickDocProviderTest;
|
||||
@TestMetadata("idea/testData/editor/quickDoc")
|
||||
public class JetQuickDocProviderTestGenerated extends AbstractJetQuickDocProviderTest {
|
||||
public void testAllFilesPresentInQuickDoc() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/editor/quickDoc"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/editor/quickDoc"), Pattern.compile("^([^_]+)\\.[^\\.]*$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("AtFunctionParameter.kt")
|
||||
public void testAtFunctionParameter() throws Exception {
|
||||
doTest("idea/testData/editor/quickDoc/AtFunctionParameter.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("AtTypeParameter.kt")
|
||||
public void testAtTypeParameter() throws Exception {
|
||||
doTest("idea/testData/editor/quickDoc/AtTypeParameter.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("AtVariableDeclaration.kt")
|
||||
public void testAtVariableDeclaration() throws Exception {
|
||||
doTest("idea/testData/editor/quickDoc/AtVariableDeclaration.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("JavaClassUsedInKotlin.kt")
|
||||
public void testJavaClassUsedInKotlin() throws Exception {
|
||||
doTest("idea/testData/editor/quickDoc/JavaClassUsedInKotlin.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("JavaMethodUsedInKotlin.kt")
|
||||
public void testJavaMethodUsedInKotlin() throws Exception {
|
||||
doTest("idea/testData/editor/quickDoc/JavaMethodUsedInKotlin.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("KotlinClassUsedFromJava.java")
|
||||
public void testKotlinClassUsedFromJava() throws Exception {
|
||||
doTest("idea/testData/editor/quickDoc/KotlinClassUsedFromJava.java");
|
||||
}
|
||||
|
||||
@TestMetadata("KotlinPackageClassUsedFromJava.java")
|
||||
public void testKotlinPackageClassUsedFromJava() throws Exception {
|
||||
doTest("idea/testData/editor/quickDoc/KotlinPackageClassUsedFromJava.java");
|
||||
}
|
||||
|
||||
@TestMetadata("MethodFromStdLib.kt")
|
||||
@@ -56,4 +91,9 @@ public class JetQuickDocProviderTestGenerated extends AbstractJetQuickDocProvide
|
||||
doTest("idea/testData/editor/quickDoc/OnMethodUsage.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("TopLevelMethodFromJava.java")
|
||||
public void testTopLevelMethodFromJava() throws Exception {
|
||||
doTest("idea/testData/editor/quickDoc/TopLevelMethodFromJava.java");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user