| 1 | import { v4 as uuidv4 } from "uuid"; |
| 2 | |
| 3 | import { ContextItemWithId, RangeInFileWithContents } from "../"; |
| 4 | import { findUriInDirs, getUriPathBasename } from "../util/uri"; |
| 5 | |
| 6 | export function rifWithContentsToContextItem( |
| 7 | rif: RangeInFileWithContents, |
| 8 | ): ContextItemWithId { |
| 9 | const basename = getUriPathBasename(rif.filepath); |
| 10 | const { relativePathOrBasename, foundInDir, uri } = findUriInDirs( |
| 11 | rif.filepath, |
| 12 | window.workspacePaths ?? [], |
| 13 | ); |
| 14 | const rangeStr = `(${rif.range.start.line + 1}-${rif.range.end.line + 1})`; |
| 15 | |
| 16 | return { |
| 17 | content: rif.contents, |
| 18 | name: `${basename} ${rangeStr}`, |
| 19 | description: `${relativePathOrBasename} ${rangeStr}`, |
| 20 | id: { |
| 21 | providerTitle: "code", |
| 22 | itemId: uuidv4(), |
| 23 | }, |
| 24 | uri: { |
| 25 | type: "file", |
| 26 | value: rif.filepath, |
| 27 | }, |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | export function ctxItemToRifWithContents( |
| 32 | item: ContextItemWithId, |
| 33 | linesOffByOne = false, |
| 34 | ): RangeInFileWithContents { |
| 35 | let startLine = 0; |
| 36 | let endLine = 0; |
| 37 | let adjustLines = linesOffByOne ? 1 : 0; |
| 38 | |
| 39 | const nameSplit = item.name.split("("); |
| 40 | |
| 41 | if (nameSplit.length > 1) { |
| 42 | const lines = nameSplit[1].split(")")[0].split("-"); |
| 43 | startLine = Number.parseInt(lines[0], 10); |
| 44 | if (startLine === 0) { |
| 45 | adjustLines = 0; // safety to make sure doesn't go negative |
| 46 | } |
| 47 | startLine -= adjustLines; |
| 48 | endLine = Number.parseInt(lines[1], 10) - adjustLines; |
| 49 | } |
| 50 | |
| 51 | const rif: RangeInFileWithContents = { |
| 52 | filepath: item.uri?.value || "", |
| 53 | range: { |
| 54 | start: { |
| 55 | line: startLine, |
| 56 | character: 0, |
| 57 | }, |
| 58 | end: { |
| 59 | line: endLine, |
| 60 | character: 0, |
| 61 | }, |
| 62 | }, |
| 63 | contents: item.content, |
| 64 | }; |
| 65 | |
| 66 | return rif; |
| 67 | } |