(dbPath: string, cwd: string)
| 87 | } |
| 88 | |
| 89 | function queryZedActiveEditor(dbPath: string, cwd: string) { |
| 90 | let db: Database | undefined |
| 91 | try { |
| 92 | db = new Database(dbPath, { readonly: true }) |
| 93 | const raw = db |
| 94 | .query( |
| 95 | `select |
| 96 | i.kind as item_kind, |
| 97 | e.item_id as editor_id, |
| 98 | i.workspace_id as workspace_id, |
| 99 | w.paths as workspace_paths, |
| 100 | w.timestamp as timestamp, |
| 101 | e.buffer_path as buffer_path |
| 102 | from items i |
| 103 | join panes p on p.pane_id = i.pane_id and p.workspace_id = i.workspace_id |
| 104 | join workspaces w on w.workspace_id = i.workspace_id |
| 105 | left join editors e on e.item_id = i.item_id and e.workspace_id = i.workspace_id |
| 106 | where i.active = 1 and p.active = 1 |
| 107 | order by w.timestamp desc`, |
| 108 | ) |
| 109 | .all() |
| 110 | |
| 111 | const rows = raw.flatMap((row) => { |
| 112 | const parsed = decodeZedEditorRow(row) |
| 113 | return Option.isSome(parsed) ? [parsed.value] : [] |
| 114 | }) |
| 115 | |
| 116 | if (raw.length > 0 && rows.length === 0) return { type: "unavailable" as const } |
| 117 | |
| 118 | const row = rows |
| 119 | .map((row) => ({ row, score: scoreZedWorkspace(row.workspace_paths, cwd) })) |
| 120 | .filter((entry) => entry.score > 0) |
| 121 | .sort((left, right) => right.score - left.score || right.row.timestamp.localeCompare(left.row.timestamp))[0]?.row |
| 122 | if (!row) return { type: "empty" as const } |
| 123 | if (row.item_kind !== "Editor") return { type: "unavailable" as const } |
| 124 | if (!isZedActiveEditorRow(row)) return { type: "empty" as const } |
| 125 | return { type: "row" as const, row } |
| 126 | } catch { |
| 127 | return { type: "unavailable" as const } |
| 128 | } finally { |
| 129 | db?.close() |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | function queryZedEditorSelections(dbPath: string, row: ZedActiveEditorRow) { |
| 134 | let db: Database | undefined |
no test coverage detected