Skip to content

Commit 2f99d63

Browse files
committed
feat: able rename keys and remove keys
1 parent b5fd968 commit 2f99d63

File tree

5 files changed

+113
-4
lines changed

5 files changed

+113
-4
lines changed

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@
8383
"title": "Go to definaition",
8484
"icon": "./static/icon-open-file.svg"
8585
},
86+
{
87+
"command": "extension.vue-i18n-ally.delete-key",
88+
"title": "Delete key"
89+
},
90+
{
91+
"command": "extension.vue-i18n-ally.rename-key",
92+
"title": "Rename key path"
93+
},
8694
{
8795
"command": "extension.vue-i18n-ally.locale-visibility-toggle",
8896
"title": "Toggle locale visibility",
@@ -108,6 +116,14 @@
108116
{
109117
"command": "extension.vue-i18n-ally.set-source-language",
110118
"when": "config.no_exits"
119+
},
120+
{
121+
"command": "extension.vue-i18n-ally.delete-key",
122+
"when": "config.no_exits"
123+
},
124+
{
125+
"command": "extension.vue-i18n-ally.rename-key",
126+
"when": "config.no_exits"
111127
}
112128
],
113129
"view/item/context": [
@@ -139,6 +155,14 @@
139155
"command": "extension.vue-i18n-ally.set-source-language",
140156
"when": "view =~ /locales-progress/ && viewItem =~ /notsource/"
141157
},
158+
{
159+
"command": "extension.vue-i18n-ally.delete-key",
160+
"when": "view =~ /locales-tree/ && viewItem =~ /node/"
161+
},
162+
{
163+
"command": "extension.vue-i18n-ally.rename-key",
164+
"when": "view =~ /locales-tree/ && viewItem =~ /node/"
165+
},
142166
{
143167
"command": "extension.vue-i18n-ally.locale-visibility-show",
144168
"when": "view =~ /locales-progress/ && viewItem =~ /show/",

src/commands/keyManipulations.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { window, commands, workspace, Selection, TextEditorRevealType, env } from 'vscode'
2-
import { Global, Commands } from '../core'
2+
import { Global, Commands, LocaleRecord } from '../core'
33
import { ExtensionModule } from '../modules'
44
import { LocaleTreeItem, Node } from '../views/LocalesTreeView'
55
import * as path from 'path'
6+
import { flatten } from 'lodash'
67

78
async function getRecordFromNode (node: Node, defaultLocale?: string) {
89
if (node.type === 'tree')
@@ -125,6 +126,72 @@ const m: ExtensionModule = (ctx) => {
125126
window.showErrorMessage(err.toString())
126127
}
127128
}),
129+
130+
commands.registerCommand(Commands.rename_key,
131+
async ({ node }: LocaleTreeItem) => {
132+
let records: LocaleRecord[] = []
133+
134+
if (node.type === 'tree')
135+
return
136+
137+
else if (node.type === 'record')
138+
records = [node]
139+
140+
else
141+
records = Object.values(node.locales)
142+
143+
try {
144+
const newkeypath = await window.showInputBox({
145+
value: node.keypath,
146+
prompt: 'Enter the new keypath',
147+
})
148+
149+
if (newkeypath !== undefined && newkeypath !== node.keypath) {
150+
const writes = flatten(records
151+
.map(record => [{
152+
value: undefined,
153+
keypath: record.keypath,
154+
filepath: record.filepath,
155+
locale: record.locale,
156+
}, {
157+
value: record.value,
158+
keypath: newkeypath,
159+
filepath: record.filepath,
160+
locale: record.locale,
161+
}]))
162+
await Global.loader.writeToFile(writes)
163+
}
164+
}
165+
catch (err) {
166+
window.showErrorMessage(err.toString())
167+
}
168+
}),
169+
170+
commands.registerCommand(Commands.delete_key,
171+
async ({ node }: LocaleTreeItem) => {
172+
let records: LocaleRecord[] = []
173+
174+
if (node.type === 'tree')
175+
return
176+
177+
else if (node.type === 'record')
178+
records = [node]
179+
180+
else
181+
records = Object.values(node.locales)
182+
183+
try {
184+
await Global.loader.writeToFile(records.map(record => ({
185+
value: undefined,
186+
keypath: record.keypath,
187+
filepath: record.filepath,
188+
locale: record.locale,
189+
})))
190+
}
191+
catch (err) {
192+
window.showErrorMessage(err.toString())
193+
}
194+
}),
128195
]
129196
}
130197

src/core/Commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export enum Commands {
1111
translate_key = 'extension.vue-i18n-ally.translate-key',
1212
edit_key = 'extension.vue-i18n-ally.edit-key',
1313
open_key = 'extension.vue-i18n-ally.open-key',
14+
delete_key = 'extension.vue-i18n-ally.delete-key',
15+
rename_key = 'extension.vue-i18n-ally.rename-key',
1416
locale_visibility_toggle = 'extension.vue-i18n-ally.locale-visibility-toggle',
1517
locale_visibility_show = 'extension.vue-i18n-ally.locale-visibility-show',
1618
locale_visibility_hide = 'extension.vue-i18n-ally.locale-visibility-hide',

src/core/LocaleLoader.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { MachinTranslate } from './MachineTranslate'
88
import { getKeyname, getFileInfo, replaceLocalePath, notEmpty } from './utils'
99
import { LocaleTree, ParsedFile, FlattenLocaleTree, Coverage, LocaleNode, LocaleRecord, PendingWrite } from './types'
1010
import { AllyError, ErrorType } from './Errors'
11+
import { load } from 'js-yaml'
1112

1213
function newTree (keypath = '', values = {}): LocaleTree {
1314
return {
@@ -298,12 +299,25 @@ export class LocaleLoader extends Disposable {
298299
await parser.save(filepath, original)
299300
}
300301

302+
private _ignoreChanges = false
301303
async writeToFile (pendings: PendingWrite|PendingWrite[]) {
304+
this._ignoreChanges = true
302305
if (!Array.isArray(pendings))
303306
pendings = [pendings]
304307
pendings = pendings.filter(i => i)
305-
for (const pending of pendings)
306-
await this.writeToSingleFile(pending)
308+
try {
309+
for (const pending of pendings)
310+
await this.writeToSingleFile(pending)
311+
}
312+
catch (e) {
313+
this._ignoreChanges = false
314+
throw e
315+
}
316+
this._ignoreChanges = false
317+
for (const pending of pendings) {
318+
if (pending.filepath)
319+
await this.loadFile(pending.filepath)
320+
}
307321
}
308322

309323
private async loadFile (filepath: string) {
@@ -362,6 +376,8 @@ export class LocaleLoader extends Disposable {
362376
const watcher = workspace.createFileSystemWatcher(`${rootPath}/**`)
363377

364378
const updateFile = async (type: string, { fsPath: filepath }: { fsPath: string }) => {
379+
if (this._ignoreChanges)
380+
return
365381
filepath = path.resolve(filepath)
366382
const { ext } = path.parse(filepath)
367383
if (!Global.getMatchedParser(ext))

src/core/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export interface PendingWrite {
6262
locale: string
6363
keypath: string
6464
filepath?: string
65-
value: string
65+
value?: string
6666
}
6767

6868
export interface ExtractTextOptions {

0 commit comments

Comments
 (0)