Skip to content

Commit

Permalink
feat: allow custom convert target language
Browse files Browse the repository at this point in the history
  • Loading branch information
2214962083 committed Jul 11, 2024
1 parent 91e1f7c commit 0454b70
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 34 deletions.
1 change: 1 addition & 0 deletions package.nls.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"error.noTargetLanguage": "No target language selected",
"error.noContext": "Context not initialized",
"info.copied": "File contents have been copied to clipboard",
"info.customLanguage": "Custom language",
"input.array.promptEnding": "Enter comma separated values",
"input.json.promptEnding": "Enter JSON formatted value",
"input.aiCommand.prompt": "Enter question for AI command",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"error.noTargetLanguage": "No target language selected",
"error.noContext": "Context not initialized",
"info.copied": "File contents have been copied to clipboard",
"info.customLanguage": "Custom language",
"input.array.promptEnding": "Enter comma separated values",
"input.json.promptEnding": "Enter JSON formatted value",
"input.aiCommand.prompt": "Enter question for AI command",
Expand Down
1 change: 1 addition & 0 deletions package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"error.noTargetLanguage": "未选择目标语言",
"error.noContext": "上下文未初始化",
"info.copied": "文件内容已复制到剪贴板",
"info.customLanguage": "自定义语言",
"input.array.promptEnding": "输入逗号分隔的值",
"input.json.promptEnding": "输入 JSON 格式的值",
"input.aiCommand.prompt": "输入 AI 命令的问题",
Expand Down
26 changes: 17 additions & 9 deletions src/commands/code-convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,27 @@ const getTargetLanguageId = async (originalFileLanguageId: string) => {
targetForSet: vscode.ConfigurationTarget.WorkspaceFolder,
allowCustomOptionValue: true
})

let targetLanguageId = convertLanguagePairs?.[originalFileLanguageId] || ''
const customLanguageOption = t('info.customLanguage')

if (!targetLanguageId) {
targetLanguageId =
(await vscode.window.showQuickPick(languageIds, {
placeHolder: t('input.codeConvertTargetLanguage.prompt'),
canPickMany: false
})) || ''
(await vscode.window.showQuickPick(
[customLanguageOption, ...languageIds],
{
placeHolder: t('input.codeConvertTargetLanguage.prompt'),
canPickMany: false
}
)) || ''

if (!targetLanguageId) throw new Error(t('error.noTargetLanguage'))

if (targetLanguageId === customLanguageOption) {
targetLanguageId =
(await vscode.window.showInputBox({
prompt: t('info.customLanguage')
})) || ''
}

if (!targetLanguageId) throw new Error(t('error.noTargetLanguage'))

Expand All @@ -64,10 +76,6 @@ const getTargetLanguageId = async (originalFileLanguageId: string) => {
)
}

targetLanguageId = languageIds.includes(targetLanguageId)
? targetLanguageId
: 'plaintext'

return targetLanguageId
}

Expand Down
9 changes: 7 additions & 2 deletions src/create-tmp-file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import * as vscode from 'vscode'

import { languageIds } from './constants'
import { t } from './i18n'
import { getLanguageIdExt } from './utils'

Expand All @@ -12,7 +13,7 @@ export const getTmpFileUri = (
const originalFileName = path.parse(originalFileUri.fsPath).name
const originalFileExt = path.parse(originalFileUri.fsPath).ext

const languageExt = getLanguageIdExt(languageId || 'plaintext')
const languageExt = getLanguageIdExt(languageId) || languageId

return vscode.Uri.parse(
`untitled:${path.join(originalFileDir, `${originalFileName}${originalFileExt}.aide${languageExt ? `.${languageExt}` : ''}`)}`
Expand Down Expand Up @@ -128,7 +129,11 @@ export const createTmpFileAndWriter = async (
})
}

vscode.languages.setTextDocumentLanguage(tmpDocument, languageId)
const docLanguageId = languageIds.includes(languageId)
? languageId
: 'plaintext'

vscode.languages.setTextDocumentLanguage(tmpDocument, docLanguageId)

const writeText = async (text: string) => {
const edit = new vscode.WorkspaceEdit()
Expand Down
32 changes: 11 additions & 21 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
import * as path from 'path'
import * as vscode from 'vscode'

import { logger } from './logger'
// locale files
import en from '../package.nls.en.json'
import zhCn from '../package.nls.zh-cn.json'
import { LocalizeFunction, Messages } from './types'
import { VsCodeFS } from './vscode-fs'

const localeFilesMap = {
en,
'zh-cn': zhCn
}

let messages: Messages = {}

export const initializeLocalization = async (
extensionPath: string
): Promise<void> => {
export const initializeLocalization = async (): Promise<void> => {
const { language } = vscode.env
const languageFilePath = path.join(
extensionPath,
`package.nls.${language}.json`
)
const defaultFilePath = path.join(extensionPath, 'package.nls.en.json')

try {
messages = JSON.parse(await VsCodeFS.readFile(languageFilePath, 'utf-8'))
} catch (err) {
logger.warn(
`Failed to load language file for ${language}, falling back to default`,
err
)
messages = JSON.parse(await VsCodeFS.readFile(defaultFilePath, 'utf-8'))
}

messages = localeFilesMap[language as keyof typeof localeFilesMap] ?? en
}

const format = (message: string, args: any[]): string =>
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import { WorkspaceStorage } from './workspace-storage'

export const activate = async (context: vscode.ExtensionContext) => {
try {
const { extensionPath } = context
const isDev = context.extensionMode !== vscode.ExtensionMode.Production

logger.log('"aide" is now active!')

initializeLocalization(extensionPath)
initializeLocalization()
setContext(context)
WorkspaceStorage.initialize(context)
await enablePolyfill()
Expand Down

0 comments on commit 0454b70

Please sign in to comment.