Elffuss BraidHub

kikocis/continue public

main · 4.5 KiBRawPermalink
1import * as vscode from "vscode";
2import {
3 HandlerPriority,
4 SelectionChangeManager,
5} from "../activation/SelectionChangeManager";
6
7export interface ExpectedGhostTextAcceptance {
8 documentUri: string;
9 documentVersion: number;
10 text: string;
11 startLine: number;
12 startCharacter: number;
13 endLine: number;
14 endCharacter: number;
15}
16
17/**
18 * This singleton tracks whether a given ghost text is accepted or not.
19 * We need this because there is no clean way of determining if a ghost text has been accepted outside of vscode command callback.
20 * The above mentioned callback is not viable because it's too slow.
21 * We need a way to reject model predictions on cursor movement, but cursor can move due to many reasons -- one being accepting a ghost text.
22 * We need to differentiate the ghost text acceptance from a deliberate cursor movement to reject the completion.
23 * The cursor movement event listener fires much before the vscode command callback, so the chain of edits often breaks when cursor moves due to accepting a ghost text.
24 * This is not what we want, as we want to keep the current chain of edits alive when the user accepts the completion.
25 */
26export class GhostTextAcceptanceTracker {
27 private static instance: GhostTextAcceptanceTracker | undefined;
28 private expectedAcceptance: ExpectedGhostTextAcceptance | null = null;
29
30 private constructor() {}
31
32 public static getInstance(): GhostTextAcceptanceTracker {
33 if (!GhostTextAcceptanceTracker.instance) {
34 GhostTextAcceptanceTracker.instance = new GhostTextAcceptanceTracker();
35 }
36 return GhostTextAcceptanceTracker.instance;
37 }
38
39 public static clearInstance() {
40 GhostTextAcceptanceTracker.instance = undefined;
41 }
42
43 public setExpectedGhostTextAcceptance(
44 document: vscode.TextDocument,
45 text: string,
46 startPosition: vscode.Position,
47 ) {
48 // Calculate end position
49 const lines = text.split("\n");
50 let endLine: number;
51 let endCharacter: number;
52
53 if (lines.length > 1) {
54 endLine = startPosition.line + lines.length - 1;
55 endCharacter = lines[lines.length - 1].length;
56 } else {
57 endLine = startPosition.line;
58 endCharacter = startPosition.character + text.length;
59 }
60
61 this.expectedAcceptance = {
62 documentUri: document.uri.toString(),
63 documentVersion: document.version,
64 text,
65 startLine: startPosition.line,
66 startCharacter: startPosition.character,
67 endLine,
68 endCharacter,
69 };
70 }
71
72 public checkGhostTextWasAccepted(
73 document: vscode.TextDocument,
74 newPosition: vscode.Position,
75 ): boolean {
76 if (!this.expectedAcceptance) return false;
77
78 // Check document match.
79 if (this.expectedAcceptance.documentUri !== document.uri.toString()) {
80 return false;
81 }
82
83 // Check document version (must be newer).
84 if (document.version <= this.expectedAcceptance.documentVersion) {
85 return false;
86 }
87
88 // Check if cursor is at expected end position.
89 const expectedEndPos = new vscode.Position(
90 this.expectedAcceptance.endLine,
91 this.expectedAcceptance.endCharacter,
92 );
93
94 if (newPosition.isEqual(expectedEndPos)) {
95 // The cursor is where we'd expect after accepting the ghost text.
96
97 // Verify text was inserted (optional additional check).
98 const startPos = new vscode.Position(
99 this.expectedAcceptance.startLine,
100 this.expectedAcceptance.startCharacter,
101 );
102 const expectedText = this.expectedAcceptance.text;
103
104 try {
105 const actualRange = new vscode.Range(startPos, expectedEndPos);
106
107 const actualText = document.getText(actualRange);
108
109 if (actualText === expectedText) {
110 // Clear the expectation.
111 this.expectedAcceptance = null;
112 return true;
113 }
114 } catch (error) {
115 // Range might be invalid, just fall through.
116 }
117 }
118
119 return false;
120 }
121
122 public registerSelectionChangeHandler(): void {
123 const manager = SelectionChangeManager.getInstance();
124
125 manager.registerListener(
126 "ghostTextTracker",
127 async (e, state) => {
128 if (!state.document || !state.cursorPosition) {
129 return false;
130 }
131
132 const wasGhostTextAccepted = this.checkGhostTextWasAccepted(
133 state.document,
134 state.cursorPosition,
135 );
136
137 if (wasGhostTextAccepted) {
138 console.log(
139 "GhostTextAcceptanceTracker: ghost text was accepted, preserving chain",
140 );
141 return true;
142 }
143
144 return false;
145 },
146 HandlerPriority.HIGH,
147 );
148 }
149}