( allowEmpty?: boolean, range?: vscode.Range, )
| 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({ |
no test coverage detected