| 1 | import { IDE, Position, Range, RangeInFileWithNextEditInfo } from "core"; |
| 2 | import { AutocompleteCodeSnippet } from "core/autocomplete/snippets/types"; |
| 3 | import { GetLspDefinitionsFunction } from "core/autocomplete/types"; |
| 4 | import { ConfigHandler } from "core/config/ConfigHandler"; |
| 5 | import { RecentlyEditedRange } from "core/nextEdit/types"; |
| 6 | import { getContinueGlobalPath, isFileWithinFolder } from "core/util/paths"; |
| 7 | import fs from "fs"; |
| 8 | import { resolve } from "path"; |
| 9 | import * as vscode from "vscode"; |
| 10 | import { ContinueCompletionProvider } from "../autocomplete/completionProvider"; |
| 11 | |
| 12 | export const getBeforeCursorPos = (range: Range, activePos: Position) => { |
| 13 | // whichever side of the range isn't active is the before position |
| 14 | if ( |
| 15 | range.start.line === activePos.line && |
| 16 | range.start.character === activePos.character |
| 17 | ) { |
| 18 | return range.end as Position; |
| 19 | } else { |
| 20 | return range.start as Position; |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | const isEditLoggingAllowed = async (editedFileURI: string) => { |
| 25 | const globalContinuePath = getContinueGlobalPath(); |
| 26 | const editLogDirsPath = resolve(globalContinuePath, ".editlogdirs"); |
| 27 | |
| 28 | try { |
| 29 | const fileContent = await fs.promises.readFile(editLogDirsPath, "utf-8"); |
| 30 | const stringItems = fileContent |
| 31 | .split("\n") |
| 32 | .map((line) => line.trim()) |
| 33 | .filter((line) => line.length > 0); |
| 34 | |
| 35 | return stringItems.some((dir) => isFileWithinFolder(editedFileURI, dir)); |
| 36 | } catch (error) { |
| 37 | return false; |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | const getWorkspaceDirUri = async (event: vscode.TextDocumentChangeEvent) => { |
| 42 | const workspaceFolder = vscode.workspace.getWorkspaceFolder( |
| 43 | event.document.uri, |
| 44 | ); |
| 45 | if (!workspaceFolder) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | const workspaceDirUri = workspaceFolder.uri.toString(); |
| 50 | return workspaceDirUri; |
| 51 | }; |
| 52 | |
| 53 | export const handleTextDocumentChange = async ( |
| 54 | event: vscode.TextDocumentChangeEvent, |
| 55 | configHandler: ConfigHandler, |
| 56 | ide: IDE, |
| 57 | completionProvider: ContinueCompletionProvider, |
| 58 | getDefinitionsFromLsp: GetLspDefinitionsFunction, |
| 59 | ) => { |
| 60 | const changes = event.contentChanges; |
| 61 | const editor = vscode.window.activeTextEditor; |
| 62 | const { config } = await configHandler.loadConfig(); |
| 63 | |
| 64 | // if (!config?.experimental?.logEditingData) return; |
| 65 | if (!editor) return; |
| 66 | if (event.contentChanges.length === 0) return; |
| 67 | |
| 68 | // Ensure that logging will only happen in the open-source continue repo |
| 69 | const workspaceDirUri = await getWorkspaceDirUri(event); |
| 70 | if (!workspaceDirUri) return; |
| 71 | if (!(await isEditLoggingAllowed(event.document.uri.toString()))) return; |
| 72 | |
| 73 | const activeCursorPos = editor.selection.active; |
| 74 | const editActions: RangeInFileWithNextEditInfo[] = changes.map((change) => ({ |
| 75 | filepath: event.document.uri.toString(), |
| 76 | range: { |
| 77 | start: change.range.start as Position, |
| 78 | end: change.range.end as Position, |
| 79 | }, |
| 80 | fileContents: event.document.getText(), |
| 81 | editText: change.text, |
| 82 | beforeCursorPos: getBeforeCursorPos(change.range, activeCursorPos), |
| 83 | afterCursorPos: activeCursorPos as Position, |
| 84 | workspaceDir: workspaceDirUri, |
| 85 | })); |
| 86 | |
| 87 | let recentlyEditedRanges: RecentlyEditedRange[] = []; |
| 88 | let recentlyVisitedRanges: AutocompleteCodeSnippet[] = []; |
| 89 | |
| 90 | if (completionProvider) { |
| 91 | recentlyEditedRanges = |
| 92 | await completionProvider.recentlyEditedTracker.getRecentlyEditedRanges(); |
| 93 | recentlyVisitedRanges = |
| 94 | completionProvider.recentlyVisitedRanges.getSnippets(); |
| 95 | } |
| 96 | |
| 97 | return { |
| 98 | actions: editActions, |
| 99 | configHandler: configHandler, |
| 100 | getDefsFromLspFunction: getDefinitionsFromLsp, |
| 101 | recentlyEditedRanges, |
| 102 | recentlyVisitedRanges, |
| 103 | }; |
| 104 | }; |