Accurate work with adding/removing context of tests.

Need this because of KEEP_FIRST_COLUMN_COMMENT setting. Comments accidentally left on first
collumn cause the bad formatting.

 #KT-4175 Fixed
This commit is contained in:
Nikolay Krasko
2016-03-14 17:09:54 +03:00
parent 8ad0c1ac01
commit 2db888a646
@@ -89,7 +89,8 @@ abstract class AbstractJavaToKotlinConverterSingleFileTest : AbstractJavaToKotli
} }
private fun reformat(text: String, project: Project, inFunContext: Boolean): String { private fun reformat(text: String, project: Project, inFunContext: Boolean): String {
val textToFormat = if (inFunContext) "fun convertedTemp() {\n$text\n}" else text val funBody = text.lines().joinToString(separator = "\n", transform = { " $it" })
val textToFormat = if (inFunContext) "fun convertedTemp() {\n$funBody\n}" else text
val convertedFile = KotlinTestUtils.createFile("converted", textToFormat, project) val convertedFile = KotlinTestUtils.createFile("converted", textToFormat, project)
WriteCommandAction.runWriteCommandAction(project) { WriteCommandAction.runWriteCommandAction(project) {
@@ -111,18 +112,31 @@ abstract class AbstractJavaToKotlinConverterSingleFileTest : AbstractJavaToKotli
} }
private fun methodToKotlin(text: String, settings: ConverterSettings, project: Project): String { private fun methodToKotlin(text: String, settings: ConverterSettings, project: Project): String {
val result = fileToKotlin("final class C {$text}", settings, project).replace("internal class C {", "").replace("internal object C {", "") val result = fileToKotlin("final class C {$text}", settings, project)
return result.substring(0, (result.lastIndexOf("}"))).trim() return result
.substringBeforeLast("}")
.replace("internal class C {", "\n")
.replace("internal object C {", "\n")
.trimIndent().trim()
} }
private fun statementToKotlin(text: String, settings: ConverterSettings, project: Project): String { private fun statementToKotlin(text: String, settings: ConverterSettings, project: Project): String {
val result = methodToKotlin("void main() {$text}", settings, project) val funBody = text.lines().joinToString(separator = "\n", transform = { " $it" })
return result.substring(0, result.lastIndexOf("}")).replaceFirst("fun main() {", "").trim() val result = methodToKotlin("void main() {\n$funBody\n}", settings, project)
return result
.substringBeforeLast("}")
.replaceFirst("fun main() {", "\n")
.trimIndent().trim()
} }
private fun expressionToKotlin(code: String, settings: ConverterSettings, project: Project): String { private fun expressionToKotlin(code: String, settings: ConverterSettings, project: Project): String {
val result = statementToKotlin("final Object o =$code}", settings, project) val result = statementToKotlin("final Object o =$code}", settings, project)
return result.replaceFirst("val o:Any? = ", "").replaceFirst("val o:Any = ", "").replaceFirst("val o = ", "").trim() return result
.replaceFirst("val o:Any? = ", "")
.replaceFirst("val o:Any = ", "")
.replaceFirst("val o = ", "")
.trim()
} }
override fun getProjectDescriptor() override fun getProjectDescriptor()