refactor: clean up

This commit is contained in:
Jacob Kim 2025-07-28 15:34:51 -07:00
parent e5708fcb82
commit f68bf5759f
7 changed files with 32 additions and 107 deletions

View file

@ -189,12 +189,6 @@ export default class BaseRetrievalPipeline implements IRetrievalPipeline {
);
return [];
}
// if (!this.lanceDbIndex) {
// console.warn(
// "LanceDB index not available, skipping embeddings retrieval",
// );
// return [];
// }
return this.lanceDbIndex.retrieve(
input,

View file

@ -242,10 +242,3 @@ export function cosineSimilarity(a: number[], b: number[]): number {
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
// export async function getJumpDestinations(
// codeSnippet: string,
// cursorPosition: Position,
// filepath: string,
// ): Promise<Chunk[]> {
// }

View file

@ -1,59 +1,38 @@
// TODO:
// Create a DocumentAstTracker singleton class that keeps track of a map of document paths to their history of ASTs.
// There should be a map from document path to history of ASTs (a LIFO stack of ASTs where the newest AST is at the front)
// We want to expose these methods:
// add document and its first AST
// push to an existing document's AST history stack
// get the most recent AST of an existing document's AST
// The AST and nodes will be using web-tree-sitter types.
// save map to file as a documentAstTracker.json inside the user's global continue path
// delete document from map
// clear map
import * as fs from "fs";
import * as path from "path";
import Parser from "web-tree-sitter";
import { getContinueGlobalPath } from "../util/paths";
/**
* Singleton class that keeps track of a map of document paths to their history of ASTs.
* Singleton class that keeps track of a map of document paths to their history.
* The point here is to prevent re-calculating the AST,
* and to preserve an older, original state of the document before any user edits.
*/
export class DocumentAstTracker {
private static instance: DocumentAstTracker | null = null;
export class DocumentHistoryTracker {
private static instance: DocumentHistoryTracker | null = null;
// Map from document path to history of ASTs (LIFO stack where newest AST is at the front).
// Map from document path to history (LIFO stack where newest representation is at the front).
private documentAstMap: Map<string, Parser.Tree[]>;
private documentContentHistoryMap: Map<string, string[]>;
// Path to save the AST tracker data.
private readonly savePath: string;
private constructor() {
this.documentAstMap = new Map<string, Parser.Tree[]>();
this.documentContentHistoryMap = new Map<string, string[]>();
this.savePath = path.join(
getContinueGlobalPath(),
"documentAstTracker.jsonl",
);
// Try to load existing data.
// this.loadFromFile();
}
/**
* Get the singleton instance of DocumentAstTracker.
* Get the singleton instance of DocumentHistoryTracker.
*/
public static getInstance(): DocumentAstTracker {
if (!DocumentAstTracker.instance) {
DocumentAstTracker.instance = new DocumentAstTracker();
public static getInstance(): DocumentHistoryTracker {
if (!DocumentHistoryTracker.instance) {
DocumentHistoryTracker.instance = new DocumentHistoryTracker();
}
return DocumentAstTracker.instance;
return DocumentHistoryTracker.instance;
}
/**
* Add a document and its first AST to the tracker.
* Add a document and its first state to the tracker.
*
* @param documentPath The path of the document.
* @param documentContent The first content of the document.
* @param ast The first AST of the document.
*/
public addDocument(
@ -63,17 +42,17 @@ export class DocumentAstTracker {
): void {
this.documentAstMap.set(documentPath, [ast]);
this.documentContentHistoryMap.set(documentPath, [documentContent]);
// this.saveToFile();
}
/**
* Push a new AST to an existing document's history stack.
*
* @param documentPath The path of the document.
* @param documentContent The new content to push to the document's history stack.
* @param ast The new AST to push to the document's history stack.
* @throws Error if the document doesn't exist in the tracker.
*/
public pushAst(
public push(
documentPath: string,
documentContent: string,
ast: Parser.Tree,
@ -86,12 +65,9 @@ export class DocumentAstTracker {
this.addDocument(documentPath, documentContent, ast);
}
console.log("same AST:", this.getMostRecentAst(documentPath) !== ast);
// Add the new AST to the front of the array (LIFO stack).
// Push to top (LIFO).
astHistory!.unshift(ast);
documentHistory!.unshift(documentContent);
// this.saveToFile();
}
/**
@ -148,7 +124,6 @@ export class DocumentAstTracker {
public deleteDocument(documentPath: string): void {
this.documentAstMap.delete(documentPath);
this.documentContentHistoryMap.delete(documentPath);
// this.saveToFile();
}
/**
@ -157,51 +132,5 @@ export class DocumentAstTracker {
public clearMap(): void {
this.documentAstMap.clear();
this.documentContentHistoryMap.clear();
// this.saveToFile();
}
/**
* Save the current state of the tracker to a file.
*/
private saveToFile(): void {
try {
// We can't directly serialize Tree objects to JSON.
// So we'll just save the paths - this serves as a record of which documents we're tracking.
// The actual ASTs will need to be recreated when needed.
const documentPaths = Array.from(this.documentAstMap.keys());
const serializableData = { documentPaths };
fs.writeFileSync(
this.savePath,
JSON.stringify(serializableData, null, 2),
);
} catch (error) {
console.error("Error saving AST tracker data to file:", error);
}
}
/**
* Load the tracker state from a file.
*/
private loadFromFile(): void {
try {
if (fs.existsSync(this.savePath)) {
const fileContent = fs.readFileSync(this.savePath, "utf-8");
const data = JSON.parse(fileContent);
// We only saved the paths, not the actual Trees.
// The Trees need to be reparsed when the documents are loaded again.
if (data && data.documentPaths) {
// Initialize empty arrays for each document.
data.documentPaths.forEach((path: string) => {
this.documentAstMap.set(path, []);
});
}
}
} catch (error) {
console.error("Error loading AST tracker data from file:", error);
// If loading fails, we'll start with an empty map.
this.documentAstMap = new Map<string, Parser.Tree[]>();
}
}
}

View file

@ -1,7 +1,7 @@
import Parser from "web-tree-sitter";
import { Chunk, IDE, ILLM, Position, Range, RangeInFile } from "..";
import { getAst } from "../autocomplete/util/ast";
import { DocumentAstTracker } from "./DocumentHistoryTracker";
import { DocumentHistoryTracker } from "./DocumentHistoryTracker";
export enum EditableRegionStrategy {
Naive = "naive",
@ -362,7 +362,7 @@ async function staticJump(ctx: {
// Get the file's AST.
// Getting this once helps us live-track the current node.
const tree =
await DocumentAstTracker.getInstance().getMostRecentAst(filepath);
await DocumentHistoryTracker.getInstance().getMostRecentAst(filepath);
// const tree = await getAst(filepath, fileContent);
if (!tree) return null;

View file

@ -26,7 +26,7 @@ import {
} from "./constants.js";
import { createDiff, DiffFormatType } from "./context/diffFormatting.js";
import { calculateFinalCursorPosition } from "./diff/diff.js";
import { DocumentAstTracker } from "./DocumentHistoryTracker.js";
import { DocumentHistoryTracker } from "./DocumentHistoryTracker.js";
import {
EditableRegionStrategy,
getNextEditableRegion,
@ -224,7 +224,7 @@ export class NextEditProvider {
).toString();
const ast = await getAst(this.previousRequest.filepath, fileContent);
if (ast) {
DocumentAstTracker.getInstance().pushAst(
DocumentHistoryTracker.getInstance().push(
localPathOrUriToPath(this.previousRequest.filepath),
fileContent,
ast,
@ -311,7 +311,7 @@ export class NextEditProvider {
} else {
const historyDiff = createDiff({
beforeContent:
DocumentAstTracker.getInstance().getMostRecentDocumentHistory(
DocumentHistoryTracker.getInstance().getMostRecentDocumentHistory(
localPathOrUriToPath(helper.filepath),
) ?? "",
afterContent: helper.fileContents,

View file

@ -10,6 +10,15 @@ export interface ExpectedGhostTextAcceptance {
endCharacter: number;
}
/**
* This singleton tracks whether a given ghost text is accepted or not.
* We need this because there is no clean way of determining if a ghost text has been accepted outside of vscode command callback.
* The above mentioned callback is not viable because it's too slow.
* 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.
* We need to differentiate the ghost text acceptance from a deliberate cursor movement to reject the completion.
* 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.
* This is not what we want, as we want to keep the current chain of edits alive when the user accepts the completion.
*/
export class GhostTextAcceptanceTracker {
private static instance: GhostTextAcceptanceTracker | undefined;
private expectedAcceptance: ExpectedGhostTextAcceptance | null = null;

View file

@ -43,7 +43,7 @@ import { ConfigYamlDocumentLinkProvider } from "./ConfigYamlDocumentLinkProvider
import { VsCodeMessenger } from "./VsCodeMessenger";
import { getAst } from "core/autocomplete/util/ast";
import { DocumentAstTracker } from "core/nextEdit/DocumentHistoryTracker";
import { DocumentHistoryTracker } from "core/nextEdit/DocumentHistoryTracker";
import {
EditableRegionStrategy,
getNextEditableRegion,
@ -384,7 +384,7 @@ export class VsCodeExtension {
console.log("onDidOpenTextDocument");
const ast = await getAst(event.fileName, event.getText());
if (ast) {
DocumentAstTracker.getInstance().addDocument(
DocumentHistoryTracker.getInstance().addDocument(
localPathOrUriToPath(event.fileName),
event.getText(),
ast,