Skip to content

Commit c3f5aae

Browse files
committed
refactor: refactoring client extension
1 parent 838335a commit c3f5aae

File tree

3 files changed

+89
-54
lines changed

3 files changed

+89
-54
lines changed

client/src/extension.ts

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import * as path from 'path';
44
import { ExtensionContext, window as Window, Uri, workspace } from 'vscode';
55
import { 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

1111
let 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
};

client/src/lib/constant.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ export const ActivationLanguage = [
99
"onLanguage:vue"
1010
]
1111

12+
// 从语言服务器发出的要执行的插件命令
13+
export enum LinterCommands
14+
{
15+
Extract = 'react-intl-linter.extract', // 抽取中文字符串为 react-intl 代码
16+
}
17+
1218
// 翻译结果 Map 缓存,将在 deactive 的时候清除
1319
export const TranslationResultMap = new Map<string, string[]>()
1420

client/src/middleware/extract.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { Uri, window as Window } from 'vscode'
2+
import { ExecuteCommandSignature } from "vscode-languageclient"
3+
4+
import { getIntlMessage, LinterCommands } from "../lib/constant"
5+
import { getIntlConfig, initializeWorkplaceIntlConfig, writeConfigIntoWorkSpace, writeResultIntoIntlConfig } from "../lib/file"
6+
import { getExistingIntl, getIntlIdWithQuickPick, getTranslateResultsWithProgress, processArgsWithSelectResult } from "../lib/util"
7+
8+
/**
9+
* 命令中间件 - 执行语言服务器发出的抽取中文文本为 react-intl 代码命令
10+
*
11+
* @param {string} intlConfigTemp react-intl 国际化模版配置路径
12+
* @param {(Uri | undefined)} workspaceIntlConfigPath 工作区 react-intl 国际化配置路径
13+
* @param {any[]} args 语言服务器发出该命令时给的参数
14+
* @param {ExecuteCommandSignature} next next 调用语言服务器的 onExecuteCommand 函数
15+
* @return {*}
16+
*/
17+
const ExtractMiddleware = async (intlConfigTemp: string, workspaceIntlConfigPath: Uri | undefined, args: any[], next: ExecuteCommandSignature) =>
18+
{
19+
// 想要替换的中文文本
20+
const searchText = args[1] as string
21+
22+
// 初始化工作区国际化配置文件
23+
await initializeWorkplaceIntlConfig(intlConfigTemp, workspaceIntlConfigPath)
24+
25+
// 获取已有配置文件
26+
const [zhConfig, enConfig] = await getIntlConfig(workspaceIntlConfigPath)
27+
28+
// 查找工作区是否已存在对应中文文本配置
29+
const { intlId, zhText, enText } = getExistingIntl(searchText, zhConfig, enConfig)
30+
31+
// 工作区已存在对应配置
32+
if (intlId && zhText && enText)
33+
{
34+
// 传给语言服务器的 onExecuteCommand 函数
35+
return next(LinterCommands.Extract, [...args, getIntlMessage(intlId)])
36+
}
37+
38+
// 翻译结果相关进度条
39+
const translateResults = await getTranslateResultsWithProgress(searchText)
40+
41+
// picker 选择结果
42+
const [selectedIntlId, translationText] = await getIntlIdWithQuickPick(translateResults, zhConfig);
43+
44+
// 获得处理后的参数,用于传给语言服务器的 onExecuteCommand 函数
45+
const { newArgs, customIntlId } = await processArgsWithSelectResult(args, selectedIntlId)
46+
47+
if (!newArgs || !selectedIntlId || !translationText) return
48+
49+
try
50+
{
51+
// 替换文本操作可以首先实行减少用户对延迟的感知
52+
// 文件写入错误和文本替换并不冲突,只不过需要用户重新执行一遍替换操作来执行文件写入或手动写入文件
53+
next(LinterCommands.Extract, newArgs)
54+
55+
// 获取新的 intl 配置文件
56+
const [newZhConfig, newEnConfig] = await writeResultIntoIntlConfig(
57+
customIntlId || selectedIntlId,
58+
searchText,
59+
translationText,
60+
zhConfig,
61+
enConfig
62+
)
63+
64+
// 在这里执行写入配置文件操作(这个操作是费时的)
65+
await writeConfigIntoWorkSpace(newZhConfig, newEnConfig, workspaceIntlConfigPath)
66+
}
67+
catch (e)
68+
{
69+
Window.showErrorMessage('写入国际化文件发生错误!')
70+
console.log('write config failed', e)
71+
}
72+
}
73+
74+
export default ExtractMiddleware

0 commit comments

Comments
 (0)