|
| 1 | +package com.picocode |
| 2 | + |
| 3 | +import com.intellij.openapi.application.ApplicationManager |
| 4 | +import com.intellij.openapi.project.Project |
| 5 | +import com.intellij.openapi.wm.StatusBar |
| 6 | +import com.intellij.openapi.wm.StatusBarWidget |
| 7 | +import com.intellij.openapi.wm.StatusBarWidgetFactory |
| 8 | +import com.intellij.util.Consumer |
| 9 | +import com.google.gson.Gson |
| 10 | +import com.google.gson.JsonObject |
| 11 | +import java.awt.event.MouseEvent |
| 12 | +import java.net.HttpURLConnection |
| 13 | +import java.net.URL |
| 14 | +import java.util.concurrent.Executors |
| 15 | +import java.util.concurrent.TimeUnit |
| 16 | + |
| 17 | +/** |
| 18 | + * Status bar widget that displays PicoCode indexing status |
| 19 | + */ |
| 20 | +class PicoCodeStatusBarWidget(private val project: Project) : StatusBarWidget, |
| 21 | + StatusBarWidget.TextPresentation { |
| 22 | + |
| 23 | + companion object { |
| 24 | + const val ID = "PicoCodeStatusWidget" |
| 25 | + private const val DEFAULT_HOST = "localhost" |
| 26 | + private const val DEFAULT_PORT = 8000 |
| 27 | + private const val POLLING_INTERVAL_SECONDS = 5L |
| 28 | + } |
| 29 | + |
| 30 | + private val gson = Gson() |
| 31 | + private val executor = Executors.newSingleThreadScheduledExecutor() |
| 32 | + private var currentStatus: String = "Unknown" |
| 33 | + private var statusBar: StatusBar? = null |
| 34 | + private var projectId: String? = null |
| 35 | + private var indexingStats: IndexingStats? = null |
| 36 | + |
| 37 | + data class IndexingStats( |
| 38 | + val fileCount: Int = 0, |
| 39 | + val embeddingCount: Int = 0, |
| 40 | + val isIndexed: Boolean = false |
| 41 | + ) |
| 42 | + |
| 43 | + init { |
| 44 | + // Start polling for status updates |
| 45 | + executor.scheduleAtFixedRate( |
| 46 | + { updateStatus() }, |
| 47 | + 0, |
| 48 | + POLLING_INTERVAL_SECONDS, |
| 49 | + TimeUnit.SECONDS |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + override fun ID(): String = ID |
| 54 | + |
| 55 | + override fun getPresentation(): StatusBarWidget.WidgetPresentation = this |
| 56 | + |
| 57 | + override fun install(statusBar: StatusBar) { |
| 58 | + this.statusBar = statusBar |
| 59 | + } |
| 60 | + |
| 61 | + override fun dispose() { |
| 62 | + executor.shutdown() |
| 63 | + try { |
| 64 | + executor.awaitTermination(5, TimeUnit.SECONDS) |
| 65 | + } catch (e: InterruptedException) { |
| 66 | + executor.shutdownNow() |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + override fun getText(): String { |
| 71 | + return when { |
| 72 | + currentStatus == "indexing" -> "⚡ PicoCode: Indexing..." |
| 73 | + currentStatus == "ready" && indexingStats?.isIndexed == true -> |
| 74 | + "✓ PicoCode: ${indexingStats?.fileCount ?: 0} files" |
| 75 | + currentStatus == "error" -> "✗ PicoCode: Error" |
| 76 | + currentStatus == "created" -> "○ PicoCode: Not indexed" |
| 77 | + else -> "PicoCode" |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + override fun getAlignment(): Float = 0.5f |
| 82 | + |
| 83 | + override fun getTooltipText(): String? { |
| 84 | + return when { |
| 85 | + currentStatus == "indexing" -> "PicoCode is indexing your project..." |
| 86 | + currentStatus == "ready" && indexingStats != null -> |
| 87 | + "PicoCode: ${indexingStats?.fileCount} files, ${indexingStats?.embeddingCount} embeddings indexed" |
| 88 | + currentStatus == "error" -> "PicoCode indexing error occurred" |
| 89 | + currentStatus == "created" -> "PicoCode: Project created but not indexed yet" |
| 90 | + else -> "PicoCode status unknown - check if server is running" |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + override fun getClickConsumer(): Consumer<MouseEvent>? { |
| 95 | + return Consumer { |
| 96 | + // Optional: could open tool window or trigger re-indexing |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private fun updateStatus() { |
| 101 | + ApplicationManager.getApplication().executeOnPooledThread { |
| 102 | + try { |
| 103 | + val projectPath = project.basePath ?: return@executeOnPooledThread |
| 104 | + |
| 105 | + // Get or create project to get project ID |
| 106 | + if (projectId == null) { |
| 107 | + projectId = getOrCreateProject(projectPath) |
| 108 | + } |
| 109 | + |
| 110 | + projectId?.let { id -> |
| 111 | + // Fetch project status |
| 112 | + val status = fetchProjectStatus(id) |
| 113 | + currentStatus = status.first |
| 114 | + indexingStats = status.second |
| 115 | + |
| 116 | + // Update status bar on EDT |
| 117 | + ApplicationManager.getApplication().invokeLater { |
| 118 | + statusBar?.updateWidget(ID) |
| 119 | + } |
| 120 | + } |
| 121 | + } catch (e: Exception) { |
| 122 | + // Silently fail - don't spam logs if server is not running |
| 123 | + currentStatus = "Unavailable" |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private fun getOrCreateProject(projectPath: String): String? { |
| 129 | + return try { |
| 130 | + val url = URL("http://$DEFAULT_HOST:$DEFAULT_PORT/api/projects") |
| 131 | + val connection = url.openConnection() as HttpURLConnection |
| 132 | + connection.requestMethod = "POST" |
| 133 | + connection.setRequestProperty("Content-Type", "application/json") |
| 134 | + connection.doOutput = true |
| 135 | + |
| 136 | + val body = gson.toJson(mapOf( |
| 137 | + "path" to projectPath, |
| 138 | + "name" to project.name |
| 139 | + )) |
| 140 | + connection.outputStream.use { it.write(body.toByteArray()) } |
| 141 | + |
| 142 | + val response = connection.inputStream.bufferedReader().readText() |
| 143 | + val json = gson.fromJson(response, JsonObject::class.java) |
| 144 | + json.get("id")?.asString |
| 145 | + } catch (e: Exception) { |
| 146 | + null |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private fun fetchProjectStatus(projectId: String): Pair<String, IndexingStats?> { |
| 151 | + return try { |
| 152 | + val url = URL("http://$DEFAULT_HOST:$DEFAULT_PORT/api/projects/$projectId") |
| 153 | + val connection = url.openConnection() as HttpURLConnection |
| 154 | + connection.requestMethod = "GET" |
| 155 | + |
| 156 | + val response = connection.inputStream.bufferedReader().readText() |
| 157 | + val json = gson.fromJson(response, JsonObject::class.java) |
| 158 | + |
| 159 | + val status = json.get("status")?.asString ?: "unknown" |
| 160 | + val statsJson = json.getAsJsonObject("indexing_stats") |
| 161 | + |
| 162 | + val stats = if (statsJson != null) { |
| 163 | + IndexingStats( |
| 164 | + fileCount = statsJson.get("file_count")?.asInt ?: 0, |
| 165 | + embeddingCount = statsJson.get("embedding_count")?.asInt ?: 0, |
| 166 | + isIndexed = statsJson.get("is_indexed")?.asBoolean ?: false |
| 167 | + ) |
| 168 | + } else { |
| 169 | + null |
| 170 | + } |
| 171 | + |
| 172 | + Pair(status, stats) |
| 173 | + } catch (e: Exception) { |
| 174 | + Pair("unavailable", null) |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +/** |
| 180 | + * Factory for creating the status bar widget |
| 181 | + */ |
| 182 | +class PicoCodeStatusBarWidgetFactory : StatusBarWidgetFactory { |
| 183 | + override fun getId(): String = PicoCodeStatusBarWidget.ID |
| 184 | + |
| 185 | + override fun getDisplayName(): String = "PicoCode Status" |
| 186 | + |
| 187 | + override fun isAvailable(project: Project): Boolean = true |
| 188 | + |
| 189 | + override fun createWidget(project: Project): StatusBarWidget { |
| 190 | + return PicoCodeStatusBarWidget(project) |
| 191 | + } |
| 192 | + |
| 193 | + override fun disposeWidget(widget: StatusBarWidget) { |
| 194 | + // Disposal is handled by the widget itself |
| 195 | + } |
| 196 | + |
| 197 | + override fun canBeEnabledOn(statusBar: StatusBar): Boolean = true |
| 198 | +} |
0 commit comments