kdoc: compute source file relative paths in model
This commit is contained in:
+7
-5
@@ -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()
|
||||
|
||||
+4
-1
@@ -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<File> = arguments.getSourceDirs().orEmpty().requireNoNulls().map { path -> File(path) }
|
||||
val model = KModel(bindingContext, arguments.apply(), sourceDirs, sources.requireNoNulls())
|
||||
|
||||
processModel(model)
|
||||
}
|
||||
|
||||
@@ -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<JetFile>) {
|
||||
// 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<File>, val sources: List<JetFile>) {
|
||||
// TODO generates java.lang.NoSuchMethodError: kotlin.util.namespace.hashMap(Ljet/TypeInfo;Ljet/TypeInfo;)Ljava/util/HashMap;
|
||||
//val packages = sortedMap<String,KPackage>()
|
||||
public val packageMap: SortedMap<String, KPackage> = TreeMap<String, KPackage>()
|
||||
@@ -236,9 +240,35 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sources: L
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
val sourcesInfo: List<SourceInfo>
|
||||
;{
|
||||
|
||||
val normalizedSourceDirs: List<String> =
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user