|
3 | 3 | "source.fixAll.eslint": "explicit", |
4 | 4 | "source.organizeImports": "always" |
5 | 5 | }, |
| 6 | + "editor.rename.enablePreview": false, |
6 | 7 |
|
7 | 8 | // a macro to rename a variable to _variable |
8 | 9 | "macros": { |
9 | 10 | "rename_": [ |
10 | 11 | { |
11 | 12 | "javascript": [ |
12 | | - "const ed = vscode.window.activeTextEditor;", |
13 | | - "if (!ed) return;", |
| 13 | + "const ed = vscode.window.activeTextEditor; if (!ed) return;", |
14 | 14 | "const pos = ed.selection.active;", |
15 | | - "const rng = ed.document.getWordRangeAtPosition(pos);", |
16 | | - "if (!rng) return;", |
17 | | - "const oldName = ed.document.getText(rng);", |
18 | | - "// if it already starts with '_', do nothing (remove this guard if you always want to re-run)", |
19 | | - "if (oldName.startsWith('_')) return;", |
| 15 | + "const rng = ed.document.getWordRangeAtPosition(pos); if (!rng) return;", |
| 16 | + "const oldName = ed.document.getText(rng); if (oldName.startsWith('_')) return;", |
20 | 17 | "const newName = '_' + oldName;", |
21 | | - "// Ask VS Code for a rename edit at this position", |
22 | | - "const edit = await vscode.commands.executeCommand(", |
23 | | - " 'vscode.executeDocumentRenameProvider',", |
24 | | - " ed.document.uri,", |
25 | | - " pos,", |
26 | | - " newName", |
27 | | - ");", |
28 | | - "if (edit) await vscode.workspace.applyEdit(edit);", |
29 | | - "await new Promise(resolve => setTimeout(resolve, 100));", |
30 | | - "await vscode.commands.executeCommand('workbench.action.files.saveAll');" |
| 18 | + "const edit = await vscode.commands.executeCommand('vscode.executeDocumentRenameProvider', ed.document.uri, pos, newName);", |
| 19 | + "if (!edit) return;", |
| 20 | + "await vscode.workspace.applyEdit(edit);", |
| 21 | + "(async () => {", |
| 22 | + " for (let attempt = 0; attempt < 5; attempt++) {", |
| 23 | + " let remaining = 0;", |
| 24 | + " for (const doc of vscode.workspace.textDocuments) {", |
| 25 | + " try { if (doc.isDirty) { remaining++; await doc.save(); } } catch (_) {}", |
| 26 | + " }", |
| 27 | + " if (remaining === 0) break;", |
| 28 | + " await new Promise(r => setTimeout(r, 80));", |
| 29 | + " }", |
| 30 | + "})();" |
31 | 31 | ] |
32 | 32 | } |
33 | 33 | ] |
|
0 commit comments