diff --git a/plugins/android-extensions/android-extensions-idea/src/org/jetbrains/kotlin/android/synthetic/idea/AndroidPsiTreeChangePreprocessor.kt b/plugins/android-extensions/android-extensions-idea/src/org/jetbrains/kotlin/android/synthetic/idea/AndroidPsiTreeChangePreprocessor.kt index 1fab2db9df7..516bd55f808 100644 --- a/plugins/android-extensions/android-extensions-idea/src/org/jetbrains/kotlin/android/synthetic/idea/AndroidPsiTreeChangePreprocessor.kt +++ b/plugins/android-extensions/android-extensions-idea/src/org/jetbrains/kotlin/android/synthetic/idea/AndroidPsiTreeChangePreprocessor.kt @@ -21,6 +21,7 @@ import com.intellij.openapi.roots.ProjectRootManager import com.intellij.openapi.util.SimpleModificationTracker import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.VirtualFileManager +import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.psi.impl.PsiTreeChangeEventImpl @@ -82,7 +83,7 @@ class AndroidPsiTreeChangePreprocessor : PsiTreeChangePreprocessor, SimpleModifi val projectFileIndex = ProjectRootManager.getInstance(xmlFile.project).fileIndex val module = projectFileIndex.getModuleForFile(xmlFile.virtualFile) - ?: element.parent?.let { projectFileIndex.getModuleForFile(it.virtualFile) } + ?: element.getContainingDirectorySafe()?.let { projectFileIndex.getModuleForFile(it.virtualFile) } if (module != null && !module.isDisposed) { val resourceManager = AndroidLayoutXmlFileManager.getInstance(module) ?: return false @@ -97,6 +98,12 @@ class AndroidPsiTreeChangePreprocessor : PsiTreeChangePreprocessor, SimpleModifi return false } + private fun PsiFile.getContainingDirectorySafe(): PsiDirectory? { + val virtualFile = this.viewProvider.virtualFile.takeIf { it.isValid } ?: return null + val parentDirectory = virtualFile.parent.takeIf { it.isValid } ?: return null + return this.manager.findDirectory(parentDirectory) + } + private fun findXmlAttribute(element: PsiElement?): XmlAttribute? { return when (element) { is XmlToken, is XmlAttributeValue -> findXmlAttribute(element.parent)