Refactoring: extract common part to function
This commit is contained in:
@@ -31,9 +31,8 @@ import com.intellij.xdebugger.XSourcePosition
|
|||||||
import com.intellij.xdebugger.impl.XSourcePositionImpl
|
import com.intellij.xdebugger.impl.XSourcePositionImpl
|
||||||
import org.jetbrains.kotlin.idea.KotlinFileType
|
import org.jetbrains.kotlin.idea.KotlinFileType
|
||||||
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils
|
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils
|
||||||
import org.jetbrains.kotlin.idea.refactoring.getLineEndOffset
|
import org.jetbrains.kotlin.idea.debugger.findElementAtLine
|
||||||
import org.jetbrains.kotlin.idea.refactoring.getLineNumber
|
import org.jetbrains.kotlin.idea.refactoring.getLineNumber
|
||||||
import org.jetbrains.kotlin.idea.refactoring.getLineStartOffset
|
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||||
@@ -124,23 +123,10 @@ fun getLambdasAtLineIfAny(sourcePosition: SourcePosition): List<KtFunction> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getLambdasAtLineIfAny(file: KtFile, line: Int): List<KtFunction> {
|
fun getLambdasAtLineIfAny(file: KtFile, line: Int): List<KtFunction> {
|
||||||
var lineStartOffset = file.getLineStartOffset(line) ?: return emptyList()
|
val lineElement = findElementAtLine(file, line) as? KtElement ?: return emptyList()
|
||||||
var lineEndOffset = file.getLineEndOffset(line) ?: return emptyList()
|
|
||||||
|
|
||||||
var topMostElement: PsiElement? = null
|
val start = lineElement.startOffset
|
||||||
var elementAt: PsiElement?
|
val end = lineElement.endOffset
|
||||||
while (topMostElement !is KtElement && lineStartOffset < lineEndOffset) {
|
|
||||||
elementAt = file.findElementAt(lineStartOffset)
|
|
||||||
if (elementAt != null) {
|
|
||||||
topMostElement = CodeInsightUtils.getTopmostElementAtOffset(elementAt, lineStartOffset)
|
|
||||||
}
|
|
||||||
lineStartOffset++
|
|
||||||
}
|
|
||||||
|
|
||||||
if (topMostElement !is KtElement) return emptyList()
|
|
||||||
|
|
||||||
val start = topMostElement.startOffset
|
|
||||||
val end = topMostElement.endOffset
|
|
||||||
|
|
||||||
val allLiterals = CodeInsightUtils.
|
val allLiterals = CodeInsightUtils.
|
||||||
findElementsOfClassInRange(file, start, end, KtFunction::class.java)
|
findElementsOfClassInRange(file, start, end, KtFunction::class.java)
|
||||||
@@ -155,4 +141,3 @@ fun getLambdasAtLineIfAny(file: KtFile, line: Int): List<KtFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,14 +21,20 @@ import com.intellij.debugger.engine.DebuggerManagerThreadImpl
|
|||||||
import com.intellij.debugger.engine.events.DebuggerCommandImpl
|
import com.intellij.debugger.engine.events.DebuggerCommandImpl
|
||||||
import com.intellij.debugger.impl.DebuggerContextImpl
|
import com.intellij.debugger.impl.DebuggerContextImpl
|
||||||
import com.intellij.debugger.jdi.LocalVariableProxyImpl
|
import com.intellij.debugger.jdi.LocalVariableProxyImpl
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
import com.sun.jdi.*
|
import com.sun.jdi.*
|
||||||
import com.sun.tools.jdi.LocalVariableImpl
|
import com.sun.tools.jdi.LocalVariableImpl
|
||||||
import org.jetbrains.kotlin.codegen.binding.CodegenBinding
|
import org.jetbrains.kotlin.codegen.binding.CodegenBinding
|
||||||
import org.jetbrains.kotlin.codegen.coroutines.CONTINUATION_ASM_TYPE
|
import org.jetbrains.kotlin.codegen.coroutines.CONTINUATION_ASM_TYPE
|
||||||
import org.jetbrains.kotlin.codegen.coroutines.DO_RESUME_METHOD_NAME
|
import org.jetbrains.kotlin.codegen.coroutines.DO_RESUME_METHOD_NAME
|
||||||
|
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils
|
||||||
import org.jetbrains.kotlin.idea.debugger.evaluate.KotlinDebuggerCaches
|
import org.jetbrains.kotlin.idea.debugger.evaluate.KotlinDebuggerCaches
|
||||||
|
import org.jetbrains.kotlin.idea.refactoring.getLineEndOffset
|
||||||
|
import org.jetbrains.kotlin.idea.refactoring.getLineStartOffset
|
||||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||||
|
import org.jetbrains.kotlin.psi.KtElement
|
||||||
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import org.jetbrains.kotlin.psi.KtFunction
|
import org.jetbrains.kotlin.psi.KtFunction
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.inline.InlineUtil
|
import org.jetbrains.kotlin.resolve.inline.InlineUtil
|
||||||
@@ -193,3 +199,22 @@ fun isOneLineMethod(location: Location): Boolean {
|
|||||||
|
|
||||||
return firstLine != null && firstLine == lastLine
|
return firstLine != null && firstLine == lastLine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun findElementAtLine(file: KtFile, line: Int): PsiElement? {
|
||||||
|
val lineStartOffset = file.getLineStartOffset(line) ?: return null
|
||||||
|
val lineEndOffset = file.getLineEndOffset(line) ?: return null
|
||||||
|
|
||||||
|
var topMostElement: PsiElement? = null
|
||||||
|
var elementAt: PsiElement?
|
||||||
|
for (offset in lineStartOffset until lineEndOffset) {
|
||||||
|
elementAt = file.findElementAt(offset)
|
||||||
|
if (elementAt != null) {
|
||||||
|
topMostElement = CodeInsightUtils.getTopmostElementAtOffset(elementAt, offset)
|
||||||
|
if (topMostElement is KtElement) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return topMostElement
|
||||||
|
}
|
||||||
|
|||||||
+3
-17
@@ -45,7 +45,6 @@ import org.jetbrains.kotlin.idea.debugger.stepping.DexBytecode.RETURN
|
|||||||
import org.jetbrains.kotlin.idea.debugger.stepping.DexBytecode.RETURN_OBJECT
|
import org.jetbrains.kotlin.idea.debugger.stepping.DexBytecode.RETURN_OBJECT
|
||||||
import org.jetbrains.kotlin.idea.debugger.stepping.DexBytecode.RETURN_VOID
|
import org.jetbrains.kotlin.idea.debugger.stepping.DexBytecode.RETURN_VOID
|
||||||
import org.jetbrains.kotlin.idea.debugger.stepping.DexBytecode.RETURN_WIDE
|
import org.jetbrains.kotlin.idea.debugger.stepping.DexBytecode.RETURN_WIDE
|
||||||
import org.jetbrains.kotlin.idea.refactoring.getLineEndOffset
|
|
||||||
import org.jetbrains.kotlin.idea.refactoring.getLineNumber
|
import org.jetbrains.kotlin.idea.refactoring.getLineNumber
|
||||||
import org.jetbrains.kotlin.idea.refactoring.getLineStartOffset
|
import org.jetbrains.kotlin.idea.refactoring.getLineStartOffset
|
||||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||||
@@ -235,24 +234,11 @@ private fun getInlineFunctionCallsIfAny(sourcePosition: SourcePosition): List<Kt
|
|||||||
private fun findCallsOnPosition(sourcePosition: SourcePosition, filter: (KtCallExpression) -> Boolean): List<KtCallExpression> {
|
private fun findCallsOnPosition(sourcePosition: SourcePosition, filter: (KtCallExpression) -> Boolean): List<KtCallExpression> {
|
||||||
val file = sourcePosition.file as? KtFile ?: return emptyList()
|
val file = sourcePosition.file as? KtFile ?: return emptyList()
|
||||||
val lineNumber = sourcePosition.line
|
val lineNumber = sourcePosition.line
|
||||||
var elementAt = sourcePosition.elementAt
|
|
||||||
|
|
||||||
var startOffset = file.getLineStartOffset(lineNumber) ?: elementAt.startOffset
|
val lineElement = findElementAtLine(file, lineNumber) as? KtElement ?: return emptyList()
|
||||||
val endOffset = file.getLineEndOffset(lineNumber) ?: elementAt.endOffset
|
|
||||||
|
|
||||||
var topMostElement: PsiElement? = null
|
val start = lineElement.startOffset
|
||||||
while (topMostElement !is KtElement && startOffset < endOffset) {
|
val end = lineElement.endOffset
|
||||||
elementAt = file.findElementAt(startOffset)
|
|
||||||
if (elementAt != null) {
|
|
||||||
topMostElement = CodeInsightUtils.getTopmostElementAtOffset(elementAt, startOffset)
|
|
||||||
}
|
|
||||||
startOffset++
|
|
||||||
}
|
|
||||||
|
|
||||||
if (topMostElement !is KtElement) return emptyList()
|
|
||||||
|
|
||||||
val start = topMostElement.startOffset
|
|
||||||
val end = topMostElement.endOffset
|
|
||||||
|
|
||||||
val allFilteredCalls = CodeInsightUtils.
|
val allFilteredCalls = CodeInsightUtils.
|
||||||
findElementsOfClassInRange(file, start, end, KtExpression::class.java)
|
findElementsOfClassInRange(file, start, end, KtExpression::class.java)
|
||||||
|
|||||||
Reference in New Issue
Block a user