From 76c7e32f74b8982303a86f81b2ffed2b2a7e7c70 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Wed, 16 Nov 2016 16:51:08 +0300 Subject: Type alias support --- core/testdata/typealias/chain.kt | 8 ++++++++ core/testdata/typealias/deprecated.kt | 7 +++++++ core/testdata/typealias/documented.kt | 9 +++++++++ core/testdata/typealias/functional.kt | 10 ++++++++++ core/testdata/typealias/generic.kt | 7 +++++++ core/testdata/typealias/inheritanceFromTypeAlias.kt | 7 +++++++ core/testdata/typealias/simple.kt | 5 +++++ 7 files changed, 53 insertions(+) create mode 100644 core/testdata/typealias/chain.kt create mode 100644 core/testdata/typealias/deprecated.kt create mode 100644 core/testdata/typealias/documented.kt create mode 100644 core/testdata/typealias/functional.kt create mode 100644 core/testdata/typealias/generic.kt create mode 100644 core/testdata/typealias/inheritanceFromTypeAlias.kt create mode 100644 core/testdata/typealias/simple.kt (limited to 'core/testdata') diff --git a/core/testdata/typealias/chain.kt b/core/testdata/typealias/chain.kt new file mode 100644 index 00000000..520be553 --- /dev/null +++ b/core/testdata/typealias/chain.kt @@ -0,0 +1,8 @@ +package _typealias.chain + +class A + +typealias B = A + +typealias C = B + diff --git a/core/testdata/typealias/deprecated.kt b/core/testdata/typealias/deprecated.kt new file mode 100644 index 00000000..b53d3a20 --- /dev/null +++ b/core/testdata/typealias/deprecated.kt @@ -0,0 +1,7 @@ +package _typealias.deprecated + +class Lol + +@Deprecated("Not mainstream now") +typealias Kek = Lol + diff --git a/core/testdata/typealias/documented.kt b/core/testdata/typealias/documented.kt new file mode 100644 index 00000000..3ca110e5 --- /dev/null +++ b/core/testdata/typealias/documented.kt @@ -0,0 +1,9 @@ +package _typealias.documented + +class A + +/** + * Just typealias + */ +typealias B = A + diff --git a/core/testdata/typealias/functional.kt b/core/testdata/typealias/functional.kt new file mode 100644 index 00000000..dadafa5e --- /dev/null +++ b/core/testdata/typealias/functional.kt @@ -0,0 +1,10 @@ +package _typealias.functional + +class A +class B + +typealias Spell = (A) -> B + +fun magic(spell: Spell) { + +} \ No newline at end of file diff --git a/core/testdata/typealias/generic.kt b/core/testdata/typealias/generic.kt new file mode 100644 index 00000000..43bc0e23 --- /dev/null +++ b/core/testdata/typealias/generic.kt @@ -0,0 +1,7 @@ +package _typealias.generic + +interface A + +typealias B = A + +typealias C = A \ No newline at end of file diff --git a/core/testdata/typealias/inheritanceFromTypeAlias.kt b/core/testdata/typealias/inheritanceFromTypeAlias.kt new file mode 100644 index 00000000..f929ecd0 --- /dev/null +++ b/core/testdata/typealias/inheritanceFromTypeAlias.kt @@ -0,0 +1,7 @@ +package _typealias.inheritance + +open class Some + +typealias Same = Some + +class My : Same \ No newline at end of file diff --git a/core/testdata/typealias/simple.kt b/core/testdata/typealias/simple.kt new file mode 100644 index 00000000..d688a84d --- /dev/null +++ b/core/testdata/typealias/simple.kt @@ -0,0 +1,5 @@ +package _typealias.simple + +class A + +typealias B = A \ No newline at end of file -- cgit From bac315d15c7de87cacf152d31f08c4f703abee34 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Thu, 17 Nov 2016 12:44:08 +0300 Subject: Test for previous fix, in/out modifiers not lost --- core/src/test/kotlin/model/TypeAliasTest.kt | 18 ++++++++++++++++++ core/testdata/typealias/asTypeBoundWithVariance.kt | 7 +++++++ 2 files changed, 25 insertions(+) create mode 100644 core/testdata/typealias/asTypeBoundWithVariance.kt (limited to 'core/testdata') diff --git a/core/src/test/kotlin/model/TypeAliasTest.kt b/core/src/test/kotlin/model/TypeAliasTest.kt index e67572f1..812fd9dc 100644 --- a/core/src/test/kotlin/model/TypeAliasTest.kt +++ b/core/src/test/kotlin/model/TypeAliasTest.kt @@ -102,4 +102,22 @@ class TypeAliasTest { } } } + + @Test + fun testAsTypeBoundWithVariance() { + verifyModel("testdata/typealias/asTypeBoundWithVariance.kt") { + val pkg = it.members.single() + with(pkg.members(NodeKind.Class).find { it.name == "C" }!!) { + val tParam = detail(NodeKind.TypeParameter) + assertEquals("out", tParam.detail(NodeKind.Modifier).name) + assertEquals("B", tParam.detail(NodeKind.Type).link(NodeKind.TypeAlias).name) + } + + with(pkg.members(NodeKind.Class).find { it.name == "D" }!!) { + val tParam = detail(NodeKind.TypeParameter) + assertEquals("in", tParam.detail(NodeKind.Modifier).name) + assertEquals("B", tParam.detail(NodeKind.Type).link(NodeKind.TypeAlias).name) + } + } + } } \ No newline at end of file diff --git a/core/testdata/typealias/asTypeBoundWithVariance.kt b/core/testdata/typealias/asTypeBoundWithVariance.kt new file mode 100644 index 00000000..1aef84d6 --- /dev/null +++ b/core/testdata/typealias/asTypeBoundWithVariance.kt @@ -0,0 +1,7 @@ +package _typealias.astypebound +class A + +typealias B = A + +class C +class D \ No newline at end of file -- cgit From d769c4db3fdd0f2075fdfb2f9109327f3b1ef386 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Thu, 17 Nov 2016 13:07:29 +0300 Subject: Added javadoc format test for simple typealiases --- core/src/test/kotlin/javadoc/JavadocTest.kt | 17 +++++++++++++++++ core/testdata/javadoc/typealiases.kt | 11 +++++++++++ 2 files changed, 28 insertions(+) create mode 100644 core/testdata/javadoc/typealiases.kt (limited to 'core/testdata') diff --git a/core/src/test/kotlin/javadoc/JavadocTest.kt b/core/src/test/kotlin/javadoc/JavadocTest.kt index 41d22b47..4c0f7f0f 100644 --- a/core/src/test/kotlin/javadoc/JavadocTest.kt +++ b/core/src/test/kotlin/javadoc/JavadocTest.kt @@ -1,5 +1,6 @@ package org.jetbrains.dokka.javadoc +import com.sun.javadoc.Type import org.jetbrains.dokka.DokkaConsoleLogger import org.jetbrains.dokka.tests.verifyModel import org.junit.Assert.* @@ -107,6 +108,22 @@ class JavadocTest { } } + @Test fun testTypeAliases() { + verifyJavadoc("testdata/javadoc/typealiases.kt", withKotlinRuntime = true) { doc -> + assertNull(doc.classNamed("B")) + assertNull(doc.classNamed("D")) + + assertEquals("A", doc.classNamed("C")!!.superclass().name()) + val methodParamType = doc.classNamed("TypealiasesKt")!!.methods() + .find { it.name() == "some" }!!.parameters().first() + .type() + assertEquals("kotlin.jvm.functions.Function1", methodParamType.qualifiedTypeName()) + assertEquals("? super A, C", methodParamType.asParameterizedType().typeArguments() + .map(Type::qualifiedTypeName).joinToString()) + } + } + + private fun verifyJavadoc(name: String, withJdk: Boolean = false, withKotlinRuntime: Boolean = false, diff --git a/core/testdata/javadoc/typealiases.kt b/core/testdata/javadoc/typealiases.kt new file mode 100644 index 00000000..bb09bfad --- /dev/null +++ b/core/testdata/javadoc/typealiases.kt @@ -0,0 +1,11 @@ +class A + +typealias B = A + +class C : B + +typealias D = (A) -> C + +fun some(d: D) { + +} \ No newline at end of file -- cgit From 4b86a1410d0427178132c07f6f04033645663bb0 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Thu, 17 Nov 2016 22:06:53 +0300 Subject: Fixup, added some type alias rendering tests --- .../main/kotlin/Formats/StructuredFormatService.kt | 2 +- core/src/test/kotlin/format/MarkdownFormatTest.kt | 5 ++ core/testdata/format/typeAliases.kt | 27 ++++++++++ core/testdata/format/typeAliases.md | 63 ++++++++++++++++++++++ core/testdata/format/typeAliases.package.md | 24 +++++++++ 5 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 core/testdata/format/typeAliases.kt create mode 100644 core/testdata/format/typeAliases.md create mode 100644 core/testdata/format/typeAliases.package.md (limited to 'core/testdata') diff --git a/core/src/main/kotlin/Formats/StructuredFormatService.kt b/core/src/main/kotlin/Formats/StructuredFormatService.kt index b7306486..a7a50f91 100644 --- a/core/src/main/kotlin/Formats/StructuredFormatService.kt +++ b/core/src/main/kotlin/Formats/StructuredFormatService.kt @@ -372,7 +372,7 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, appendSection("Types", node.members.filter { it.kind in NodeKind.classLike && it.kind != NodeKind.TypeAlias && it.kind != NodeKind.AnnotationClass && it.kind != NodeKind.Exception }) appendSection("Annotations", node.members(NodeKind.AnnotationClass)) appendSection("Exceptions", node.members(NodeKind.Exception)) - appendSection("Type aliases", node.members(NodeKind.TypeAlias)) + appendSection("Type Aliases", node.members(NodeKind.TypeAlias)) appendSection("Extensions for External Classes", node.members(NodeKind.ExternalClass)) appendSection("Enum Values", node.members(NodeKind.EnumItem), sortMembers = false) appendSection("Constructors", node.members(NodeKind.Constructor)) diff --git a/core/src/test/kotlin/format/MarkdownFormatTest.kt b/core/src/test/kotlin/format/MarkdownFormatTest.kt index c9f86503..6e4c44c8 100644 --- a/core/src/test/kotlin/format/MarkdownFormatTest.kt +++ b/core/src/test/kotlin/format/MarkdownFormatTest.kt @@ -227,6 +227,11 @@ class MarkdownFormatTest { verifyMarkdownNodeByName("qualifiedNameLink", "foo", withKotlinRuntime = true) } + @Test fun typeAliases() { + verifyMarkdownNode("typeAliases") + verifyMarkdownPackage("typeAliases") + } + private fun verifyMarkdownPackage(fileName: String, withKotlinRuntime: Boolean = false) { verifyOutput("testdata/format/$fileName.kt", ".package.md", withKotlinRuntime = withKotlinRuntime) { model, output -> markdownService.createOutputBuilder(output, tempLocation).appendNodes(model.members) diff --git a/core/testdata/format/typeAliases.kt b/core/testdata/format/typeAliases.kt new file mode 100644 index 00000000..9657963e --- /dev/null +++ b/core/testdata/format/typeAliases.kt @@ -0,0 +1,27 @@ + +class A +class B +class C + +typealias D = A +typealias E = D + +typealias F = (A) -> B + +typealias G = C +typealias H = C + +typealias I = H +typealias J = H + +typealias K = H + +typealias L = (K, B) -> J + +/** + * Documented + */ +typealias M = A + +@Deprecated("!!!") +typealias N = A \ No newline at end of file diff --git a/core/testdata/format/typeAliases.md b/core/testdata/format/typeAliases.md new file mode 100644 index 00000000..55e9317e --- /dev/null +++ b/core/testdata/format/typeAliases.md @@ -0,0 +1,63 @@ +[test](test/index) / [A](test/-a/index) + +# A + +`class A`[test](test/index) / [B](test/-b/index) + +# B + +`class B`[test](test/index) / [C](test/-c/index) + +# C + +`class C`[test](test/index) / [D](test/-d) + +# D + +`typealias D = `[`A`](test/-a/index)[test](test/index) / [E](test/-e) + +# E + +`typealias E = `[`D`](test/-d)[test](test/index) / [F](test/-f) + +# F + +`typealias F = (`[`A`](test/-a/index)`) -> `[`B`](test/-b/index)[test](test/index) / [G](test/-g) + +# G + +`typealias G = `[`C`](test/-c/index)`<`[`A`](test/-a/index)`>`[test](test/index) / [H](test/-h) + +# H + +`typealias H = `[`C`](test/-c/index)``[test](test/index) / [I](test/-i) + +# I + +`typealias I = `[`H`](test/-h)``[test](test/index) / [J](test/-j) + +# J + +`typealias J = `[`H`](test/-h)`<`[`A`](test/-a/index)`>`[test](test/index) / [K](test/-k) + +# K + +`typealias K = `[`H`](test/-h)`<`[`J`](test/-j)`>`[test](test/index) / [L](test/-l) + +# L + +`typealias L = (`[`K`](test/-k)`, `[`B`](test/-b/index)`) -> `[`J`](test/-j)[test](test/index) / [M](test/-m) + +# M + +`typealias M = `[`A`](test/-a/index) + +Documented + +[test](test/index) / [N](test/-n) + +# N + +`typealias ~~N~~ = `[`A`](test/-a/index) +**Deprecated:** !!! + diff --git a/core/testdata/format/typeAliases.package.md b/core/testdata/format/typeAliases.package.md new file mode 100644 index 00000000..0eff1ed5 --- /dev/null +++ b/core/testdata/format/typeAliases.package.md @@ -0,0 +1,24 @@ +[test](test/index) + +## Package <root> + +### Types + +| [A](test/-a/index) | `class A` | +| [B](test/-b/index) | `class B` | +| [C](test/-c/index) | `class C` | + +### Type Aliases + +| [D](test/-d) | `typealias D = `[`A`](test/-a/index) | +| [E](test/-e) | `typealias E = `[`D`](test/-d) | +| [F](test/-f) | `typealias F = (`[`A`](test/-a/index)`) -> `[`B`](test/-b/index) | +| [G](test/-g) | `typealias G = `[`C`](test/-c/index)`<`[`A`](test/-a/index)`>` | +| [H](test/-h) | `typealias H = `[`C`](test/-c/index)`` | +| [I](test/-i) | `typealias I = `[`H`](test/-h)`` | +| [J](test/-j) | `typealias J = `[`H`](test/-h)`<`[`A`](test/-a/index)`>` | +| [K](test/-k) | `typealias K = `[`H`](test/-h)`<`[`J`](test/-j)`>` | +| [L](test/-l) | `typealias L = (`[`K`](test/-k)`, `[`B`](test/-b/index)`) -> `[`J`](test/-j) | +| [M](test/-m) | `typealias M = `[`A`](test/-a/index)
Documented | +| [N](test/-n) | `typealias ~~N~~ = `[`A`](test/-a/index) | + -- cgit From 3704bf4c0f5b8ab3fdaa04c9a542e04f023b0e56 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Fri, 25 Nov 2016 19:32:55 +0300 Subject: Fix AnalysisEnvironment to configure resolver properly according to latest changes, which is required to proper platform type aliases resolving --- .../main/kotlin/Analysis/AnalysisEnvironment.kt | 47 +++++++++++++++++++--- core/testdata/format/exceptionClass.md | 2 +- core/testdata/format/exceptionClass.package.md | 2 +- 3 files changed, 43 insertions(+), 8 deletions(-) (limited to 'core/testdata') diff --git a/core/src/main/kotlin/Analysis/AnalysisEnvironment.kt b/core/src/main/kotlin/Analysis/AnalysisEnvironment.kt index e365207c..40951dbc 100644 --- a/core/src/main/kotlin/Analysis/AnalysisEnvironment.kt +++ b/core/src/main/kotlin/Analysis/AnalysisEnvironment.kt @@ -12,6 +12,7 @@ import com.intellij.openapi.roots.OrderEnumerationHandler import com.intellij.openapi.roots.ProjectFileIndex import com.intellij.openapi.roots.ProjectRootManager import com.intellij.openapi.util.Disposer +import com.intellij.psi.JavaPsiFacade import com.intellij.psi.PsiElement import com.intellij.psi.search.GlobalSearchScope import org.jetbrains.kotlin.analyzer.AnalysisResult @@ -30,14 +31,19 @@ import org.jetbrains.kotlin.context.ProjectContext import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.idea.resolve.ResolutionFacade +import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl +import org.jetbrains.kotlin.load.kotlin.JvmVirtualFileFinder +import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.platform.JvmBuiltIns import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.psi.KtElement +import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.CompilerEnvironment import org.jetbrains.kotlin.resolve.jvm.JvmAnalyzerFacade import org.jetbrains.kotlin.resolve.jvm.JvmPlatformParameters +import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.resolve.lazy.ResolveSession import java.io.File @@ -82,28 +88,57 @@ class AnalysisEnvironment(val messageCollector: MessageCollector) : Disposable { return environment } + fun createSourceModuleSearchScope(project: Project, sourceFiles: List): GlobalSearchScope { + // TODO: Fix when going to implement dokka for JS + return TopDownAnalyzerFacadeForJVM.newModuleSearchScope(project, sourceFiles) + } + + fun createResolutionFacade(environment: KotlinCoreEnvironment): DokkaResolutionFacade { + val projectContext = ProjectContext(environment.project) val sourceFiles = environment.getSourceFiles() + + val library = object : ModuleInfo { + override val name: Name = Name.special("") + override fun dependencies(): List = listOf(this) + } val module = object : ModuleInfo { override val name: Name = Name.special("") - override fun dependencies(): List = listOf(this) + override fun dependencies(): List = listOf(this, library) } + val sourcesScope = createSourceModuleSearchScope(environment.project, sourceFiles) + val builtIns = JvmBuiltIns(projectContext.storageManager) val resolverForProject = JvmAnalyzerFacade.setupResolverForProject( "Dokka", projectContext, - listOf(module), - { ModuleContent(sourceFiles, GlobalSearchScope.allScope(environment.project)) }, - JvmPlatformParameters { module }, + listOf(library, module), + { + when (it) { + library -> ModuleContent(emptyList(), GlobalSearchScope.notScope(sourcesScope)) + module -> ModuleContent(sourceFiles, sourcesScope) + else -> throw IllegalArgumentException("Unexpected module info") + } + }, + JvmPlatformParameters { + val file = (it as JavaClassImpl).psi.containingFile.virtualFile + if (file in sourcesScope) + module + else + library + }, CompilerEnvironment, - packagePartProviderFactory = { info, content -> JvmPackagePartProvider(environment, content.moduleContentScope) }, + packagePartProviderFactory = { + info, content -> + JvmPackagePartProvider(environment, content.moduleContentScope) + }, builtIns = builtIns ) - + resolverForProject.resolverForModule(library) // Required before module to initialize library properly val resolverForModule = resolverForProject.resolverForModule(module) val moduleDescriptor = resolverForProject.descriptorForModule(module) builtIns.initialize(moduleDescriptor, true) diff --git a/core/testdata/format/exceptionClass.md b/core/testdata/format/exceptionClass.md index df3457d7..e3714ecc 100644 --- a/core/testdata/format/exceptionClass.md +++ b/core/testdata/format/exceptionClass.md @@ -2,7 +2,7 @@ # MyException -`class MyException : `[`Exception`](http://docs.oracle.com/javase/6/docs/api/java/lang/Exception.html) +`class MyException : Exception` ### Constructors diff --git a/core/testdata/format/exceptionClass.package.md b/core/testdata/format/exceptionClass.package.md index e27dd0ab..e10478e4 100644 --- a/core/testdata/format/exceptionClass.package.md +++ b/core/testdata/format/exceptionClass.package.md @@ -4,5 +4,5 @@ ### Exceptions -| [MyException](test/-my-exception/index) | `class MyException : `[`Exception`](http://docs.oracle.com/javase/6/docs/api/java/lang/Exception.html) | +| [MyException](test/-my-exception/index) | `class MyException : Exception` | -- cgit