|
| 1 | +/** |
| 2 | + * Gutter annotations and inline text for agent-touched files. |
| 3 | + * |
| 4 | + * Files the agent read: subtle left-border highlight + inline "← agent read Nx" |
| 5 | + * Files the agent modified: stronger highlight + inline "← agent modified Nx" |
| 6 | + * |
| 7 | + * Decorations are applied to the first line of the file (non-intrusive). |
| 8 | + * Cleared when the session ends or the user runs agentTrace.clearDecorations. |
| 9 | + */ |
| 10 | + |
| 11 | +import * as vscode from "vscode"; |
| 12 | +import { FileAccess, SessionState } from "./traceStore"; |
| 13 | + |
| 14 | +// Decoration type for files the agent has read (but not written) |
| 15 | +const readDecoration = vscode.window.createTextEditorDecorationType({ |
| 16 | + isWholeLine: true, |
| 17 | + overviewRulerColor: new vscode.ThemeColor("editorInfo.foreground"), |
| 18 | + overviewRulerLane: vscode.OverviewRulerLane.Left, |
| 19 | + borderWidth: "0 0 0 2px", |
| 20 | + borderStyle: "solid", |
| 21 | + borderColor: new vscode.ThemeColor("editorInfo.foreground"), |
| 22 | + light: { borderColor: "#4a9eff55" }, |
| 23 | + dark: { borderColor: "#4a9eff55" }, |
| 24 | +}); |
| 25 | + |
| 26 | +// Decoration type for files the agent has written |
| 27 | +const writeDecoration = vscode.window.createTextEditorDecorationType({ |
| 28 | + isWholeLine: true, |
| 29 | + overviewRulerColor: new vscode.ThemeColor("editorWarning.foreground"), |
| 30 | + overviewRulerLane: vscode.OverviewRulerLane.Left, |
| 31 | + borderWidth: "0 0 0 3px", |
| 32 | + borderStyle: "solid", |
| 33 | + borderColor: new vscode.ThemeColor("editorWarning.foreground"), |
| 34 | + light: { borderColor: "#e5a00d88" }, |
| 35 | + dark: { borderColor: "#e5a00d88" }, |
| 36 | +}); |
| 37 | + |
| 38 | +export class DecorationManager extends vscode.Disposable { |
| 39 | + private readonly disposables: vscode.Disposable[] = []; |
| 40 | + |
| 41 | + constructor() { |
| 42 | + super(() => this._disposeAll()); |
| 43 | + |
| 44 | + // Re-apply decorations when the active editor changes |
| 45 | + this.disposables.push( |
| 46 | + vscode.window.onDidChangeActiveTextEditor((editor) => { |
| 47 | + if (editor && this.currentState) { |
| 48 | + this._applyToEditor(editor, this.currentState.fileAccess); |
| 49 | + } |
| 50 | + }) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + private currentState: SessionState | null = null; |
| 55 | + |
| 56 | + update(state: SessionState | null): void { |
| 57 | + this.currentState = state; |
| 58 | + |
| 59 | + if (!state) { |
| 60 | + this._clearAll(); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + for (const editor of vscode.window.visibleTextEditors) { |
| 65 | + this._applyToEditor(editor, state.fileAccess); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + clear(): void { |
| 70 | + this._clearAll(); |
| 71 | + this.currentState = null; |
| 72 | + } |
| 73 | + |
| 74 | + private _applyToEditor( |
| 75 | + editor: vscode.TextEditor, |
| 76 | + fileAccess: Map<string, FileAccess> |
| 77 | + ): void { |
| 78 | + const filePath = editor.document.uri.fsPath; |
| 79 | + const access = fileAccess.get(filePath); |
| 80 | + |
| 81 | + if (!access) { |
| 82 | + editor.setDecorations(readDecoration, []); |
| 83 | + editor.setDecorations(writeDecoration, []); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const firstLine = new vscode.Range(0, 0, 0, 0); |
| 88 | + |
| 89 | + if (access.writes > 0) { |
| 90 | + const label = _label(access); |
| 91 | + editor.setDecorations(readDecoration, []); |
| 92 | + editor.setDecorations(writeDecoration, [ |
| 93 | + { |
| 94 | + range: firstLine, |
| 95 | + renderOptions: { |
| 96 | + after: { |
| 97 | + contentText: ` ← ${label}`, |
| 98 | + color: new vscode.ThemeColor("editorWarning.foreground"), |
| 99 | + fontStyle: "italic", |
| 100 | + margin: "0 0 0 2em", |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + ]); |
| 105 | + } else { |
| 106 | + const label = _label(access); |
| 107 | + editor.setDecorations(writeDecoration, []); |
| 108 | + editor.setDecorations(readDecoration, [ |
| 109 | + { |
| 110 | + range: firstLine, |
| 111 | + renderOptions: { |
| 112 | + after: { |
| 113 | + contentText: ` ← ${label}`, |
| 114 | + color: new vscode.ThemeColor("editorInfo.foreground"), |
| 115 | + fontStyle: "italic", |
| 116 | + margin: "0 0 0 2em", |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + ]); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private _clearAll(): void { |
| 125 | + for (const editor of vscode.window.visibleTextEditors) { |
| 126 | + editor.setDecorations(readDecoration, []); |
| 127 | + editor.setDecorations(writeDecoration, []); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private _disposeAll(): void { |
| 132 | + this._clearAll(); |
| 133 | + readDecoration.dispose(); |
| 134 | + writeDecoration.dispose(); |
| 135 | + for (const d of this.disposables) { d.dispose(); } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +function _label(access: FileAccess): string { |
| 140 | + const parts: string[] = []; |
| 141 | + if (access.reads > 0) { |
| 142 | + parts.push(`agent read ${access.reads}×`); |
| 143 | + } |
| 144 | + if (access.writes > 0) { |
| 145 | + parts.push(`agent modified ${access.writes}×`); |
| 146 | + } |
| 147 | + return parts.join(", ") + " this session"; |
| 148 | +} |
0 commit comments