|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as path from 'path' |
| 3 | +import * as fs from 'fs/promises' |
| 4 | +import { FileItem } from '../context/providers/workspace-provider' |
| 5 | + |
| 6 | +export const set_range_command = () => { |
| 7 | + return vscode.commands.registerCommand( |
| 8 | + 'codeWebChat.setRange', |
| 9 | + async (item: FileItem) => { |
| 10 | + if (!item || !item.resourceUri) { |
| 11 | + return |
| 12 | + } |
| 13 | + const file_path = item.resourceUri.fsPath |
| 14 | + const workspace_folder = vscode.workspace.getWorkspaceFolder( |
| 15 | + item.resourceUri |
| 16 | + ) |
| 17 | + |
| 18 | + if (!workspace_folder) { |
| 19 | + vscode.window.showErrorMessage('File is not in a workspace folder.') |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + const ranges_file_path = path.join( |
| 24 | + workspace_folder.uri.fsPath, |
| 25 | + '.vscode', |
| 26 | + 'ranges.json' |
| 27 | + ) |
| 28 | + let ranges: Record<string, string> = {} |
| 29 | + try { |
| 30 | + const content = await fs.readFile(ranges_file_path, 'utf-8') |
| 31 | + ranges = JSON.parse(content) |
| 32 | + } catch (error) { |
| 33 | + // File might not exist, which is fine |
| 34 | + } |
| 35 | + |
| 36 | + const relative_path = path.relative( |
| 37 | + workspace_folder.uri.fsPath, |
| 38 | + file_path |
| 39 | + ) |
| 40 | + const current_range = ranges[relative_path] |
| 41 | + |
| 42 | + const new_range = await vscode.window.showInputBox({ |
| 43 | + prompt: `Set range of lines for ${path.basename(file_path)}`, |
| 44 | + title: 'Range', |
| 45 | + placeHolder: 'e.g., 100-300 400- -500 or empty to clear', |
| 46 | + value: current_range, |
| 47 | + validateInput: (value) => { |
| 48 | + if (value.trim() == '') return null |
| 49 | + |
| 50 | + const parts = value.trim().split(/\s+/) |
| 51 | + const parsed_ranges: { |
| 52 | + start: number |
| 53 | + end: number |
| 54 | + original: string |
| 55 | + }[] = [] |
| 56 | + |
| 57 | + for (const part of parts) { |
| 58 | + const match = part.match(/^(\d+)?-(\d+)?$/) |
| 59 | + if (!match) { |
| 60 | + return `Invalid format in "${part}". Use formats like 100-300, 100-, -300.` |
| 61 | + } |
| 62 | + |
| 63 | + const [, start_str, end_str] = match |
| 64 | + |
| 65 | + if (!start_str && !end_str) { |
| 66 | + return 'Invalid range "-". Specify at least a start or an end line.' |
| 67 | + } |
| 68 | + |
| 69 | + const start = start_str ? parseInt(start_str, 10) : null |
| 70 | + const end = end_str ? parseInt(end_str, 10) : null |
| 71 | + |
| 72 | + if (start !== null && start < 1) { |
| 73 | + return `Start line must be 1 or greater in "${part}".` |
| 74 | + } |
| 75 | + |
| 76 | + if (end !== null && end < 1) { |
| 77 | + return `End line must be 1 or greater in "${part}".` |
| 78 | + } |
| 79 | + |
| 80 | + if (start !== null && end !== null) { |
| 81 | + if (start > end) { |
| 82 | + return `Start line cannot be greater than end line in "${part}".` |
| 83 | + } |
| 84 | + if (start == end) { |
| 85 | + return `Start and end lines cannot be the same in "${part}".` |
| 86 | + } |
| 87 | + } |
| 88 | + parsed_ranges.push({ |
| 89 | + start: start ?? 1, |
| 90 | + end: end ?? Number.MAX_SAFE_INTEGER, |
| 91 | + original: part |
| 92 | + }) |
| 93 | + } |
| 94 | + parsed_ranges.sort((a, b) => a.start - b.start) |
| 95 | + |
| 96 | + for (let i = 0; i < parsed_ranges.length - 1; i++) { |
| 97 | + if (parsed_ranges[i].end >= parsed_ranges[i + 1].start) { |
| 98 | + return `Ranges cannot overlap: "${ |
| 99 | + parsed_ranges[i].original |
| 100 | + }" and "${parsed_ranges[i + 1].original}".` |
| 101 | + } |
| 102 | + } |
| 103 | + return null |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + if (new_range === undefined) { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + if (new_range) { |
| 112 | + ranges[relative_path] = new_range.trim().split(/\s+/).join(' ') |
| 113 | + } else { |
| 114 | + delete ranges[relative_path] |
| 115 | + } |
| 116 | + |
| 117 | + try { |
| 118 | + await fs.mkdir(path.dirname(ranges_file_path), { recursive: true }) |
| 119 | + await fs.writeFile(ranges_file_path, JSON.stringify(ranges, null, 2)) |
| 120 | + } catch (error: any) { |
| 121 | + vscode.window.showErrorMessage(`Failed to save range: ${error.message}`) |
| 122 | + } |
| 123 | + } |
| 124 | + ) |
| 125 | +} |
0 commit comments