Skip to content

Commit

Permalink
feat: new command "new key", #137
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
antfu committed Dec 10, 2019
1 parent 37e1ec9 commit 20e09f2
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
out/
dist/
samples/**/*.vue
examples/**/*.*
11 changes: 9 additions & 2 deletions examples/vue-i18n/demo/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@
}
}
},
"intro": "Brings a better translating experience"
}
"intro": "Brings a better translating experience",
"hi": "Hi",
"yes": {
"no": {
"and": "good"
}
},
"yad": "asd"
}
27 changes: 25 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@
"light": "./res/light/eye-off.svg",
"dark": "./res/dark/eye-off.svg"
}
},
{
"command": "extension.i18n-ally.new-key",
"category": "%extname%",
"title": "%command.new-key%",
"icon": {
"light": "./res/light/plus.svg",
"dark": "./res/dark/plus.svg"
}
},
{
"command": "extension.i18n-ally.duplicate-key",
"category": "%extname%",
"title": "%command.duplicate-key%"
}
],
"menus": {
Expand Down Expand Up @@ -250,6 +264,10 @@
"command": "extension.i18n-ally.rename-key",
"when": "view =~ /i18n-ally-locales/ && viewItem =~ /node/"
},
{
"command": "extension.i18n-ally.duplicate-key",
"when": "view =~ /i18n-ally-locales-tree/ && viewItem =~ /node/"
},
{
"command": "extension.i18n-ally.locale-visibility-show",
"when": "view =~ /i18n-ally-locales-progress/ && viewItem =~ /show/",
Expand All @@ -266,6 +284,11 @@
"command": "extension.i18n-ally.refresh-usage",
"group": "navigation",
"when": "view =~ /i18n-ally-locales-usage/"
},
{
"command": "extension.i18n-ally.new-key",
"group": "navigation",
"when": "view =~ /i18n-ally-locales-tree/"
}
]
},
Expand All @@ -281,14 +304,14 @@
"views": {
"explorer": [
{
"id": "i18n-ally-locales-tree-file-explorer",
"id": "i18n-ally-locales-file-explorer",
"name": "%view.i18n_keys%",
"when": "i18n-ally-enabled && i18n-ally-supported-file"
}
],
"i18n-ally": [
{
"id": "i18n-ally-locales-tree-file",
"id": "i18n-ally-locales-sidebar-file",
"name": "%view.current_file%",
"when": "i18n-ally-enabled && i18n-ally-supported-file"
},
Expand Down
5 changes: 5 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +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.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.open_key": "Go to definition",
"command.open_url": "Open url",
"command.refresh_usage": "Refresh usage report",
Expand Down Expand Up @@ -68,6 +70,7 @@
"prompt.frameworks_not_found": "The following framework(s) are not found, please check your settings.\n{0}",
"prompt.fullfill_missing_all_confirm": "Are you sure to fulfill all missing keys with empty string cross all locales?\n\nThis operation cannot be undone.",
"prompt.fullfill_missing_confirm": "Are you sure to fulfill {0} missing keys with empty string in {1}?\n\nThis operation cannot be undone.",
"prompt.invalid_keypath": "Invalid keypath!",
"prompt.key_already_exists": "Key already exists. Do you want to override the existing value or re-enter the path?",
"prompt.key_copied": "Key copied.",
"prompt.keystyle_flat": "Flat style",
Expand All @@ -76,6 +79,8 @@
"prompt.keystyle_nested_example": "for example: { \"a\": { \"b\": { \"c\": \"...\" } } }",
"prompt.keystyle_select": "Which kind of key style do you use to organize your locales?",
"prompt.locales_dir_not_found": "Locales directory not found. i18n Ally is disabled.",
"prompt.new_key_path": "Enter the keypath of the new key",
"prompt.new_key_value": "Enter the transalation for \"{1}\" of the new keypath \"{0}\"",
"prompt.replace_text_as": "Replace text as:",
"prompt.select_display_locale": "Select display language ({0})",
"prompt.select_file_to_open": "Select file to open",
Expand Down
3 changes: 3 additions & 0 deletions res/dark/plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions res/light/plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 12 additions & 27 deletions src/commands/extractText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { trim } from 'lodash'
import { ExtensionModule } from '../modules'
import { ExtractTextOptions, Global, Commands, Config, CurrentFile } from '../core'
import i18n from '../i18n'
import { overrideConfirm } from './overrideConfirm'
import { keypathValidate } from './keypathValidate'

const m: ExtensionModule = () => {
return commands.registerCommand(Commands.extract_text,
Expand All @@ -23,34 +25,17 @@ const m: ExtensionModule = () => {
return
}

// keypath existence check
const node = CurrentFile.loader.getNodeByKey(keypath)
let willSkip = false
if (node) {
const Override = i18n.t('prompt.button_override')
const Skip = i18n.t('prompt.button_skip')
const Reenter = i18n.t('prompt.button_reenter')
const result = await window.showInformationMessage(
i18n.t('prompt.key_already_exists'),
{ modal: true },
Override,
Skip,
Reenter,
)
if (!keypathValidate(keypath))
return window.showWarningMessage(i18n.t('prompt.invalid_keypath'))

// canceled
if (!result) {
return
}
else if (result === Reenter) {
commands.executeCommand(Commands.extract_text, options)
return
}
else if (result === Skip) {
willSkip = true
}
// else override
const shouldOverride = await overrideConfirm(keypath, true, true)

if (shouldOverride === 'retry') {
commands.executeCommand(Commands.extract_text, options)
return
}
if (shouldOverride === 'canceled')
return

const value = trim(text, '\'"')

Expand All @@ -76,7 +61,7 @@ const m: ExtensionModule = () => {
editBuilder.replace(range, replacer)
})

if (willSkip)
if (shouldOverride === 'skip')
return

// save key
Expand Down
5 changes: 3 additions & 2 deletions src/commands/keyManipulations.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { commands } from 'vscode'
import { Commands } from '../core'
import { ExtensionModule } from '../modules'
import { TranslateKeys, OpenKey, CopyKey, RenameKey, DeleteKey, EditKey, FulfillKeys } from './manipulations'
import { TranslateKeys, OpenKey, CopyKey, RenameKey, DeleteKey, NewKey, FulfillKeys } from './manipulations'

const m: ExtensionModule = () => {
return [
commands.registerCommand(Commands.copy_key, CopyKey),
commands.registerCommand(Commands.translate_key, TranslateKeys),
commands.registerCommand(Commands.open_key, OpenKey),
commands.registerCommand(Commands.rename_key, RenameKey),
commands.registerCommand(Commands.edit_key, EditKey),
commands.registerCommand(Commands.edit_key, NewKey),
commands.registerCommand(Commands.delete_key, DeleteKey),
commands.registerCommand(Commands.fulfill_keys, FulfillKeys),
commands.registerCommand(Commands.new_key, NewKey),
]
}

Expand Down
3 changes: 3 additions & 0 deletions src/commands/keypathValidate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function keypathValidate (keypath: string) {
return !!keypath.match(/^[\w\d][\w\d\-\[\]\.\ ]*$/g)
}
1 change: 1 addition & 0 deletions src/commands/manipulations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './renameKey'
export * from './deleteKey'
export * from './editKey'
export * from './fulfillkeys'
export * from './newKey'
50 changes: 50 additions & 0 deletions src/commands/manipulations/newKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { window, commands } from 'vscode'
import { CurrentFile, Config, Commands } from '../../core'
import i18n from '../../i18n'
import { Log } from '../../utils'
import { overrideConfirm } from '../overrideConfirm'
import { keypathValidate } from '../keypathValidate'

export async function NewKey (keypath?: string) {
try {
keypath = await window.showInputBox({
value: keypath || '',
prompt: i18n.t('prompt.new_key_path'),
})

if (!keypath)
return

if (!keypathValidate(keypath))
return window.showWarningMessage(i18n.t('prompt.invalid_keypath'))

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

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

if (shouldOverride !== 'override')
return

const locale = Config.sourceLanguage
const value = await window.showInputBox({
value: '',
prompt: i18n.t('prompt.new_key_value', keypath, locale),
})

if (value === undefined)
return

await CurrentFile.loader.write({
value,
keypath,
filepath: undefined,
locale,
})
}
catch (err) {
Log.error(err.toString())
}
}
8 changes: 8 additions & 0 deletions src/commands/manipulations/renameKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { LocaleTreeItem } from '../../views'
import { Node, CurrentFile, Global } from '../../core'
import i18n from '../../i18n'
import { Log } from '../../utils'
import { overrideConfirm } from '../overrideConfirm'
import { keypathValidate } from '../keypathValidate'

export async function RenameKey (item?: LocaleTreeItem | string) {
if (!item)
Expand All @@ -28,6 +30,12 @@ export async function RenameKey (item?: LocaleTreeItem | string) {
if (!newkeypath)
return

if (!keypathValidate(newkeypath))
return window.showWarningMessage(i18n.t('prompt.invalid_keypath'))

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

const edit = await Global.loader.renameKey(oldkeypath, newkeypath) // TODO:sfc
await workspace.applyEdit(edit)
}
Expand Down
40 changes: 40 additions & 0 deletions src/commands/overrideConfirm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { window } from 'vscode'
import { CurrentFile } from '../core'
import i18n from '../i18n'

export async function overrideConfirm (keypath: string, allowSkip = false, allowRenter = false) {
const node = CurrentFile.loader.getNodeByKey(keypath)

if (node) {
const Override = i18n.t('prompt.button_override')
const Skip = i18n.t('prompt.button_skip')
const Reenter = i18n.t('prompt.button_reenter')

const options = [Override]

if (allowSkip)
options.push(Skip)

if (allowRenter)
options.push(Reenter)

const result = await window.showInformationMessage(
i18n.t('prompt.key_already_exists'),
{ modal: true },
...options,
)

if (result === Override)
return 'override'

if (result === Reenter)
return 'retry'

else if (result === Skip)
return 'skip'

return 'canceled'
}

return 'override'
}
2 changes: 2 additions & 0 deletions src/core/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export enum Commands {
open_key = 'extension.i18n-ally.open-key',
delete_key = 'extension.i18n-ally.delete-key',
rename_key = 'extension.i18n-ally.rename-key',
new_key = 'extension.i18n-ally.new-key',
duplicate_key = 'extension.i18n-ally.duplicate-key',
fulfill_keys = 'extension.i18n-ally.fulfill-keys',
locale_visibility_toggle = 'extension.i18n-ally.locale-visibility-toggle',
locale_visibility_show = 'extension.i18n-ally.locale-visibility-show',
Expand Down
4 changes: 2 additions & 2 deletions src/views/ViewIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export enum ViewIds {
tree = 'i18n-ally-locales-tree',
feedback = 'i18n-ally-help-feedback',
usage = 'i18n-ally-locales-usage',
file = 'i18n-ally-locales-tree-file',
file_in_explorer = 'i18n-ally-locales-tree-file-explorer',
file = 'i18n-ally-locales-sidebar-file',
file_in_explorer = 'i18n-ally-locales-file-explorer',
}

0 comments on commit 20e09f2

Please sign in to comment.