| 1 | import { getLastNUriRelativePathParts } from "../../util/uri"; |
| 2 | import { |
| 3 | AutocompleteClipboardSnippet, |
| 4 | AutocompleteCodeSnippet, |
| 5 | AutocompleteDiffSnippet, |
| 6 | AutocompleteSnippet, |
| 7 | AutocompleteSnippetType, |
| 8 | } from "../snippets/types"; |
| 9 | import { HelperVars } from "../util/HelperVars"; |
| 10 | |
| 11 | const getCommentMark = (helper: HelperVars) => { |
| 12 | return helper.lang.singleLineComment; |
| 13 | }; |
| 14 | |
| 15 | const addCommentMarks = (text: string, helper: HelperVars) => { |
| 16 | const commentMark = getCommentMark(helper); |
| 17 | return text |
| 18 | .trim() |
| 19 | .split("\n") |
| 20 | .map((line) => `${commentMark} ${line}`) |
| 21 | .join("\n"); |
| 22 | }; |
| 23 | |
| 24 | const formatClipboardSnippet = ( |
| 25 | snippet: AutocompleteClipboardSnippet, |
| 26 | workspaceDirs: string[], |
| 27 | ): AutocompleteCodeSnippet => { |
| 28 | return formatCodeSnippet( |
| 29 | { |
| 30 | filepath: "file:///Untitled.txt", |
| 31 | content: snippet.content, |
| 32 | type: AutocompleteSnippetType.Code, |
| 33 | }, |
| 34 | workspaceDirs, |
| 35 | ); |
| 36 | }; |
| 37 | |
| 38 | const formatCodeSnippet = ( |
| 39 | snippet: AutocompleteCodeSnippet, |
| 40 | workspaceDirs: string[], |
| 41 | ): AutocompleteCodeSnippet => { |
| 42 | return { |
| 43 | ...snippet, |
| 44 | content: `Path: ${getLastNUriRelativePathParts(workspaceDirs, snippet.filepath, 2)}\n${snippet.content}`, |
| 45 | }; |
| 46 | }; |
| 47 | |
| 48 | const formatDiffSnippet = ( |
| 49 | snippet: AutocompleteDiffSnippet, |
| 50 | ): AutocompleteDiffSnippet => { |
| 51 | return snippet; |
| 52 | }; |
| 53 | |
| 54 | const commentifySnippet = ( |
| 55 | helper: HelperVars, |
| 56 | snippet: AutocompleteSnippet, |
| 57 | ): AutocompleteSnippet => { |
| 58 | return { |
| 59 | ...snippet, |
| 60 | content: addCommentMarks(snippet.content, helper), |
| 61 | }; |
| 62 | }; |
| 63 | |
| 64 | export const formatSnippets = ( |
| 65 | helper: HelperVars, |
| 66 | snippets: AutocompleteSnippet[], |
| 67 | workspaceDirs: string[], |
| 68 | ): string => { |
| 69 | const currentFilepathComment = addCommentMarks( |
| 70 | getLastNUriRelativePathParts(workspaceDirs, helper.filepath, 2), |
| 71 | helper, |
| 72 | ); |
| 73 | |
| 74 | return ( |
| 75 | snippets |
| 76 | .map((snippet) => { |
| 77 | switch (snippet.type) { |
| 78 | case AutocompleteSnippetType.Code: |
| 79 | return formatCodeSnippet(snippet, workspaceDirs); |
| 80 | case AutocompleteSnippetType.Diff: |
| 81 | return formatDiffSnippet(snippet); |
| 82 | case AutocompleteSnippetType.Clipboard: |
| 83 | return formatClipboardSnippet(snippet, workspaceDirs); |
| 84 | } |
| 85 | }) |
| 86 | .map((item) => { |
| 87 | return commentifySnippet(helper, item).content; |
| 88 | }) |
| 89 | .join("\n") + `\n${currentFilepathComment}` |
| 90 | ); |
| 91 | }; |