| 1 | import { IDE } from "../.."; |
| 2 | import { getRangeInString } from "../../util/ranges"; |
| 3 | import { languageForFilepath } from "../constants/AutocompleteLanguageInfo"; |
| 4 | import { AutocompleteInput } from "../util/types"; |
| 5 | |
| 6 | /** |
| 7 | * We have to handle a few edge cases in getting the entire prefix/suffix for the current file. |
| 8 | * This is entirely prior to finding snippets from other files |
| 9 | */ |
| 10 | export async function constructInitialPrefixSuffix( |
| 11 | input: AutocompleteInput, |
| 12 | ide: IDE, |
| 13 | ): Promise<{ |
| 14 | prefix: string; |
| 15 | suffix: string; |
| 16 | }> { |
| 17 | const lang = languageForFilepath(input.filepath); |
| 18 | |
| 19 | const fileContents = |
| 20 | input.manuallyPassFileContents ?? (await ide.readFile(input.filepath)); |
| 21 | const fileLines = fileContents.split("\n"); |
| 22 | let prefix = |
| 23 | getRangeInString(fileContents, { |
| 24 | start: { line: 0, character: 0 }, |
| 25 | end: input.selectedCompletionInfo?.range.start ?? input.pos, |
| 26 | }) + (input.selectedCompletionInfo?.text ?? ""); |
| 27 | |
| 28 | if (input.injectDetails) { |
| 29 | const lines = prefix.split("\n"); |
| 30 | prefix = `${lines.slice(0, -1).join("\n")}\n${ |
| 31 | lang.singleLineComment |
| 32 | } ${input.injectDetails |
| 33 | .split("\n") |
| 34 | .join(`\n${lang.singleLineComment} `)}\n${lines[lines.length - 1]}`; |
| 35 | } |
| 36 | |
| 37 | const suffix = getRangeInString(fileContents, { |
| 38 | start: input.pos, |
| 39 | end: { line: fileLines.length - 1, character: Number.MAX_SAFE_INTEGER }, |
| 40 | }); |
| 41 | |
| 42 | return { prefix, suffix }; |
| 43 | } |