-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension copy.js
221 lines (180 loc) · 5.98 KB
/
extension copy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const vscode = require('vscode');
const { getProjectPath } = require('./src/utils')
const provideHover = require('./src/provideHover')
const disposable = require('./src/translate')
const hf = require('./src/handleFiles')
const fs = require('fs')
let filelanguageDatas, cacheDoc
function explainPaths(projectPath) {
// 判断有没有import 如果没有 则认为是单文件直接导出语言包配置反之是多文件
let fileData = fs.readFileSync(`${projectPath}/${pt}`, 'utf-8')
if (/^\s*import\s+/.test(fileData)) {
// 多文件
// 1、多文件路径只能用相对路径
const css = fileData.match(/\s*import\s+from\s+(['"])(.{1,2}\/)?(\w+\/)+\1/g).map(cs => {
// 取出变量
return cs.match(/export\sconst\s+(\w+)\s*/)[1]
})
}
}
function clonePage({input}, projectPath) {
let pages = []
pages = explainPaths(projectPath)
const wholeData = []
pages.forEach(pt => {
// 获取文件名
let fileInfo = pt.match(/\/(\w+)\.(\w+)$/)
const fileName = fileInfo[1]
const fileType = fileInfo[2]
let fileData = fs.readFileSync(`${projectPath}/${pt}`, 'utf-8')
if (fileType === "json") {
// json 文件直接读取数据
wholeData.push(fileData)
} else {
if (/^\s*import\s+/.test(fileData)) {
console.log('抛出错误!解析不了了')
return
}
// es6 读取数据 export default 或者 export const
let hasExportDefault = false
if (fileData.indexOf('export default') !== -1) {
hasExportDefault = true
fileData = fileData.replace(/export\sdefault/g, 'module.exports =')
}
if (fileData.indexOf('export const') !== -1) {
// 取出const 变量的值
const css = fileData.match(/export\sconst\s+(\w+)\s*/g).map(cs => {
// 取出变量
return cs.match(/export\sconst\s+(\w+)\s*/)[1]
})
if (hasExportDefault) {
// 插入进去
fileData = fileData.replace(/module\.exports\s*\=\s*\{/, `module.exports = {${css.toString()},`)
} else {
fileData = `
${fileData}
module.exports = {
${css.toString()}
}
`
}
// 删除 export
fileData = fileData.replace(/export\sconst/g, 'const')
}
fs.writeFileSync(`${projectPath}/src/language/__tem__${fileName}.js`, fileData);
fileData = require(`${projectPath}/src/language/c.json`);
wholeData.push(fileData)
}
})
return wholeData
}
function reReadFiles() {
console.log('重新扫描')
const projectPath = getProjectPath(cacheDoc);
const jsonConfig = require(`${projectPath}/package.json`);
let filelanguageDatas
if (jsonConfig["language-config"]) {
// 存在配置
filelanguageDatas = clonePage({
// 对应的中文输入集 传入参数为数组 多个模块导出的数据,非必填
pages: jsonConfig["language-config"]["zh-path"],
// 输入入口, 这个地方是一个汇总的入口文件
input: jsonConfig["language-config"]["zh-input"]
}, projectPath)
} else {
// 抛出错误
}
// var fileData = fs.readFileSync(`${projectPath}/${filePath.zh}`, 'utf-8');
// if (fileData.indexOf('export default') !== -1) {
// let fd = fileData.replace(/export\sdefault/g, 'module.exports =')
// fs.writeFileSync(`${projectPath}/src/language/__tem__.js`, fd);
// filelanguageDatas = require(`${projectPath}/src/language/c.json`);
// // console.log(languageJson, "jsonConfig")
// // console.log('---', languageJson.c1)
// }
return filelanguageDatas
}
/**
* @param {vscode.ExtensionContext} context
*/
function provideCompletionItems(document, position) {
const line = document.lineAt(position);
cacheDoc = document
// 只截取到光标位置为止,防止一些特殊情况
const lineText = line.text
let languageJson = filelanguageDatas || reReadFiles()
console.log(languageJson)
if (/\$t/g.test(lineText)) {
return [
new vscode.CompletionItem(`t('1111')`, vscode.CompletionItemKind.Field),
new vscode.CompletionItem(`t('222')`, vscode.CompletionItemKind.Field),
new vscode.CompletionItem(`t('333')`, vscode.CompletionItemKind.Field),
];
}
// 简单匹配,只要当前光标前的字符串为`this.dependencies.`都自动带出所有的依赖
// const moduleskey = Object.keys(languageJson[baseKey] || {})
// let matchKey = moduleskey.filter(k => {
// if (lineText.indexOf(k) !== -1) {
// return true
// }
// return false
// })
// if (/\w*\.?\$t/g.test(lineText) && matchKey && matchKey.length > 0) {
// const matchInnerKey = languageJson[baseKey][matchKey[0]]
// return Object.keys(matchInnerKey).map(ik => {
// return new vscode.CompletionItem(`${ik}-${matchInnerKey[ik]}`, vscode.CompletionItemKind.Field);
// })
// }else if(/\w*\.?\$t/g.test(lineText)) {
// return moduleskey.map(k => {
// return new vscode.CompletionItem(`${k}`, vscode.CompletionItemKind.Field);
// })
// }
}
function resolveCompletionItem(item) {
if (item.label.indexOf('-') !== -1) [
item.label = item.label.split('-')[0]
]
return item
}
function activate(context) {
const reRead = vscode.commands.registerCommand('read.file', async function () {
reReadFiles()
})
// 注册重新扫描页面
context.subscriptions.push(reRead)
// 注册翻译
context.subscriptions.push(disposable)
const TYPES = [
'javascript',
'html',
'typescript',
'vue'
];
// 遍历注册插件需要执行的文本类型
TYPES.forEach(item => {
let providerDisposable = vscode.languages.registerCompletionItemProvider(
{
scheme: 'file',
language: item
},
{
provideCompletionItems,
resolveCompletionItem
},
't'
);
context.subscriptions.push(providerDisposable); // 完成订阅
});
// context.subscriptions.push(vscode.languages.registerCompletionItemProvider(['javascript', 'json', 'jsx', 'typescript'], {
// provideCompletionItems,
// resolveCompletionItem
// }, '.'));
// 注册鼠标悬停提示
provideHover(context)
}
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
}