/** * workspacePath — pure, vscode-free helpers for the "is this document one * Cowriting can anchor to?" decision (F2/F3/F4 require a SAVED file under the * workspace folder, because threads/attribution/proposals persist to a * `.threads/` sidecar beside that file). * * Split out so the membership test and the per-condition warning are * deterministic and unit-testable with no editor — see `test/workspacePath.test.ts`. */ import * as path from "node:path"; /** * Is `fsPath` the workspace root or a path strictly inside it? * * Uses a path-separator boundary, NOT a bare `startsWith(root)` — otherwise a * sibling whose name merely begins with the root's name would falsely match * (e.g. `.../vscode-cowriting-plugin-content/x` vs root `.../vscode-cowriting-plugin`). */ export function isUnderRoot(fsPath: string, root: string): boolean { return fsPath === root || fsPath.startsWith(root + path.sep); } export interface SelectionContext { /** Is there an active text editor at all? */ hasEditor: boolean; /** Is the active editor's selection empty (no highlight)? */ selectionEmpty: boolean; /** The active document's URI scheme (`file`, `untitled`, …). */ scheme: string; /** The active document's filesystem path (empty if none). */ fsPath: string; /** The workspace folder Cowriting is anchored to. */ root: string; } /** * Why a selection can't be sent to Claude — or `null` if it can. Each condition * gets its OWN message so the user learns the real reason instead of always * being told to "select some text" (the reported bug: an out-of-workspace file * was rejected with the no-selection message even though text was selected). */ export function selectionRejection(ctx: SelectionContext): string | null { if (!ctx.hasEditor) { return "Cowriting: focus a text editor with a selection first."; } if (ctx.selectionEmpty) { return "Cowriting: select some text to send to Claude first."; } if (ctx.scheme !== "file") { return "Cowriting: save this document to a file first — Cowriting anchors edits to a saved workspace file."; } if (!isUnderRoot(ctx.fsPath, ctx.root)) { return "Cowriting: this file is outside your workspace folder — open a file inside it to ask Claude to edit (Cowriting anchors edits and attribution to files in the workspace)."; } return null; }