Fixed EA-73613

This commit is contained in:
Valentin Kipyatkov
2016-02-08 21:26:18 +03:00
parent aed2f2b993
commit 8056c5182a
17 changed files with 50 additions and 23 deletions
+1
View File
@@ -17,6 +17,7 @@
<w>renderers</w>
<w>rparenth</w>
<w>selectioner</w>
<w>summand</w>
<w>unpluralize</w>
<w>weighers</w>
</words>
@@ -56,11 +56,19 @@ class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention
private fun isApplicableToNoParentCheck(expression: KtBinaryExpression): Boolean {
if (expression.operationToken != KtTokens.PLUS) return false
if (!KotlinBuiltIns.isString(expression.analyze().getType(expression))) return false
val expressionType = expression.analyze(BodyResolveMode.PARTIAL).getType(expression)
if (!KotlinBuiltIns.isString(expressionType)) return false
return isSuitable(expression)
}
val left = expression.left ?: return false
val right = expression.right ?: return false
return !PsiUtilCore.hasErrorElementChild(left) && !PsiUtilCore.hasErrorElementChild(right)
private fun isSuitable(expression: KtExpression): Boolean {
if (expression is KtBinaryExpression && expression.operationToken == KtTokens.PLUS) {
return isSuitable(expression.left ?: return false) && isSuitable(expression.right ?: return false)
}
if (PsiUtilCore.hasErrorElementChild(expression)) return false
if (expression.textContains('\n')) return false
return true
}
private fun buildReplacement(expression: KtBinaryExpression): KtStringTemplateExpression {
@@ -121,6 +129,6 @@ class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention
return "$" + (if (forceBraces) "{$expressionText}" else expressionText)
}
return "\${" + expressionText.replace("\n+".toRegex(), " ") + "}"
return "\${$expressionText}"
}
}
@@ -1,3 +1,4 @@
// IS_APPLICABLE: false
fun main(args: Array<String>){
val x = "foo" +<caret> """bar\n
"""
@@ -1,3 +0,0 @@
fun main(args: Array<String>){
val x = "foobar\\n\n "
}
@@ -0,0 +1 @@
val v = "a" + (<caret>1.hashCode() /*comment*/ * 2)
@@ -0,0 +1 @@
val v = "a${1.hashCode() /*comment*/ * 2}"
@@ -1,4 +1,5 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun main(args: Array<String>){
val y = "abcd" +<caret> listOf( 1,
@@ -1,4 +0,0 @@
// WITH_RUNTIME
fun main(args: Array<String>){
val y = "abcd${listOf(1, 2)}"
}
@@ -0,0 +1,3 @@
// IS_APPLICABLE: false
val v = "a" + (<caret>1.hashCode() // comment
* 2)
@@ -1,9 +1,9 @@
<problems>
<problem>
<file>multilineString.kt</file>
<line>3</line>
<file>simple.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="multilineString.kt" />
<entry_point TYPE="file" FQNAME="simple.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert string concatenation to string template</problem_class>
<description>Convert concatenation to template</description>
</problem>
@@ -1,4 +1,5 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun main(args: Array<String>){
val y = "abcd" +<caret> listOf( 1,
2 )
@@ -1,4 +0,0 @@
// WITH_RUNTIME
fun main(args: Array<String>){
val y = "abcd${listOf(1, 2)}"
}
@@ -1,3 +1,4 @@
// IS_APPLICABLE: false
fun main(args: Array<String>){
var r = "a"
val x = "foo" +<caret> """bar
@@ -1,4 +0,0 @@
fun main(args: Array<String>){
var r = "a"
val x = "foobar\n $r"
}
@@ -0,0 +1,3 @@
fun foo(p: Int) {
val v = <caret>"(" + p + ")"
}
@@ -0,0 +1,3 @@
fun foo(p: Int) {
val v = <caret>"($p)"
}
@@ -4561,6 +4561,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("comment.kt")
public void testComment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/comment.kt");
doTest(fileName);
}
@TestMetadata("consecutiveNewlines.kt")
public void testConsecutiveNewlines() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/consecutiveNewlines.kt");
@@ -4579,6 +4585,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("endOfLineComment.kt")
public void testEndOfLineComment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/endOfLineComment.kt");
doTest(fileName);
}
@TestMetadata("insertBracesForSimpleNamedExpression.kt")
public void testInsertBracesForSimpleNamedExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/insertBracesForSimpleNamedExpression.kt");
@@ -4699,6 +4711,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/simple.kt");
doTest(fileName);
}
@TestMetadata("specialCharsInCharLiteral.kt")
public void testSpecialCharsInCharLiteral() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToStringTemplate/specialCharsInCharLiteral.kt");