| 1 | import { RangeInFileWithContents } from "core"; |
| 2 | import { getSymbolsForSnippet } from "core/autocomplete/context/ranking"; |
| 3 | import { RecentlyEditedRange } from "core/autocomplete/util/types"; |
| 4 | import * as vscode from "vscode"; |
| 5 | |
| 6 | import { VsCodeIdeUtils } from "../util/ideUtils"; |
| 7 | |
| 8 | type VsCodeRecentlyEditedRange = { |
| 9 | uri: vscode.Uri; |
| 10 | range: vscode.Range; |
| 11 | } & Omit<RecentlyEditedRange, "filepath" | "range">; |
| 12 | |
| 13 | interface VsCodeRecentlyEditedDocument { |
| 14 | timestamp: number; |
| 15 | uri: vscode.Uri; |
| 16 | } |
| 17 | |
| 18 | export class RecentlyEditedTracker { |
| 19 | private static staleTime = 1000 * 60 * 2; |
| 20 | private static maxRecentlyEditedRanges = 3; |
| 21 | private recentlyEditedRanges: VsCodeRecentlyEditedRange[] = []; |
| 22 | |
| 23 | private recentlyEditedDocuments: VsCodeRecentlyEditedDocument[] = []; |
| 24 | private static maxRecentlyEditedDocuments = 10; |
| 25 | |
| 26 | constructor(private ideUtils: VsCodeIdeUtils) { |
| 27 | vscode.workspace.onDidChangeTextDocument((event) => { |
| 28 | event.contentChanges.forEach((change) => { |
| 29 | const editedRange = { |
| 30 | uri: event.document.uri, |
| 31 | range: new vscode.Range( |
| 32 | new vscode.Position(change.range.start.line, 0), |
| 33 | new vscode.Position(change.range.end.line + 1, 0), |
| 34 | ), |
| 35 | timestamp: Date.now(), |
| 36 | }; |
| 37 | this.insertRange(editedRange); |
| 38 | }); |
| 39 | |
| 40 | this.insertDocument(event.document.uri); |
| 41 | }); |
| 42 | |
| 43 | setInterval(() => { |
| 44 | this.removeOldEntries(); |
| 45 | }, 1000 * 15); |
| 46 | } |
| 47 | |
| 48 | private async insertRange( |
| 49 | editedRange: Omit<VsCodeRecentlyEditedRange, "lines" | "symbols">, |
| 50 | ): Promise<void> { |
| 51 | if (editedRange.uri.scheme !== "file") { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Check for overlap with any existing ranges |
| 56 | for (let i = 0; i < this.recentlyEditedRanges.length; i++) { |
| 57 | let range = this.recentlyEditedRanges[i]; |
| 58 | if (range.range.intersection(editedRange.range)) { |
| 59 | const union = range.range.union(editedRange.range); |
| 60 | const contents = await this._getContentsForRange({ |
| 61 | ...range, |
| 62 | range: union, |
| 63 | }); |
| 64 | range = { |
| 65 | ...range, |
| 66 | range: union, |
| 67 | lines: contents.split("\n"), |
| 68 | symbols: getSymbolsForSnippet(contents), |
| 69 | }; |
| 70 | this.recentlyEditedRanges[i] = range; |
| 71 | return; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Otherwise, just add the new and maintain max size |
| 76 | const contents = await this._getContentsForRange(editedRange); |
| 77 | const newLength = this.recentlyEditedRanges.unshift({ |
| 78 | ...editedRange, |
| 79 | lines: contents.split("\n"), |
| 80 | symbols: getSymbolsForSnippet(contents), |
| 81 | }); |
| 82 | if (newLength >= RecentlyEditedTracker.maxRecentlyEditedRanges) { |
| 83 | this.recentlyEditedRanges = this.recentlyEditedRanges.slice( |
| 84 | 0, |
| 85 | RecentlyEditedTracker.maxRecentlyEditedRanges, |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private insertDocument(uri: vscode.Uri): void { |
| 91 | // Don't add a duplicate |
| 92 | if (this.recentlyEditedDocuments.some((doc) => doc.uri === uri)) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | const newLength = this.recentlyEditedDocuments.unshift({ |
| 97 | uri, |
| 98 | timestamp: Date.now(), |
| 99 | }); |
| 100 | if (newLength >= RecentlyEditedTracker.maxRecentlyEditedDocuments) { |
| 101 | this.recentlyEditedDocuments = this.recentlyEditedDocuments.slice( |
| 102 | 0, |
| 103 | RecentlyEditedTracker.maxRecentlyEditedDocuments, |
| 104 | ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private removeOldEntries() { |
| 109 | this.recentlyEditedRanges = this.recentlyEditedRanges.filter( |
| 110 | (entry) => entry.timestamp > Date.now() - RecentlyEditedTracker.staleTime, |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | private async _getContentsForRange( |
| 115 | entry: Omit<VsCodeRecentlyEditedRange, "lines" | "symbols">, |
| 116 | ): Promise<string> { |
| 117 | const content = await this.ideUtils.readFile(entry.uri); |
| 118 | if (content === null) { |
| 119 | return ""; |
| 120 | } |
| 121 | return content |
| 122 | .toString() |
| 123 | .split("\n") |
| 124 | .slice(entry.range.start.line, entry.range.end.line + 1) |
| 125 | .join("\n"); |
| 126 | } |
| 127 | |
| 128 | public async getRecentlyEditedRanges(): Promise<RecentlyEditedRange[]> { |
| 129 | return this.recentlyEditedRanges.map((entry) => { |
| 130 | return { |
| 131 | ...entry, |
| 132 | filepath: entry.uri.toString(), |
| 133 | }; |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | public async getRecentlyEditedDocuments(): Promise< |
| 138 | RangeInFileWithContents[] |
| 139 | > { |
| 140 | const results = await Promise.all( |
| 141 | this.recentlyEditedDocuments.map(async (entry) => { |
| 142 | try { |
| 143 | const contents = await vscode.workspace.fs |
| 144 | .readFile(entry.uri) |
| 145 | .then((content) => content.toString()); |
| 146 | const lines = contents.split("\n"); |
| 147 | |
| 148 | return { |
| 149 | filepath: entry.uri.toString(), |
| 150 | contents, |
| 151 | range: { |
| 152 | start: { line: 0, character: 0 }, |
| 153 | end: { |
| 154 | line: lines.length - 1, |
| 155 | character: lines[lines.length - 1].length, |
| 156 | }, |
| 157 | }, |
| 158 | }; |
| 159 | } catch (e) { |
| 160 | return null; |
| 161 | } |
| 162 | }), |
| 163 | ); |
| 164 | |
| 165 | return results.filter((result) => result !== null) as any; |
| 166 | } |
| 167 | } |