Skip to content

Commit

Permalink
feat: new command "duplicate key", #137
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 10, 2019
1 parent 20e09f2 commit 8aeb217
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 18 deletions.
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
{
"command": "extension.i18n-ally.new-key",
"category": "%extname%",
"title": "%command.new-key%",
"title": "%command.new_key%",
"icon": {
"light": "./res/light/plus.svg",
"dark": "./res/dark/plus.svg"
Expand All @@ -172,7 +172,7 @@
{
"command": "extension.i18n-ally.duplicate-key",
"category": "%extname%",
"title": "%command.duplicate-key%"
"title": "%command.duplicate_key%"
}
],
"menus": {
Expand Down Expand Up @@ -256,17 +256,20 @@
"command": "extension.i18n-ally.set-source-language",
"when": "view =~ /i18n-ally-locales-progress/ && viewItem =~ /notsource/"
},
{
"command": "extension.i18n-ally.delete-key",
"when": "view =~ /i18n-ally-locales/ && viewItem =~ /node/"
},
{
"command": "extension.i18n-ally.rename-key",
"when": "view =~ /i18n-ally-locales/ && viewItem =~ /node/"
"when": "view =~ /i18n-ally-locales/ && viewItem =~ /node/",
"group": "i18nally@1"
},
{
"command": "extension.i18n-ally.duplicate-key",
"when": "view =~ /i18n-ally-locales-tree/ && viewItem =~ /node/"
"when": "view =~ /i18n-ally-locales-tree/ && viewItem =~ /node/",
"group": "i18nally@2"
},
{
"command": "extension.i18n-ally.delete-key",
"when": "view =~ /i18n-ally-locales/ && viewItem =~ /node/",
"group": "i18nally@3"
},
{
"command": "extension.i18n-ally.locale-visibility-show",
Expand Down
4 changes: 2 additions & 2 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"command.config_source_language": "Change source language",
"command.copy_key": "Copy i18n key",
"command.delete_key": "Delete Key",
"command.duplicate-key": "Duplicate",
"command.duplicate_key": "Duplicate key",
"command.edit_key": "Edit translation",
"command.fulfill_keys": "Fulfill keys with empty string",
"command.locale_visibility_hide": "Hide locale",
"command.locale_visibility_show": "Show locale",
"command.new-key": "Create a new key",
"command.new_key": "Create a new key",
"command.open_key": "Go to definition",
"command.open_url": "Open url",
"command.refresh_usage": "Refresh usage report",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/keyManipulations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { commands } from 'vscode'
import { Commands } from '../core'
import { ExtensionModule } from '../modules'
import { TranslateKeys, OpenKey, CopyKey, RenameKey, DeleteKey, NewKey, FulfillKeys } from './manipulations'
import { TranslateKeys, OpenKey, CopyKey, RenameKey, DeleteKey, NewKey, FulfillKeys, DuplicateKey } from './manipulations'

const m: ExtensionModule = () => {
return [
Expand All @@ -13,6 +13,7 @@ const m: ExtensionModule = () => {
commands.registerCommand(Commands.delete_key, DeleteKey),
commands.registerCommand(Commands.fulfill_keys, FulfillKeys),
commands.registerCommand(Commands.new_key, NewKey),
commands.registerCommand(Commands.duplicate_key, DuplicateKey),
]
}

Expand Down
57 changes: 57 additions & 0 deletions src/commands/manipulations/duplicateKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { window } from 'vscode'
import { LocaleTreeItem } from '../../views'
import { Node, CurrentFile, PendingWrite } from '../../core'
import i18n from '../../i18n'
import { Log } from '../../utils'
import { overrideConfirm } from '../overrideConfirm'
import { keypathValidate } from '../keypathValidate'

export async function DuplicateKey (item?: LocaleTreeItem | string) {
if (!item)
return

let node: Node | undefined

if (typeof item === 'string')
node = CurrentFile.loader.getTreeNodeByKey(item)
else
node = item.node

if (!node || node.type !== 'node')
return

try {
const oldkeypath = node.keypath
const newkeypath = await window.showInputBox({
value: oldkeypath,
prompt: i18n.t('prompt.enter_new_keypath'),
})

if (!newkeypath)
return

if (!keypathValidate(newkeypath)) {
window.showWarningMessage(i18n.t('prompt.invalid_keypath'))
await DuplicateKey(item)
return
}

if (await overrideConfirm(newkeypath) !== 'override')
return

const writes: PendingWrite[] = Object.values(node.locales)
.map(v=>{
return ({
value: v.value,
keypath: newkeypath,
filepath: v.filepath,
locale: v.locale,
})
})

await CurrentFile.loader.write(writes)
}
catch (err) {
Log.error(err)
}
}
1 change: 1 addition & 0 deletions src/commands/manipulations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './deleteKey'
export * from './editKey'
export * from './fulfillkeys'
export * from './newKey'
export * from './duplicateKey'
12 changes: 7 additions & 5 deletions src/commands/manipulations/newKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { window, commands } from 'vscode'
import { CurrentFile, Config, Commands } from '../../core'
import { window } from 'vscode'
import { CurrentFile, Config } from '../../core'
import i18n from '../../i18n'
import { Log } from '../../utils'
import { overrideConfirm } from '../overrideConfirm'
Expand All @@ -15,13 +15,15 @@ export async function NewKey (keypath?: string) {
if (!keypath)
return

if (!keypathValidate(keypath))
return window.showWarningMessage(i18n.t('prompt.invalid_keypath'))
if (!keypathValidate(keypath)) {
window.showWarningMessage(i18n.t('prompt.invalid_keypath'))
await NewKey(keypath)
}

const shouldOverride = await overrideConfirm(keypath, false, true)

if (shouldOverride === 'retry') {
commands.executeCommand(Commands.new_key, keypath)
await NewKey(keypath)
return
}

Expand Down
7 changes: 5 additions & 2 deletions src/commands/manipulations/renameKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ export async function RenameKey (item?: LocaleTreeItem | string) {
if (!newkeypath)
return

if (!keypathValidate(newkeypath))
return window.showWarningMessage(i18n.t('prompt.invalid_keypath'))
if (!keypathValidate(newkeypath)){
window.showWarningMessage(i18n.t('prompt.invalid_keypath'))
await RenameKey(item)
return
}

if (await overrideConfirm(newkeypath) !== 'override')
return
Expand Down

0 comments on commit 8aeb217

Please sign in to comment.