| 1 | import * as os from "node:os"; |
| 2 | |
| 3 | import { RangeInFileWithContents } from "core"; |
| 4 | import * as vscode from "vscode"; |
| 5 | |
| 6 | import { VsCodeIdeUtils } from "./ideUtils"; |
| 7 | |
| 8 | import type { VsCodeWebviewProtocol } from "../webviewProtocol"; |
| 9 | |
| 10 | export function getRangeInFileWithContents( |
| 11 | allowEmpty?: boolean, |
| 12 | range?: vscode.Range, |
| 13 | ): RangeInFileWithContents | null { |
| 14 | const editor = vscode.window.activeTextEditor; |
| 15 | |
| 16 | if (editor) { |
| 17 | const selection = editor.selection; |
| 18 | const filepath = editor.document.uri.toString(); |
| 19 | |
| 20 | if (range) { |
| 21 | const contents = editor.document.getText(range); |
| 22 | |
| 23 | return { |
| 24 | range: { |
| 25 | start: { |
| 26 | line: range.start.line, |
| 27 | character: range.start.character, |
| 28 | }, |
| 29 | end: { |
| 30 | line: range.end.line, |
| 31 | character: range.end.character, |
| 32 | }, |
| 33 | }, |
| 34 | filepath, |
| 35 | contents, |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | if (selection.isEmpty && !allowEmpty) { |
| 40 | return null; |
| 41 | } |
| 42 | |
| 43 | let selectionRange = new vscode.Range(selection.start, selection.end); |
| 44 | const document = editor.document; |
| 45 | // Select the context from the beginning of the selection start line to the selection start position |
| 46 | const beginningOfSelectionStartLine = selection.start.with(undefined, 0); |
| 47 | const textBeforeSelectionStart = document.getText( |
| 48 | new vscode.Range(beginningOfSelectionStartLine, selection.start), |
| 49 | ); |
| 50 | // If there are only whitespace before the start of the selection, include the indentation |
| 51 | if (textBeforeSelectionStart.trim().length === 0) { |
| 52 | selectionRange = selectionRange.with({ |
| 53 | start: beginningOfSelectionStartLine, |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | const contents = editor.document.getText(selectionRange); |
| 58 | |
| 59 | return { |
| 60 | filepath, |
| 61 | contents, |
| 62 | range: { |
| 63 | start: { |
| 64 | line: selection.start.line, |
| 65 | character: selection.start.character, |
| 66 | }, |
| 67 | end: { |
| 68 | line: selection.end.line, |
| 69 | character: selection.end.character, |
| 70 | }, |
| 71 | }, |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | export async function addHighlightedCodeToContext( |
| 79 | webviewProtocol: VsCodeWebviewProtocol | undefined, |
| 80 | ) { |
| 81 | const rangeInFileWithContents = getRangeInFileWithContents(); |
| 82 | if (rangeInFileWithContents) { |
| 83 | webviewProtocol?.request("highlightedCode", { |
| 84 | rangeInFileWithContents, |
| 85 | }); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | export async function addEntireFileToContext( |
| 90 | uri: vscode.Uri, |
| 91 | webviewProtocol: VsCodeWebviewProtocol | undefined, |
| 92 | ideUtils: VsCodeIdeUtils, |
| 93 | ) { |
| 94 | // If a directory, add all files in the directory |
| 95 | const stat = await ideUtils.stat(uri); |
| 96 | if (stat?.type === vscode.FileType.Directory) { |
| 97 | const files = (await ideUtils.readDirectory(uri))!; //files can't be null if we reached this point |
| 98 | for (const [filename, type] of files) { |
| 99 | if (type === vscode.FileType.File) { |
| 100 | addEntireFileToContext( |
| 101 | vscode.Uri.joinPath(uri, filename), |
| 102 | webviewProtocol, |
| 103 | ideUtils, |
| 104 | ); |
| 105 | } |
| 106 | } |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Get the contents of the file |
| 111 | const contents = (await vscode.workspace.fs.readFile(uri)).toString(); |
| 112 | const rangeInFileWithContents = { |
| 113 | filepath: uri.toString(), |
| 114 | contents: contents, |
| 115 | range: { |
| 116 | start: { |
| 117 | line: 0, |
| 118 | character: 0, |
| 119 | }, |
| 120 | end: { |
| 121 | line: contents.split(os.EOL).length - 1, |
| 122 | character: 0, |
| 123 | }, |
| 124 | }, |
| 125 | }; |
| 126 | |
| 127 | webviewProtocol?.request("highlightedCode", { |
| 128 | rangeInFileWithContents, |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | export function addCodeToContextFromRange( |
| 133 | range: vscode.Range, |
| 134 | webviewProtocol: VsCodeWebviewProtocol, |
| 135 | prompt?: string, |
| 136 | ) { |
| 137 | const document = vscode.window.activeTextEditor?.document; |
| 138 | |
| 139 | if (!document) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | const rangeInFileWithContents = { |
| 144 | filepath: document.uri.toString(), |
| 145 | contents: document.getText(range), |
| 146 | range: { |
| 147 | start: { |
| 148 | line: range.start.line, |
| 149 | character: range.start.character, |
| 150 | }, |
| 151 | end: { |
| 152 | line: range.end.line, |
| 153 | character: range.end.character, |
| 154 | }, |
| 155 | }, |
| 156 | }; |
| 157 | |
| 158 | webviewProtocol?.request("highlightedCode", { |
| 159 | rangeInFileWithContents, |
| 160 | prompt, |
| 161 | // Assume `true` since range selection is currently only used for quick actions/fixes |
| 162 | shouldRun: true, |
| 163 | }); |
| 164 | } |