From be1b9a011b240b4022aa4f88662ac2d5149dafab Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Mon, 25 Jun 2012 23:55:30 +0400 Subject: [PATCH] kdoc: link to generated html source from kdoc --- .../org/jetbrains/kotlin/doc/collections.kt | 18 +++++ .../doc/highlighter2/Html2CompilerPlugin.kt | 2 +- .../jetbrains/kotlin/doc/model/KotlinModel.kt | 65 +++++++++++-------- .../kotlin/doc/templates/KDocTemplate.kt | 12 ++-- .../doc/templates/PackageTemplateSupport.kt | 2 +- 5 files changed, 65 insertions(+), 34 deletions(-) create mode 100644 libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/collections.kt diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/collections.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/collections.kt new file mode 100644 index 00000000000..72f629888ab --- /dev/null +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/collections.kt @@ -0,0 +1,18 @@ +package org.jetbrains.kotlin.doc + +import java.util.List +import java.util.Map +import java.util.HashMap + + +fun List.toHashMap(f: (A) -> #(K, V)): Map { + val r = HashMap() + for (item in this) { + val entry = f(item) + r.put(entry._1, entry._2) + } + return r +} + + +fun List.toHashMapMappingToKey(f: (V) -> K): Map = toHashMap { v -> #(f(v), v) } diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter2/Html2CompilerPlugin.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter2/Html2CompilerPlugin.kt index 6288548d811..eef1d32d07d 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter2/Html2CompilerPlugin.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter2/Html2CompilerPlugin.kt @@ -32,7 +32,7 @@ class Html2CompilerPlugin(private val compilerArguments: KDocArguments) : Doclet docOutputRoot = File(docOutputDir) } - private val srcOutputRoot = File(docOutputRoot, "src") + private val srcOutputRoot = File(docOutputRoot, "src-html") private val sourceDirs: List = compilerArguments diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt index 9234c9fb5e4..abfafbcd6d9 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KotlinModel.kt @@ -4,6 +4,7 @@ import kotlin.* import kotlin.util.* import org.jetbrains.kotlin.doc.KDocConfig +import org.jetbrains.kotlin.doc.* import org.jetbrains.kotlin.doc.highlighter.SyntaxHighligher import java.util.* @@ -212,6 +213,37 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs private val readMeDirsScanned = HashSet() + + + val sourcesInfo: List + ;{ + + val normalizedSourceDirs: List = + sourceDirs.map { file -> file.getCanonicalPath()!! } + + private fun relativePath(psiFile: PsiFile): String { + val file = File((psiFile.getVirtualFile() as CoreLocalVirtualFile).getPath()).getCanonicalFile()!! + val filePath = file.getPath()!! + for (sourceDirPath in normalizedSourceDirs) { + if (filePath.startsWith(sourceDirPath) && filePath.length() > sourceDirPath.length()) { + return filePath.substring(sourceDirPath.length + 1) + } + } + throw Exception("$file is not a child of any source roots $normalizedSourceDirs") + } + + sourcesInfo = sources.map { source -> + val relativePath = relativePath(source) + val htmlPath = relativePath.replaceFirst("\\.kt$", "") + ".html" + SourceInfo(source, relativePath, htmlPath) + } + } + + private val sourceInfoByFile = sourcesInfo.toHashMapMappingToKey { sourceInfo -> sourceInfo.psi } + + fun sourceInfoByFile(file: JetFile) = sourceInfoByFile.get(file)!! + + ;{ /** Loads the model from the given set of source files */ val allNamespaces = HashSet() @@ -241,29 +273,6 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs } - val sourcesInfo: List - ;{ - - val normalizedSourceDirs: List = - sourceDirs.map { file -> file.getCanonicalPath()!! } - - private fun relativePath(psiFile: PsiFile): String { - val file = File((psiFile.getVirtualFile() as CoreLocalVirtualFile).getPath()).getCanonicalFile()!! - val filePath = file.getPath()!! - for (sourceDirPath in normalizedSourceDirs) { - if (filePath.startsWith(sourceDirPath) && filePath.length() > sourceDirPath.length()) { - return filePath.substring(sourceDirPath.length + 1) - } - } - throw Exception("$file is not a child of any source roots $normalizedSourceDirs") - } - - sourcesInfo = sources.map { source -> - val relativePath = relativePath(source) - val htmlPath = relativePath.replaceFirst("\\.kt$", "") + ".html" - SourceInfo(source, relativePath, htmlPath) - } - } /** @@ -492,7 +501,7 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs } - protected fun getPsiElement(descriptor: DeclarationDescriptor): PsiElement? { + fun getPsiElement(descriptor: DeclarationDescriptor): PsiElement? { return try { BindingContextUtils.descriptorToDeclaration(context, descriptor) } catch (e: Throwable) { @@ -963,7 +972,10 @@ class KPackage(model: KModel, val descriptor: NamespaceDescriptor, var created = false val klass = classMap.getOrPut(name) { created = true - KClass(this, descriptor) + val psiFile = model.getPsiElement(descriptor)?.getContainingFile() + val jetFile = psiFile as? JetFile + val sourceInfo = if (jetFile != null) model.sourceInfoByFile(jetFile) else null + KClass(this, descriptor, sourceInfo) } if (created) { // sometimes we may have source files for a package in different source directories @@ -1068,7 +1080,8 @@ class KType(val jetType: JetType, model: KModel, val klass: KClass?, val argumen class KClass( val pkg: KPackage, - val descriptor: ClassDescriptor) + val descriptor: ClassDescriptor, + val sourceInfo: SourceInfo?) : KClassOrPackage(pkg.model, descriptor), Comparable { val simpleName = descriptor.getName().getName() diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/KDocTemplate.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/KDocTemplate.kt index c73185b7591..91a8488fcf7 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/KDocTemplate.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/KDocTemplate.kt @@ -72,8 +72,8 @@ abstract class KDocTemplate() : TextTemplate() { return klass.sourceLink() } else { val pkg = klass.pkg - return if (!pkg.useExternalLink) { - "${pkg.nameAsRelativePath}src-html/${klass.nameAsPath}.html#line.${klass.sourceLine}" + return if (klass.sourceInfo != null) { + "${pkg.nameAsRelativePath}src-html/${klass.sourceInfo.htmlPath}.html#line.${klass.sourceLine}" } else { href(klass) } @@ -86,8 +86,8 @@ abstract class KDocTemplate() : TextTemplate() { val owner = f.owner return if (owner is KClass) { val pkg = owner.pkg - if (!pkg.useExternalLink) { - "${rootHref(pkg)}src-html/${owner.simpleName}.html#line.${f.sourceLine}" + if (owner.sourceInfo != null) { + "${rootHref(pkg)}src-html/${owner.sourceInfo.htmlPath}.html#line.${f.sourceLine}" } else { href(f) } @@ -109,8 +109,8 @@ abstract class KDocTemplate() : TextTemplate() { val owner = f.owner return if (owner is KClass) { val pkg = owner.pkg - if (!pkg.useExternalLink) { - "${rootHref(pkg)}src-html/${owner.simpleName}.html#line.${f.sourceLine}" + if (owner.sourceInfo != null) { + "${rootHref(pkg)}src-html/${owner.sourceInfo.htmlPath}#line.${f.sourceLine}" } else { href(f) } diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageTemplateSupport.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageTemplateSupport.kt index 32beb8d1189..161b927ea89 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageTemplateSupport.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageTemplateSupport.kt @@ -284,4 +284,4 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() { println(link(a)) } } -} \ No newline at end of file +}