Simplify logic for last lambda fixer and set caret directly in first pass

This commit is contained in:
Kirill Rakhman
2016-03-09 12:35:48 +01:00
parent f7af927e8f
commit 828a7de980
2 changed files with 6 additions and 21 deletions
@@ -19,11 +19,9 @@ package org.jetbrains.kotlin.idea.editor.fixers
import com.intellij.lang.SmartEnterProcessorWithFixers import com.intellij.lang.SmartEnterProcessorWithFixers
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtCallExpression import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
@@ -31,15 +29,8 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class KotlinLastLambdaParameterFixer : SmartEnterProcessorWithFixers.Fixer<KotlinSmartEnterHandler>() { class KotlinLastLambdaParameterFixer : SmartEnterProcessorWithFixers.Fixer<KotlinSmartEnterHandler>() {
override fun apply(editor: Editor, processor: KotlinSmartEnterHandler, element: PsiElement) { override fun apply(editor: Editor, processor: KotlinSmartEnterHandler, element: PsiElement) {
if (element is KtCallExpression) { if (element !is KtCallExpression) return
addBracesIfNecessary(editor, element, processor)
}
else if (element is LeafPsiElement) {
registerErrorIfNecessary(element, processor)
}
}
private fun addBracesIfNecessary(editor: Editor, element: KtCallExpression, processor: KotlinSmartEnterHandler) {
val bindingContext = element.analyze(BodyResolveMode.PARTIAL) val bindingContext = element.analyze(BodyResolveMode.PARTIAL)
val resolvedCall = element.getResolvedCall(bindingContext) ?: return val resolvedCall = element.getResolvedCall(bindingContext) ?: return
@@ -56,16 +47,10 @@ class KotlinLastLambdaParameterFixer : SmartEnterProcessorWithFixers.Fixer<Kotli
offset++ offset++
} }
doc.insertString(offset, "{ }") doc.insertString(offset, "{ }")
processor.registerUnresolvedError(offset + 2) processor.registerUnresolvedError(offset + 2)
processor.commit(editor)
} }
} }
} }
private fun registerErrorIfNecessary(element: LeafPsiElement, processor: KotlinSmartEnterHandler) {
if(element.elementType != KtTokens.LBRACE) return
if(element.parent?.parent?.parent?.parent !is KtCallExpression) return
processor.registerUnresolvedError(element.endOffset + 1)
}
} }
@@ -1294,12 +1294,12 @@ class SmartEnterTest : KotlinLightCodeInsightFixtureTestCase() {
fun foo(a: Any, block: () -> Unit) { fun foo(a: Any, block: () -> Unit) {
} }
fun test() { fun test() {
foo(Any()) { <caret> } foo(Any()) { <caret>}
} }
""" """
) )
fun testExtensionLambdaParamImplicit1() = doFileTest( fun testExtensionLambdaParam() = doFileTest(
""" """
fun foo(a: Any, block: Any.() -> Unit) { fun foo(a: Any, block: Any.() -> Unit) {
} }
@@ -1312,7 +1312,7 @@ class SmartEnterTest : KotlinLightCodeInsightFixtureTestCase() {
fun foo(a: Any, block: Any.() -> Unit) { fun foo(a: Any, block: Any.() -> Unit) {
} }
fun test() { fun test() {
foo(Any()) { <caret> } foo(Any()) { <caret>}
} }
""" """
) )