first spike attempting to implement KT-1453 though hit a compiler error
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
package org.jetbrains.kotlin.doc
|
||||
|
||||
import java.io.File
|
||||
import java.io.PrintStream
|
||||
import org.jetbrains.jet.cli.CompilerArguments
|
||||
import org.jetbrains.jet.cli.KotlinCompiler
|
||||
import org.jetbrains.jet.compiler.CompileEnvironment
|
||||
import org.jetbrains.jet.cli.CompilerArguments
|
||||
import java.io.PrintStream
|
||||
import java.io.File
|
||||
import org.jetbrains.kotlin.doc.highlighter.HtmlCompilerPlugin
|
||||
|
||||
/**
|
||||
* Main for running the KDocCompiler
|
||||
*/
|
||||
* Main for running the KDocCompiler
|
||||
*/
|
||||
fun main(args: Array<String?>): Unit {
|
||||
KotlinCompiler.doMain(KDocCompiler(), args);
|
||||
}
|
||||
@@ -39,7 +40,12 @@ class KDocCompiler() : KotlinCompiler() {
|
||||
if (arguments is KDocArguments) {
|
||||
kdoc.config = arguments.apply()
|
||||
}
|
||||
coreEnvironment.getCompilerPlugins().orEmpty().add(kdoc);
|
||||
val plugins = coreEnvironment.getCompilerPlugins().orEmpty()
|
||||
/*
|
||||
val sourcePlugin = HtmlCompilerPlugin()
|
||||
plugins.add(sourcePlugin)
|
||||
*/
|
||||
plugins.add(kdoc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package org.jetbrains.kotlin.doc.highlighter
|
||||
|
||||
import org.jetbrains.jet.compiler.CompilerPlugin
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext
|
||||
import java.util.List
|
||||
import org.jetbrains.jet.lang.psi.JetFile
|
||||
|
||||
/**
|
||||
*/
|
||||
class HtmlCompilerPlugin : CompilerPlugin {
|
||||
|
||||
override fun processFiles(bindingContext: BindingContext?, files: List<JetFile?>?) {
|
||||
if (files != null && bindingContext != null) {
|
||||
for (file in files) {
|
||||
if (file != null) {
|
||||
val visitor = HtmlKotlinVisitor()
|
||||
file.accept(visitor)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package org.jetbrains.kotlin.doc.highlighter
|
||||
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.jet.lang.psi.*
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiWhiteSpace
|
||||
import com.intellij.psi.PsiComment
|
||||
|
||||
class HtmlKotlinVisitor: JetTreeVisitor<StringBuilder>() {
|
||||
|
||||
override fun visitFile(file: PsiFile?) {
|
||||
if (file is JetFile) {
|
||||
val data = StringBuilder()
|
||||
visitJetFile(file, data)
|
||||
}
|
||||
}
|
||||
override fun visitJetFile(file: JetFile?, data: StringBuilder?): Void? {
|
||||
if (file != null) {
|
||||
println("============ Jet File ${file.getName()}")
|
||||
val data = StringBuilder()
|
||||
acceptChildren(file, data)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
override fun visitClassObject(classObject: JetClassObject?, data: StringBuilder?): Void? {
|
||||
println("============ class $classObject data $data")
|
||||
return super.visitClassObject(classObject, data)
|
||||
}
|
||||
|
||||
override fun visitClass(klass: JetClass?, data: StringBuilder?): Void? {
|
||||
println("============ class $klass")
|
||||
if (klass != null) {
|
||||
acceptChildren(klass, data)
|
||||
return null
|
||||
} else {
|
||||
return super.visitClass(klass, data)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override fun visitClassBody(classBody: JetClassBody?, data: StringBuilder?): Void? {
|
||||
println("============ class body $classBody data $data")
|
||||
return super.visitClassBody(classBody, data)
|
||||
}
|
||||
|
||||
|
||||
override fun visitFunctionType(fnType: JetFunctionType?, data: StringBuilder?): Void? {
|
||||
println("======================= function Type $fnType")
|
||||
return super.visitFunctionType(fnType, data)
|
||||
}
|
||||
|
||||
protected fun accept(child: PsiElement?, data: StringBuilder?): Unit {
|
||||
if (child is JetElement) {
|
||||
child.accept(this, data)
|
||||
} else {
|
||||
if (child is PsiComment || child is PsiWhiteSpace) {
|
||||
// ignore
|
||||
} else {
|
||||
println("------- Child $child of type ${child.javaClass}")
|
||||
}
|
||||
child?.accept(this)
|
||||
}
|
||||
}
|
||||
|
||||
protected fun acceptChildren(element: PsiElement, data: StringBuilder?): Unit {
|
||||
for (child in element.getChildren()) {
|
||||
accept(child, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package test.kotlin.kdoc
|
||||
|
||||
import java.io.File
|
||||
import kotlin.test.assertTrue
|
||||
import org.jetbrains.jet.cli.CompilerArguments
|
||||
import org.jetbrains.jet.cli.KotlinCompiler
|
||||
import org.jetbrains.kotlin.doc.highlighter.HtmlCompilerPlugin
|
||||
import org.junit.Test
|
||||
|
||||
class HtmlVisitorTest {
|
||||
|
||||
Test fun generateHtmlFromSource() {
|
||||
val src = "src/test/sample"
|
||||
var dir = File(".")
|
||||
if (!File(dir, src).exists()) {
|
||||
dir = File("kdoc")
|
||||
assertTrue(File(dir, src).exists(), "Cannot find file $src")
|
||||
}
|
||||
val srcDir = File(dir, src)
|
||||
val outDir = File(dir, "target/htmldocs")
|
||||
println("Generating source HTML to $outDir")
|
||||
|
||||
val args = CompilerArguments()
|
||||
args.setSrc(srcDir.toString())
|
||||
args.setOutputDir(File(dir, "target/classes-htmldocs").toString())
|
||||
args.getCompilerPlugins()?.add(HtmlCompilerPlugin())
|
||||
|
||||
val compiler = KotlinCompiler()
|
||||
compiler.exec(System.out, args)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package sample
|
||||
|
||||
/** A Comment */
|
||||
class Person(val name: String, val city: String) {
|
||||
fun toString(): String = "Person($name, $city)"
|
||||
|
||||
fun hello(): String = "Hello $name"
|
||||
}
|
||||
Reference in New Issue
Block a user