feat(editor): EditorProposalController — optimistic apply + decorations + CodeLens (#64)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
/**
|
||||
* EditorProposalController — F12/#64 editor surface (spec coauthoring-inline-editor-diff
|
||||
* §3.5). Reverses INV-32 for pending proposals: on `onDidChangeProposals` it
|
||||
* (1) optimistically applies any not-yet-applied proposal into the active editor's
|
||||
* buffer (ProposalController.optimisticApply, INV-48), (2) decorates each applied
|
||||
* proposal — insertion tint over the proposed text + a non-editable struck-red hint
|
||||
* for deletions (INV-52, decorationPlan/INV-49), and (3) provides a CodeLens
|
||||
* `Accept ▾ / Reject ▾` above each block whose ▾ opens a QuickPick (this / all).
|
||||
* Owns no proposal STATE — it is a view over ProposalController (which stays the
|
||||
* pure F4 owner). A document with no pending proposals shows nothing (INV-32's
|
||||
* spirit when nothing is pending).
|
||||
*/
|
||||
import * as vscode from "vscode";
|
||||
import type { ProposalController } from "./proposalController";
|
||||
import { decorationPlan } from "./trackChangesModel";
|
||||
import { isAuthorable } from "./workspacePath";
|
||||
|
||||
export class EditorProposalController implements vscode.Disposable, vscode.CodeLensProvider {
|
||||
private readonly disposables: vscode.Disposable[] = [];
|
||||
private readonly insertionDeco = vscode.window.createTextEditorDecorationType({
|
||||
backgroundColor: new vscode.ThemeColor("diffEditor.insertedTextBackground"),
|
||||
});
|
||||
private readonly deletionDeco = vscode.window.createTextEditorDecorationType({
|
||||
// a non-editable struck-red hint injected AFTER the insertion (INV-52)
|
||||
after: { color: new vscode.ThemeColor("gitDecoration.deletedResourceForeground") },
|
||||
textDecoration: "none",
|
||||
});
|
||||
private readonly lensEmitter = new vscode.EventEmitter<void>();
|
||||
readonly onDidChangeCodeLenses = this.lensEmitter.event;
|
||||
/** Pending debounce timers — one per URI — coalesce rapid-fire propose events
|
||||
* (e.g. runEditAndPropose's N sequential propose() calls) into a single
|
||||
* optimistic-apply pass that runs after ALL proposals are created. Without this,
|
||||
* each propose() fires onDidChangeProposals synchronously and the controller's
|
||||
* optimisticApply runs concurrently with the still-in-progress propose loop,
|
||||
* causing "file changed in the meantime" workspace-edit conflicts. */
|
||||
private readonly pendingApply = new Map<string, ReturnType<typeof setTimeout>>();
|
||||
|
||||
constructor(private readonly proposals: ProposalController) {
|
||||
this.disposables.push(
|
||||
this.insertionDeco, this.deletionDeco, this.lensEmitter,
|
||||
vscode.languages.registerCodeLensProvider({ language: "markdown" }, this),
|
||||
this.proposals.onDidChangeProposals(({ uri }) => this.scheduleApply(uri)),
|
||||
vscode.window.onDidChangeActiveTextEditor((ed) => ed && this.renderEditor(ed)),
|
||||
// the four QuickPick-backed menu commands
|
||||
vscode.commands.registerCommand("cowriting.proposalAcceptMenu", (id?: string) => this.menu("accept", id)),
|
||||
vscode.commands.registerCommand("cowriting.proposalRejectMenu", (id?: string) => this.menu("reject", id)),
|
||||
);
|
||||
}
|
||||
|
||||
/** Debounce-schedule an optimistic-apply pass for the given URI. Multiple rapid
|
||||
* onDidChangeProposals events (from a single runEditAndPropose batch) collapse
|
||||
* into one pass that runs after the batch completes. */
|
||||
private scheduleApply(uri: string): void {
|
||||
const prev = this.pendingApply.get(uri);
|
||||
if (prev !== undefined) clearTimeout(prev);
|
||||
this.pendingApply.set(
|
||||
uri,
|
||||
setTimeout(() => {
|
||||
this.pendingApply.delete(uri);
|
||||
void this.onProposalsChanged(uri);
|
||||
}, 0),
|
||||
);
|
||||
}
|
||||
|
||||
/** Apply any not-yet-applied proposals on the doc, then re-decorate + refresh lenses. */
|
||||
private async onProposalsChanged(uri: string): Promise<void> {
|
||||
const doc = vscode.workspace.textDocuments.find((d) => d.uri.toString() === uri);
|
||||
if (!doc || doc.languageId !== "markdown" || !isAuthorable(doc.uri.scheme)) return;
|
||||
const key = this.proposals.keyFor(doc);
|
||||
for (const v of this.proposals.listProposals(doc)) {
|
||||
if (v.anchorStart !== null && !this.proposals.isApplied(key, v.id)) {
|
||||
await this.proposals.optimisticApply(doc, v.id); // fires onDidChangeProposals again; guarded by isApplied
|
||||
}
|
||||
}
|
||||
const ed = vscode.window.visibleTextEditors.find((e) => e.document.uri.toString() === uri);
|
||||
if (ed) this.renderEditor(ed);
|
||||
this.lensEmitter.fire();
|
||||
}
|
||||
|
||||
/** Decorate the editor for every applied proposal on its document (INV-52). */
|
||||
private renderEditor(editor: vscode.TextEditor): void {
|
||||
const doc = editor.document;
|
||||
if (doc.languageId !== "markdown") {
|
||||
editor.setDecorations(this.insertionDeco, []);
|
||||
editor.setDecorations(this.deletionDeco, []);
|
||||
return;
|
||||
}
|
||||
const key = this.proposals.keyFor(doc);
|
||||
const insertions: vscode.Range[] = [];
|
||||
const deletions: vscode.DecorationOptions[] = [];
|
||||
for (const v of this.proposals.listProposals(doc)) {
|
||||
if (v.anchorStart === null || v.original === undefined || !this.proposals.isApplied(key, v.id)) continue;
|
||||
const plan = decorationPlan(v.anchorStart, v.original, v.replacement);
|
||||
for (const ins of plan.insertions) {
|
||||
insertions.push(new vscode.Range(doc.positionAt(ins.start), doc.positionAt(ins.end)));
|
||||
}
|
||||
for (const del of plan.deletions) {
|
||||
deletions.push({
|
||||
range: new vscode.Range(doc.positionAt(del.at), doc.positionAt(del.at)),
|
||||
renderOptions: { after: { contentText: ` ${del.text} `, textDecoration: "line-through" } },
|
||||
});
|
||||
}
|
||||
}
|
||||
editor.setDecorations(this.insertionDeco, insertions);
|
||||
editor.setDecorations(this.deletionDeco, deletions);
|
||||
}
|
||||
|
||||
/** CodeLensProvider: a `Accept ▾` / `Reject ▾` pair above each applied block. */
|
||||
provideCodeLenses(document: vscode.TextDocument): vscode.CodeLens[] {
|
||||
if (document.languageId !== "markdown") return [];
|
||||
const key = this.proposals.keyFor(document);
|
||||
const lenses: vscode.CodeLens[] = [];
|
||||
for (const v of this.proposals.listProposals(document)) {
|
||||
if (v.anchorStart === null || !this.proposals.isApplied(key, v.id)) continue;
|
||||
const pos = document.positionAt(v.anchorStart);
|
||||
const line = new vscode.Range(pos.line, 0, pos.line, 0);
|
||||
lenses.push(
|
||||
new vscode.CodeLens(line, { title: "Accept ▾", command: "cowriting.proposalAcceptMenu", arguments: [v.id] }),
|
||||
new vscode.CodeLens(line, { title: "Reject ▾", command: "cowriting.proposalRejectMenu", arguments: [v.id] }),
|
||||
);
|
||||
}
|
||||
return lenses;
|
||||
}
|
||||
|
||||
/** The dropdown: this-proposal vs all-proposals, then dispatch. */
|
||||
private async menu(kind: "accept" | "reject", id?: string): Promise<void> {
|
||||
const doc = vscode.window.activeTextEditor?.document;
|
||||
if (!doc || !id) return;
|
||||
const key = this.proposals.keyFor(doc);
|
||||
const verb = kind === "accept" ? "Accept" : "Reject";
|
||||
const pick = await vscode.window.showQuickPick(
|
||||
[`${verb} this proposal`, `${verb} ALL proposals`],
|
||||
{ placeHolder: `${verb} Claude's proposal` },
|
||||
);
|
||||
if (!pick) return;
|
||||
const all = pick.includes("ALL");
|
||||
if (kind === "accept") {
|
||||
if (all) await this.proposals.acceptAllProposals(doc);
|
||||
else await this.proposals.finalizeInPlace(key, id);
|
||||
} else {
|
||||
if (all) await this.proposals.rejectAll(doc);
|
||||
else await this.proposals.revertInPlace(key, id);
|
||||
}
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
for (const t of this.pendingApply.values()) clearTimeout(t);
|
||||
this.pendingApply.clear();
|
||||
for (const d of this.disposables) d.dispose();
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import { DiffViewController } from "./diffViewController";
|
||||
import { TrackChangesPreviewController } from "./trackChangesPreview";
|
||||
import { LiveProgressUi } from "./liveProgressUi";
|
||||
import { InlineAskController } from "./inlineAsk";
|
||||
import { EditorProposalController } from "./editorProposalController";
|
||||
import { isAuthorable, routeEdit, selectionRejection } from "./workspacePath";
|
||||
|
||||
const CHANNEL_NAME = "Cowriting (Cline SDK)";
|
||||
@@ -27,6 +28,7 @@ export interface CowritingApi {
|
||||
sidecarRouter: SidecarRouter;
|
||||
liveProgressUi: LiveProgressUi;
|
||||
inlineAsk: InlineAskController;
|
||||
editorProposalController: EditorProposalController;
|
||||
}
|
||||
|
||||
export function activate(context: vscode.ExtensionContext): CowritingApi | undefined {
|
||||
@@ -123,6 +125,11 @@ export function activate(context: vscode.ExtensionContext): CowritingApi | undef
|
||||
);
|
||||
context.subscriptions.push(trackChangesPreviewController);
|
||||
|
||||
// --- F12 (#64): the editor surface — optimistic-apply proposals into the buffer,
|
||||
// decorate the diff, and provide Accept ▾/Reject ▾ CodeLens (INV-48/49/52/53). ---
|
||||
const editorProposalController = new EditorProposalController(proposalController);
|
||||
context.subscriptions.push(editorProposalController);
|
||||
|
||||
// #46 (INV-42): accept every pending proposal on the active doc in one gesture
|
||||
// (also reachable from the preview toolbar's "Accept all" button). Reuses the
|
||||
// batched F4 seam + reports applied-vs-skipped.
|
||||
@@ -345,6 +352,7 @@ export function activate(context: vscode.ExtensionContext): CowritingApi | undef
|
||||
sidecarRouter,
|
||||
liveProgressUi,
|
||||
inlineAsk,
|
||||
editorProposalController,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ export class ProposalController implements vscode.Disposable {
|
||||
id: p.id,
|
||||
anchorStart: resolved === "orphaned" ? null : resolved.start,
|
||||
anchorEnd: resolved === "orphaned" ? null : resolved.end,
|
||||
replaced: fp?.text ?? "",
|
||||
replaced: p.original ?? fp?.text ?? "",
|
||||
replacement: p.replacement,
|
||||
original: p.original,
|
||||
};
|
||||
@@ -143,7 +143,29 @@ export class ProposalController implements vscode.Disposable {
|
||||
|
||||
/** Accept by id — F12: finalize the already-applied text in place (INV-51). */
|
||||
async acceptById(docPath: string, proposalId: string, opts?: { silent?: boolean }): Promise<boolean> {
|
||||
if (this.isApplied(docPath, proposalId)) return this.finalizeInPlace(docPath, proposalId);
|
||||
if (this.isApplied(docPath, proposalId)) {
|
||||
// INV-11 (applied path): if an external write mangled the optimistically-applied
|
||||
// text so the fingerprint no longer resolves, refuse finalize — same guard as the
|
||||
// legacy accept path. Direct finalizeInPlace calls (CodeLens Accept gesture where
|
||||
// the user may have edited inside the applied span) bypass this check intentionally.
|
||||
const hit = this.byId(docPath, proposalId);
|
||||
if (hit) {
|
||||
const document = this.openDoc(hit.state);
|
||||
if (document) {
|
||||
const fp = hit.state.artifact.anchors[hit.proposal.anchorId]?.fingerprint;
|
||||
const resolved = fp ? resolve(document.getText(), fp) : "orphaned";
|
||||
if (resolved === "orphaned") {
|
||||
if (!opts?.silent) {
|
||||
void vscode.window.showWarningMessage(
|
||||
"Cowriting: this proposal's target text changed or is missing — undo to restore it, or reject to discard (it is never applied by guess).",
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.finalizeInPlace(docPath, proposalId);
|
||||
}
|
||||
// Fallback: a proposal that was never optimistically applied (e.g. orphaned at
|
||||
// apply time) keeps the legacy seam-apply accept.
|
||||
const hit = this.byId(docPath, proposalId);
|
||||
|
||||
Reference in New Issue
Block a user