@@ -4,9 +4,9 @@ import * as path from 'path';
44import { ExtensionContext , window as Window , Uri , workspace } from 'vscode' ;
55import { ExecuteCommandSignature , LanguageClient , LanguageClientOptions , RevealOutputChannelOn , ServerOptions , TransportKind } from 'vscode-languageclient/node' ;
66
7- import { getIntlMessage , TranslationResultMap } from './lib/constant' ;
8- import { getIntlConfig , initializeWorkplaceIntlConfig , writeConfigIntoWorkSpace , writeResultIntoIntlConfig } from './lib/file ' ;
9- import { getDocumentSelector , getExistingIntl , getIntlIdWithQuickPick , getTranslateResultsWithProgress , processArgsWithSelectResult } from './lib/util ' ;
7+ import { LinterCommands , TranslationResultMap } from './lib/constant' ;
8+ import { getDocumentSelector } from './lib/util ' ;
9+ import ExtractMiddleware from './middleware/extract ' ;
1010
1111let client : LanguageClient ;
1212
@@ -39,62 +39,17 @@ export async function activate(context: ExtensionContext): Promise<void>
3939 revealOutputChannelOn : RevealOutputChannelOn . Never ,
4040 progressOnInitialization : true ,
4141 middleware : {
42- // 样例中间件
42+ // 执行从语言服务器发送的要执行的命令
4343 executeCommand : async ( command : string , args : any [ ] , next : ExecuteCommandSignature ) =>
4444 {
45- // 想要替换的中文文本
46- const searchText = args [ 1 ] as string
47-
48- // 初始化工作区国际化配置文件
49- await initializeWorkplaceIntlConfig ( intlConfigTemp , workspaceIntlConfigPath )
50-
51- // 获取已有配置文件
52- const [ zhConfig , enConfig ] = await getIntlConfig ( workspaceIntlConfigPath )
53-
54- // 查找工作区是否已存在对应中文文本配置
55- const { intlId, zhText, enText } = getExistingIntl ( searchText , zhConfig , enConfig )
56-
57- // 工作区已存在对应配置
58- if ( intlId && zhText && enText )
45+ switch ( command )
5946 {
60- // 传给语言服务器的 onExecuteCommand 函数
61- return next ( command , [ ...args , getIntlMessage ( intlId ) ] )
47+ case LinterCommands . Extract :
48+ return await ExtractMiddleware ( intlConfigTemp , workspaceIntlConfigPath , args , next )
49+ default :
50+ return
6251 }
6352
64- // 翻译结果相关进度条
65- const translateResults = await getTranslateResultsWithProgress ( searchText )
66-
67- // picker 选择结果
68- const [ selectedIntlId , translationText ] = await getIntlIdWithQuickPick ( translateResults , zhConfig ) ;
69-
70- // 获得处理后的参数,用于传给语言服务器的 onExecuteCommand 函数
71- const { newArgs, customIntlId } = await processArgsWithSelectResult ( args , selectedIntlId )
72-
73- if ( ! newArgs || ! selectedIntlId || ! translationText ) return
74-
75- try
76- {
77- // 替换文本操作可以首先实行减少用户对延迟的感知
78- // 文件写入错误和文本替换并不冲突,只不过需要用户重新执行一遍替换操作来执行文件写入或手动写入文件
79- next ( command , newArgs )
80-
81- // 获取新的 intl 配置文件
82- const [ newZhConfig , newEnConfig ] = await writeResultIntoIntlConfig (
83- customIntlId || selectedIntlId ,
84- searchText ,
85- translationText ,
86- zhConfig ,
87- enConfig
88- )
89-
90- // 在这里执行写入配置文件操作(这个操作是费时的)
91- await writeConfigIntoWorkSpace ( newZhConfig , newEnConfig , workspaceIntlConfigPath )
92- }
93- catch ( e )
94- {
95- Window . showErrorMessage ( '写入国际化文件发生错误!' )
96- console . log ( 'write config failed' , e )
97- }
9853 }
9954 }
10055 } ;
0 commit comments