Repository: intellij-rust/intellij-rust
Issue: IndexNotReadyException in NavBar

<!--
Hello and thank you for the issue!
If you would like to report a bug, we have added some points below that you can fill out.
Feel free to remove all the irrelevant text to request a new feature.
-->

## Environment

* **IntelliJ Rust plugin version:** 0.4.180.SNAPSHOT-222-dev
* **Rust toolchain version:** 1.64.0 (a55dd71d5 2022-09-19) x86_64-unknown-linux-gnu
* **IDE name and version:** IntelliJ IDEA 2022.2.2 Ultimate Edition (IU-222.4167.29)
* **Operating system:** Linux 5.15.0-47-generic
* **Macro expansion:** enabled
* **Additional experimental features:** org.rust.cargo.emulate.terminal, org.rust.cargo.evaluate.build.scripts, org.rust.macros.proc

## Problem description

<details>
  <summary>Stacktrace</summary>
  
  ```
  com.intellij.openapi.project.IndexNotReadyException: Please change caller according to com.intellij.openapi.project.IndexNotReadyException documentation
	at com.intellij.openapi.project.IndexNotReadyException.create(IndexNotReadyException.java:67)
	at com.intellij.openapi.project.IndexNotReadyException.create(IndexNotReadyException.java:62)
	at org.rust.openapiext.UtilsKt.checkIsSmartMode(utils.kt:130)
	at org.rust.lang.core.resolve2.FacadeUpdateDefMapKt.getOrUpdateIfNeeded(FacadeUpdateDefMap.kt:48)
	at org.rust.lang.core.resolve2.FacadeResolveKt.findModDataFor(FacadeResolve.kt:673)
	at org.rust.lang.core.psi.RsFile.doGetCachedData(RsFile.kt:134)
	at org.rust.lang.core.psi.RsFile._get_cachedData_$lambda-1(RsFile.kt:100)
	at com.intellij.psi.util.CachedValuesManager$1.compute(CachedValuesManager.java:158)
	at com.intellij.psi.impl.PsiCachedValueImpl.doCompute(PsiCachedValueImpl.java:39)
	at com.intellij.util.CachedValueBase.lambda$getValueWithLock$3(CachedValueBase.java:227)
	at com.intellij.util.CachedValueBase.computeData(CachedValueBase.java:42)
	at com.intellij.util.CachedValueBase.lambda$getValueWithLock$4(CachedValueBase.java:227)
	at com.intellij.openapi.util.RecursionManager$1.computePreventingRecursion(RecursionManager.java:114)
	at com.intellij.openapi.util.RecursionGuard.doPreventingRecursion(RecursionGuard.java:44)
	at com.intellij.openapi.util.RecursionManager.doPreventingRecursion(RecursionManager.java:68)
	at com.intellij.util.CachedValueBase.getValueWithLock(CachedValueBase.java:228)
	at com.intellij.psi.impl.PsiCachedValueImpl.getValue(PsiCachedValueImpl.java:28)
	at com.intellij.util.CachedValuesManagerImpl.getCachedValue(CachedValuesManagerImpl.java:72)
	at com.intellij.psi.util.CachedValuesManager.getCachedValue(CachedValuesManager.java:155)
	at org.rust.lang.core.psi.RsFile.getCachedData(RsFile.kt:99)
	at org.rust.lang.core.psi.RsFile.getCrate(RsFile.kt:80)
	at org.rust.lang.core.psi.ext.RsElementKt.getContainingCrate(RsElement.kt:59)
	at org.rust.lang.core.stubs.common.RsAttributeOwnerPsiOrStub.getContainingCrate(PsiOrStubInterfaces.kt:46)
	at org.rust.lang.core.resolve2.FacadeResolveKt.getModInfo(FacadeResolve.kt:429)
	at org.rust.lang.core.resolve2.FacadeMetaInfoKt.getRecursionLimit(FacadeMetaInfo.kt:63)
	at org.rust.lang.core.psi.ext.RsMacroCallKt.processExpansionRecursively(RsMacroCall.kt:176)
	at org.rust.lang.core.psi.ext.RsMacroCallKt.getExpansionFlatten(RsMacroCall.kt:165)
	at org.rust.ide.structure.RsStructureViewElement.extractItems(RsStructureViewModel.kt:113)
	at org.rust.ide.structure.RsStructureViewElement.extractItems(RsStructureViewModel.kt:102)
	at org.rust.ide.structure.RsStructureViewElement.getChildElements(RsStructureViewModel.kt:92)
	at org.rust.ide.structure.RsStructureViewElement.getChildren(RsStructureViewModel.kt:85)
	at com.intellij.ide.navigationToolbar.StructureAwareNavBarModelExtension.childrenFromNodeAndProviders(StructureAwareNavBarModelExtension.kt:137)
	at com.intellij.ide.navigationToolbar.StructureAwareNavBarModelExtension.findParentInModel(StructureAwareNavBarModelExtension.kt:87)
	at com.intellij.ide.navigationToolbar.StructureAwareNavBarModelExtension.getParent(StructureAwareNavBarModelExtension.kt:73)
	at com.intellij.ide.navigationToolbar.NavBarModelBuilderImpl.traverseToRoot(NavBarModelBuilderImpl.java:37)
	at com.intellij.ide.navigationToolbar.NavBarModelBuilder.createModel(NavBarModelBuilder.java:30)
	at com.intellij.ide.navigationToolbar.NavBarModel.lambda$createModel$2(NavBarModel.java:232)
	at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:941)
	at com.intellij.openapi.application.ReadAction.compute(ReadAction.java:68)
	at com.intellij.ide.navigationToolbar.NavBarModel.createModel(NavBarModel.java:232)
	at com.intellij.ide.navigationToolbar.NavBarModel.createModel(NavBarModel.java:169)
	at com.intellij.ide.navigationToolbar.NavBarModel.lambda$updateModelAsync$0(NavBarModel.java:105)
	at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.insideReadAction(NonBlockingReadActionImpl.java:536)
	at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.lambda$attemptComputation$3(NonBlockingReadActionImpl.java:501)
	at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1154)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.lambda$runInReadActionWithWriteActionPriority$0(ProgressIndicatorUtils.java:75)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runActionAndCancelBeforeWrite(ProgressIndicatorUtils.java:158)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.lambda$runWithWriteActionPriority$1(ProgressIndicatorUtils.java:115)
	at com.intellij.openapi.progress.ProgressManager.lambda$runProcess$0(ProgressManager.java:66)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:188)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$executeProcessUnderProgress$12(CoreProgressManager.java:608)
	at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:683)
	at com.intellij.openapi.progress.impl.CoreProgressManager.computeUnderProgress(CoreProgressManager.java:639)
	at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:607)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:60)
	at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:175)
	at com.intellij.openapi.progress.ProgressManager.runProcess(ProgressManager.java:66)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runWithWriteActionPriority(ProgressIndicatorUtils.java:112)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runInReadActionWithWriteActionPriority(ProgressIndicatorUtils.java:75)
	at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.attemptComputation(NonBlockingReadActionImpl.java:501)
	at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.lambda$transferToBgThread$1(NonBlockingReadActionImpl.java:408)
	at com.intellij.util.concurrency.BoundedTaskExecutor.doRun(BoundedTaskExecutor.java:241)
	at com.intellij.util.concurrency.BoundedTaskExecutor.access$200(BoundedTaskExecutor.java:31)
	at com.intellij.util.concurrency.BoundedTaskExecutor$1.execute(BoundedTaskExecutor.java:214)
	at com.intellij.util.ConcurrencyUtil.runUnderThreadName(ConcurrencyUtil.java:212)
	at com.intellij.util.concurrency.BoundedTaskExecutor$1.run(BoundedTaskExecutor.java:203)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:702)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:699)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1.run(Executors.java:699)
	at java.base/java.lang.Thread.run(Thread.java:833)
  ```
</details>


<!--
Please include as much of your codebase as needed to reproduce the error.
If the relevant files are large, please provide a link to a public repository or a [Gist](https://gist.github.com/).
-->

Changed files:
src/main/kotlin/org/rust/ide/navigationToolbar/RsNavBarModelExtension.kt, src/main/kotlin/org/rust/ide/structure/RsStructureViewModel.kt

diff --git a/src/main/kotlin/org/rust/ide/navigationToolbar/RsNavBarModelExtension.kt b/src/main/kotlin/org/rust/ide/navigationToolbar/RsNavBarModelExtension.kt
index 588d5048b..d51edbe82 100644
--- a/src/main/kotlin/org/rust/ide/navigationToolbar/RsNavBarModelExtension.kt
+++ b/src/main/kotlin/org/rust/ide/navigationToolbar/RsNavBarModelExtension.kt
@@ -6,8 +6,12 @@
 package org.rust.ide.navigationToolbar
 
 import com.intellij.ide.navigationToolbar.StructureAwareNavBarModelExtension
+import com.intellij.ide.structureView.StructureViewModel
 import com.intellij.lang.Language
+import com.intellij.openapi.editor.Editor
+import com.intellij.psi.PsiFile
 import org.rust.ide.miscExtensions.RsBreadcrumbsInfoProvider
+import org.rust.ide.structure.RsStructureViewModel
 import org.rust.lang.RsLanguage
 import org.rust.lang.core.psi.RsFile
 import org.rust.lang.core.psi.ext.RsElement
@@ -15,6 +19,11 @@ import org.rust.lang.core.psi.ext.RsElement
 class RsNavBarModelExtension : StructureAwareNavBarModelExtension() {
     override val language: Language = RsLanguage
 
+    override fun createModel(file: PsiFile, editor: Editor?): StructureViewModel? {
+        if (file !is RsFile) return null
+        return RsStructureViewModel(editor, file, expandMacros = false)
+    }
+
     override fun getPresentableText(item: Any?): String? {
         val element = item as? RsElement ?: return null
         if (element is RsFile) {
diff --git a/src/main/kotlin/org/rust/ide/structure/RsStructureViewModel.kt b/src/main/kotlin/org/rust/ide/structure/RsStructureViewModel.kt
index 782828b2f..ac6f2c3d3 100644
--- a/src/main/kotlin/org/rust/ide/structure/RsStructureViewModel.kt
+++ b/src/main/kotlin/org/rust/ide/structure/RsStructureViewModel.kt
@@ -19,14 +19,15 @@ import com.intellij.openapi.ui.Queryable
 import com.intellij.pom.Navigatable
 import com.intellij.ui.icons.RowIcon
 import com.intellij.util.PlatformIcons
+import com.intellij.util.containers.map2Array
 import org.rust.ide.presentation.getPresentationForStructure
 import org.rust.lang.core.psi.*
 import org.rust.lang.core.psi.ext.*
 import org.rust.openapiext.isUnitTestMode
 import org.rust.stdext.buildList
 
-class RsStructureViewModel(editor: Editor?, file: RsFileBase) :
-    StructureViewModelBase(file, editor, RsStructureViewElement(file)),
+class RsStructureViewModel(editor: Editor?, file: RsFileBase, expandMacros: Boolean = true) :
+    StructureViewModelBase(file, editor, RsStructureViewElement(file, expandMacros)),
     StructureViewModel.ElementInfoProvider {
 
     init {
@@ -60,7 +61,8 @@ class RsStructureViewModel(editor: Editor?, file: RsFileBase) :
 }
 
 class RsStructureViewElement(
-    psiArg: RsElement
+    psiArg: RsElement,
+    private val expandMacros: Boolean,
 ) : StructureViewTreeElement, Queryable {
 
     private val psiAnchor = TreeAnchorizer.getService().createAnchor(psiArg)
@@ -82,13 +84,13 @@ class RsStructureViewElement(
     }
 
     override fun getChildren(): Array<TreeElement> =
-        childElements.map(::RsStructureViewElement).toTypedArray()
+        childElements.map2Array { RsStructureViewElement(it, expandMacros) }
 
     private val childElements: List<RsElement>
         get() {
             return when (val psi = psi) {
                 is RsEnumItem -> psi.variants
-                is RsTraitOrImpl -> psi.expandedMembers
+                is RsTraitOrImpl -> if (expandMacros) psi.expandedMembers else psi.explicitMembers
                 is RsMod -> extractItems(psi)
                 is RsStructItem -> psi.blockFields?.namedFieldDeclList.orEmpty()
                 is RsEnumVariant -> psi.blockFields?.namedFieldDeclList.orEmpty()
@@ -110,7 +112,9 @@ class RsStructureViewElement(
                     }
 
                     is RsMacroCall -> {
-                        addAll(extractItems(item.expansionFlatten.asSequence()))
+                        if (expandMacros) {
+                            addAll(extractItems(item.expansionFlatten.asSequence()))
+                        }
                     }
 
                     is RsUseItem -> Unit