Skip to content

Commit c351293

Browse files
KotlinPositionManager: drop PositionElement, classNameForPosition now returns collection of names
1 parent e8aef7d commit c351293

File tree

2 files changed

+51
-62
lines changed

2 files changed

+51
-62
lines changed

idea/src/org/jetbrains/kotlin/idea/ExtraSteppingFilter.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ public class ExtraSteppingFilter : com.intellij.debugger.engine.ExtraSteppingFil
5656

5757
if (sourcePosition == null) return false
5858

59-
val className = positionManager.classNameForPosition(sourcePosition)?.replace('/', '.') ?: return false
59+
val classNames = positionManager.classNamesForPosition(sourcePosition).map { it.replace('/', '.') }
6060

61-
val settings = DebuggerSettings.getInstance()
62-
if (settings.TRACING_FILTERS_ENABLED) {
63-
for (filter in settings.getSteppingFilters()) {
64-
if (filter.isEnabled()) {
65-
if (filter.matches(className)) {
66-
return true;
61+
classNames.forEach { className ->
62+
val settings = DebuggerSettings.getInstance()
63+
if (settings.TRACING_FILTERS_ENABLED) {
64+
for (filter in settings.steppingFilters) {
65+
if (filter.isEnabled) {
66+
if (filter.matches(className)) {
67+
return true
68+
}
6769
}
6870
}
6971
}

idea/src/org/jetbrains/kotlin/idea/debugger/KotlinPositionManager.kt

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ import org.jetbrains.kotlin.utils.toReadOnlyList
7070
import java.util.*
7171
import com.intellij.debugger.engine.DebuggerUtils as JDebuggerUtils
7272

73-
class PositionedElement(val className: String?, val element: PsiElement?)
74-
7573
public class KotlinPositionManager(private val myDebugProcess: DebugProcess) : MultiRequestPositionManager, PositionManagerEx() {
7674

7775
private val myTypeMappers = WeakHashMap<String, CachedValue<JetTypeMapper>>()
@@ -177,8 +175,8 @@ public class KotlinPositionManager(private val myDebugProcess: DebugProcess) : M
177175
continue
178176
}
179177

180-
val internalClassName = getInternalClassNameForElement(literal.firstChild, typeMapper, file, isInLibrary).className
181-
if (internalClassName == currentLocationClassName) {
178+
val internalClassNames = getInternalClassNameForElement(literal.firstChild, typeMapper, file, isInLibrary)
179+
if (internalClassNames.any { it == currentLocationClassName }) {
182180
return literal
183181
}
184182
}
@@ -233,7 +231,7 @@ public class KotlinPositionManager(private val myDebugProcess: DebugProcess) : M
233231

234232
if (!ProjectRootsUtil.isInProjectOrLibSource(psiFile)) return result
235233

236-
val names = classNameForPositionAndInlinedOnes(sourcePosition)
234+
val names = classNamesForPositionAndInlinedOnes(sourcePosition)
237235
for (name in names) {
238236
result.addAll(myDebugProcess.virtualMachineProxy.classesByName(name))
239237
}
@@ -252,48 +250,44 @@ public class KotlinPositionManager(private val myDebugProcess: DebugProcess) : M
252250
throw NoDataException.INSTANCE
253251
}
254252

255-
private fun classNameForPositionAndInlinedOnes(sourcePosition: SourcePosition): List<String> {
253+
private fun classNamesForPositionAndInlinedOnes(sourcePosition: SourcePosition): List<String> {
256254
val result = hashSetOf<String>()
257-
val name = classNameForPosition(sourcePosition)
258-
if (name != null) {
259-
result.add(name)
260-
}
261-
val list = findInlinedCalls(sourcePosition.elementAt, sourcePosition.file)
262-
result.addAll(list)
255+
val names = classNamesForPosition(sourcePosition)
256+
result.addAll(names)
263257

264258
val lambdas = findLambdas(sourcePosition)
265259
result.addAll(lambdas)
266260

267261
return result.toReadOnlyList();
268262
}
269263

270-
private fun findLambdas(sourcePosition: SourcePosition): List<String> {
264+
private fun findLambdas(sourcePosition: SourcePosition): Collection<String> {
271265
return runReadAction {
272266
val lambdas = getLambdasAtLineIfAny(sourcePosition)
273267
val file = sourcePosition.file.containingFile as KtFile
274268
val isInLibrary = LibraryUtil.findLibraryEntry(file.virtualFile, file.project) != null
275-
lambdas.mapNotNull {
269+
lambdas.flatMap {
276270
val typeMapper = if (!isInLibrary) prepareTypeMapper(file) else createTypeMapperForLibraryFile(it, file)
277-
getInternalClassNameForElement(it, typeMapper, file, isInLibrary).className
271+
getInternalClassNameForElement(it, typeMapper, file, isInLibrary)
278272
}
279273
}
280274
}
281275

282-
public fun classNameForPosition(sourcePosition: SourcePosition): String? {
283-
val psiElement = runReadAction { sourcePosition.elementAt } ?: return null
284-
return classNameForPosition(psiElement)
276+
public fun classNamesForPosition(sourcePosition: SourcePosition): Collection<String> {
277+
val psiElement = runReadAction { sourcePosition.elementAt } ?: return emptyList()
278+
return classNamesForPosition(psiElement)
285279
}
286280

287-
private fun classNameForPosition(element: PsiElement): String? {
281+
private fun classNamesForPosition(element: PsiElement): Collection<String> {
288282
return runReadAction {
289283
if (DumbService.getInstance(element.project).isDumb) {
290-
null
284+
emptySet()
291285
}
292286
else {
293287
val file = element.containingFile as KtFile
294288
val isInLibrary = LibraryUtil.findLibraryEntry(file.virtualFile, file.project) != null
295289
val typeMapper = if (!isInLibrary) prepareTypeMapper(file) else createTypeMapperForLibraryFile(element, file)
296-
getInternalClassNameForElement(element, typeMapper, file, isInLibrary).className
290+
getInternalClassNameForElement(element, typeMapper, file, isInLibrary)
297291
}
298292
}
299293
}
@@ -335,19 +329,15 @@ public class KotlinPositionManager(private val myDebugProcess: DebugProcess) : M
335329

336330
@Deprecated("Since Idea 14.0.3 use createPrepareRequests fun")
337331
override fun createPrepareRequest(classPrepareRequestor: ClassPrepareRequestor, sourcePosition: SourcePosition): ClassPrepareRequest? {
338-
if (sourcePosition.file !is KtFile) {
339-
throw NoDataException.INSTANCE
340-
}
341-
val className = classNameForPosition(sourcePosition) ?: return null
342-
return myDebugProcess.requestsManager.createClassPrepareRequest(classPrepareRequestor, className.replace('/', '.'))
332+
return createPrepareRequests(classPrepareRequestor, sourcePosition).firstOrNull()
343333
}
344334

345335
override fun createPrepareRequests(requestor: ClassPrepareRequestor, position: SourcePosition): List<ClassPrepareRequest> {
346336
if (position.file !is KtFile) {
347337
throw NoDataException.INSTANCE
348338
}
349339

350-
return classNameForPositionAndInlinedOnes(position).mapNotNull {
340+
return classNamesForPositionAndInlinedOnes(position).mapNotNull {
351341
className -> myDebugProcess.requestsManager.createClassPrepareRequest(requestor, className.replace('/', '.'))
352342
}
353343
}
@@ -360,32 +350,32 @@ public class KotlinPositionManager(private val myDebugProcess: DebugProcess) : M
360350
myTypeMappers.put(key, value)
361351
}
362352

363-
private fun getInternalClassNameForElement(notPositionedElement: PsiElement?, typeMapper: JetTypeMapper, file: KtFile, isInLibrary: Boolean): PositionedElement {
353+
private fun getInternalClassNameForElement(notPositionedElement: PsiElement?, typeMapper: JetTypeMapper, file: KtFile, isInLibrary: Boolean): Collection<String> {
364354
val element = getElementToCalculateClassName(notPositionedElement)
365355
when {
366-
element is KtClassOrObject -> return PositionedElement(getJvmInternalNameForImpl(typeMapper, element), element)
356+
element is KtClassOrObject -> return getJvmInternalNameForImpl(typeMapper, element).toCollection()
367357
element is KtFunctionLiteral -> {
368358
if (isInlinedLambda(element, typeMapper.bindingContext)) {
369359
return getInternalClassNameForElement(element.parent, typeMapper, file, isInLibrary)
370360
}
371361
else {
372362
val asmType = CodegenBinding.asmTypeForAnonymousClass(typeMapper.bindingContext, element)
373-
return PositionedElement(asmType.internalName, element)
363+
return asmType.internalName.toCollection()
374364
}
375365
}
376366
element is KtAnonymousInitializer -> {
377367
val parent = getElementToCalculateClassName(element.parent)
378368
// Class-object initializer
379369
if (parent is KtObjectDeclaration && parent.isCompanion()) {
380-
return PositionedElement(getInternalClassNameForElement(parent.parent, typeMapper, file, isInLibrary).className, parent)
370+
return getInternalClassNameForElement(parent.parent, typeMapper, file, isInLibrary)
381371
}
382372
return getInternalClassNameForElement(element.parent, typeMapper, file, isInLibrary)
383373
}
384374
element is KtProperty && (!element.isTopLevel || !isInLibrary) -> {
385375
if (isInPropertyAccessor(notPositionedElement)) {
386376
val classOrObject = PsiTreeUtil.getParentOfType(element, KtClassOrObject::class.java)
387377
if (classOrObject != null) {
388-
return PositionedElement(getJvmInternalNameForImpl(typeMapper, classOrObject), element)
378+
return getJvmInternalNameForImpl(typeMapper, classOrObject).toCollection()
389379
}
390380
}
391381

@@ -394,25 +384,31 @@ public class KotlinPositionManager(private val myDebugProcess: DebugProcess) : M
394384
return getInternalClassNameForElement(element.parent, typeMapper, file, isInLibrary)
395385
}
396386

397-
return PositionedElement(getJvmInternalNameForPropertyOwner(typeMapper, descriptor), element)
387+
return getJvmInternalNameForPropertyOwner(typeMapper, descriptor).toCollection()
398388
}
399389
element is KtNamedFunction -> {
400390
if (isInlinedLambda(element, typeMapper.bindingContext)) {
401391
return getInternalClassNameForElement(element.parent, typeMapper, file, isInLibrary)
402392
}
403393

394+
val inlinedCalls = findInlinedCalls(element, typeMapper.bindingContext)
404395
val parent = getElementToCalculateClassName(element.parent)
405-
if (parent is KtClassOrObject) {
406-
return PositionedElement(getJvmInternalNameForImpl(typeMapper, parent), element)
396+
val parentInternalName = if (parent is KtClassOrObject) {
397+
getJvmInternalNameForImpl(typeMapper, parent)
407398
}
408399
else if (parent != null) {
409400
val asmType = CodegenBinding.asmTypeForAnonymousClass(typeMapper.bindingContext, element)
410-
return PositionedElement(asmType.internalName, element)
401+
asmType.internalName
402+
}
403+
else {
404+
NoResolveFileClassesProvider.getFileClassInternalName(file)
411405
}
406+
407+
return (inlinedCalls + parentInternalName.toCollection()).toSet()
412408
}
413409
}
414410

415-
return PositionedElement(NoResolveFileClassesProvider.getFileClassInternalName(file), element)
411+
return NoResolveFileClassesProvider.getFileClassInternalName(file).toCollection()
416412
}
417413

418414
private val TYPES_TO_CALCULATE_CLASSNAME: Array<Class<out KtElement>> =
@@ -468,29 +464,18 @@ public class KotlinPositionManager(private val myDebugProcess: DebugProcess) : M
468464
private fun createKeyForTypeMapper(file: KtFile) = NoResolveFileClassesProvider.getFileClassInternalName(file)
469465

470466

471-
private fun findInlinedCalls(element: PsiElement?, jetFile: PsiFile?): List<String> {
472-
if (element == null || jetFile !is KtFile) {
473-
return emptyList()
474-
}
475-
467+
private fun findInlinedCalls(function: KtNamedFunction, context: BindingContext): Collection<String> {
476468
return runReadAction {
477-
val result = arrayListOf<String>()
478-
val isInLibrary = LibraryUtil.findLibraryEntry(jetFile.virtualFile, jetFile.project) != null
479-
val typeMapper = if (!isInLibrary) prepareTypeMapper(jetFile) else createTypeMapperForLibraryFile(element, jetFile)
480-
val psiElement = getInternalClassNameForElement(element, typeMapper, jetFile, isInLibrary).element;
481-
482-
if (psiElement is KtNamedFunction &&
483-
InlineUtil.isInline(typeMapper.bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, psiElement))
484-
) {
485-
ReferencesSearch.search(psiElement).forEach {
469+
val result = hashSetOf<String>()
470+
471+
if (InlineUtil.isInline(context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, function))) {
472+
ReferencesSearch.search(function).forEach {
486473
if (!it.isImportUsage()) {
487474
val usage = it.element
488475
if (usage is KtElement) {
489476
//TODO recursive search
490-
val name = classNameForPosition(usage)
491-
if (name != null) {
492-
result.add(name)
493-
}
477+
val names = classNamesForPosition(usage)
478+
result.addAll(names)
494479
}
495480
}
496481
true
@@ -502,6 +487,8 @@ public class KotlinPositionManager(private val myDebugProcess: DebugProcess) : M
502487

503488
private fun ReferenceType.containsKotlinStrata() = availableStrata().contains("Kotlin")
504489

490+
private fun String?.toCollection() = if (this == null) emptySet() else setOf(this)
491+
505492
companion object {
506493
public fun createTypeMapper(file: KtFile): JetTypeMapper {
507494
val project = file.project

0 commit comments

Comments
 (0)