From 00170357fd0d49679930541624396d2f779964aa Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 14 Mar 2014 19:15:07 +0400 Subject: [PATCH] Clean code in KDoc Fix warnings, outdated TODOs, formatting, etc. --- .../org/jetbrains/kotlin/doc/KDocConfig.kt | 12 +- .../doc/highlighter/HtmlCompilerPlugin.kt | 8 +- .../doc/highlighter/HtmlKotlinVisitor.kt | 37 +-- .../doc/highlighter/SyntaxHighlighter.kt | 24 +- .../doc/highlighter2/Html2CompilerPlugin.kt | 10 +- .../jetbrains/kotlin/doc/model/KotlinModel.kt | 247 +++++++----------- .../doc/templates/PackageFrameTemplate.kt | 10 +- .../doc/templates/PackageSummaryTemplate.kt | 36 +-- .../kotlin/doc/templates/SearchXmlTemplate.kt | 22 +- .../jetbrains/kotlin/template/HtmlTemplate.kt | 4 +- .../jetbrains/kotlin/template/TemplateCore.kt | 2 +- .../kotlin/test/kotlin/kdoc/KDocSampleTest.kt | 6 +- .../test/kotlin/test/kotlin/psiUtilsTest.kt | 5 +- .../test/kotlin/template/PegdownTest.kt | 13 +- .../test/kotlin/template/TemplateCoreTest.kt | 4 +- 15 files changed, 175 insertions(+), 265 deletions(-) diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocConfig.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocConfig.kt index b9540bdb1c3..7383122fa87 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocConfig.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocConfig.kt @@ -27,7 +27,7 @@ class KDocConfig() { * Returns a map of the package prefix to the HTML URL for the root of the apidoc using javadoc/kdoc style * directory layouts so that this API doc report can link to external packages */ - public val packagePrefixToUrls: MutableMap = TreeMap(LongestFirstStringComparator()) + public val packagePrefixToUrls: MutableMap = TreeMap(LongestFirstStringComparator) /** * Returns a Set of the package name prefixes to ignore from the KDoc report @@ -112,12 +112,12 @@ class KDocConfig() { } } -private class LongestFirstStringComparator() : Comparator { - public override fun compare(s1: String, s2: String): Int { - return compareBy(s1, s2, { length() }, { this }) +private object LongestFirstStringComparator : Comparator { + override fun compare(o1: String, o2: String): Int { + return compareBy(o1, o2, { length() }, { this }) } - public override fun equals(obj : Any?) : Boolean { - return obj is LongestFirstStringComparator + override fun equals(other: Any?): Boolean { + return other is LongestFirstStringComparator } } diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/HtmlCompilerPlugin.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/HtmlCompilerPlugin.kt index 5d5c9cc5d52..18aa0d5a6db 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/HtmlCompilerPlugin.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/HtmlCompilerPlugin.kt @@ -5,15 +5,11 @@ import org.jetbrains.jet.cli.common.CompilerPluginContext /** */ -class HtmlCompilerPlugin: CompilerPlugin { - +class HtmlCompilerPlugin : CompilerPlugin { public override fun processFiles(context: CompilerPluginContext) { val files = context.getFiles() for (file in files) { - if (file != null) { - val visitor = HtmlKotlinVisitor() - file.accept(visitor) - } + file.accept(HtmlKotlinVisitor()) } } } diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/HtmlKotlinVisitor.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/HtmlKotlinVisitor.kt index 2f5fe8d53f2..1b5d82085bc 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/HtmlKotlinVisitor.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/HtmlKotlinVisitor.kt @@ -8,46 +8,37 @@ import org.jetbrains.jet.lang.psi.* class HtmlKotlinVisitor: JetTreeVisitor() { - public override fun visitFile(file: PsiFile?) { + override fun visitFile(file: PsiFile?) { if (file is JetFile) { - val data = StringBuilder() - visitJetFile(file, data) + visitJetFile(file, StringBuilder()) } } - public override fun visitJetFile(file: JetFile, data: StringBuilder?): Void? { - if (file != null) { - println("============ Jet File ${file.getName()}") - acceptChildren(file, data) - } + + override fun visitJetFile(file: JetFile, data: StringBuilder?): Void? { + println("============ Jet File ${file.getName()}") + acceptChildren(file, data) return null } - - public override fun visitClassObject(classObject: JetClassObject, data: StringBuilder?): Void? { + override fun visitClassObject(classObject: JetClassObject, data: StringBuilder?): Void? { println("============ class $classObject data $data") return super.visitClassObject(classObject, data) } - public override fun visitClass(klass: JetClass, data: StringBuilder?): Void? { + 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) - } + acceptChildren(klass, data) + return null } - - public override fun visitClassBody(classBody: JetClassBody, data: StringBuilder?): Void? { + override fun visitClassBody(classBody: JetClassBody, data: StringBuilder?): Void? { println("============ class body $classBody data $data") return super.visitClassBody(classBody, data) } - - public override fun visitFunctionType(fnType: JetFunctionType, data: StringBuilder?): Void? { - println("======================= function Type $fnType") - return super.visitFunctionType(fnType, data) + override fun visitFunctionType(`type`: JetFunctionType, data: StringBuilder?): Void? { + println("======================= function Type $`type`") + return super.visitFunctionType(`type`, data) } protected fun accept(child: PsiElement?, data: StringBuilder?): Unit { diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/SyntaxHighlighter.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/SyntaxHighlighter.kt index c31636c50bf..290d143e18d 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/SyntaxHighlighter.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/highlighter/SyntaxHighlighter.kt @@ -8,8 +8,8 @@ import com.intellij.psi.tree.TokenSet import org.jetbrains.jet.lexer.* fun main(args: Array) { - val tool = SyntaxHighligher() - val answer = tool.highlight(""" val x = arrayList(1, 2, 3) + val tool = SyntaxHighlighter() + val answer = tool.highlight(""" val x = listOf(1, 2, 3) println("hello")""") println(answer) } @@ -17,7 +17,7 @@ fun main(args: Array) { /** * Syntax highlights Kotlin code */ -class SyntaxHighligher() { +class SyntaxHighlighter() { var formatter: HtmlFormatter = HtmlFormatter() val styleMap = createStyleMap() @@ -42,7 +42,6 @@ class SyntaxHighligher() { } val lexer = JetLexer() lexer.start(code) - val end = lexer.getTokenEnd() while (true) { lexer.advance() val token = lexer.getTokenType() @@ -52,14 +51,11 @@ class SyntaxHighligher() { if (token is JetKeywordToken) { style = "keyword" } else if (token == JetTokens.IDENTIFIER) { - val types = JetTokens.SOFT_KEYWORDS.getTypes() - if (types != null) { - for (softKeyword in types) { - if (softKeyword is JetKeywordToken) { - if (softKeyword.getValue().equals(tokenText)) { - style = "softkeyword" - break - } + for (softKeyword in JetTokens.SOFT_KEYWORDS.getTypes()) { + if (softKeyword is JetKeywordToken) { + if (softKeyword.getValue() == tokenText) { + style = "softkeyword" + break } } } @@ -80,7 +76,7 @@ class SyntaxHighligher() { builder.append("") builder.append("") builder.append("") - return builder.toString() ?: "" + return builder.toString() } catch (e: Exception) { println("Warning: failed to parse code $e") val builder = StringBuilder() @@ -92,7 +88,7 @@ class SyntaxHighligher() { formatter.format(builder, code) builder.append("") builder.append("") - return builder.toString() ?: "" + return builder.toString() } } 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 03545f445ad..8793bd3706b 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 @@ -30,13 +30,13 @@ class Html2CompilerPlugin(private val compilerArguments: KDocArguments) : Doclet .src .orEmpty() .split(File.pathSeparatorChar) - .map { path -> File(path).getCanonicalFile()!! } + .map { path -> File(path).getCanonicalFile() } - private val sourceDirPaths: List = sourceDirs.map { d -> d.getPath()!! } + private val sourceDirPaths: List = sourceDirs.map { d -> d.getPath() } private fun fileToWrite(psiFile: PsiFile): String { - val file = File((psiFile.getVirtualFile() as CoreLocalVirtualFile).getPath()!!).getCanonicalFile()!! - val filePath = file.getPath()!! + val file = File((psiFile.getVirtualFile() as CoreLocalVirtualFile).getPath()!!).getCanonicalFile() + val filePath = file.getPath() for (sourceDirPath in sourceDirPaths) { if (filePath.startsWith(sourceDirPath) && filePath.length() > sourceDirPath.length()) { val relativePath = filePath.substring(sourceDirPath.length + 1) @@ -111,7 +111,7 @@ class Html2CompilerPlugin(private val compilerArguments: KDocArguments) : Doclet elementType.toString() } // TODO - else -> psi.getClass()!!.getName() + else -> psi.getClass().getName() } for (t in splitPsi(psiFile)) { 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 58324742ff0..c8299f04259 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 @@ -22,19 +22,15 @@ import org.jetbrains.jet.lang.psi.JetFile import org.jetbrains.jet.lang.resolve.BindingContext import org.jetbrains.jet.lang.resolve.BindingContextUtils import org.jetbrains.jet.lang.resolve.scopes.JetScope -import org.jetbrains.jet.lang.resolve.scopes.receivers.ExtensionReceiver import org.jetbrains.jet.lang.types.JetType import org.jetbrains.jet.lexer.JetTokens import org.jetbrains.kotlin.doc.* -import org.jetbrains.kotlin.doc.highlighter.SyntaxHighligher +import org.jetbrains.kotlin.doc.highlighter.SyntaxHighlighter import org.jetbrains.kotlin.doc.templates.KDocTemplate import org.pegdown.Extensions import org.pegdown.LinkRenderer import org.pegdown.LinkRenderer.Rendering import org.pegdown.PegDownProcessor -import org.pegdown.ast.AutoLinkNode -import org.pegdown.ast.ExpLinkNode -import org.pegdown.ast.RefLinkNode import org.pegdown.ast.WikiLinkNode import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor @@ -86,22 +82,14 @@ fun inheritedExtensionFunctions(functions: Collection): Map>() - for (c in map.keySet()) { - val allFunctions = map.get(c).orEmpty().toSortedSet() - answer.put(c, allFunctions) - val des = c.descendants() - for (b in des) { - val list = map.get(b) - if (list != null) { - if (allFunctions != null) { - for (f in list) { - if (f != null) { - // add the methods from the base class if we don't have a matching method - if (!allFunctions.any{ it.name == f.name && it.parameterTypeText == f.parameterTypeText}) { - allFunctions.add(f) - } - } - } + for (klass in map.keySet()) { + val allFunctions = map.get(klass).orEmpty().toSortedSet() + answer.put(klass, allFunctions) + for (descendant in klass.descendants()) { + for (f in map.get(descendant).orEmpty()) { + // add the methods from the base class if we don't have a matching method + if (!allFunctions.any { it.name == f.name && it.parameterTypeText == f.parameterTypeText }) { + allFunctions.add(f) } } } @@ -114,22 +102,14 @@ fun inheritedExtensionProperties(properties: Collection): Map>() - for (c in map.keySet()) { - val allProperties = map.get(c).orEmpty().toSortedSet() - answer.put(c, allProperties) - val des = c.descendants() - for (b in des) { - val list = map.get(b) - if (list != null) { - if (allProperties != null) { - for (f in list) { - if (f != null) { - // add the proeprties from the base class if we don't have a matching method - if (!allProperties.any{ it.name == f.name}) { - allProperties.add(f) - } - } - } + for (klass in map.keySet()) { + val allProperties = map.get(klass).orEmpty().toSortedSet() + answer.put(klass, allProperties) + for (descendant in klass.descendants()) { + for (f in map.get(descendant).orEmpty()) { + // add the properties from the base class if we don't have a matching property + if (!allProperties.any { it.name == f.name }) { + allProperties.add(f) } } } @@ -153,10 +133,8 @@ fun extensionProperties(properties: Collection): Map = TreeSet() - - public open val properties: SortedSet = TreeSet() + open val functions = sortedSetOf() + open val properties = sortedSetOf() fun findProperty(name: String): KProperty? { // TODO we should use a Map? @@ -175,27 +153,25 @@ abstract class KClassOrPackage(model: KModel, declarationDescriptor: Declaration 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.UtilPackage.hashMap(Ljet/TypeInfo;Ljet/TypeInfo;)Ljava/util/HashMap; - //val packages = sortedMap() - public val packageMap: SortedMap = TreeMap() + val packageMap = sortedMapOf() - public val allPackages: Collection - get() = packageMap.values()!! + val allPackages: Collection + get() = packageMap.values() /** Returns the local packages */ - public val packages: Collection - get() = allPackages.filter{ it.local && config.includePackage(it) } + val packages: Collection + get() = allPackages.filter { it.local && config.includePackage(it) } - public val classes: Collection - get() = packages.flatMap{ it.classes } + val classes: Collection + get() = packages.flatMap { it.classes } - public var markdownProcessor: PegDownProcessor = PegDownProcessor(Extensions.ALL) - public var highlighter: SyntaxHighligher = SyntaxHighligher() + var markdownProcessor = PegDownProcessor(Extensions.ALL) + var highlighter = SyntaxHighlighter() - public val title: String + val title: String get() = config.title - public val version: String + val version: String get() = config.version private var _projectRootDir: String? = null @@ -203,7 +179,7 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs /** * File names we look for in a package directory for the overall description of a package for KDoc */ - val packageDescriptionFiles = arrayList("readme.md", "ReadMe.md, readme.html, ReadMe.html") + val packageDescriptionFiles = listOf("readme.md", "ReadMe.md, readme.html, ReadMe.html") private val readMeDirsScanned = HashSet() @@ -213,11 +189,11 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs ;{ val normalizedSourceDirs: List = - sourceDirs.map { file -> file.getCanonicalPath()!! } + sourceDirs.map { file -> file.getCanonicalPath() } fun relativePath(psiFile: PsiFile): String { - val file = File((psiFile.getVirtualFile() as CoreLocalVirtualFile).getPath()!!).getCanonicalFile()!! - val filePath = file.getPath()!! + 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) @@ -277,7 +253,7 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs warning("KDocConfig does not have a projectRootDir defined so we cannot generate relative source Hrefs") "" } else { - File(rootDir).getCanonicalPath() ?: "" + File(rootDir).getCanonicalPath() } } return _projectRootDir ?: "" @@ -332,7 +308,7 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs if (srcPath != null) { val srcFile = File(srcPath) val dir = if (srcFile.isDirectory()) srcFile else srcFile.getParentFile() - if (dir != null && readMeDirsScanned.add(dir.getPath()!!)) { + if (dir != null && readMeDirsScanned.add(dir.getPath())) { val f = packageDescriptionFiles.map{ File(dir, it) }.find{ it.exists() } if (f != null) { val file = f.getCanonicalPath() @@ -355,7 +331,7 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs if (root != null) { // lets remove the root project directory val rootDir = projectRootDir() - val canonicalFile = File(filePath).getCanonicalPath() ?: "" + val canonicalFile = File(filePath).getCanonicalPath() //println("=========== root dir for filePath: $canonicalFile is $rootDir") val relativeFile = if (canonicalFile.startsWith(rootDir)) @@ -419,11 +395,9 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs val parameters = ArrayList() val params = descriptor.getValueParameters() for (param in params) { - if (param != null) { - val p = createParameter(param) - if (p != null) { - parameters.add(p) - } + val p = createParameter(param) + if (p != null) { + parameters.add(p) } } val function = KFunction(descriptor, owner, name, returnType, parameters) @@ -503,7 +477,7 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs } protected fun commentsFor(descriptor: DeclarationDescriptor): String { - val psiElement = getPsiElement(descriptor) + val psiElement = getPsiElement(descriptor) // This method is a hack. Doc comments should be easily accessible, but they aren't for now. if (psiElement != null) { @@ -513,14 +487,14 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs } if (node == null) return "" if (node?.getElementType() != JetTokens.DOC_COMMENT) return "" - var text = node?.getText() ?: "" + var nodeText = node?.getText() ?: "" // lets remove the comment tokens - val lines = text.trim().split("\\n") + val lines = nodeText.trim().split("\\n") // lets remove the /** ... * ... */ tokens val buffer = StringBuilder() val last = lines.size - 1 for (i in 0.rangeTo(last)) { - var text = lines[i] ?: "" + var text = lines[i] text = text.trim() if (i == 0) { text = text.trimLeading("/**").trimLeading("/*") @@ -536,7 +510,7 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs text = processMacros(text, psiElement) buffer.append(text) } - return buffer.toString() ?: "" + return buffer.toString() } return "" } @@ -554,8 +528,8 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs // source code function if folks adopted a convention of naming the test method after the // method its acting as a demo/test for if (words.size > 1) { - val includeFile = words[0]!! - val fnName = words[1]!! + val includeFile = words[0] + val fnName = words[1] val content = findFunctionInclude(psiElement, includeFile, fnName) if (content != null) { return content @@ -579,7 +553,7 @@ class KModel(val context: BindingContext, val config: KDocConfig, val sourceDirs if (text != null) { // lets find the function definition val regex = """fun\s+$functionName\(.*\)""".toRegex() - val matcher = regex.matcher(text)!! + val matcher = regex.matcher(text) if (matcher.find()) { val idx = matcher.end() val remaining = text.substring(idx) @@ -735,7 +709,7 @@ $highlight""" class TemplateLinkRenderer(val annotated: KAnnotated, val template: KDocTemplate): LinkRenderer() { // TODO dirty hack - remove when this issue is fixed // http://youtrack.jetbrains.com/issue/KT-1524 - val hackedLinks = hashMap( + val hackedLinks = mapOf( Pair("IllegalArgumentException", Pair("java.lang", "java/lang/IllegalArgumentException.html")), Pair("IllegalStateException", Pair("java.lang", "java/lang/IllegalStateException.html")), Pair("Map.Entry", Pair("java.util", "java/util/Map.Entry.html")), @@ -745,8 +719,7 @@ class TemplateLinkRenderer(val annotated: KAnnotated, val template: KDocTemplate Pair("#hashCode()", Pair("java.lang", "java/lang/Object.html#hashCode()")) ) - - public override fun render(node: WikiLinkNode?): Rendering? { + override fun render(node: WikiLinkNode?): Rendering? { val answer = super.render(node) if (answer != null) { val text = answer.text @@ -756,19 +729,16 @@ class TemplateLinkRenderer(val annotated: KAnnotated, val template: KDocTemplate if (href != null) { answer.href = href } else { - // TODO really dirty hack alert!!! // until the resolver is working, lets try adding a few prefixes :) - for (prefix in arrayList("java.lang", "java.util", "java.util.concurrent", "java.util.regex", "java.io", + for (prefix in listOf("java.lang", "java.util", "java.util.concurrent", "java.util.regex", "java.io", "java.awt", "java.awt.event", "java.sql", "java.beans", "javax.swing", "javax.swing.event", "org.w3c.dom", "kotlin.template")) { - if (href == null) { - href = resolveClassNameLink(prefix + "." + qualified) - if (href != null) { - break - } + href = resolveClassNameLink(prefix + "." + qualified) + if (href != null) { + break } } } @@ -827,7 +797,7 @@ class TemplateLinkRenderer(val annotated: KAnnotated, val template: KDocTemplate } /** * Attempts to resolve the class, method or property expression using the - * current imports and declaraiton + * current imports and declaration */ protected fun resolveToQualifiedName(text: String): String { // TODO use the CompletionContributors maybe to figure out what local names are imported??? @@ -859,26 +829,11 @@ class TemplateLinkRenderer(val annotated: KAnnotated, val template: KDocTemplate */ } - - public override fun render(node: RefLinkNode?, url: String?, title: String?, text: String?): Rendering? { - return super.render(node, url, title, text) - } - - public override fun render(node: AutoLinkNode?): Rendering? { - return super.render(node) - } - - public override fun render(node: ExpLinkNode?, text: String?): Rendering? { - return super.render(node, text) - } - - } abstract class KAnnotated(val model: KModel, val declarationDescriptor: DeclarationDescriptor) { - public open var wikiDescription: String = "" - - public open var deprecated: Boolean = false + open var wikiDescription: String = "" + open var deprecated: Boolean = false open fun description(template: KDocTemplate): String { val detailedText = detailedDescription(template) @@ -928,9 +883,8 @@ abstract class KAnnotated(val model: KModel, val declarationDescriptor: Declarat } } -abstract class KNamed(val name: String, model: KModel, declarationDescriptor: DeclarationDescriptor): KAnnotated(model, declarationDescriptor), Comparable { - - public override fun compareTo(other: KNamed): Int = name.compareTo(other.name) +abstract class KNamed(val name: String, model: KModel, descriptor: DeclarationDescriptor): KAnnotated(model, descriptor), Comparable { + override fun compareTo(other: KNamed): Int = name.compareTo(other.name) open fun equals(other: KPackage) = name == other.name @@ -938,22 +892,21 @@ abstract class KNamed(val name: String, model: KModel, declarationDescriptor: De } -class KPackage(model: KModel, val descriptor: PackageFragmentDescriptor, +class KPackage( + model: KModel, + descriptor: PackageFragmentDescriptor, val name: String, var local: Boolean = false, - var useExternalLink: Boolean = false): KClassOrPackage(model, descriptor), Comparable { + var useExternalLink: Boolean = false +): KClassOrPackage(model, descriptor), Comparable { + val classMap = sortedMapOf() + val classes: Collection + get() = classMap.values().filter{ it.isApi() } - // TODO generates java.lang.NoSuchMethodError: kotlin.util.UtilPackage.hashMap(Ljet/TypeInfo;Ljet/TypeInfo;)Ljava/util/HashMap; - //val classes = sortedMap() - public val classMap: SortedMap = TreeMap() + val annotations = arrayListOf() - public val classes: Collection - get() = classMap.values()!!.filter{ it.isApi() } - - public val annotations: Collection = ArrayList() - - public override fun compareTo(other: KPackage): Int = name.compareTo(other.name) + override fun compareTo(other: KPackage): Int = name.compareTo(other.name) fun equals(other: KPackage) = name == other.name @@ -991,11 +944,11 @@ class KPackage(model: KModel, val descriptor: PackageFragmentDescriptor, } /** Returns the name as a directory using '/' instead of '.' */ - public val nameAsPath: String + val nameAsPath: String get() = if (name.length() == 0) "." else name.replace('.', '/') /** Returns a list of all the paths in the package name */ - public val namePaths: List + val namePaths: List get() { val answer = ArrayList() for (n in name.split("\\.")) { @@ -1005,7 +958,7 @@ class KPackage(model: KModel, val descriptor: PackageFragmentDescriptor, } /** Returns a relative path like ../.. for each path in the name */ - public val nameAsRelativePath: String + val nameAsRelativePath: String get() { val answer = namePaths.map{ ".." }.makeString("/") return if (answer.length == 0) "" else answer + "/" @@ -1054,12 +1007,9 @@ class KType(val jetType: JetType, model: KModel, val klass: KClass?, val argumen this.wikiDescription = klass.wikiDescription } for (arg in jetType.getArguments()) { - if (arg != null) { - val argJetType = arg.getType() - val t = model.getType(argJetType) - if (t != null) { - arguments.add(t) - } + val t = model.getType(arg.getType()) + if (t != null) { + arguments.add(t) } } } @@ -1078,23 +1028,20 @@ class KClass( { val simpleName = descriptor.getName().asString() var group: String = "Other" - var annotations: List = arrayList() - var typeParameters: MutableList = arrayList() + var annotations: List = listOf() + var typeParameters: MutableList = arrayListOf() var since: String = "" - var authors: List = arrayList() - var baseClasses: MutableList = arrayList() - var nestedClasses: List = arrayList() + var authors: List = listOf() + var baseClasses: MutableList = arrayListOf() + var nestedClasses: List = listOf() - public override fun compareTo(other: KClass): Int = name.compareTo(other.name) + override fun compareTo(other: KClass): Int = name.compareTo(other.name) fun equals(other: KClass) = name == other.name override fun toString() = "$kind($name)" - fun isApi(): Boolean { - val visibility = descriptor.getVisibility() - return visibility.isPublicAPI() - } + fun isApi() = descriptor.getVisibility().isPublicAPI() val kind: String get() { @@ -1126,18 +1073,18 @@ class KClass( } /** Link to the type which is relative if its a local type but could be a type in a different library or null if no link */ - public var url: String? = null + var url: String? = null get() { if ($url == null) $url = "${nameAsPath}.html" return $url } - public val name: String = pkg.qualifiedName(descriptor.getName().asString()) + val name: String = pkg.qualifiedName(descriptor.getName().asString()) - public val packageName: String = pkg.name + val packageName: String = pkg.name /** Returns the name as a directory using '/' instead of '.' */ - public val nameAsPath: String + val nameAsPath: String get() = name.replace('.', '/') @@ -1162,14 +1109,14 @@ class KFunction(val descriptor: CallableDescriptor, val owner: KClassOrPackage, var parameters: List, var receiverType: KType? = null, var extensionClass: KClass? = null, - var modifiers: List = arrayList(), - var typeParameters: MutableList = arrayList(), - var exceptions: List = arrayList(), - var annotations: List = arrayList()): KAnnotated(owner.model, descriptor), Comparable { + var modifiers: List = listOf(), + var typeParameters: MutableList = arrayListOf(), + var exceptions: List = listOf(), + var annotations: List = listOf()): KAnnotated(owner.model, descriptor), Comparable { - public val parameterTypeText: String = parameters.map{ it.aType.name }.makeString(", ") + val parameterTypeText: String = parameters.map{ it.aType.name }.makeString(", ") - public override fun compareTo(other: KFunction): Int { + override fun compareTo(other: KFunction): Int { var answer = name.compareTo(other.name) if (answer == 0) { answer = parameterTypeText.compareTo(other.parameterTypeText) @@ -1187,19 +1134,19 @@ class KFunction(val descriptor: CallableDescriptor, val owner: KClassOrPackage, override fun toString() = "fun $name($parameterTypeText): $returnType" - public val link: String = "$name($parameterTypeText)" + val link: String = "$name($parameterTypeText)" /** Returns a list of generic type parameter names kinds like "A, I" */ - public val typeParametersText: String + val typeParametersText: String get() = typeParameters.map{ it.name }.makeString(", ") } class KProperty(val owner: KClassOrPackage, val descriptor: PropertyDescriptor, val name: String, val returnType: KType, val extensionClass: KClass?): KAnnotated(owner.model, descriptor), Comparable { - public override fun compareTo(other: KProperty): Int = name.compareTo(other.name) + override fun compareTo(other: KProperty): Int = name.compareTo(other.name) - public val link: String = "$name" + val link = "$name" fun equals(other: KFunction) = name == other.name @@ -1235,7 +1182,7 @@ class KParameter(val descriptor: ValueParameterDescriptor, val name: String, class KTypeParameter(val name: String, val descriptor: TypeParameterDescriptor, model: KModel, - var extends: List = arrayList()): KAnnotated(model, descriptor) { + var extends: List = listOf()): KAnnotated(model, descriptor) { override fun toString() = "$name" } diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageFrameTemplate.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageFrameTemplate.kt index 32594db6568..96187cb1de8 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageFrameTemplate.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageFrameTemplate.kt @@ -84,7 +84,7 @@ ${stylesheets()} protected fun printExtensionFunctions(): Unit { val map = extensionFunctions(pkg.functions) - if (! map.isEmpty()) { + if (!map.isEmpty()) { println("""
Extensions  @@ -92,10 +92,8 @@ ${stylesheets()}
""") for (e in map) { val c = e.key - if (c != null) { - println("""${c.name} + println("""${c.name}
""") - } } println("""
""") @@ -110,10 +108,8 @@ ${stylesheets()}
""") for (c in list) { - if (c != null) { - println("""${c.name} + println("""${c.name}
""") - } } println(""" """) diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageSummaryTemplate.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageSummaryTemplate.kt index 40f291d0bf0..a371aa78ee8 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageSummaryTemplate.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/PackageSummaryTemplate.kt @@ -130,20 +130,15 @@ ${pkg.detailedDescription(this)} """) - val groupMap = pkg.groupClassMap() - for (e in groupMap) { - val group = e.key ?: "Other" - val list = e.value - if (list != null) { - println("""

$group

+ for ((group, list) in pkg.groupClassMap()) { + println("""

$group

    """) - for (c in list) { - println("""
  • ${c.simpleName}""") - } - println(""" -
""") + for (c in list) { + println("""
  • ${c.simpleName}""") } + println(""" + """) } println("""

    """) @@ -270,22 +265,15 @@ Copyright © 2010-2012. All Rights Reserved. Extensions Summary """) - for (e in map) { - val c = e.key - if (c != null) { - println(""" + for ((c, list) in map) { + println(""" ${c.name} """) - val list = e.value - if (list != null) { - val functions = filterDuplicateNames(list) - for (f in functions) { - println("""${f.name} """) - } - } - println(""" -""") + for (f in filterDuplicateNames(list)) { + println("""${f.name} """) } + println(""" +""") } println("""   diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/SearchXmlTemplate.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/SearchXmlTemplate.kt index 50e460b3bb5..c26407fb981 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/SearchXmlTemplate.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/SearchXmlTemplate.kt @@ -30,30 +30,28 @@ class SearchXmlTemplate(val model: KModel): KDocTemplate() { for (c in model.classes) { add("${c.simpleName} [${c.pkg.name}]", "${c.nameAsPath}.html", c.kind) - c.functions.forEach{ add(c, it, href(it)) } - c.properties.forEach{ add(c, it, href(it)) } + c.functions.forEach { add(c, it, href(it)) } + c.properties.forEach { add(c, it, href(it)) } } for (p in model.packages) { - val map = inheritedExtensionFunctions(p.functions) + val fmap = inheritedExtensionFunctions(p.functions) val pmap = inheritedExtensionProperties(p.properties) - val classes = hashSet() - classes.addAll(map.keySet()) + val classes = hashSetOf() + classes.addAll(fmap.keySet()) classes.addAll(pmap.keySet()) for (c in classes) { - if (c != null) { - val functions = map.get(c).orEmpty() - val properties = pmap.get(c).orEmpty() + val functions = fmap.get(c).orEmpty() + val properties = pmap.get(c).orEmpty() - functions.forEach{ add(c, it, p.nameAsPath + "/" + extensionsHref(p, c, it)) } - functions.forEach{ add(c, it, p.nameAsPath + "/" + extensionsHref(p, c, it)) } - } + functions.forEach { add(c, it, p.nameAsPath + "/" + extensionsHref(p, c, it)) } + properties.forEach { add(c, it, p.nameAsPath + "/" + extensionsHref(p, c, it)) } } } println(""" """) - for (s in map.values()!!) { + for (s in map.values()) { println(""" ${s.href} ${s.name} diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/template/HtmlTemplate.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/template/HtmlTemplate.kt index 0b59cec7407..a08426e9705 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/template/HtmlTemplate.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/template/HtmlTemplate.kt @@ -6,7 +6,7 @@ abstract class HtmlTemplate() : TextTemplate() { tagName: String, style: String? = null, className: String? = null, - attributes: List> = arrayList(), + attributes: List> = listOf(), content: () -> Unit) { val allAttributesBuilder = listBuilder>() if (style != null) @@ -53,7 +53,7 @@ abstract class HtmlTemplate() : TextTemplate() { fun linkCssStylesheet(href: String) = tag( tagName = "link", - attributes = arrayList(Pair("rel", "stylesheet"), Pair("type", "text/css"), Pair("href", href))) {} + attributes = listOf(Pair("rel", "stylesheet"), Pair("type", "text/css"), Pair("href", href))) {} fun body(style: String? = null, className: String? = null, content: () -> Unit) = tag(tagName = "body", style = style, className = className, content = content) diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/template/TemplateCore.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/template/TemplateCore.kt index 5b53c120572..17932fa54df 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/template/TemplateCore.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/template/TemplateCore.kt @@ -72,7 +72,7 @@ abstract class TextTemplate() : TemplateSupport(), Printer { fun renderToText(): String { val buffer = StringWriter() renderTo(buffer) - return buffer.toString()!! + return buffer.toString() } fun renderTo(writer: Writer): Unit { diff --git a/libraries/tools/kdoc/src/test/kotlin/test/kotlin/kdoc/KDocSampleTest.kt b/libraries/tools/kdoc/src/test/kotlin/test/kotlin/kdoc/KDocSampleTest.kt index 76e9d4737de..6f1a5ca8a77 100644 --- a/libraries/tools/kdoc/src/test/kotlin/test/kotlin/kdoc/KDocSampleTest.kt +++ b/libraries/tools/kdoc/src/test/kotlin/test/kotlin/kdoc/KDocSampleTest.kt @@ -12,7 +12,7 @@ fun File.rmrf() { val children = listFiles() if (children != null) { for (child in children) { - child!!.rmrf() + child.rmrf() } } delete() @@ -50,9 +50,9 @@ class KDocSampleTest { classesOutputDir.rmrf() classesOutputDir.mkdirsProperly() - args.outputDir = classesOutputDir.getPath()!! + args.outputDir = classesOutputDir.getPath() - args.docConfig.docOutputDir = outputDir.getPath()!! + args.docConfig.docOutputDir = outputDir.getPath() args.docConfig.title = "Sample" val exitCode = compiler.exec(System.err, args) diff --git a/libraries/tools/kdoc/src/test/kotlin/test/kotlin/psiUtilsTest.kt b/libraries/tools/kdoc/src/test/kotlin/test/kotlin/psiUtilsTest.kt index 95e5bd9d885..cc074b0a911 100644 --- a/libraries/tools/kdoc/src/test/kotlin/test/kotlin/psiUtilsTest.kt +++ b/libraries/tools/kdoc/src/test/kotlin/test/kotlin/psiUtilsTest.kt @@ -21,7 +21,7 @@ import org.junit.Test class PsiUtilsTest { val rootDisposable = object : Disposable { - public override fun dispose() { + override fun dispose() { } } @@ -52,7 +52,6 @@ class PsiUtilsTest { fun splitPsi() { val file = createFile("class Foo") val items: List = splitPsi(file).map { t -> t.first } - Assert.assertEquals(arrayList("class", " ", "Foo"), items) + Assert.assertEquals(listOf("class", " ", "Foo"), items) } - } diff --git a/libraries/tools/kdoc/src/test/kotlin/test/kotlin/template/PegdownTest.kt b/libraries/tools/kdoc/src/test/kotlin/test/kotlin/template/PegdownTest.kt index 5e3513bde0e..f00af67f971 100644 --- a/libraries/tools/kdoc/src/test/kotlin/test/kotlin/template/PegdownTest.kt +++ b/libraries/tools/kdoc/src/test/kotlin/test/kotlin/template/PegdownTest.kt @@ -10,7 +10,7 @@ class PegdownTest() : TestCase() { var linkRenderer = CustomLinkRenderer() fun testPegDown() { - val markups = arrayList( + val markups = listOf( "hello **there **", "a [[WikiLink]] blah", "a [[WikiLink someText]] blah", @@ -26,24 +26,23 @@ class PegdownTest() : TestCase() { } -class CustomLinkRenderer() : LinkRenderer() { - - public override fun render(node : WikiLinkNode?) : Rendering? { +class CustomLinkRenderer : LinkRenderer() { + override fun render(node: WikiLinkNode?): Rendering? { println("LinkRenderer.render(WikiLinkNode): $node") return super.render(node) } - public override fun render(node : RefLinkNode?, url : String?, title : String?, text : String?) : Rendering? { + override fun render(node: RefLinkNode?, url: String?, title: String?, text: String?): Rendering? { println("LinkRenderer.render(RefLinkNode): $node url: $url title: $title text: $text") return super.render(node, url, title, text) } - public override fun render(node : AutoLinkNode?) : Rendering? { + override fun render(node: AutoLinkNode?): Rendering? { println("LinkRenderer.render(AutoLinkNode): $node") return super.render(node) } - public override fun render(node : ExpLinkNode?, text : String?) : Rendering? { + override fun render(node: ExpLinkNode?, text: String?): Rendering? { println("LinkRenderer.render(ExpLinkNode): $node text: $text") return super.render(node, text) } diff --git a/libraries/tools/kdoc/src/test/kotlin/test/kotlin/template/TemplateCoreTest.kt b/libraries/tools/kdoc/src/test/kotlin/test/kotlin/template/TemplateCoreTest.kt index c345aed779b..2f519155ce1 100644 --- a/libraries/tools/kdoc/src/test/kotlin/test/kotlin/template/TemplateCoreTest.kt +++ b/libraries/tools/kdoc/src/test/kotlin/test/kotlin/template/TemplateCoreTest.kt @@ -6,13 +6,13 @@ import junit.framework.TestCase import org.jetbrains.kotlin.template.* class EmailTemplate(var name: String = "James", var time: Date = Date()) : TextTemplate() { - public override fun render() { + override fun render() { print("Hello there $name and how are you? Today is $time. Kotlin rocks") } } class MoreDryTemplate(var name: String = "James", var time: Date = Date()) : TextTemplate() { - public override fun render() { + override fun render() { +"Hey there $name and how are you? Today is $time. Kotlin rocks" } }