Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultdalban committed Feb 11, 2020
1 parent 0b15bd8 commit 8b7e046
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
67 changes: 34 additions & 33 deletions src/commands/extractText.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
// @ts-ignore
import * as limax from 'limax'
// import * as limax from 'limax'
import { commands, window, workspace } from 'vscode'
import { trim } from 'lodash'
import { ExtensionModule } from '../modules'
import { ExtractTextOptions, Global, Commands, Config, CurrentFile } from '../core'
// import { ExtractTextOptions, Global, Commands, Config, CurrentFile } from '../core'
import { ExtractTextOptions, Global, Commands } from '../core'
import i18n from '../i18n'
import { overrideConfirm } from './overrideConfirm'
import { keypathValidate } from './keypathValidate'
// import { overrideConfirm } from './overrideConfirm'
// import { keypathValidate } from './keypathValidate'

const m: ExtensionModule = () => {
return commands.registerCommand(Commands.extract_text,
async (options: ExtractTextOptions) => {
const { filepath, text, range, languageId } = options
const default_keypath = limax(text, { separator: Config.preferredDelimiter, tone: false }) as string
const locale = Config.sourceLanguage
// const default_keypath = limax(text, { separator: Config.preferredDelimiter, tone: false }) as string
// const locale = Config.sourceLanguage

// prompt for keypath
const keypath = await window.showInputBox({
prompt: i18n.t('prompt.enter_i18n_key'),
value: default_keypath,
})
// const keypath = await window.showInputBox({
// prompt: i18n.t('prompt.enter_i18n_key'),
// value: default_keypath,
// })

if (!keypath) {
window.showWarningMessage(i18n.t('prompt.extraction_canceled'))
return
}
// if (!keypath) {
// window.showWarningMessage(i18n.t('prompt.extraction_canceled'))
// return
// }

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

const writeKeypath = CurrentFile.loader.rewriteKeys(keypath, 'write', { locale })
// const writeKeypath = CurrentFile.loader.rewriteKeys(keypath, 'write', { locale })

const shouldOverride = await overrideConfirm(writeKeypath, true, true)
// const shouldOverride = await overrideConfirm(writeKeypath, true, true)

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

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

// prompt for template
const replacer = await window.showQuickPick(
Global.refactorTemplates(keypath, languageId),
Global.refactorTemplates('', languageId, value),
{
placeHolder: i18n.t('prompt.replace_text_as'),
})
Expand All @@ -64,16 +65,16 @@ const m: ExtensionModule = () => {
editBuilder.replace(range, replacer)
})

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

// save key
await CurrentFile.loader.write({
filepath: undefined,
keypath: writeKeypath,
value,
locale,
})
// await CurrentFile.loader.write({
// filepath: undefined,
// keypath: writeKeypath,
// value,
// locale,
// })
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/manipulations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export * from './copyKey'
export * from './renameKey'
export * from './deleteKey'
export * from './editKey'
export * from './fulfillkeys'
export * from './fulfillKeys'
export * from './newKey'
export * from './duplicateKey'
4 changes: 2 additions & 2 deletions src/core/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export class Global {
.flatMap(f => f.getKeyMatchReg(languageId, filepath))
}

static refactorTemplates (keypath: string, languageId?: string) {
return uniq(this.enabledFrameworks.flatMap(f => f.refactorTemplates(keypath, languageId)))
static refactorTemplates (keypath: string, languageId?: string, content?: string) {
return uniq(this.enabledFrameworks.flatMap(f => f.refactorTemplates(keypath, languageId, content)))
}

static isLanguageIdSupported (languageId: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/frameworks/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export abstract class Framework {
/**
* Return possible choices of replacement for messages extracted from code
*/
abstract refactorTemplates (keypath: string, languageId?: string): string[]
abstract refactorTemplates (keypath: string, languageId?: string, content?: string): string[]

/**
* Locale file's name match
Expand Down
4 changes: 2 additions & 2 deletions src/frameworks/custom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class CustomFramework extends Framework {
return this.data?.monopoly || false
}

refactorTemplates (keypath: string) {
refactorTemplates (keypath: string, languageId?: string, content?: string) {
return (this.data?.refactorTemplates || ['$1'])
.map(i => i.replace(/\$1/g, keypath))
.map(i => i.replace(/\$1/g, content || ''))
}

startWatch (root?: string) {
Expand Down

0 comments on commit 8b7e046

Please sign in to comment.