| 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) || isEmptyFile(editor.document)) { |
| 40 | return null; |
| 41 | } |
| 42 | |
| 43 | let selectionRange: vscode.Range | undefined; |
| 44 | // if the selection is empty and document is not empty, select the whole document |
| 45 | if (selection.isEmpty) { |
| 46 | selectionRange = new vscode.Range( |
| 47 | new vscode.Position(0, 0), |
| 48 | new vscode.Position( |
| 49 | editor.document.lineCount - 1, |
| 50 | editor.document.lineAt( |
| 51 | editor.document.lineCount - 1, |
| 52 | ).range.end.character, |
| 53 | ), |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | if (!selectionRange) { |
| 58 | selectionRange = new vscode.Range(selection.start, selection.end); |
| 59 | const document = editor.document; |
| 60 | // Select the context from the beginning of the selection start line to the selection start position |
| 61 | const beginningOfSelectionStartLine = selection.start.with(undefined, 0); |
| 62 | const textBeforeSelectionStart = document.getText( |
| 63 | new vscode.Range(beginningOfSelectionStartLine, selection.start), |
| 64 | ); |
| 65 | // If there are only whitespace before the start of the selection, include the indentation |
| 66 | if (textBeforeSelectionStart.trim().length === 0) { |
| 67 | selectionRange = selectionRange.with({ |
| 68 | start: beginningOfSelectionStartLine, |
| 69 | }); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const contents = editor.document.getText(selectionRange); |
| 74 | |
| 75 | return { |
| 76 | filepath, |
| 77 | contents, |
| 78 | range: { |
| 79 | start: { |
| 80 | line: selectionRange.start.line, |
| 81 | character: selectionRange.start.character, |
| 82 | }, |
| 83 | end: { |
| 84 | line: selectionRange.end.line, |
| 85 | character: selectionRange.end.character, |
| 86 | }, |
| 87 | }, |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | export async function addHighlightedCodeToContext( |
| 95 | webviewProtocol: VsCodeWebviewProtocol | undefined, |
| 96 | ) { |
| 97 | const rangeInFileWithContents = getRangeInFileWithContents(true); |
| 98 | if (rangeInFileWithContents) { |
| 99 | webviewProtocol?.request("highlightedCode", { |
| 100 | rangeInFileWithContents, |
| 101 | }); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | export async function addEntireFileToContext( |
| 106 | uri: vscode.Uri, |
| 107 | webviewProtocol: VsCodeWebviewProtocol | undefined, |
| 108 | ideUtils: VsCodeIdeUtils, |
| 109 | ) { |
| 110 | // If a directory, add all files in the directory |
| 111 | const stat = await ideUtils.stat(uri); |
| 112 | if (stat?.type === vscode.FileType.Directory) { |
| 113 | const files = (await ideUtils.readDirectory(uri))!; //files can't be null if we reached this point |
| 114 | for (const [filename, type] of files) { |
| 115 | if (type === vscode.FileType.File) { |
| 116 | addEntireFileToContext( |
| 117 | vscode.Uri.joinPath(uri, filename), |
| 118 | webviewProtocol, |
| 119 | ideUtils, |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | // Get the contents of the file |
| 127 | const contents = (await vscode.workspace.fs.readFile(uri)).toString(); |
| 128 | const rangeInFileWithContents = { |
| 129 | filepath: uri.toString(), |
| 130 | contents: contents, |
| 131 | range: { |
| 132 | start: { |
| 133 | line: 0, |
| 134 | character: 0, |
| 135 | }, |
| 136 | end: { |
| 137 | line: contents.split(os.EOL).length - 1, |
| 138 | character: 0, |
| 139 | }, |
| 140 | }, |
| 141 | }; |
| 142 | |
| 143 | webviewProtocol?.request("highlightedCode", { |
| 144 | rangeInFileWithContents, |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | export function isEmptyFile(document: vscode.TextDocument) { |
| 149 | return document.lineCount === 1 && document.lineAt(0).range.isEmpty; |
| 150 | } |
| 151 | |
| 152 | export function addCodeToContextFromRange( |
| 153 | range: vscode.Range, |
| 154 | webviewProtocol: VsCodeWebviewProtocol, |
| 155 | prompt?: string, |
| 156 | ) { |
| 157 | const document = vscode.window.activeTextEditor?.document; |
| 158 | |
| 159 | if (!document) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | const rangeInFileWithContents = { |
| 164 | filepath: document.uri.toString(), |
| 165 | contents: document.getText(range), |
| 166 | range: { |
| 167 | start: { |
| 168 | line: range.start.line, |
| 169 | character: range.start.character, |
| 170 | }, |
| 171 | end: { |
| 172 | line: range.end.line, |
| 173 | character: range.end.character, |
| 174 | }, |
| 175 | }, |
| 176 | }; |
| 177 | |
| 178 | webviewProtocol?.request("highlightedCode", { |
| 179 | rangeInFileWithContents, |
| 180 | prompt, |
| 181 | // Assume `true` since range selection is currently only used for quick actions/fixes |
| 182 | shouldRun: true, |
| 183 | }); |
| 184 | } |