| 1 | import Parser from "web-tree-sitter"; |
| 2 | |
| 3 | /** |
| 4 | * Singleton class that keeps track of a map of document paths to their history. |
| 5 | * The point here is to prevent re-calculating the AST, |
| 6 | * and to preserve an older, original state of the document before any user edits. |
| 7 | */ |
| 8 | export class DocumentHistoryTracker { |
| 9 | private static instance: DocumentHistoryTracker | null = null; |
| 10 | |
| 11 | // Map from document path to history (LIFO stack where newest representation is at the front). |
| 12 | private documentAstMap: Map<string, Parser.Tree[]>; |
| 13 | private documentContentHistoryMap: Map<string, string[]>; |
| 14 | |
| 15 | private constructor() { |
| 16 | this.documentAstMap = new Map<string, Parser.Tree[]>(); |
| 17 | this.documentContentHistoryMap = new Map<string, string[]>(); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Get the singleton instance of DocumentHistoryTracker. |
| 22 | */ |
| 23 | public static getInstance(): DocumentHistoryTracker { |
| 24 | if (!DocumentHistoryTracker.instance) { |
| 25 | DocumentHistoryTracker.instance = new DocumentHistoryTracker(); |
| 26 | } |
| 27 | |
| 28 | return DocumentHistoryTracker.instance; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Add a document and its first state to the tracker. |
| 33 | * |
| 34 | * @param documentPath The path of the document. |
| 35 | * @param documentContent The first content of the document. |
| 36 | * @param ast The first AST of the document. |
| 37 | */ |
| 38 | public addDocument( |
| 39 | documentPath: string, |
| 40 | documentContent: string, |
| 41 | ast: Parser.Tree, |
| 42 | ): void { |
| 43 | this.documentAstMap.set(documentPath, [ast]); |
| 44 | this.documentContentHistoryMap.set(documentPath, [documentContent]); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Push a new AST to an existing document's history stack. |
| 49 | * |
| 50 | * @param documentPath The path of the document. |
| 51 | * @param documentContent The new content to push to the document's history stack. |
| 52 | * @param ast The new AST to push to the document's history stack. |
| 53 | * @throws Error if the document doesn't exist in the tracker. |
| 54 | */ |
| 55 | public push( |
| 56 | documentPath: string, |
| 57 | documentContent: string, |
| 58 | ast: Parser.Tree, |
| 59 | ): void { |
| 60 | const astHistory = this.documentAstMap.get(documentPath); |
| 61 | const documentHistory = this.documentContentHistoryMap.get(documentPath); |
| 62 | |
| 63 | if (!astHistory || !documentHistory) { |
| 64 | console.error(`Document ${documentPath} not found in AST tracker`); |
| 65 | this.addDocument(documentPath, documentContent, ast); |
| 66 | return; // Early return - document was added with initial state |
| 67 | } |
| 68 | |
| 69 | // Only execute this if the arrays already existed |
| 70 | astHistory.unshift(ast); |
| 71 | documentHistory.unshift(documentContent); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get the most recent AST of a document. |
| 76 | * |
| 77 | * @param documentPath The path of the document. |
| 78 | * @returns The most recent AST of the document. |
| 79 | * @throws Error if the document doesn't exist in the tracker. |
| 80 | */ |
| 81 | public getMostRecentAst(documentPath: string): Parser.Tree | null { |
| 82 | const astHistory = this.documentAstMap.get(documentPath); |
| 83 | |
| 84 | if (!astHistory) { |
| 85 | console.error(`Document ${documentPath} not found in AST tracker`); |
| 86 | return null; |
| 87 | } |
| 88 | if (astHistory.length === 0) { |
| 89 | console.error(`Document ${documentPath} has no ASTs`); |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | // Return the first element (most recent AST). |
| 94 | return astHistory[0]; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get the most recent AST of a document. |
| 99 | * |
| 100 | * @param documentPath The path of the document. |
| 101 | * @returns The most recent document history of the document. |
| 102 | * @throws Error if the document doesn't exist in the tracker. |
| 103 | */ |
| 104 | public getMostRecentDocumentHistory(documentPath: string): string | null { |
| 105 | const documentHistory = this.documentContentHistoryMap.get(documentPath); |
| 106 | |
| 107 | if (!documentHistory) { |
| 108 | console.error(`Document ${documentPath} not found in AST tracker`); |
| 109 | return null; |
| 110 | } |
| 111 | if (documentHistory.length === 0) { |
| 112 | console.error(`Document ${documentPath} has no history`); |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | // Return the first element (most recent doc history). |
| 117 | return documentHistory[0]; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Delete a document from the tracker. |
| 122 | * |
| 123 | * @param documentPath The path of the document to delete. |
| 124 | */ |
| 125 | public deleteDocument(documentPath: string): void { |
| 126 | this.documentAstMap.delete(documentPath); |
| 127 | this.documentContentHistoryMap.delete(documentPath); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Clear all documents from the tracker. |
| 132 | */ |
| 133 | public clearMap(): void { |
| 134 | this.documentAstMap.clear(); |
| 135 | this.documentContentHistoryMap.clear(); |
| 136 | } |
| 137 | } |