Repository: intellij-rust/intellij-rust
Issue: No way to "unhide" cargo tool window

## Environment

* **IntelliJ Rust plugin version:** 0.2.115.SNAPSHOT-193-dev
* **Rust toolchain version:** 1.39.0 (4560ea788 2019-11-04) x86_64-apple-darwin
* **IDE name and version:** CLion 2019.3.2 (CL-193.5662.56)
* **Operating system:** macOS 10.15

## Problem description

With #4102 the cargo tool window is now hidden for non-Rust project, but there's no way to enable it back if the project *is* the Rust one but the plugin doesn't understand that.

## Steps to reproduce

Consider the following project structure

```
project
└── crate1
    ├── Cargo.lock
    ├── Cargo.toml
    └── src
        └── main.rs
```

open `project` as a project root. CLion doesn't recognize it as a Rust one and I can't find a way to open cargo tool window.

#4836 helps somewhat, but you have to restart IDE after attaching cargo project for tool window to reappear.

Changed files:
src/main/kotlin/org/rust/cargo/project/toolwindow/CargoToolWindow.kt, src/main/kotlin/org/rust/cargo/project/model/impl/CargoProjectImpl.kt

diff --git a/src/main/kotlin/org/rust/cargo/project/model/impl/CargoProjectImpl.kt b/src/main/kotlin/org/rust/cargo/project/model/impl/CargoProjectImpl.kt
index 881d8a529..67e114dce 100644
--- a/src/main/kotlin/org/rust/cargo/project/model/impl/CargoProjectImpl.kt
+++ b/src/main/kotlin/org/rust/cargo/project/model/impl/CargoProjectImpl.kt
@@ -46,6 +46,7 @@ import org.rust.cargo.project.model.setup
 import org.rust.cargo.project.settings.RustProjectSettingsService
 import org.rust.cargo.project.settings.rustSettings
 import org.rust.cargo.project.settings.toolchain
+import org.rust.cargo.project.toolwindow.CargoToolWindow.Companion.initializeToolWindowIfNeeded
 import org.rust.cargo.project.workspace.CargoWorkspace
 import org.rust.cargo.project.workspace.PackageOrigin
 import org.rust.cargo.project.workspace.StandardLibrary
@@ -62,7 +63,6 @@ import org.rust.stdext.joinAll
 import java.nio.file.Path
 import java.nio.file.Paths
 import java.util.concurrent.CompletableFuture
-import java.util.concurrent.TimeUnit
 import java.util.concurrent.atomic.AtomicReference
 
 
@@ -85,6 +85,12 @@ open class CargoProjectsServiceImpl(
                     refreshAllProjects()
                 }
             })
+
+            subscribe(CargoProjectsService.CARGO_PROJECTS_TOPIC, object : CargoProjectsService.CargoProjectsListener {
+                override fun cargoProjectsUpdated(projects: Collection<CargoProject>) {
+                    initializeToolWindowIfNeeded(project)
+                }
+            })
         }
     }
 
diff --git a/src/main/kotlin/org/rust/cargo/project/toolwindow/CargoToolWindow.kt b/src/main/kotlin/org/rust/cargo/project/toolwindow/CargoToolWindow.kt
index 9317962aa..027ab5e6a 100644
--- a/src/main/kotlin/org/rust/cargo/project/toolwindow/CargoToolWindow.kt
+++ b/src/main/kotlin/org/rust/cargo/project/toolwindow/CargoToolWindow.kt
@@ -9,12 +9,16 @@ import com.intellij.ide.DefaultTreeExpander
 import com.intellij.ide.TreeExpander
 import com.intellij.openapi.actionSystem.*
 import com.intellij.openapi.application.ApplicationManager
+import com.intellij.openapi.diagnostic.Logger
 import com.intellij.openapi.project.DumbAware
 import com.intellij.openapi.project.Project
 import com.intellij.openapi.ui.SimpleToolWindowPanel
 import com.intellij.openapi.util.Condition
 import com.intellij.openapi.wm.ToolWindow
+import com.intellij.openapi.wm.ToolWindowEP
 import com.intellij.openapi.wm.ToolWindowFactory
+import com.intellij.openapi.wm.ToolWindowManager
+import com.intellij.openapi.wm.ex.ToolWindowManagerEx
 import com.intellij.ui.ColorUtil
 import com.intellij.ui.ScrollPaneFactory
 import com.intellij.ui.content.ContentFactory
@@ -116,5 +120,24 @@ class CargoToolWindow(
         val SELECTED_CARGO_PROJECT: DataKey<CargoProject> = DataKey.create<CargoProject>("SELECTED_CARGO_PROJECT")
 
         const val CARGO_TOOLBAR_PLACE: String = "Cargo Toolbar"
+
+        private const val ID: String = "Cargo"
+
+        private val LOG: Logger = Logger.getInstance(CargoToolWindow::class.java)
+
+        fun initializeToolWindowIfNeeded(project: Project) {
+            try {
+                val manager = ToolWindowManager.getInstance(project) as? ToolWindowManagerEx ?: return
+                val window = manager.getToolWindow(ID)
+                if (window != null) return
+                for (bean in ToolWindowEP.EP_NAME.extensionList) {
+                    if (ID == bean.id) {
+                        manager.initToolWindow(bean)
+                    }
+                }
+            } catch (e: Exception) {
+                LOG.error("Unable to initialize $ID tool window", e)
+            }
+        }
     }
 }