Comments and formatting preseving added

This commit is contained in:
Valentin Kipyatkov
2015-10-20 19:07:03 +03:00
parent 7c8967ea4e
commit 612f9b1041
10 changed files with 56 additions and 10 deletions
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.references.ReferenceAccess
import org.jetbrains.kotlin.idea.references.readWriteAccess
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElementSelector
@@ -86,6 +87,7 @@ public class OperatorToFunctionIntention : JetSelfTargetingIntention<KtExpressio
return false
}
//TODO: don't use creation by plain text
private fun convertPrefix(element: KtPrefixExpression): KtExpression {
val op = element.getOperationReference().getReferencedNameElementType()
val base = element.getBaseExpression()!!.getText()
@@ -104,6 +106,7 @@ public class OperatorToFunctionIntention : JetSelfTargetingIntention<KtExpressio
return element.replace(transformed) as KtExpression
}
//TODO: don't use creation by plain text
private fun convertPostFix(element: KtPostfixExpression): KtExpression {
val op = element.getOperationReference().getReferencedNameElementType()
val base = element.getBaseExpression()!!.getText()
@@ -119,6 +122,7 @@ public class OperatorToFunctionIntention : JetSelfTargetingIntention<KtExpressio
return element.replace(transformed) as KtExpression
}
//TODO: don't use creation by plain text
private fun convertBinary(element: KtBinaryExpression): KtExpression {
val op = element.getOperationReference().getReferencedNameElementType()
val left = element.getLeft()!!
@@ -173,9 +177,9 @@ public class OperatorToFunctionIntention : JetSelfTargetingIntention<KtExpressio
appendFixedText(".")
val parent = element.parent
if (parent is KtBinaryExpression && parent.operationReference.getReferencedNameElementType() == KtTokens.EQ && element == parent.left) {
expressionToReplace = parent
if (isAssignmentLeftSide(element)) {
val parent = element.parent
expressionToReplace = parent as KtBinaryExpression
appendFixedText("set(")
appendExpressions(element.indexExpressions)
@@ -193,6 +197,12 @@ public class OperatorToFunctionIntention : JetSelfTargetingIntention<KtExpressio
return expressionToReplace.replace(transformed) as KtExpression
}
private fun isAssignmentLeftSide(element: KtArrayAccessExpression): Boolean {
val parent = element.parent
return parent is KtBinaryExpression && parent.operationReference.getReferencedNameElementType() == KtTokens.EQ && element == parent.left
}
//TODO: don't use creation by plain text
private fun convertCall(element: KtCallExpression): KtExpression {
val callee = element.getCalleeExpression()!!
val arguments = element.getValueArgumentList()
@@ -207,6 +217,13 @@ public class OperatorToFunctionIntention : JetSelfTargetingIntention<KtExpressio
}
public fun convert(element: KtExpression): Pair<KtExpression, KtSimpleNameExpression> {
var elementToBeReplaced = element
if (element is KtArrayAccessExpression && isAssignmentLeftSide(element)) {
elementToBeReplaced = element.parent as KtExpression
}
val commentSaver = CommentSaver(elementToBeReplaced, saveLineBreaks = true)
val result = when (element) {
is KtPrefixExpression -> convertPrefix(element)
is KtPostfixExpression -> convertPostFix(element)
@@ -216,6 +233,8 @@ public class OperatorToFunctionIntention : JetSelfTargetingIntention<KtExpressio
else -> throw IllegalArgumentException(element.toString())
}
commentSaver.restore(result)
val callName = findCallName(result)
?: error("No call name found in ${result.text}")
return result to callName
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.core
package org.jetbrains.kotlin.idea.util
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.TextRange
@@ -24,8 +24,7 @@ import org.jetbrains.kotlin.lexer.KtToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.psiUtil.*
import java.util.ArrayList
import java.util.HashMap
import java.util.*
import kotlin.properties.Delegates
public class CommentSaver(originalElements: PsiChildRange, private val saveLineBreaks: Boolean = false/*TODO?*/) {
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.CommentSaver
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
@@ -20,7 +20,7 @@ import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.CommentSaver
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.idea.core.canOmitDeclaredType
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.lexer.KtTokens
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.core.CommentSaver
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
@@ -21,7 +21,7 @@ import com.intellij.openapi.util.TextRange
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.idea.conversion.copy.range
import org.jetbrains.kotlin.idea.conversion.copy.start
import org.jetbrains.kotlin.idea.core.CommentSaver
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference
import org.jetbrains.kotlin.idea.core.*
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArgumentsIntention
import org.jetbrains.kotlin.idea.intentions.setType
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
import org.jetbrains.kotlin.idea.util.ShortenReferences
@@ -0,0 +1,11 @@
interface C {
operator fun set(p1: String, p2: String, value: Int)
}
class D(val c: C) {
fun foo() {
this.c/* and this is c */[<caret>
"a", // it's "a"
"b" /* and this is "b" */] = /* we use 10 */10
}
}
@@ -0,0 +1,10 @@
interface C {
operator fun set(p1: String, p2: String, value: Int)
}
class D(val c: C) {
fun foo() {
this.c/* and this is c */.set("a", // it's "a"
"b" /* and this is "b" */, /* we use 10 */10)
}
}
@@ -5943,6 +5943,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("keepComments.kt")
public void testKeepComments() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/operatorToFunction/keepComments.kt");
doTest(fileName);
}
@TestMetadata("notApplicableAssignment.kt")
public void testNotApplicableAssignment() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/operatorToFunction/notApplicableAssignment.kt");