1- import { window as Window , ProgressLocation } from 'vscode'
1+ import { window as Window , ProgressLocation , QuickPickItem } from 'vscode'
22import { DocumentSelector } from 'vscode-languageclient'
3- import { getZhTranslation } from './baidu'
3+ import { getBaiduZhTranslation } from './translate /baidu'
44
55import
66{
77 ActivationLanguage ,
88 CUSTOM_INPUT_PLACEHOLDER ,
99 CUSTOM_INTL_ID_REGX ,
1010 CUSTOM_PICK_OPTION ,
11+ CUSTOM_PICK_OPTION_DESC ,
1112 CUSTOM_PICK_PLACEHOLDER ,
1213 getIntlMessage ,
1314 INVALID_CUSTOM_ID_MESSAGE ,
@@ -79,7 +80,7 @@ export const getTranslateResultsWithProgress = async (searchText: string): Promi
7980 {
8081 // 获取供选择的 Options
8182 // 这是最终的返回结果
82- const result = await getZhTranslation ( searchText )
83+ const result = await getBaiduZhTranslation ( searchText )
8384 Array . isArray ( result ) && result . length && TranslationResultMap . set ( searchText , result )
8485 return result
8586 } )
@@ -130,9 +131,35 @@ export const getIntlIdWithQuickPick = async (
130131 intlConfig ?: Record < string , string > ,
131132) : Promise < [ string | undefined , string | undefined ] > =>
132133{
133- const _intlIdOptions = translateResults . map ( re => getCleanIntlId ( re ) . toUpperCase ( ) . split ( ' ' ) . join ( '_' ) )
134+ // intl id 对应 translation result 的 map
135+ // intlId -> translateResult
136+ const intlResultMap = new Map < string , string > ( )
134137
135- let intlId = await Window . showQuickPick ( [ ..._intlIdOptions , CUSTOM_PICK_OPTION ] , { placeHolder : CUSTOM_PICK_PLACEHOLDER } )
138+ // 包含描述信息和细节的 quick pick
139+ // 参考 https://stackoverflow.com/questions/62312943/vscode-use-quickpick-list-items-with-description
140+ const quickPickOptions : QuickPickItem [ ] = translateResults . map ( re =>
141+ {
142+ const intlId = getCleanIntlId ( re ) . toUpperCase ( ) . split ( ' ' ) . join ( '_' )
143+ intlResultMap . set ( intlId , re )
144+ return {
145+ label : intlId ,
146+ detail : `对应翻译结果:${ intlResultMap . get ( intlId ) || '无' } `
147+ }
148+ } )
149+
150+ // 自定义选项
151+ const customPickOption : QuickPickItem = { label : CUSTOM_PICK_OPTION , description : CUSTOM_PICK_OPTION_DESC }
152+
153+ const pickedIntlOption = await Window . showQuickPick ( [ ...quickPickOptions , customPickOption ] , { placeHolder : CUSTOM_PICK_PLACEHOLDER } )
154+
155+ // 选中的 option 对应的 intl id
156+ let intlId = pickedIntlOption ?. label
157+
158+ // 自定义 id 会选择第一个翻译结果
159+ // 否则取 intlId 对应的翻译结果
160+ const translateResult = ! ! intlId
161+ ? ( intlId !== CUSTOM_PICK_OPTION && intlResultMap . get ( intlId ) ) ? intlResultMap . get ( intlId ) : translateResults && translateResults [ 0 ]
162+ : undefined
136163
137164 // 用户选择的 id 有可能已存在,我们会给它加上数字用以区分
138165 if ( intlConfig && intlId && ( intlId in intlConfig ) && intlId !== CUSTOM_PICK_OPTION )
@@ -145,8 +172,7 @@ export const getIntlIdWithQuickPick = async (
145172 intlId = `${ intlId } _${ getIntlIdCount ( idCount ) } `
146173 }
147174
148- // 具体的英文翻译文本 doesn't matter
149- return [ intlId , translateResults && translateResults [ 0 ] ]
175+ return [ intlId , translateResult ]
150176}
151177
152178/**
0 commit comments