@@ -24,11 +24,9 @@ import com.intellij.psi.PsiElement
24
24
import com.intellij.psi.PsiFile
25
25
import com.intellij.util.ExceptionUtil
26
26
import org.jetbrains.kotlin.idea.actions.internal.KotlinInternalMode
27
- import org.jetbrains.kotlin.idea.caches.resolve.analyze
28
27
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
29
28
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils
30
29
import org.jetbrains.kotlin.idea.core.replaced
31
- import org.jetbrains.kotlin.idea.intentions.InsertExplicitTypeArgumentsIntention
32
30
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.*
33
31
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.AnalysisResult.ErrorMessage
34
32
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.AnalysisResult.Status
@@ -42,7 +40,6 @@ import org.jetbrains.kotlin.psi.codeFragmentUtil.suppressDiagnosticsInDebugMode
42
40
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
43
41
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
44
42
import org.jetbrains.kotlin.renderer.DescriptorRenderer
45
- import org.jetbrains.kotlin.resolve.BindingContext.IMPLICIT_RECEIVER_SMARTCAST
46
43
import org.jetbrains.kotlin.resolve.BindingContext.SMARTCAST
47
44
48
45
fun getFunctionForExtractedFragment (
@@ -290,12 +287,12 @@ private fun findElementBefore(contextElement: PsiElement): PsiElement? {
290
287
contextElement is KtProperty && ! contextElement.isLocal -> {
291
288
val delegateExpressionOrInitializer = contextElement.delegateExpressionOrInitializer
292
289
if (delegateExpressionOrInitializer != null ) {
293
- wrapInRunFun (delegateExpressionOrInitializer)
290
+ wrapInLambdaCall (delegateExpressionOrInitializer)
294
291
}
295
292
else {
296
293
val getter = contextElement.getter!!
297
294
if (! getter.hasBlockBody()) {
298
- wrapInRunFun (getter.bodyExpression!! )
295
+ wrapInLambdaCall (getter.bodyExpression!! )
299
296
}
300
297
else {
301
298
(getter.bodyExpression as KtBlockExpression ).statements.first()
@@ -323,7 +320,7 @@ private fun findElementBefore(contextElement: PsiElement): PsiElement? {
323
320
newBlock.rBrace
324
321
}
325
322
contextElement is KtDeclarationWithBody && ! contextElement.hasBlockBody() -> {
326
- wrapInRunFun (contextElement.bodyExpression!! )
323
+ wrapInLambdaCall (contextElement.bodyExpression!! )
327
324
}
328
325
contextElement is KtDeclarationWithBody && contextElement.hasBlockBody() -> {
329
326
val block = contextElement.bodyExpression as KtBlockExpression
@@ -336,7 +333,7 @@ private fun findElementBefore(contextElement: PsiElement): PsiElement? {
336
333
entryExpression.statements.firstOrNull() ? : entryExpression.lastChild
337
334
}
338
335
else {
339
- wrapInRunFun (entryExpression!! )
336
+ wrapInLambdaCall (entryExpression!! )
340
337
}
341
338
}
342
339
else -> {
@@ -345,18 +342,12 @@ private fun findElementBefore(contextElement: PsiElement): PsiElement? {
345
342
}
346
343
}
347
344
348
- private fun replaceByRunFunction (expression : KtExpression ): KtCallExpression {
349
- val callExpression = KtPsiFactory (expression).createExpression(" run { \n ${expression.text} \n }" ) as KtCallExpression
350
- val replaced = expression.replaced(callExpression)
351
- val typeArguments = InsertExplicitTypeArgumentsIntention .createTypeArguments(replaced, replaced.analyze())
352
- if (typeArguments?.arguments?.isNotEmpty() ? : false ) {
353
- val calleeExpression = replaced.calleeExpression
354
- replaced.addAfter(typeArguments!! , calleeExpression)
355
- }
356
- return replaced
345
+ private fun replaceByLambdaCall (expression : KtExpression ): KtCallExpression {
346
+ val callExpression = KtPsiFactory (expression).createExpression(" { \n ${expression.text} \n }()" ) as KtCallExpression
347
+ return expression.replaced(callExpression)
357
348
}
358
349
359
- private fun wrapInRunFun (expression : KtExpression ): PsiElement ? {
360
- val replacedBody = replaceByRunFunction (expression)
361
- return replacedBody.lambdaArguments.first().getLambdaExpression() .bodyExpression?.firstChild
350
+ private fun wrapInLambdaCall (expression : KtExpression ): PsiElement ? {
351
+ val replacedBody = replaceByLambdaCall (expression)
352
+ return ( replacedBody.calleeExpression as ? KtLambdaExpression )? .bodyExpression?.firstChild
362
353
}
0 commit comments