|
1 | 1 | import * as vscode from 'vscode' |
2 | 2 | import * as path from 'path' |
3 | 3 | import * as fs from 'fs/promises' |
4 | | -import { FileItem } from '../context/providers/workspace-provider' |
| 4 | +import { |
| 5 | + WorkspaceProvider, |
| 6 | + FileItem |
| 7 | +} from '../context/providers/workspace-provider' |
| 8 | +import { |
| 9 | + RANGES_STATE_KEY, |
| 10 | + LAST_RANGE_SAVE_LOCATION_STATE_KEY |
| 11 | +} from '../constants/state-keys' |
5 | 12 |
|
6 | | -export const set_range_command = () => { |
| 13 | +export const set_range_command = ( |
| 14 | + workspace_provider: WorkspaceProvider, |
| 15 | + context: vscode.ExtensionContext |
| 16 | +) => { |
7 | 17 | return vscode.commands.registerCommand( |
8 | 18 | 'codeWebChat.setRange', |
9 | 19 | async (item: FileItem) => { |
10 | 20 | if (!item || !item.resourceUri) { |
11 | 21 | return |
12 | 22 | } |
| 23 | + |
13 | 24 | const file_path = item.resourceUri.fsPath |
14 | 25 | const workspace_folder = vscode.workspace.getWorkspaceFolder( |
15 | 26 | item.resourceUri |
16 | 27 | ) |
| 28 | + const workspace_folders = vscode.workspace.workspaceFolders || [] |
17 | 29 |
|
18 | 30 | if (!workspace_folder) { |
19 | 31 | vscode.window.showErrorMessage('File is not in a workspace folder.') |
20 | 32 | return |
21 | 33 | } |
22 | 34 |
|
| 35 | + const relative_path = path.relative( |
| 36 | + workspace_folder.uri.fsPath, |
| 37 | + file_path |
| 38 | + ) |
| 39 | + |
| 40 | + const get_path_key_for_state = () => { |
| 41 | + const rel_path_unix = relative_path.replace(/\\/g, '/') |
| 42 | + if (workspace_folders.length > 1) { |
| 43 | + return `${workspace_folder.name}:${rel_path_unix}` |
| 44 | + } |
| 45 | + return rel_path_unix |
| 46 | + } |
| 47 | + const state_key = get_path_key_for_state() |
| 48 | + |
| 49 | + // Load ranges from file |
23 | 50 | const ranges_file_path = path.join( |
24 | 51 | workspace_folder.uri.fsPath, |
25 | 52 | '.vscode', |
26 | 53 | 'ranges.json' |
27 | 54 | ) |
28 | | - let ranges: Record<string, string> = {} |
| 55 | + let file_ranges: Record<string, string> = {} |
29 | 56 | try { |
30 | 57 | const content = await fs.readFile(ranges_file_path, 'utf-8') |
31 | | - ranges = JSON.parse(content) |
| 58 | + file_ranges = JSON.parse(content) |
32 | 59 | } catch (error) { |
33 | 60 | // File might not exist, which is fine |
34 | 61 | } |
35 | 62 |
|
36 | | - const relative_path = path.relative( |
37 | | - workspace_folder.uri.fsPath, |
38 | | - file_path |
| 63 | + // Load ranges from state |
| 64 | + const state_ranges = context.workspaceState.get<Record<string, string>>( |
| 65 | + RANGES_STATE_KEY, |
| 66 | + {} |
39 | 67 | ) |
40 | | - const current_range = ranges[relative_path] |
| 68 | + |
| 69 | + const file_range_value = file_ranges[relative_path] |
| 70 | + const state_range_value = state_ranges[state_key] |
| 71 | + const current_range = file_range_value || state_range_value |
41 | 72 |
|
42 | 73 | const new_range = await vscode.window.showInputBox({ |
43 | 74 | prompt: `Set range of lines for ${path.basename(file_path)}`, |
@@ -108,17 +139,130 @@ export const set_range_command = () => { |
108 | 139 | return |
109 | 140 | } |
110 | 141 |
|
111 | | - if (new_range) { |
112 | | - ranges[relative_path] = new_range.trim().split(/\s+/).join(' ') |
113 | | - } else { |
114 | | - delete ranges[relative_path] |
| 142 | + // If range is cleared, remove from both locations |
| 143 | + if (!new_range) { |
| 144 | + let changed = false |
| 145 | + if (file_ranges[relative_path]) { |
| 146 | + delete file_ranges[relative_path] |
| 147 | + try { |
| 148 | + if (Object.keys(file_ranges).length === 0) { |
| 149 | + await fs.unlink(ranges_file_path).catch(() => {}) |
| 150 | + } else { |
| 151 | + await fs.writeFile( |
| 152 | + ranges_file_path, |
| 153 | + JSON.stringify(file_ranges, null, 2) |
| 154 | + ) |
| 155 | + } |
| 156 | + changed = true |
| 157 | + } catch (error: any) { |
| 158 | + vscode.window.showErrorMessage( |
| 159 | + `Failed to update range in file: ${error.message}` |
| 160 | + ) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if (state_ranges[state_key]) { |
| 165 | + const updated_state = { ...state_ranges } |
| 166 | + delete updated_state[state_key] |
| 167 | + await context.workspaceState.update(RANGES_STATE_KEY, updated_state) |
| 168 | + changed = true |
| 169 | + } |
| 170 | + |
| 171 | + if (changed) { |
| 172 | + await workspace_provider.load_all_ranges() |
| 173 | + } |
| 174 | + return |
115 | 175 | } |
116 | 176 |
|
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}`) |
| 177 | + // If range is set, determine where to save |
| 178 | + let save_location: 'file' | 'state' | undefined |
| 179 | + |
| 180 | + if (file_range_value) save_location = 'file' |
| 181 | + else if (state_range_value) save_location = 'state' |
| 182 | + else { |
| 183 | + const last_location = context.workspaceState.get< |
| 184 | + 'file' | 'state' | undefined |
| 185 | + >(LAST_RANGE_SAVE_LOCATION_STATE_KEY) |
| 186 | + |
| 187 | + const choice = await vscode.window.showQuickPick( |
| 188 | + [ |
| 189 | + { |
| 190 | + label: 'JSON File', |
| 191 | + description: '.vscode/ranges.json', |
| 192 | + picked: last_location != 'state' |
| 193 | + }, |
| 194 | + { |
| 195 | + label: 'Workspace State', |
| 196 | + description: 'Internal storage', |
| 197 | + picked: last_location == 'state' |
| 198 | + } |
| 199 | + ], |
| 200 | + { placeHolder: 'Where do you want to save this range?' } |
| 201 | + ) |
| 202 | + |
| 203 | + if (!choice) return |
| 204 | + save_location = choice.label == 'JSON File' ? 'file' : 'state' |
| 205 | + await context.workspaceState.update( |
| 206 | + LAST_RANGE_SAVE_LOCATION_STATE_KEY, |
| 207 | + save_location |
| 208 | + ) |
| 209 | + } |
| 210 | + |
| 211 | + if (new_range) { |
| 212 | + const formatted_range = new_range.trim().split(/\s+/).join(' ') |
| 213 | + |
| 214 | + if (save_location === 'file') { |
| 215 | + file_ranges[relative_path] = formatted_range |
| 216 | + try { |
| 217 | + await fs.mkdir(path.dirname(ranges_file_path), { recursive: true }) |
| 218 | + await fs.writeFile( |
| 219 | + ranges_file_path, |
| 220 | + JSON.stringify(file_ranges, null, 2) |
| 221 | + ) |
| 222 | + |
| 223 | + // Remove from state if exists |
| 224 | + if (state_ranges[state_key]) { |
| 225 | + const updated_state = { ...state_ranges } |
| 226 | + delete updated_state[state_key] |
| 227 | + await context.workspaceState.update( |
| 228 | + RANGES_STATE_KEY, |
| 229 | + updated_state |
| 230 | + ) |
| 231 | + } |
| 232 | + } catch (error: any) { |
| 233 | + vscode.window.showErrorMessage( |
| 234 | + `Failed to save range to file: ${error.message}` |
| 235 | + ) |
| 236 | + } |
| 237 | + } else { |
| 238 | + // Save to state |
| 239 | + const updated_state = { |
| 240 | + ...state_ranges, |
| 241 | + [state_key]: formatted_range |
| 242 | + } |
| 243 | + await context.workspaceState.update(RANGES_STATE_KEY, updated_state) |
| 244 | + |
| 245 | + // Remove from file if exists |
| 246 | + if (file_ranges[relative_path]) { |
| 247 | + delete file_ranges[relative_path] |
| 248 | + try { |
| 249 | + if (Object.keys(file_ranges).length === 0) { |
| 250 | + await fs.unlink(ranges_file_path).catch(() => {}) |
| 251 | + } else { |
| 252 | + await fs.writeFile( |
| 253 | + ranges_file_path, |
| 254 | + JSON.stringify(file_ranges, null, 2) |
| 255 | + ) |
| 256 | + } |
| 257 | + } catch (error: any) { |
| 258 | + vscode.window.showErrorMessage( |
| 259 | + `Failed to clean up range from file: ${error.message}` |
| 260 | + ) |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + await workspace_provider.load_all_ranges() |
122 | 266 | } |
123 | 267 | } |
124 | 268 | ) |
|
0 commit comments