kdoc: cleanup

This commit is contained in:
Stepan Koltsov
2012-06-25 23:55:28 +04:00
parent 72ef07acef
commit 8372ad7ec2
4 changed files with 52 additions and 54 deletions
@@ -32,13 +32,13 @@ import org.jetbrains.jet.util.slicedmap.WritableSlice
import org.jetbrains.jet.lang.resolve.BindingContextUtils import org.jetbrains.jet.lang.resolve.BindingContextUtils
/** Generates the Kotlin Documentation for the model */ /** Generates the Kotlin Documentation for the model */
class KDoc() : KModelCompilerPlugin() { class KDoc(protected arguments: KDocArguments) : KModelCompilerPlugin(arguments) {
protected override fun processModel(model: KModel) { protected override fun processModel(model: KModel) {
// TODO allow this to be configured; maybe we use configuration on the KotlinModule // TODO allow this to be configured; maybe we use configuration on the KotlinModule
// to define what doclets to use? // to define what doclets to use?
val generator = JavadocStyleHtmlDoclet() val generator = JavadocStyleHtmlDoclet()
val outputDir = File(config.docOutputDir) val outputDir = File(arguments.apply().docOutputDir)
generator.generate(model, outputDir) generator.generate(model, outputDir)
} }
} }
@@ -27,12 +27,8 @@ class KDocCompiler() : K2JVMCompiler() {
super.configureEnvironment(configuration, arguments) super.configureEnvironment(configuration, arguments)
val coreEnvironment = configuration.getEnvironment() val coreEnvironment = configuration.getEnvironment()
if (coreEnvironment != null) { if (coreEnvironment != null) {
val kdoc = KDoc() val kdoc = KDoc(arguments as KDocArguments)
val plugins = configuration.getCompilerPlugins()
if (arguments is KDocArguments) {
kdoc.config = arguments.apply()
}
val plugins = configuration.getCompilerPlugins().orEmpty()
val sourcePlugin = Html2CompilerPlugin(arguments as KDocArguments) val sourcePlugin = Html2CompilerPlugin(arguments as KDocArguments)
plugins.add(sourcePlugin) plugins.add(sourcePlugin)
plugins.add(kdoc); plugins.add(kdoc);
@@ -3,23 +3,23 @@ package org.jetbrains.kotlin.doc.model
import org.jetbrains.jet.cli.common.CompilerPlugin import org.jetbrains.jet.cli.common.CompilerPlugin
import org.jetbrains.jet.cli.common.CompilerPluginContext import org.jetbrains.jet.cli.common.CompilerPluginContext
import org.jetbrains.kotlin.doc.KDocConfig import org.jetbrains.kotlin.doc.KDocConfig
import org.jetbrains.kotlin.doc.KDocArguments
/** Base class for any compiler plugin which needs to process a KModel */ /** Base class for any compiler plugin which needs to process a KModel */
abstract class KModelCompilerPlugin: CompilerPlugin { abstract class KModelCompilerPlugin(
// TODO: fix compiler and make protected
public open var config: KDocConfig = KDocConfig() val arguments: KDocArguments)
: CompilerPlugin
{
public override fun processFiles(context: CompilerPluginContext) { public override fun processFiles(context: CompilerPluginContext) {
val bindingContext = context.getContext() val bindingContext = context.getContext()
val sources = context.getFiles() val sources = context.getFiles()
if (bindingContext != null && sources != null) { val model = KModel(bindingContext, arguments.apply(), sources.requireNoNulls())
val model = KModel(bindingContext, config)
model.load(sources)
processModel(model) processModel(model)
}
} }
protected abstract fun processModel(model: KModel): Unit protected abstract fun processModel(model: KModel): Unit
} }
@@ -175,7 +175,7 @@ abstract class KClassOrPackage(model: KModel, declarationDescriptor: Declaration
} }
} }
class KModel(var context: BindingContext, val config: KDocConfig) { class KModel(val context: BindingContext, val config: KDocConfig, private val sources: List<JetFile>) {
// TODO generates java.lang.NoSuchMethodError: kotlin.util.namespace.hashMap(Ljet/TypeInfo;Ljet/TypeInfo;)Ljava/util/HashMap; // TODO generates java.lang.NoSuchMethodError: kotlin.util.namespace.hashMap(Ljet/TypeInfo;Ljet/TypeInfo;)Ljava/util/HashMap;
//val packages = sortedMap<String,KPackage>() //val packages = sortedMap<String,KPackage>()
public val packageMap: SortedMap<String, KPackage> = TreeMap<String, KPackage>() public val packageMap: SortedMap<String, KPackage> = TreeMap<String, KPackage>()
@@ -208,35 +208,16 @@ class KModel(var context: BindingContext, val config: KDocConfig) {
private val readMeDirsScanned = HashSet<String>() private val readMeDirsScanned = HashSet<String>()
/** ;{
* Returns the root project directory for calculating relative source links /** Loads the model from the given set of source files */
*/
fun projectRootDir(): String {
if (_projectRootDir == null) {
val rootDir = config.projectRootDir
_projectRootDir = if (rootDir == null) {
warning("KDocConfig does not have a projectRootDir defined so we cannot generate relative source Hrefs")
""
} else {
File(rootDir).getCanonicalPath() ?: ""
}
}
return _projectRootDir ?: ""
}
/** Loads the model from the given set of source files */
fun load(sources: List<JetFile?>): Unit {
val allNamespaces = HashSet<NamespaceDescriptor>() val allNamespaces = HashSet<NamespaceDescriptor>()
for (source in sources) { for (source in sources) {
if (source != null) { // We retrieve a descriptor by a PSI element from the context
// We retrieve a descriptor by a PSI element from the context val namespaceDescriptor = BindingContextUtils.namespaceDescriptor(context, source)
val namespaceDescriptor = BindingContextUtils.namespaceDescriptor(context, source) if (namespaceDescriptor != null) {
if (namespaceDescriptor != null) { allNamespaces.add(namespaceDescriptor);
allNamespaces.add(namespaceDescriptor); } else {
} else { warning("No NamespaceDescriptor for source $source")
warning("No NamespaceDescriptor for source $source")
}
} }
} }
val allClasses = HashSet<KClass>() val allClasses = HashSet<KClass>()
@@ -255,6 +236,23 @@ class KModel(var context: BindingContext, val config: KDocConfig) {
} }
} }
/**
* Returns the root project directory for calculating relative source links
*/
fun projectRootDir(): String {
if (_projectRootDir == null) {
val rootDir = config.projectRootDir
_projectRootDir = if (rootDir == null) {
warning("KDocConfig does not have a projectRootDir defined so we cannot generate relative source Hrefs")
""
} else {
File(rootDir).getCanonicalPath() ?: ""
}
}
return _projectRootDir ?: ""
}
/* Returns the package for the given name or null if it does not exist */ /* Returns the package for the given name or null if it does not exist */
fun getPackage(name: String): KPackage? = packageMap.get(name) fun getPackage(name: String): KPackage? = packageMap.get(name)
@@ -1037,15 +1035,19 @@ class KType(val jetType: JetType, model: KModel, val klass: KClass?, val argumen
get() = jetType.isNullable() get() = jetType.isNullable()
} }
class KClass(val pkg: KPackage, val descriptor: ClassDescriptor, class KClass(
val simpleName: String, val pkg: KPackage,
var group: String = "Other", val descriptor: ClassDescriptor,
var annotations: List<KAnnotation> = arrayList<KAnnotation>(), val simpleName: String)
var typeParameters: List<KTypeParameter> = arrayList<KTypeParameter>(), : KClassOrPackage(pkg.model, descriptor), Comparable<KClass>
var since: String = "", {
var authors: List<String> = arrayList<String>(), var group: String = "Other"
var baseClasses: List<KType> = arrayList<KType>(), var annotations: List<KAnnotation> = arrayList<KAnnotation>()
var nestedClasses: List<KClass> = arrayList<KClass>()): KClassOrPackage(pkg.model, descriptor), Comparable<KClass> { var typeParameters: List<KTypeParameter> = arrayList<KTypeParameter>()
var since: String = ""
var authors: List<String> = arrayList<String>()
var baseClasses: List<KType> = arrayList<KType>()
var nestedClasses: List<KClass> = arrayList<KClass>()
public override fun compareTo(other: KClass): Int = name.compareTo(other.name) public override fun compareTo(other: KClass): Int = name.compareTo(other.name)