From 309bb9639087b5de21a5fbf991fb152e0275a284 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Fri, 15 Jun 2012 15:55:50 +0400 Subject: [PATCH] remove warnings in kdoc code --- .../kotlin/doc/JavadocStyleHtmlDoclet.kt | 10 +- .../org/jetbrains/kotlin/doc/KDocCompiler.kt | 6 +- .../org/jetbrains/kotlin/doc/KDocConfig.kt | 8 +- .../doc/highlighter/HtmlCompilerPlugin.kt | 15 +-- .../doc/highlighter/SyntaxHighlighter.kt | 4 +- .../jetbrains/kotlin/doc/model/KotlinModel.kt | 127 ++++++++---------- .../test/kotlin/kdoc/HtmlVisitorTest.kt | 4 +- 7 files changed, 78 insertions(+), 96 deletions(-) diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/JavadocStyleHtmlDoclet.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/JavadocStyleHtmlDoclet.kt index 28306f6a68c..938d449b3c9 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/JavadocStyleHtmlDoclet.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/JavadocStyleHtmlDoclet.kt @@ -50,12 +50,10 @@ class JavadocStyleHtmlDoclet() : Doclet { classes.addAll(map.keySet()) classes.addAll(pmap.keySet()) for (c in classes) { - if (c != null) { - val functions = map.get(c).orEmpty() - val properties = pmap.get(c).orEmpty() - run("${p.nameAsPath}/${c.nameAsPath}-extensions.html", - ClassExtensionsTemplate(model, p, c, functions, properties)) - } + val functions = map.get(c).orEmpty() + val properties = pmap.get(c).orEmpty() + run("${p.nameAsPath}/${c.nameAsPath}-extensions.html", + ClassExtensionsTemplate(model, p, c, functions, properties)) } } diff --git a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocCompiler.kt b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocCompiler.kt index 4ac8589c638..848d602b01b 100644 --- a/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocCompiler.kt +++ b/libraries/tools/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDocCompiler.kt @@ -24,14 +24,14 @@ class KDocCompiler() : K2JVMCompiler() { protected override fun configureEnvironment(configuration : K2JVMCompileEnvironmentConfiguration, arguments : K2JVMCompilerArguments) { super.configureEnvironment(configuration, arguments) - val coreEnvironment = configuration?.getEnvironment() + val coreEnvironment = configuration.getEnvironment() if (coreEnvironment != null) { val kdoc = KDoc() if (arguments is KDocArguments) { kdoc.config = arguments.apply() } - val plugins = configuration?.getCompilerPlugins().orEmpty() + val plugins = configuration.getCompilerPlugins().orEmpty() /* val sourcePlugin = HtmlCompilerPlugin() plugins.add(sourcePlugin) @@ -45,7 +45,7 @@ class KDocCompiler() : K2JVMCompiler() { } protected override fun usage(target : PrintStream) { - target?.println("Usage: KDocCompiler -docOutput [-output |-jar ] [-stdlib ] [-src |-module ] [-includeRuntime]"); + target.println("Usage: KDocCompiler -docOutput [-output |-jar ] [-stdlib ] [-src |-module ] [-includeRuntime]"); } } 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 dc5e24a371d..478dbce74f1 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 @@ -95,10 +95,8 @@ class KDocConfig() { for (e in packagePrefixToUrls) { val p = e.key val url = e.value - if (p != null && url != null) { - if (packageName.startsWith(p)) { - return url - } + if (packageName.startsWith(p)) { + return url } } if (warn && missingPackageUrls.add(packageName)) { @@ -123,4 +121,4 @@ private class LongestFirstStringComparator() : Comparator { public override fun equals(obj : Any?) : Boolean { return obj is LongestFirstStringComparator } -} \ No newline at end of file +} 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 8d55d9a345c..5d5c9cc5d52 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 @@ -8,17 +8,12 @@ import org.jetbrains.jet.cli.common.CompilerPluginContext class HtmlCompilerPlugin: CompilerPlugin { public override fun processFiles(context: CompilerPluginContext) { - val bindingContext = context.getContext() val files = context.getFiles() - if (bindingContext != null && files != null) { - if (files != null && bindingContext != null) { - for (file in files) { - if (file != null) { - val visitor = HtmlKotlinVisitor() - file.accept(visitor) - } - } + for (file in files) { + if (file != null) { + val visitor = HtmlKotlinVisitor() + file.accept(visitor) } } } -} \ No newline at end of file +} 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 e334f62bf66..fc64c8d0b6c 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 @@ -53,7 +53,7 @@ class SyntaxHighligher() { if (token is JetKeywordToken) { style = "keyword" } else if (token == JetTokens.IDENTIFIER) { - val types = JetTokens.SOFT_KEYWORDS?.getTypes() + val types = JetTokens.SOFT_KEYWORDS.getTypes() if (types != null) { for (softKeyword in types) { if (softKeyword is JetKeywordToken) { @@ -135,4 +135,4 @@ class SyntaxHighligher() { putAll(JetTokens.OPERATIONS, "operation") return styleMap } -} \ No newline at end of file +} 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 89f35dbdacd..5fac9286525 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 @@ -328,13 +328,14 @@ class KModel(var context: BindingContext, val config: KDocConfig) { val rootDir = projectRootDir() val canonicalFile = File(filePath).getCanonicalPath() ?: "" //println("=========== root dir for filePath: $canonicalFile is $rootDir") - val relativeFile = if (rootDir != null && canonicalFile.startsWith(rootDir)) - canonicalFile.substring(rootDir.length()) else canonicalFile - if (relativeFile != null) { - val cleanRoot = root.trimTrailing("/") - val cleanPath = relativeFile.trimLeading("/") - return "$cleanRoot/$cleanPath$lineLinkText$sourceLine" - } + val relativeFile = + if (canonicalFile.startsWith(rootDir)) + canonicalFile.substring(rootDir.length()) + else + canonicalFile + val cleanRoot = root.trimTrailing("/") + val cleanPath = relativeFile.trimLeading("/") + return "$cleanRoot/$cleanPath$lineLinkText$sourceLine" } return null } @@ -357,7 +358,7 @@ class KModel(var context: BindingContext, val config: KDocConfig) { val descriptors = scope.getAllDescriptors() for (descriptor in descriptors) { if (descriptor is PropertyDescriptor) { - val name = descriptor.getName()?.getName() + val name = descriptor.getName().getName() val returnType = getType(descriptor.getReturnType()) if (returnType != null) { val receiver = descriptor.getReceiverParameter() @@ -385,7 +386,7 @@ class KModel(var context: BindingContext, val config: KDocConfig) { protected fun createFunction(owner: KClassOrPackage, descriptor: CallableDescriptor): KFunction? { val returnType = getType(descriptor.getReturnType()) if (returnType != null) { - val name = descriptor.getName()?.getName() ?: "null" + val name = descriptor.getName().getName() val parameters = ArrayList() val params = descriptor.getValueParameters() for (param in params) { @@ -422,7 +423,7 @@ class KModel(var context: BindingContext, val config: KDocConfig) { } protected fun createTypeParameter(descriptor: TypeParameterDescriptor): KTypeParameter? { - val name = descriptor.getName()?.getName() + val name = descriptor.getName().getName() val answer = KTypeParameter(name, descriptor, this) configureComments(answer, descriptor) return answer @@ -431,7 +432,7 @@ class KModel(var context: BindingContext, val config: KDocConfig) { protected fun createParameter(descriptor: ValueParameterDescriptor): KParameter? { val returnType = getType(descriptor.getReturnType()) if (returnType != null) { - val name = descriptor.getName()?.getName() + val name = descriptor.getName().getName() val answer = KParameter(descriptor, name, returnType) configureComments(answer, descriptor) return answer @@ -486,31 +487,27 @@ class KModel(var context: BindingContext, val config: KDocConfig) { var text = node?.getText() ?: "" // lets remove the comment tokens val lines = text.trim().split("\\n") - if (lines != null) { - // lets remove the /** ... * ... */ tokens - val buffer = StringBuilder() - val last = lines.size - 1 - for (i in 0.upto(last)) { - var text = lines[i] ?: "" - text = text.trim() - if (i == 0) { - text = text.trimLeading("/**").trimLeading("/*") - } else { - buffer.append("\n") - } - if (i >= last) { - text = text.trimTrailing("*/") - } else if (i > 0) { - text = text.trimLeading("* ") - if (text == "*") text = "" - } - text = processMacros(text, psiElement) - buffer.append(text) + // lets remove the /** ... * ... */ tokens + val buffer = StringBuilder() + val last = lines.size - 1 + for (i in 0.upto(last)) { + var text = lines[i] ?: "" + text = text.trim() + if (i == 0) { + text = text.trimLeading("/**").trimLeading("/*") + } else { + buffer.append("\n") } - return buffer.toString() ?: "" - } else { - return text + if (i >= last) { + text = text.trimTrailing("*/") + } else if (i > 0) { + text = text.trimLeading("* ") + if (text == "*") text = "" + } + text = processMacros(text, psiElement) + buffer.append(text) } + return buffer.toString() ?: "" } return "" } @@ -527,7 +524,7 @@ class KModel(var context: BindingContext, val config: KDocConfig) { // TODO we could default the test function name to match that of the // 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 != null && words.size > 1) { + if (words.size > 1) { val includeFile = words[0].sure() val fnName = words[1].sure() val content = findFunctionInclude(psiElement, includeFile, fnName) @@ -628,22 +625,20 @@ $highlight""" // lets try resolve the include name relative to this file val paths = relativeName.split("/") - if (paths != null) { - val size = paths.size - for (i in 0.upto(size - 2)) { - val path = paths[i] - if (path == ".") continue - else if (path == "..") dir = dir?.getParent() - else if (path != null) dir = dir?.findSubdirectory(path) - } - val name = paths[size - 1] - if (dir != null && name != null) { - val file = dir?.findFile(name) - if (file != null) { - return file - } else { - warning("could not find file $relativeName in $dir with name $name") - } + val size = paths.size + for (i in 0.upto(size - 2)) { + val path = paths[i] + if (path == ".") continue + else if (path == "..") dir = dir?.getParent() + else dir = dir?.findSubdirectory(path) + } + val name = paths[size - 1] + if (dir != null) { + val file = dir?.findFile(name) + if (file != null) { + return file + } else { + warning("could not find file $relativeName in $dir with name $name") } } return null @@ -672,30 +667,28 @@ $highlight""" // TODO warning this only works for top level classes // a better algorithm is to walk down each dot path dealing with nested packages/classes val idx = qualifiedName.lastIndexOf('.') - val pkgName = if (idx >= 0) qualifiedName.substring(0, idx) ?: "" else "" + val pkgName = if (idx >= 0) qualifiedName.substring(0, idx) else "" val pkg = getPackage(pkgName) if (pkg != null) { - val simpleName = if (idx >= 0) qualifiedName.substring(idx + 1) ?: "" else qualifiedName + val simpleName = if (idx >= 0) qualifiedName.substring(idx + 1) else qualifiedName return pkg.classMap.get(simpleName) } return null } fun getClass(classElement: ClassDescriptor): KClass? { - val name = classElement.getName()?.getName() - if (name != null) { - var dec: DeclarationDescriptor? = classElement.getContainingDeclaration() - while (dec != null) { - val container = dec - if (container is NamespaceDescriptor) { - val pkg = getPackage(container) - return pkg.getClass(name, classElement) - } else { - dec = dec?.getContainingDeclaration() - } + val name = classElement.getName().getName() + var dec: DeclarationDescriptor? = classElement.getContainingDeclaration() + while (dec != null) { + val container = dec + if (container is NamespaceDescriptor) { + val pkg = getPackage(container) + return pkg.getClass(name, classElement) + } else { + dec = dec?.getContainingDeclaration() } - warning("no package found for class $name") } + warning("no package found for class $name") return null } @@ -973,9 +966,7 @@ class KPackage(model: KModel, val descriptor: NamespaceDescriptor, get() { val answer = ArrayList() for (n in name.split("\\.")) { - if (n != null) { - answer.add(n) - } + answer.add(n) } return answer; } diff --git a/libraries/tools/kdoc/src/test/kotlin/test/kotlin/kdoc/HtmlVisitorTest.kt b/libraries/tools/kdoc/src/test/kotlin/test/kotlin/kdoc/HtmlVisitorTest.kt index 7b873d58922..3f0d92b1cf5 100644 --- a/libraries/tools/kdoc/src/test/kotlin/test/kotlin/kdoc/HtmlVisitorTest.kt +++ b/libraries/tools/kdoc/src/test/kotlin/test/kotlin/kdoc/HtmlVisitorTest.kt @@ -23,9 +23,9 @@ class HtmlVisitorTest { val args = K2JVMCompilerArguments() args.setSrc(srcDir.toString()) args.setOutputDir(File(dir, "target/classes-htmldocs").toString()) - args.getCompilerPlugins()?.add(HtmlCompilerPlugin()) + args.getCompilerPlugins().add(HtmlCompilerPlugin()) val compiler = K2JVMCompiler() compiler.exec(System.out, args) } -} \ No newline at end of file +}