Run formatter in function context for converted expression and statements

This commit is contained in:
Nikolay Krasko
2014-04-25 15:39:48 +04:00
parent f11e35f988
commit 13dce4dd11
75 changed files with 331 additions and 343 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -36,6 +36,8 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.application.Result
import java.io.BufferedReader
import java.io.StringReader
public abstract class AbstractJavaToKotlinConverterPluginTest() : AbstractJavaToKotlinConverterTest("ide.kt", PluginSettings)
public abstract class AbstractJavaToKotlinConverterBasicTest() : AbstractJavaToKotlinConverterTest("kt", TestSettings)
@@ -66,18 +68,32 @@ abstract class AbstractJavaToKotlinConverterTest(
else -> throw IllegalStateException("Specify what is it: file, class, method, statement or expression " +
"using the first line of test data file")
}
val actual = reformat(rawConverted, project)
val reformatInFun = when (prefix) {
"element", "expression", "statement" -> true
else -> false
}
val actual = reformat(rawConverted, project, reformatInFun)
val kotlinPath = javaPath.replace(".java", ".$kotlinFileExtension")
val expectedFile = File(kotlinPath)
JetTestUtils.assertEqualsToFile(expectedFile, actual)
}
private fun reformat(text: String, project: Project): String {
val convertedFile = JetTestUtils.createFile("converted", text, project)
private fun reformat(text: String, project: Project, inFunContext: Boolean): String {
val textToFormat = if (inFunContext) "fun convertedTemp() {\n$text\n}" else text
val convertedFile = JetTestUtils.createFile("converted", textToFormat, project)
WriteCommandAction.runWriteCommandAction(project) {
CodeStyleManager.getInstance(project)!!.reformat(convertedFile)
}
return convertedFile.getText()!!
val reformattedText = convertedFile.getText()!!
return if (inFunContext)
reformattedText.removeFirstLine().removeLastLine().trimIndent()
else
reformattedText
}
private fun elementToKotlin(converter: Converter, text: String): String {
@@ -121,4 +137,42 @@ abstract class AbstractJavaToKotlinConverterTest(
override fun getProjectJDK(): Sdk? {
return PluginTestCaseBase.jdkFromIdeaHome()
}
private fun String.removeFirstLine(): String {
val lastNewLine = indexOf('\n')
return if (lastNewLine == -1) "" else substring(lastNewLine)
}
private fun String.removeLastLine(): String {
val lastNewLine = lastIndexOf('\n')
return if (lastNewLine == -1) "" else substring(0, lastNewLine)
}
private fun String.trimIndent(): String {
val lines = split('\n')
val firstNonEmpty = lines.firstOrNull { !it.trim().isEmpty() }
if (firstNonEmpty == null) {
return this
}
val trimmedPrefix = firstNonEmpty.takeWhile { ch -> ch.isWhitespace() }
if (trimmedPrefix.isEmpty()) {
return this
}
return lines.map { line ->
if (line.trim().isEmpty()) {
""
}
else {
if (!line.startsWith(trimmedPrefix)) {
throw IllegalArgumentException(
"""Invalid line "$line", ${trimmedPrefix.size} whitespace character are expected""")
}
line.substring(trimmedPrefix.length)
}
}.makeString(separator = "\n")
}
}
@@ -1 +1 @@
assert(true) {"string details"}
assert(true) { "string details" }
@@ -1 +1 @@
assert(true) {"string details"}
assert(true) { "string details" }
@@ -1,4 +1,4 @@
(if (a.isEmpty())
0
0
else
1)
1)
@@ -1,4 +1,4 @@
(if (a.isEmpty())
0
0
else
1)
1)
@@ -1,6 +1,4 @@
do
{
val i = 1
i = i + 1
}
while (a > b)
do {
val i = 1
i = i + 1
} while (a > b)
@@ -1,6 +1,4 @@
do
{
var i: Int = 1
i = i + 1
}
while (a > b)
do {
var i: Int = 1
i = i + 1
} while (a > b)
@@ -1,3 +1,2 @@
do
{}
while (true)
do {
} while (true)
@@ -1,3 +1,2 @@
do
{}
while (true)
do {
} while (true)
@@ -1,3 +1,3 @@
do
i = i + 1
i = i + 1
while (true)
@@ -1,3 +1,3 @@
do
i = i + 1
i = i + 1
while (true)
@@ -1,3 +1,3 @@
do
return 1
return 1
while (true)
@@ -1,3 +1,3 @@
do
return 1
return 1
while (true)
@@ -1,10 +1,9 @@
{
init()
while (condition())
{
body()
{
update()
}
}
init()
while (condition()) {
body()
{
update()
}
}
}
@@ -1,10 +1,9 @@
{
init()
while (condition())
{
body()
{
update()
}
}
init()
while (condition()) {
body()
{
update()
}
}
}
@@ -1,4 +1,4 @@
val array = IntArray(10)
for (i in 0..10) {
array[i] = i
array[i] = i
}
+1 -1
View File
@@ -1,4 +1,4 @@
var array: IntArray? = IntArray(10)
for (i in 0..10) {
array[i] = i
array[i] = i
}
@@ -1,4 +1,4 @@
val array = IntArray(10)
for (i in 0..10 - 1) {
array[i] = i
array[i] = i
}
+1 -1
View File
@@ -1,4 +1,4 @@
var array: IntArray? = IntArray(10)
for (i in 0..10 - 1) {
array[i] = i
array[i] = i
}
@@ -1,4 +1,4 @@
val array = IntArray(10)
for (i in 0..10 - 1) {
array[i] = i
array[i] = i
}
+1 -1
View File
@@ -1,4 +1,4 @@
var array: IntArray? = IntArray(10)
for (i in 0..10 - 1) {
array[i] = i
array[i] = i
}
@@ -1,14 +1,13 @@
{
val i = 0
while (i <0)
{
{
val i = 1
i++
}
{
j++
i++
}
}
val i = 0
while (i < 0) {
{
val i = 1
i++
}
{
j++
i++
}
}
}
@@ -1,14 +1,13 @@
{
var i: Int = 0
while (i <0)
{
{
var i: Int = 1
i++
}
{
j++
i++
}
}
var i: Int = 0
while (i < 0) {
{
var i: Int = 1
i++
}
{
j++
i++
}
}
}
@@ -1,11 +1,10 @@
{
val i = 0
while (i <0)
{
{}
{
j++
i++
}
}
val i = 0
while (i < 0) {
{ }
{
j++
i++
}
}
}
@@ -1,11 +1,10 @@
{
var i: Int = 0
while (i <0)
{
{}
{
j++
i++
}
}
var i: Int = 0
while (i < 0) {
{ }
{
j++
i++
}
}
}
@@ -1,10 +1,9 @@
{
init()
while (true)
{
body()
{
update()
}
}
init()
while (true) {
body()
{
update()
}
}
}
@@ -1,10 +1,9 @@
{
init()
while (true)
{
body()
{
update()
}
}
init()
while (true) {
body()
{
update()
}
}
}
@@ -1,9 +1,8 @@
{
while (condition())
{
body()
{
update()
}
}
while (condition()) {
body()
{
update()
}
}
}
@@ -1,9 +1,8 @@
{
while (condition())
{
body()
{
update()
}
}
while (condition()) {
body()
{
update()
}
}
}
@@ -1,7 +1,6 @@
{
init()
while (condition())
{
body()
}
init()
while (condition()) {
body()
}
}
@@ -1,7 +1,6 @@
{
init()
while (condition())
{
body()
}
init()
while (condition()) {
body()
}
}
@@ -1,11 +1,10 @@
{
val i = 0
while (i <0)
{
return i
{
j++
i++
}
}
val i = 0
while (i < 0) {
return i
{
j++
i++
}
}
}
+8 -9
View File
@@ -1,11 +1,10 @@
{
var i: Int = 0
while (i <0)
{
return i
{
j++
i++
}
}
var i: Int = 0
while (i < 0) {
return i
{
j++
i++
}
}
}
@@ -1,5 +1,4 @@
for (n in list)
{
val i = 1
i++
for (n in list) {
val i = 1
i++
}
@@ -1,5 +1,4 @@
for (n in list)
{
var i: Int = 1
i++
for (n in list) {
var i: Int = 1
i++
}
@@ -1,2 +1,2 @@
for (n in list)
{}
for (n in list) {
}
@@ -1,2 +1,2 @@
for (n in list)
{}
for (n in list) {
}
@@ -1,2 +1,2 @@
for (n in list)
i++
i++
@@ -1,2 +1,2 @@
for (n in list)
i++
i++
@@ -1,2 +1,2 @@
for (n in list)
return n
return n
@@ -1,2 +1,2 @@
for (n in list)
return n
return n
@@ -1,4 +1,3 @@
if (1 > 0)
{}
else
{}
if (1 > 0) {
} else {
}
@@ -1,4 +1,3 @@
if (1 > 0)
{}
else
{}
if (1 > 0) {
} else {
}
@@ -1,9 +1,6 @@
if (1 > 0)
{
val n = 1
return n
}
else
{
return 0
if (1 > 0) {
val n = 1
return n
} else {
return 0
}
@@ -1,9 +1,6 @@
if (1 > 0)
{
var n: Int = 1
return n
}
else
{
return 0
if (1 > 0) {
var n: Int = 1
return n
} else {
return 0
}
@@ -1,4 +1,4 @@
if (true)
return 1
return 1
else
return 0
return 0
@@ -1,4 +1,4 @@
if (true)
return 1
return 1
else
return 0
return 0
@@ -1,5 +1,4 @@
if (1 > 0)
{
val n = 1
return n
if (1 > 0) {
val n = 1
return n
}
@@ -1,5 +1,4 @@
if (1 > 0)
{
var n: Int = 1
return n
if (1 > 0) {
var n: Int = 1
return n
}
@@ -1,2 +1,2 @@
//element
//file
import java.util.Arrays;
@@ -1,3 +1,3 @@
//element
//file
import java.util.Arrays;
import java.util.concurrent.ArrayBlockingQueue;
@@ -1,16 +1,14 @@
@test for (i in 0..max) {
val n = substring.length()
val j = i
val k = 0
while (n-- != 0)
{
if (searchMe.charAt(j++) != substring.charAt(k++))
{
continue@test
}
}
foundIt = true
break@test
val n = substring.length()
val j = i
val k = 0
while (n-- != 0) {
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue@test
}
}
foundIt = true
break@test
}
System.out.println((if (foundIt)
"Found it"
@@ -1,16 +1,14 @@
@test for (i in 0..max) {
var n: Int = substring.length()
var j: Int = i
var k: Int = 0
while (n-- != 0)
{
if (searchMe.charAt(j++) != substring.charAt(k++))
{
continue@test
}
}
foundIt = true
break@test
var n: Int = substring.length()
var j: Int = i
var k: Int = 0
while (n-- != 0) {
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue@test
}
}
foundIt = true
break@test
}
System.out?.println((if (foundIt)
"Found it"
@@ -1,5 +1,5 @@
object : Runnable() {
override fun run() {
System.out.println("Run")
}
override fun run() {
System.out.println("Run")
}
}
@@ -1,5 +1,5 @@
object : Runnable() {
override fun run() {
System.out?.println("Run")
}
override fun run() {
System.out?.println("Run")
}
}
@@ -1,3 +1,3 @@
val s = null
if (!s.isEmpty())
{}
if (!s.isEmpty()) {
}
@@ -1,3 +1,3 @@
var s: String? = null
if (!s?.isEmpty()!!)
{}
if (!s?.isEmpty()!!) {
}
@@ -1,3 +1,3 @@
synchronized (s) {
doSomething(s)
doSomething(s)
}
@@ -1,3 +1,3 @@
synchronized (s) {
doSomething(s)
doSomething(s)
}
@@ -1,14 +1,9 @@
try
{
callMethod(params)
}
catch (e : Exception) {
println(1)
}
catch (e : IOException) {
println(0)
}
finally
{
println(3)
try {
callMethod(params)
} catch (e: Exception) {
println(1)
} catch (e: IOException) {
println(0)
} finally {
println(3)
}
@@ -1,14 +1,9 @@
try
{
callMethod(params)
}
catch (e : Exception) {
println(1)
}
catch (e : IOException) {
println(0)
}
finally
{
println(3)
try {
callMethod(params)
} catch (e: Exception) {
println(1)
} catch (e: IOException) {
println(0)
} finally {
println(3)
}
@@ -1,10 +1,7 @@
try
{}
catch (e : Exception) {
println(1)
}
catch (e : IOException) {
println(0)
}
finally
{}
try {
} catch (e: Exception) {
println(1)
} catch (e: IOException) {
println(0)
} finally {
}
@@ -1,10 +1,7 @@
try
{}
catch (e : Exception) {
println(1)
}
catch (e : IOException) {
println(0)
}
finally
{}
try {
} catch (e: Exception) {
println(1)
} catch (e: IOException) {
println(0)
} finally {
}
@@ -1,12 +1,8 @@
try
{}
catch (e : Exception) {
println(1)
}
catch (e : IOException) {
println(0)
}
finally
{
println(3)
try {
} catch (e: Exception) {
println(1)
} catch (e: IOException) {
println(0)
} finally {
println(3)
}
@@ -1,12 +1,8 @@
try
{}
catch (e : Exception) {
println(1)
}
catch (e : IOException) {
println(0)
}
finally
{
println(3)
try {
} catch (e: Exception) {
println(1)
} catch (e: IOException) {
println(0)
} finally {
println(3)
}
@@ -1,8 +1,6 @@
try
{}
catch (e : Exception) {
println(1)
}
catch (e : IOException) {
println(0)
try {
} catch (e: Exception) {
println(1)
} catch (e: IOException) {
println(0)
}
@@ -1,8 +1,6 @@
try
{}
catch (e : Exception) {
println(1)
}
catch (e : IOException) {
println(0)
try {
} catch (e: Exception) {
println(1)
} catch (e: IOException) {
println(0)
}
@@ -1,5 +1,4 @@
while (a > b)
{
val i = 1
i = i + 1
while (a > b) {
val i = 1
i = i + 1
}
@@ -1,5 +1,4 @@
while (a > b)
{
var i: Int = 1
i = i + 1
while (a > b) {
var i: Int = 1
i = i + 1
}
@@ -1,2 +1,2 @@
while (true)
{}
while (true) {
}
@@ -1,2 +1,2 @@
while (true)
{}
while (true) {
}
@@ -1,2 +1,2 @@
while (true)
i = i + 1
i = i + 1
@@ -1,2 +1,2 @@
while (true)
i = i + 1
i = i + 1
@@ -1,2 +1,2 @@
while (true)
return 1
return 1
@@ -1,2 +1,2 @@
while (true)
return 1
return 1