Elffuss BraidHub

kikocis/continue public

18c867793b67 · 3.7 KiBRawPermalink
1import * as vscode from "vscode";
2
3export interface ExpectedGhostTextAcceptance {
4 documentUri: string;
5 documentVersion: number;
6 text: string;
7 startLine: number;
8 startCharacter: number;
9 endLine: number;
10 endCharacter: number;
11}
12
13/**
14 * This singleton tracks whether a given ghost text is accepted or not.
15 * We need this because there is no clean way of determining if a ghost text has been accepted outside of vscode command callback.
16 * The above mentioned callback is not viable because it's too slow.
17 * 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.
18 * We need to differentiate the ghost text acceptance from a deliberate cursor movement to reject the completion.
19 * 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.
20 * This is not what we want, as we want to keep the current chain of edits alive when the user accepts the completion.
21 */
22export class GhostTextAcceptanceTracker {
23 private static instance: GhostTextAcceptanceTracker | undefined;
24 private expectedAcceptance: ExpectedGhostTextAcceptance | null = null;
25
26 private constructor() {}
27
28 public static getInstance(): GhostTextAcceptanceTracker {
29 if (!GhostTextAcceptanceTracker.instance) {
30 GhostTextAcceptanceTracker.instance = new GhostTextAcceptanceTracker();
31 }
32 return GhostTextAcceptanceTracker.instance;
33 }
34
35 public static clearInstance() {
36 GhostTextAcceptanceTracker.instance = undefined;
37 }
38
39 public setExpectedGhostTextAcceptance(
40 document: vscode.TextDocument,
41 text: string,
42 startPosition: vscode.Position,
43 ) {
44 // Calculate end position
45 const lines = text.split("\n");
46 let endLine: number;
47 let endCharacter: number;
48
49 if (lines.length > 1) {
50 endLine = startPosition.line + lines.length - 1;
51 endCharacter = lines[lines.length - 1].length;
52 } else {
53 endLine = startPosition.line;
54 endCharacter = startPosition.character + text.length;
55 }
56
57 this.expectedAcceptance = {
58 documentUri: document.uri.toString(),
59 documentVersion: document.version,
60 text,
61 startLine: startPosition.line,
62 startCharacter: startPosition.character,
63 endLine,
64 endCharacter,
65 };
66 }
67
68 public checkGhostTextWasAccepted(
69 document: vscode.TextDocument,
70 newPosition: vscode.Position,
71 ): boolean {
72 if (!this.expectedAcceptance) return false;
73
74 // Check document match.
75 if (this.expectedAcceptance.documentUri !== document.uri.toString()) {
76 return false;
77 }
78
79 // Check document version (must be newer).
80 if (document.version <= this.expectedAcceptance.documentVersion) {
81 return false;
82 }
83
84 // Check if cursor is at expected end position.
85 const expectedEndPos = new vscode.Position(
86 this.expectedAcceptance.endLine,
87 this.expectedAcceptance.endCharacter,
88 );
89
90 if (newPosition.isEqual(expectedEndPos)) {
91 // The cursor is where we'd expect after accepting the ghost text.
92
93 // Verify text was inserted (optional additional check).
94 const startPos = new vscode.Position(
95 this.expectedAcceptance.startLine,
96 this.expectedAcceptance.startCharacter,
97 );
98 const expectedText = this.expectedAcceptance.text;
99
100 try {
101 const actualRange = new vscode.Range(startPos, expectedEndPos);
102
103 const actualText = document.getText(actualRange);
104
105 if (actualText === expectedText) {
106 // Clear the expectation.
107 this.expectedAcceptance = null;
108 return true;
109 }
110 } catch (error) {
111 // Range might be invalid, just fall through.
112 }
113 }
114
115 return false;
116 }
117}