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 6a90073e8f4..6288548d811 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 @@ -18,6 +18,7 @@ import org.jetbrains.jet.lexer.JetTokens import org.jetbrains.kotlin.doc.Doclet import org.jetbrains.kotlin.doc.model.KModel import org.jetbrains.jet.lang.psi.JetFile +import org.jetbrains.kotlin.doc.model.SourceInfo class Html2CompilerPlugin(private val compilerArguments: KDocArguments) : Doclet { @@ -65,14 +66,15 @@ class Html2CompilerPlugin(private val compilerArguments: KDocArguments) : Doclet #() } - for (file in model.sources) { - processFile(file) + for (sourceInfo in model.sourcesInfo) { + processFile(sourceInfo) } } - private fun processFile(psiFile: JetFile) { - val relativePath = fileToWrite(psiFile) - val htmlFile = File(srcOutputRoot, relativePath.replaceFirst("\\.kt$", "") + ".html") + private fun processFile(sourceInfo: SourceInfo) { + val psiFile = sourceInfo.psi + val htmlPath = sourceInfo.htmlPath + val htmlFile = File(srcOutputRoot, htmlPath) println("Generating $htmlFile") htmlFile.getParentFile()!!.mkdirs() diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KModelCompilerPlugin.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KModelCompilerPlugin.kt index dd2fbfee013..d7e113f3cf2 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KModelCompilerPlugin.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/model/KModelCompilerPlugin.kt @@ -4,6 +4,8 @@ import org.jetbrains.jet.cli.common.CompilerPlugin import org.jetbrains.jet.cli.common.CompilerPluginContext import org.jetbrains.kotlin.doc.KDocConfig import org.jetbrains.kotlin.doc.KDocArguments +import java.io.File +import java.util.List /** Base class for any compiler plugin which needs to process a KModel */ abstract class KModelCompilerPlugin( @@ -16,7 +18,8 @@ abstract class KModelCompilerPlugin( public override fun processFiles(context: CompilerPluginContext) { val bindingContext = context.getContext() val sources = context.getFiles() - val model = KModel(bindingContext, arguments.apply(), sources.requireNoNulls()) + val sourceDirs: List = arguments.getSourceDirs().orEmpty().requireNoNulls().map { path -> File(path) } + val model = KModel(bindingContext, arguments.apply(), sourceDirs, sources.requireNoNulls()) processModel(model) } 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 136f3a91616..9e68d1bdfd2 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 @@ -44,6 +44,7 @@ import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils.LineAndColumn import org.jetbrains.jet.internal.com.intellij.psi.PsiFileSystemItem import java.io.File import org.jetbrains.jet.internal.com.intellij.psi.PsiElement +import org.jetbrains.jet.internal.com.intellij.openapi.vfs.local.CoreLocalVirtualFile /** @@ -175,7 +176,10 @@ abstract class KClassOrPackage(model: KModel, declarationDescriptor: Declaration } } -class KModel(val context: BindingContext, val config: KDocConfig, val sources: List) { +// htmlPath does not include "html-src" prefix +class SourceInfo(val psi: JetFile, val relativePath: String, val htmlPath: String) + +class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs: List, val sources: List) { // TODO generates java.lang.NoSuchMethodError: kotlin.util.namespace.hashMap(Ljet/TypeInfo;Ljet/TypeInfo;)Ljava/util/HashMap; //val packages = sortedMap() public val packageMap: SortedMap = TreeMap() @@ -236,9 +240,35 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sources: L } } + + 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) + } + } + + /** - * Returns the root project directory for calculating relative source links - */ + * Returns the root project directory for calculating relative source links + */ fun projectRootDir(): String { if (_projectRootDir == null) { val rootDir = config.projectRootDir