Skip to content

Commit 83997ca

Browse files
青羽青羽
authored andcommitted
optimizing the acquisition of python's keyword
1 parent bd7b66b commit 83997ca

File tree

1 file changed

+20
-34
lines changed

1 file changed

+20
-34
lines changed

src/js/component/editor/keyword/python.js

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import _ from 'lodash';
2-
import globalcache from '@js/service/db/globalcache.js';
1+
import { map, isEmpty } from 'lodash';
2+
import util from '../util';
33
import storage from '@/js/helper/storage';
44

55
const pyKeywordInfoProposals = [
@@ -215,69 +215,55 @@ const pyKeywordInfoProposals = [
215215
},
216216
];
217217

218-
/**
219-
* 对拿到的数据格式化成completionList格式
220-
* @param {*} monaco 编辑器
221-
* @param {*} list 格式化列表
222-
* @return {*} 格式化后的列表
223-
*/
224-
function completionListFormatter(monaco, list) {
225-
const formatList = [];
226-
list.forEach((item) => {
227-
if (item.udfType === 1 || item.udfType === 3) {
228-
formatList.push({
229-
label: item.udfName,
230-
kind: monaco.languages.CompletionItemKind.Function,
231-
insertText: item.udfName,
232-
detail: item.udfType > 2 ? '方法函数' : 'UDF函数',
233-
documentation: item.description,
234-
});
235-
}
236-
});
237-
return formatList;
238-
}
218+
let functionProposals = [];
239219

240220
export default {
241221
async register(monaco) {
242-
const userInfo = storage.get('userInfo');
243-
const userName = userInfo.basic.userName;
244-
const globalCache = await globalcache.getCache(userName);
222+
const lang = 'python';
245223

246-
const pyProposals = _.map(pyKeywordInfoProposals, (item) => ({
224+
const pyProposals = map(pyKeywordInfoProposals, (item) => ({
247225
label: item.label.toLowerCase(),
248226
kind: monaco.languages.CompletionItemKind.Keyword,
249227
insertText: item.insertText.toLowerCase(),
250228
detail: item.detail,
251229
documentation: item.documentation,
252230
}));
253231

254-
let functionProposals = completionListFormatter(monaco, globalCache.fnList);
232+
util.getHiveList(monaco, lang).then((list) => {
233+
functionProposals = list.udfProposals;
234+
});
255235

256236
monaco.languages.registerCompletionItemProvider('python', {
257237
triggerCharacters: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._'.split(''),
258238
async provideCompletionItems(model, position) {
239+
if (isEmpty(functionProposals)) {
240+
util.getHiveList(monaco, lang).then((list) => {
241+
functionProposals = list.udfProposals;
242+
});
243+
}
244+
259245
const textUntilPosition = model.getValueInRange({
260246
startLineNumber: position.lineNumber,
261247
startColumn: 1,
262248
endLineNumber: position.lineNumber,
263249
endColumn: position.column,
264250
});
265-
let completionList = null;
266251
const keywordMatch = textUntilPosition.match(/([^"]*)?$/i);
267252
const functionMatch = textUntilPosition.match(/\s+/i);
268253
if (functionMatch) {
269254
const isFunctionChange = storage.get('isFunctionChange_python');
270255
// 如果函数发生load状态变化,则重新从indexdb中获取fnlist
271256
if (isFunctionChange) {
272257
storage.set('isFunctionChange_python', false);
273-
const globalCache = await globalcache.getCache(userName);
274-
functionProposals = completionListFormatter(monaco, globalCache.fnList);
258+
await util.getHiveList(monaco, lang).then((list) => {
259+
return list.udfProposals;
260+
});
275261
}
276-
completionList = functionProposals;
262+
return functionProposals;
277263
} else if (keywordMatch) {
278-
completionList = pyProposals;
264+
return pyProposals;
279265
}
280-
return completionList;
266+
return [];
281267
},
282268
});
283269
},

0 commit comments

Comments
 (0)