Skip to content
This repository was archived by the owner on Aug 1, 2025. It is now read-only.

Commit 500654c

Browse files
authored
Fix NPE exception reported by Sentry (#8188)
## Changes Fix for the NPE in the `CodyElementListenerProvider` reported by Sentry: ``` Cannot invoke "com.intellij.psi.PsiFile.getVirtualFile()" because the return value of "com.intellij.psi.PsiElement.getContainingFile()" is null com.sourcegraph.cody.listeners.CodyElementListenerProvider in getListener at line 12 In App com.intellij.refactoring.listeners.impl.RefactoringTransactionImpl in addAffectedElement at line 46 com.intellij.refactoring.listeners.impl.RefactoringTransactionImpl$MyRefactoringElementListener in <init> at line 73 (...) ``` ## Test plan I'm not sure how to replicate the issue but the source of the problem is pretty clear. <!-- Required. See https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles. -->
1 parent a58b4b3 commit 500654c

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

jetbrains/src/main/kotlin/com/sourcegraph/cody/listeners/CodyElementRefactoringListenerProvider.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@ import com.sourcegraph.cody.agent.protocol_generated.TextDocument_DidRenameParam
99

1010
class CodyElementListenerProvider : RefactoringElementListenerProvider {
1111
override fun getListener(element: PsiElement): RefactoringElementListener {
12-
val uriBefore = vscNormalizedUriFor(element.containingFile.virtualFile)
12+
1313
return object : RefactoringElementListener {
14+
val uriBefore = getContainingFileUri(element)
15+
1416
override fun elementMoved(newPsiElement: PsiElement) = notifyAgent(newPsiElement)
1517

1618
override fun elementRenamed(newPsiElement: PsiElement) = notifyAgent(newPsiElement)
1719

20+
private fun getContainingFileUri(element: PsiElement): String? {
21+
if (element.containingFile.virtualFile == null) {
22+
return null
23+
}
24+
return vscNormalizedUriFor(element.containingFile.virtualFile)
25+
}
26+
1827
private fun notifyAgent(newPsiElement: PsiElement) {
19-
val uriAfter = vscNormalizedUriFor(newPsiElement.containingFile.virtualFile)
28+
val uriAfter = getContainingFileUri(newPsiElement)
2029
if (uriBefore == null || uriAfter == null || uriBefore == uriAfter) {
2130
return
2231
}

0 commit comments

Comments
 (0)