| 1 | import { Position, Range } from "../.."; |
| 2 | import { |
| 3 | MERCURY_CODE_TO_EDIT_CLOSE, |
| 4 | MERCURY_CODE_TO_EDIT_OPEN, |
| 5 | MERCURY_CURSOR, |
| 6 | MERCURY_RECENTLY_VIEWED_CODE_SNIPPET_CLOSE, |
| 7 | MERCURY_RECENTLY_VIEWED_CODE_SNIPPET_OPEN, |
| 8 | } from "../constants"; |
| 9 | import { insertCursorToken } from "./utils"; |
| 10 | |
| 11 | export function recentlyViewedCodeSnippetsBlock( |
| 12 | recentlyViewedCodeSnippets: { filepath: string; content: string }[], |
| 13 | ) { |
| 14 | return recentlyViewedCodeSnippets.reduce((acc, snippet, i) => { |
| 15 | const block = [ |
| 16 | MERCURY_RECENTLY_VIEWED_CODE_SNIPPET_OPEN, |
| 17 | `code_snippet_file_path: ${snippet.filepath}`, |
| 18 | snippet.content, |
| 19 | MERCURY_RECENTLY_VIEWED_CODE_SNIPPET_CLOSE, |
| 20 | ].join("\n"); |
| 21 | |
| 22 | return ( |
| 23 | acc + block + (i === recentlyViewedCodeSnippets.length - 1 ? "" : "\n") |
| 24 | ); |
| 25 | }, ""); |
| 26 | } |
| 27 | |
| 28 | export function currentFileContentBlock( |
| 29 | currentFileContent: string, |
| 30 | editableRegionStartLine: number, |
| 31 | editableRegionEndLine: number, |
| 32 | cursorPosition: Position, |
| 33 | ) { |
| 34 | const currentFileContentLines = currentFileContent.split("\n"); |
| 35 | |
| 36 | const insertedCursorLines = insertCursorToken( |
| 37 | currentFileContentLines, |
| 38 | cursorPosition, |
| 39 | MERCURY_CURSOR, |
| 40 | ); |
| 41 | |
| 42 | const instrumentedLines = [ |
| 43 | ...insertedCursorLines.slice(0, editableRegionStartLine), |
| 44 | MERCURY_CODE_TO_EDIT_OPEN, |
| 45 | ...insertedCursorLines.slice( |
| 46 | editableRegionStartLine, |
| 47 | editableRegionEndLine + 1, |
| 48 | ), |
| 49 | MERCURY_CODE_TO_EDIT_CLOSE, |
| 50 | ...insertedCursorLines.slice(editableRegionEndLine + 1), |
| 51 | ]; |
| 52 | |
| 53 | return instrumentedLines.join("\n"); |
| 54 | } |
| 55 | |
| 56 | export function editHistoryBlock( |
| 57 | editDiffHistory: string[], // could be a singe large unified diff |
| 58 | ) { |
| 59 | // diffHistory is made from createDiff. |
| 60 | // This uses createPatch from npm diff library, which includes an index line and a separator. |
| 61 | // We get rid of these first two lines. |
| 62 | return editDiffHistory |
| 63 | .map((diff) => diff.split("\n").slice(2).join("\n")) |
| 64 | .join("\n"); |
| 65 | // return editDiffHistory.split("\n").slice(2).join("\n"); |
| 66 | } |
| 67 | |
| 68 | function mercuryNextEditTemplateBuilder( |
| 69 | recentlyViewedCodeSnippets: { filepath: string; code: string }[], |
| 70 | currentFileContent: string, |
| 71 | codeToEdit: string, |
| 72 | codeToEditRange: Range, |
| 73 | cursorPosition: Position, |
| 74 | editDiffHistory: string, // could be a singe large unified diff |
| 75 | ): string { |
| 76 | return ""; |
| 77 | } |