Skip to content

Commit 24f70ab

Browse files
bryceitoc9justinmk3
authored andcommitted
feat(dev): display all globalState
- Introduce "SHOW ALL" option for globalState. - For "Edit globalState": list all keys, instead of requiring user to provide key name.
1 parent 9b05a3f commit 24f70ab

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

src/dev/activation.ts

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,12 @@ class VirtualObjectFile implements FileProvider {
169169
const value = (await this.storage.get(key)) ?? ''
170170
return JSON.stringify(JSON.parse(value), undefined, 4)
171171
} else {
172-
return JSON.stringify(this.storage.get(key, {}), undefined, 4)
172+
if (key !== '') {
173+
return JSON.stringify(this.storage.get(key, {}), undefined, 4)
174+
}
175+
// lol hax
176+
// as of November 2023, all of a memento's properties are stored as property `f` when minified
177+
return JSON.stringify((this.storage as any).f, undefined, 4)
173178
}
174179
}
175180

@@ -226,14 +231,24 @@ class ObjectEditor {
226231
}
227232

228233
private async createTab(storage: vscode.Memento | vscode.SecretStorage, key: string): Promise<Tab> {
229-
const uri = this.uriFromKey(key, storage)
230-
const disposable = this.fs.registerProvider(uri, new VirtualObjectFile(storage, key))
231-
const document = await vscode.workspace.openTextDocument(uri)
234+
const virtualFile = new VirtualObjectFile(storage, key)
235+
let disposable: vscode.Disposable
236+
let document: vscode.TextDocument
237+
if (key !== '') {
238+
const uri = this.uriFromKey(key, storage)
239+
disposable = this.fs.registerProvider(uri, virtualFile)
240+
document = await vscode.workspace.openTextDocument(uri)
241+
} else {
242+
// don't tie it to a URI so you can't save this view
243+
const stream = await virtualFile.read()
244+
document = await vscode.workspace.openTextDocument({
245+
content: new TextDecoder().decode(stream),
246+
})
247+
}
232248
const withLanguage = await vscode.languages.setTextDocumentLanguage(document, 'json')
233-
const editor = await vscode.window.showTextDocument(withLanguage)
234249

235250
return {
236-
editor,
251+
editor: await vscode.window.showTextDocument(withLanguage),
237252
dispose: () => disposable.dispose(),
238253
}
239254
}
@@ -247,7 +262,7 @@ class ObjectEditor {
247262
}
248263
}
249264

250-
async function openStorageFromInput() {
265+
async function openStorageFromInput(ctx: ExtContext) {
251266
const wizard = new (class extends Wizard<{ target: 'globals' | 'secrets'; key: string }> {
252267
constructor() {
253268
super()
@@ -264,12 +279,33 @@ async function openStorageFromInput() {
264279
)
265280
)
266281

267-
this.form.key.bindPrompter(({ target }) =>
268-
createInputBox({
269-
title: 'Enter a key',
270-
placeholder: target === 'globals' ? 'region' : '',
271-
})
272-
)
282+
this.form.key.bindPrompter(({ target }) => {
283+
if (target === 'secrets') {
284+
return createInputBox({
285+
title: 'Enter a key',
286+
})
287+
} else if (target === 'globals') {
288+
const items = ctx.extensionContext.globalState
289+
.keys()
290+
.map(key => {
291+
return {
292+
label: key,
293+
data: key,
294+
}
295+
})
296+
.sort((a, b) => {
297+
return a.data.localeCompare(b.data)
298+
})
299+
items.unshift({
300+
data: '',
301+
label: "SHOW ALL (edits here won't write to global state)",
302+
})
303+
304+
return createQuickPick(items, { title: 'Pick a global state key' })
305+
} else {
306+
throw new Error('invalid storage target')
307+
}
308+
})
273309
}
274310
})()
275311

0 commit comments

Comments
 (0)