Merge branch 'master' of github.com:JetBrains/kotlin

This commit is contained in:
James Strachan
2012-04-26 06:24:02 +01:00
229 changed files with 4414 additions and 2040 deletions
+27
View File
@@ -124,3 +124,30 @@ public inline fun <T> Array<T>.isEmpty() : Boolean = this.size == 0
/** Returns the array if its not null or else returns an empty array */
public inline fun <T> Array<T>?.orEmpty() : Array<T> = if (this != null) this else array<T>()
public inline val BooleanArray.lastIndex : Int
get() = this.size - 1
public inline val ByteArray.lastIndex : Int
get() = this.size - 1
public inline val ShortArray.lastIndex : Int
get() = this.size - 1
public inline val IntArray.lastIndex : Int
get() = this.size - 1
public inline val LongArray.lastIndex : Int
get() = this.size - 1
public inline val FloatArray.lastIndex : Int
get() = this.size - 1
public inline val DoubleArray.lastIndex : Int
get() = this.size - 1
public inline val CharArray.lastIndex : Int
get() = this.size - 1
public inline val <in T: Any?> Array<T>.lastIndex : Int
get() = this.size - 1
+7 -1
View File
@@ -60,6 +60,8 @@ public inline fun String.format(format : String, vararg args : Any?) : String =
public inline fun String.split(regex : String) : Array<String> = (this as java.lang.String).split(regex) as Array<String>
public inline fun String.split(ch : Char) : Array<String> = (this as java.lang.String).split(java.util.regex.Pattern.quote(ch.toString())) as Array<String>
public inline fun String.substring(beginIndex : Int) : String = (this as java.lang.String).substring(beginIndex).sure()
public inline fun String.substring(beginIndex : Int, endIndex : Int) : String = (this as java.lang.String).substring(beginIndex, endIndex).sure()
@@ -68,10 +70,14 @@ public inline fun String.startsWith(prefix: String) : Boolean = (this as java.la
public inline fun String.startsWith(prefix: String, toffset: Int) : Boolean = (this as java.lang.String).startsWith(prefix, toffset)
public inline fun String.startsWith(ch: Char) : Boolean = (this as java.lang.String).startsWith(ch.toString())
public inline fun String.contains(seq: CharSequence) : Boolean = (this as java.lang.String).contains(seq)
public inline fun String.endsWith(suffix: String) : Boolean = (this as java.lang.String).endsWith(suffix)
public inline fun String.endsWith(ch: Char) : Boolean = (this as java.lang.String).endsWith(ch.toString())
inline val String.size : Int
get() = length()
@@ -226,4 +232,4 @@ public inline fun String.count(predicate: (Char) -> Boolean): Int {
}
}
return answer
}
}
+55
View File
@@ -3,6 +3,8 @@ package kotlin.io
import java.io.*
import java.nio.charset.*
import java.util.NoSuchElementException
import java.util.List
import java.util.ArrayList
import java.net.URL
@@ -147,3 +149,56 @@ public fun File.copyTo(file: File, bufferSize: Int = defaultBufferSize): Long {
}
}
}
/**
* Reads file by byte blocks and calls closure for each block read. Block size depends on implementation but never less than 512.
* This functions passes byte array and amount of bytes in this buffer to the closure function.
*
* You can use this function for huge files
*/
fun File.forEachBlock(closure : (ByteArray, Int) -> Unit) : Unit {
val arr = ByteArray(4096)
val fis = FileInputStream(this)
try {
do {
val size = fis.read(arr)
if (size == -1) {
break
} else if (size > 0) {
closure(arr, size)
}
} while(true)
} finally {
fis.close()
}
}
/**
* Reads file line by line. Default charset is UTF-8.
*
* You may use this function on huge files
*/
fun File.forEachLine (charset : String = "UTF-8", closure : (line : String) -> Unit) : Unit {
val reader = BufferedReader(InputStreamReader(FileInputStream(this), charset))
try {
reader.forEachLine(closure)
} finally {
reader.close()
}
}
/**
* Reads file content as strings list. By default uses UTF-8 charset.
*
* Do not use this function for huge files.
*/
fun File.readLines(charset : String = "UTF-8") : List<String> {
val rs = ArrayList<String>()
this.forEachLine(charset) { (line : String) : Unit ->
rs.add(line);
}
return rs
}
+18
View File
@@ -39,4 +39,22 @@ class ArraysTest() : TestCase() {
assertFalse(iter.hasNext, "Invalid length (hasNext)")
}
fun testEmptyArrayLastIndex() {
val arr1 = IntArray(0)
assertEquals(-1, arr1.lastIndex)
val arr2 = Array<String>(0, {"$it"})
assertEquals(-1, arr2.lastIndex)
}
fun testArrayLastIndex() {
val arr1 = intArray(0, 1, 2, 3, 4)
assertEquals(4, arr1.lastIndex)
assertEquals(4, arr1[arr1.lastIndex])
val arr2 = Array<String>(5, {"$it"})
assertEquals(4, arr2.lastIndex)
assertEquals("4", arr2[arr2.lastIndex])
}
}
+26
View File
@@ -52,4 +52,30 @@ class StringTest {
assertEquals(3, whitespaceCount)
}
test fun testSplitByChar() {
val s = "ab\n[|^$&\\]^cd"
var list = s.split('b');
assertEquals(2, list.size)
assertEquals("a", list[0])
assertEquals("\n[|^$&\\]^cd", list[1])
list = s.split('^')
assertEquals(3, list.size)
assertEquals("cd", list[2])
list = s.split('.')
assertEquals(1, list.size)
assertEquals(s, list[0])
}
test fun testStartsWithChar() {
assertTrue("abcd".startsWith('a'))
assertFalse("abcd".startsWith('b'))
assertFalse("".startsWith('a'))
}
test fun testEndsWithChar() {
assertTrue("abcd".endsWith('d'))
assertFalse("abcd".endsWith('b'))
assertFalse("".endsWith('a'))
}
}
@@ -17,8 +17,8 @@
package org.jetbrains.kotlin.maven.doc;
import org.apache.maven.plugin.MojoExecutionException;
import org.jetbrains.jet.cli.CompilerArguments;
import org.jetbrains.jet.cli.KotlinCompiler;
import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments;
import org.jetbrains.jet.cli.jvm.K2JVMCompiler;
import org.jetbrains.kotlin.doc.KDocArguments;
import org.jetbrains.kotlin.doc.KDocCompiler;
import org.jetbrains.kotlin.doc.KDocConfig;
@@ -164,17 +164,17 @@ public class KDocMojo extends KotlinCompileMojoBase {
private Map<String, String> packageSummaryText;
@Override
protected KotlinCompiler createCompiler() {
protected K2JVMCompiler createCompiler() {
return new KDocCompiler();
}
@Override
protected CompilerArguments createCompilerArguments() {
protected K2JVMCompilerArguments createCompilerArguments() {
return new KDocArguments();
}
@Override
protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException {
protected void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException {
configureBaseCompilerArguments(getLog(), arguments, docModule, sources, classpath, output);
if (arguments instanceof KDocArguments) {
@@ -15,7 +15,7 @@ import java.util.*
import com.intellij.psi.PsiElement
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.resolve.BindingContext.*
import org.jetbrains.jet.compiler.CompilerPlugin
import org.jetbrains.jet.cli.common.CompilerPlugin
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor
import org.jetbrains.jet.lexer.JetTokens
@@ -15,7 +15,7 @@ import java.util.Collection
import com.intellij.psi.PsiElement
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.resolve.BindingContext.*
import org.jetbrains.jet.compiler.CompilerPlugin
import org.jetbrains.jet.cli.common.CompilerPlugin
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor
import org.jetbrains.jet.lexer.JetTokens
@@ -2,25 +2,25 @@ 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.CompileEnvironmentConfiguration
import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments
import org.jetbrains.jet.cli.jvm.K2JVMCompiler
import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentConfiguration
import org.jetbrains.kotlin.doc.highlighter.HtmlCompilerPlugin
/**
* Main for running the KDocCompiler
*/
fun main(args: Array<String?>): Unit {
KotlinCompiler.doMain(KDocCompiler(), args);
K2JVMCompiler.doMain(KDocCompiler(), args);
}
/**
* A version of the [[KotlinCompiler]] which includes the [[KDoc]] compiler plugin and allows
* A version of the [[K2JVMCompiler]] which includes the [[KDoc]] compiler plugin and allows
* command line validation or for the configuration to be provided via [[KDocArguments]]
*/
class KDocCompiler() : KotlinCompiler() {
class KDocCompiler() : K2JVMCompiler() {
protected override fun configureEnvironment(configuration : CompileEnvironmentConfiguration?, arguments : CompilerArguments?) {
protected override fun configureEnvironment(configuration : CompileEnvironmentConfiguration?, arguments : K2JVMCompilerArguments?) {
super.configureEnvironment(configuration, arguments)
val coreEnvironment = configuration?.getEnvironment()
if (coreEnvironment != null) {
@@ -38,7 +38,7 @@ class KDocCompiler() : KotlinCompiler() {
}
}
protected override fun createArguments() : CompilerArguments? {
protected override fun createArguments() : K2JVMCompilerArguments? {
return KDocArguments()
}
@@ -47,7 +47,7 @@ class KDocCompiler() : KotlinCompiler() {
}
}
class KDocArguments() : CompilerArguments() {
class KDocArguments() : K2JVMCompilerArguments() {
public var docConfig: KDocConfig = KDocConfig()
@@ -1,7 +1,7 @@
package org.jetbrains.kotlin.doc.highlighter
import org.jetbrains.jet.compiler.CompilerPlugin
import org.jetbrains.jet.compiler.CompilerPluginContext
import org.jetbrains.jet.cli.common.CompilerPlugin
import org.jetbrains.jet.cli.common.CompilerPluginContext
/**
*/
@@ -1,7 +1,7 @@
package org.jetbrains.kotlin.doc.model
import org.jetbrains.jet.compiler.CompilerPlugin
import org.jetbrains.jet.compiler.CompilerPluginContext
import org.jetbrains.jet.cli.common.CompilerPlugin
import org.jetbrains.jet.cli.common.CompilerPluginContext
import org.jetbrains.kotlin.doc.KDocConfig
/** Base class for any compiler plugin which needs to process a KModel */
@@ -2,8 +2,8 @@ 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.jet.cli.jvm.K2JVMCompilerArguments
import org.jetbrains.jet.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.doc.highlighter.HtmlCompilerPlugin
import org.junit.Test
@@ -20,12 +20,12 @@ class HtmlVisitorTest {
val outDir = File(dir, "target/htmldocs")
println("Generating source HTML to $outDir")
val args = CompilerArguments()
val args = K2JVMCompilerArguments()
args.setSrc(srcDir.toString())
args.setOutputDir(File(dir, "target/classes-htmldocs").toString())
args.getCompilerPlugins()?.add(HtmlCompilerPlugin())
val compiler = KotlinCompiler()
val compiler = K2JVMCompiler()
compiler.exec(System.out, args)
}
}
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.maven;
import org.apache.maven.plugin.MojoExecutionException;
import org.jetbrains.jet.cli.CompilerArguments;
import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments;
/**
* Converts Kotlin to JavaScript code
@@ -36,7 +36,7 @@ public class K2JSCompilerMojo extends KotlinCompileMojo {
private String outFile;
@Override
protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException {
protected void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException {
super.configureCompilerArguments(arguments);
K2JSCompilerPlugin plugin = new K2JSCompilerPlugin();
@@ -19,8 +19,8 @@ package org.jetbrains.kotlin.maven;
import com.google.common.io.Files;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.compiler.CompilerPlugin;
import org.jetbrains.jet.compiler.CompilerPluginContext;
import org.jetbrains.jet.cli.common.CompilerPlugin;
import org.jetbrains.jet.cli.common.CompilerPluginContext;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.k2js.config.Config;
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.maven;
import org.apache.maven.plugin.MojoExecutionException;
import org.jetbrains.jet.cli.CompilerArguments;
import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments;
/**
* Compiles kotlin sources
@@ -29,7 +29,7 @@ import org.jetbrains.jet.cli.CompilerArguments;
*/
public class KotlinCompileMojo extends KotlinCompileMojoBase {
@Override
protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException {
protected void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException {
configureBaseCompilerArguments(getLog(), arguments, module, sources, classpath, output);
}
}
@@ -23,8 +23,10 @@ import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.jetbrains.jet.cli.CompilerArguments;
import org.jetbrains.jet.cli.KotlinCompiler;
import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments;
import org.jetbrains.jet.cli.jvm.K2JVMCompiler;
import org.jetbrains.jet.cli.common.ExitCode.*;
import org.jetbrains.jet.cli.common.ExitCode;
import java.io.File;
import java.io.IOException;
@@ -104,15 +106,15 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
final CompilerArguments arguments = createCompilerArguments();
final K2JVMCompilerArguments arguments = createCompilerArguments();
configureCompilerArguments(arguments);
final KotlinCompiler compiler = createCompiler();
final K2JVMCompiler compiler = createCompiler();
printCompilerArgumentsIfDebugEnabled(arguments, compiler);
final KotlinCompiler.ExitCode exitCode = compiler.exec(System.err, arguments);
final ExitCode exitCode = compiler.exec(System.err, arguments);
switch (exitCode) {
case COMPILATION_ERROR:
@@ -123,7 +125,7 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
}
}
private void printCompilerArgumentsIfDebugEnabled(CompilerArguments arguments, KotlinCompiler compiler) {
private void printCompilerArgumentsIfDebugEnabled(K2JVMCompilerArguments arguments, K2JVMCompiler compiler) {
if (getLog().isDebugEnabled()) {
getLog().debug("Invoking compiler " + compiler + " with arguments:");
try {
@@ -142,24 +144,24 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
}
}
protected KotlinCompiler createCompiler() {
return new KotlinCompiler();
protected K2JVMCompiler createCompiler() {
return new K2JVMCompiler();
}
/**
* Derived classes can create custom compiler argument implementations
* such as for KDoc
*/
protected CompilerArguments createCompilerArguments() {
return new CompilerArguments();
protected K2JVMCompilerArguments createCompilerArguments() {
return new K2JVMCompilerArguments();
}
/**
* Derived classes can register custom plugins or configurations
*/
protected abstract void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException;
protected abstract void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException;
protected void configureBaseCompilerArguments(Log log, CompilerArguments arguments, String module,
protected void configureBaseCompilerArguments(Log log, K2JVMCompilerArguments arguments, String module,
List<String> sources, List<String> classpath, String output) throws MojoExecutionException {
// don't include runtime, it should be in maven dependencies
arguments.mode = "stdlib";
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.maven;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.jetbrains.jet.cli.CompilerArguments;
import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments;
/**
* Compiles Kotlin test sources
@@ -47,7 +47,7 @@ public class KotlinTestCompileMojo extends KotlinCompileMojoBase {
}
@Override
protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException {
protected void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException {
configureBaseCompilerArguments(
getLog(), arguments,
testModule, testSources, testClasspath, testOutput);