diff options
author | vaage <vaage@google.com> | 2021-10-06 13:21:47 -0700 |
---|---|---|
committer | Aaron Vaage <vaage@google.com> | 2021-10-08 23:12:38 +0000 |
commit | bccbfaa1b59b1baa56fd492bd2526cd090950486 (patch) | |
tree | 383a18752ad0b7bafc05d51f061cfe10b18c783c | |
parent | 332bef033d26b5e210d1263196b743247383c534 (diff) | |
download | idea-bccbfaa1b59b1baa56fd492bd2526cd090950486.tar.gz |
Refactor PsiNavSource
Refactored PsiNavSource in an effort to make the logic easier to
understand, specifically when we are using a class for navigation and
when we are using a method for navigation.
Test: Navigate to Java thread class via profiler
Bug: n/a
Change-Id: Ie9b03b98cef58a24263f52642f5c2bb26d5936a8
-rw-r--r-- | codenavigation/src/com/android/tools/idea/codenavigation/PsiNavSource.kt | 62 |
1 files changed, 33 insertions, 29 deletions
diff --git a/codenavigation/src/com/android/tools/idea/codenavigation/PsiNavSource.kt b/codenavigation/src/com/android/tools/idea/codenavigation/PsiNavSource.kt index ab3de904938..5e96f5a1411 100644 --- a/codenavigation/src/com/android/tools/idea/codenavigation/PsiNavSource.kt +++ b/codenavigation/src/com/android/tools/idea/codenavigation/PsiNavSource.kt @@ -30,46 +30,50 @@ import com.intellij.psi.util.ClassUtil */ class PsiNavSource(private val project: Project): NavSource { override fun lookUp(location: CodeLocation, arch: String?): Navigatable? { + var psiClass = findClass(location) ?: return null + + if (location.lineNumber >= 0) { + // If the specified CodeLocation has a line number, navigatable is that line + return OpenFileDescriptor(project, + psiClass.navigationElement.containingFile.virtualFile, + location.lineNumber, + 0) + } + + // If we fail to find the method, fall back to the class. + return findMethod(psiClass, location) ?: psiClass + } + + /** + * Looks for the class definition that covers [location]. Returns null if no class can be + * found (e.g. not java/kotlin code). + */ + private fun findClass(location: CodeLocation): PsiClass? { if (location.className.isNullOrEmpty()) { return null } val manager = PsiManager.getInstance(project) - var psiClass = ClassUtil.findPsiClassByJVMName(manager, location.className!!); - - if (psiClass == null) { - if (location.lineNumber >= 0) { - // There has been at least one case where the PsiManager could not find an inner class in - // Kotlin code, which caused us to abort navigating. However, if we have the outer class - // (which is easier for PsiManager to find) and a line number, that's enough information to - // help us navigate. So, to be more robust against PsiManager error, we try one more time. - psiClass = ClassUtil.findPsiClassByJVMName(manager, location.outerClassName!!); - } - } - if (psiClass == null) { - return null; - } + var psiClass = ClassUtil.findPsiClassByJVMName(manager, location.className!!); - if (location.lineNumber >= 0) { - // If the specified CodeLocation has a line number, navigatable is that line - return OpenFileDescriptor(project, psiClass.navigationElement.containingFile.virtualFile, location.lineNumber, 0); + if (psiClass == null && location.lineNumber >= 0) { + // There has been at least one case where the PsiManager could not find an inner class in + // Kotlin code, which caused us to abort navigating. However, if we have the outer class + // (which is easier for PsiManager to find) and a line number, that's enough information to + // help us navigate. So, to be more robust against PsiManager error, we try one more time. + psiClass = ClassUtil.findPsiClassByJVMName(manager, location.outerClassName!!); } - if (location.methodName != null && location.signature != null) { - // If it has both method name and signature, navigatable is the corresponding method - val method = findMethod(psiClass, location.methodName!!, location.signature!!); + return psiClass + } - if (method != null) { - return method - } + private fun findMethod(psiClass: PsiClass, location: CodeLocation,): PsiMethod? { + if (location.methodName == null || location.signature == null) { + return null } - // Otherwise, navigatable is the class - return psiClass; - } - - private fun findMethod(psiClass: PsiClass, methodName: String, signature: String): PsiMethod? { - return psiClass.findMethodsByName(methodName, true).firstOrNull{ signature == TraceSignatureConverter.getTraceSignature(it) } + val methods = psiClass.findMethodsByName(location.methodName, true) + return methods.firstOrNull{ location.signature == TraceSignatureConverter.getTraceSignature(it) } } }
\ No newline at end of file |