Skip to content

Commit 212c9f4

Browse files
committed
Update the range command to use workspace provider and context for dual storage file and state with user preference selection
1 parent f95c4e4 commit 212c9f4

4 files changed

Lines changed: 213 additions & 24 deletions

File tree

‎packages/vscode/src/commands/set-range-command.ts‎

Lines changed: 161 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,74 @@
11
import * as vscode from 'vscode'
22
import * as path from 'path'
33
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'
512

6-
export const set_range_command = () => {
13+
export const set_range_command = (
14+
workspace_provider: WorkspaceProvider,
15+
context: vscode.ExtensionContext
16+
) => {
717
return vscode.commands.registerCommand(
818
'codeWebChat.setRange',
919
async (item: FileItem) => {
1020
if (!item || !item.resourceUri) {
1121
return
1222
}
23+
1324
const file_path = item.resourceUri.fsPath
1425
const workspace_folder = vscode.workspace.getWorkspaceFolder(
1526
item.resourceUri
1627
)
28+
const workspace_folders = vscode.workspace.workspaceFolders || []
1729

1830
if (!workspace_folder) {
1931
vscode.window.showErrorMessage('File is not in a workspace folder.')
2032
return
2133
}
2234

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
2350
const ranges_file_path = path.join(
2451
workspace_folder.uri.fsPath,
2552
'.vscode',
2653
'ranges.json'
2754
)
28-
let ranges: Record<string, string> = {}
55+
let file_ranges: Record<string, string> = {}
2956
try {
3057
const content = await fs.readFile(ranges_file_path, 'utf-8')
31-
ranges = JSON.parse(content)
58+
file_ranges = JSON.parse(content)
3259
} catch (error) {
3360
// File might not exist, which is fine
3461
}
3562

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+
{}
3967
)
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
4172

4273
const new_range = await vscode.window.showInputBox({
4374
prompt: `Set range of lines for ${path.basename(file_path)}`,
@@ -108,17 +139,130 @@ export const set_range_command = () => {
108139
return
109140
}
110141

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
115175
}
116176

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()
122266
}
123267
}
124268
)

‎packages/vscode/src/constants/state-keys.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export const LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY =
66
'last-applied-clipboard-content'
77
export const SAVED_CONTEXTS_STATE_KEY = 'savedContexts'
88
export const LAST_CONTEXT_SAVE_LOCATION_STATE_KEY = 'lastSaveLocation'
9+
export const RANGES_STATE_KEY = 'ranges'
10+
export const LAST_RANGE_SAVE_LOCATION_STATE_KEY = 'last-range-save-location'
911
export const LAST_APPLY_CONTEXT_OPTION_STATE_KEY = 'last-apply-context-option'
1012
export const LAST_SELECTED_WORKSPACE_CONTEXT_NAME_STATE_KEY =
1113
'last-selected-workspace-context-name'

‎packages/vscode/src/context/providers/workspace-provider.ts‎

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import * as vscode from 'vscode'
22
import * as fs from 'fs'
33
import * as path from 'path'
44
import ignore, { Ignore } from 'ignore'
5-
import { CONTEXT_CHECKED_PATHS_STATE_KEY } from '../../constants/state-keys'
5+
import {
6+
CONTEXT_CHECKED_PATHS_STATE_KEY,
7+
RANGES_STATE_KEY
8+
} from '../../constants/state-keys'
69
import { IGNORE_PATTERNS } from '../../constants/ignore-patterns'
710
import { natural_sort } from '../../utils/natural-sort'
811
import { dictionary } from '@shared/constants/dictionary'
@@ -87,11 +90,11 @@ export class WorkspaceProvider
8790
this.ranges_watcher = vscode.workspace.createFileSystemWatcher(
8891
'**/.vscode/ranges.json'
8992
)
90-
this.ranges_watcher.onDidCreate(() => this._load_all_ranges_files())
91-
this.ranges_watcher.onDidChange(() => this._load_all_ranges_files())
92-
this.ranges_watcher.onDidDelete(() => this._load_all_ranges_files())
93+
this.ranges_watcher.onDidCreate(() => this.load_all_ranges())
94+
this.ranges_watcher.onDidChange(() => this.load_all_ranges())
95+
this.ranges_watcher.onDidDelete(() => this.load_all_ranges())
9396

94-
this.ranges_initialization = this._load_all_ranges_files()
97+
this.ranges_initialization = this.load_all_ranges()
9598
}
9699

97100
private async _save_checked_files_state(): Promise<void> {
@@ -660,7 +663,7 @@ export class WorkspaceProvider
660663
}
661664
return items
662665
}
663-
private async _load_all_ranges_files(): Promise<void> {
666+
public async load_all_ranges(): Promise<void> {
664667
this.file_ranges.clear()
665668
for (const workspace_root of this.workspace_roots) {
666669
const ranges_file_path = path.join(
@@ -681,12 +684,52 @@ export class WorkspaceProvider
681684
}
682685
}
683686
}
687+
688+
// Load from state
689+
const state_ranges = this.context.workspaceState.get<
690+
Record<string, string>
691+
>(RANGES_STATE_KEY, {})
692+
for (const [key, range] of Object.entries(state_ranges)) {
693+
const resolved_path = this._resolve_state_path(key)
694+
if (resolved_path && fs.existsSync(resolved_path)) {
695+
this.file_ranges.set(resolved_path, range)
696+
}
697+
}
698+
684699
this.file_token_counts.clear()
685700
this.directory_token_counts.clear()
686701
this.directory_selected_token_counts.clear()
687702
this.refresh()
688703
}
689704

705+
private _resolve_state_path(key: string): string | undefined {
706+
// If single root, assume relative path
707+
if (this.workspace_roots.length === 1) {
708+
return path.join(this.workspace_roots[0], key)
709+
}
710+
711+
// Multi-root: expect "Name:path"
712+
const parts = key.split(':')
713+
if (parts.length > 1) {
714+
const name = parts[0]
715+
const rel_path = parts.slice(1).join(':')
716+
const index = this.workspace_names.indexOf(name)
717+
if (index !== -1) {
718+
return path.join(this.workspace_roots[index], rel_path)
719+
}
720+
}
721+
722+
// Fallback: try to find the file in any root (legacy or if logic changes)
723+
for (const root of this.workspace_roots) {
724+
const abs = path.join(root, key)
725+
if (fs.existsSync(abs)) {
726+
return abs
727+
}
728+
}
729+
730+
return undefined
731+
}
732+
690733
public get_range(filePath: string): string | undefined {
691734
return this.file_ranges.get(filePath)
692735
}

‎packages/vscode/src/extension.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export async function activate(context: vscode.ExtensionContext) {
112112
rename_command(),
113113
delete_command(),
114114
save_context_command(workspace_provider, context),
115-
set_range_command(),
115+
set_range_command(workspace_provider, context),
116116
find_paths_in_clipboard_command(workspace_provider),
117117
duplicate_workspace_command(workspace_provider, websites_provider, context),
118118
open_url_command({

0 commit comments

Comments
 (0)