| 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 * as vscode from "vscode"; |
| 7 | import { ContinueCompletionProvider } from "../autocomplete/completionProvider"; |
| 8 | |
| 9 | export const getBeforeCursorPos = (range: Range, activePos: Position) => { |
| 10 | // whichever side of the range isn't active is the before position |
| 11 | if ( |
| 12 | range.start.line === activePos.line && |
| 13 | range.start.character === activePos.character |
| 14 | ) { |
| 15 | return range.end as Position; |
| 16 | } else { |
| 17 | return range.start as Position; |
| 18 | } |
| 19 | }; |
| 20 | |
| 21 | const getWorkspaceDirInfo = async ( |
| 22 | event: vscode.TextDocumentChangeEvent, |
| 23 | ide: IDE, |
| 24 | ) => { |
| 25 | // gets the workspace dir uri and checks if it's the open-source continue repo |
| 26 | const workspaceFolder = vscode.workspace.getWorkspaceFolder( |
| 27 | event.document.uri, |
| 28 | ); |
| 29 | if (!workspaceFolder) { |
| 30 | return false; |
| 31 | } |
| 32 | const workspaceDirUri = workspaceFolder.uri.toString(); |
| 33 | const repoName = await ide.getRepoName(workspaceDirUri); |
| 34 | return { |
| 35 | workspaceDirUri: workspaceDirUri, |
| 36 | isContinueRepo: repoName === "continuedev/continue", |
| 37 | }; |
| 38 | }; |
| 39 | |
| 40 | export const handleTextDocumentChange = async ( |
| 41 | event: vscode.TextDocumentChangeEvent, |
| 42 | configHandler: ConfigHandler, |
| 43 | ide: IDE, |
| 44 | completionProvider: ContinueCompletionProvider, |
| 45 | getDefinitionsFromLsp: GetLspDefinitionsFunction, |
| 46 | ) => { |
| 47 | const changes = event.contentChanges; |
| 48 | const editor = vscode.window.activeTextEditor; |
| 49 | const { config } = await configHandler.loadConfig(); |
| 50 | |
| 51 | if (!config?.experimental?.logEditingData) return; |
| 52 | if (!editor) return; |
| 53 | if (event.contentChanges.length === 0) return; |
| 54 | |
| 55 | // Ensure that loggin will only happen in the open-source continue repo |
| 56 | const workspaceDirInfo = await getWorkspaceDirInfo(event, ide); |
| 57 | if (!workspaceDirInfo) return; |
| 58 | // if (!workspaceDirInfo.isContinueRepo) return; |
| 59 | |
| 60 | const activeCursorPos = editor.selection.active; |
| 61 | const editActions: RangeInFileWithNextEditInfo[] = changes.map((change) => ({ |
| 62 | filepath: event.document.uri.toString(), |
| 63 | range: { |
| 64 | start: change.range.start as Position, |
| 65 | end: change.range.end as Position, |
| 66 | }, |
| 67 | fileContents: event.document.getText(), |
| 68 | editText: change.text, |
| 69 | beforeCursorPos: getBeforeCursorPos(change.range, activeCursorPos), |
| 70 | afterCursorPos: activeCursorPos as Position, |
| 71 | workspaceDir: workspaceDirInfo.workspaceDirUri, |
| 72 | })); |
| 73 | |
| 74 | let recentlyEditedRanges: RecentlyEditedRange[] = []; |
| 75 | let recentlyVisitedRanges: AutocompleteCodeSnippet[] = []; |
| 76 | |
| 77 | if (completionProvider) { |
| 78 | recentlyEditedRanges = |
| 79 | await completionProvider.recentlyEditedTracker.getRecentlyEditedRanges(); |
| 80 | recentlyVisitedRanges = |
| 81 | completionProvider.recentlyVisitedRanges.getSnippets(); |
| 82 | } |
| 83 | |
| 84 | return { |
| 85 | actions: editActions, |
| 86 | configHandler: configHandler, |
| 87 | getDefsFromLspFunction: getDefinitionsFromLsp, |
| 88 | recentlyEditedRanges, |
| 89 | recentlyVisitedRanges, |
| 90 | }; |
| 91 | }; |