1
0
mirror of https://github.com/salexdv/bsl_console.git synced 2024-11-28 08:48:48 +02:00

Функции дополнительного преобразования сниппета при первой активации

This commit is contained in:
salexdv 2022-01-30 15:26:54 +03:00
parent d46b2b34cd
commit 3907b55710

View File

@ -477,6 +477,19 @@ class bslHelper {
}
/**
* GUID genarator
*
* @returns {string} GUID
*/
guid() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
/**
* Convert code to markdown
* with syntax highlighting
@ -510,13 +523,135 @@ class bslHelper {
}
/**
* Replace standart choice elements with metadata objects
* Get array of metadata object names
*
* @param {string} snippet code of snippet
* @param {string} metadataItem name of metadata object
* @param {completionItem} completionItem current item of suggestion list
*
* @returns {array} array of metadata object names
*/
getSnippetMetadataItems(metadataItem, completionItem) {
let items = [];
if (bslMetadata.hasOwnProperty(metadataItem)) {
if (Object.keys(bslMetadata[metadataItem].items).length) {
for (const [key, value] of Object.entries(bslMetadata[metadataItem].items)) {
items.push(key);
}
}
else {
if (completionItem)
setTimeout(() => {
requestMetadata(bslMetadata[metadataItem].name.toLowerCase(), 'snippet', {
snippet_guid: completionItem.guid
});
}, 10);
}
}
return items;
}
/**
* Get array of metadata object names by snippet action
*
* @param {string} action type of action from snippet
* @param {completionItem} completionItem current item of suggestion list
*
* @returns {array} array of metadata object names
*/
getSnippetMetadataItemsByAction(action, completionItem) {
let items = [];
let relations = [];
relations['Справочник'] = 'catalogs';
relations['ВыберитеСправочник'] = 'catalogs';
relations['Документ'] = 'documents';
relations['ВыберитеДокумент'] = 'documents';
relations['РегистрСведений'] = 'infoRegs';
relations['ВыберитеРегистрСведений'] = 'infoRegs';
relations['РегистрНакопления'] = 'accumRegs';
relations['ВыберитеРегистрНакопления'] = 'accumRegs';
relations['РегистрБухгалтерии'] = 'accountRegs';
relations['ВыберитеРегистрБухгалтерии'] = 'accountRegs';
relations['РегистрРасчета'] = 'calcRegs';
relations['ВыберитеРегистрРасчета'] = 'calcRegs';
relations['Обработка'] = 'dataProc';
relations['ВыберитеОбработку'] = 'dataProc';
relations['Отчет'] = 'reports';
relations['ВыберитеОтчет'] = 'reports';
relations['Перечисление'] = 'enums';
relations['ВыберитеПеречисление'] = 'enums';
relations['ПланСчетов'] = 'сhartsOfAccounts';
relations['ВыберитеПланСчетов'] = 'сhartsOfAccounts';
relations['БизнесПроцесс'] = 'businessProcesses';
relations['ВыберитеБизнесПроцесс'] = 'businessProcesses';
relations['Задача'] = 'tasks';
relations['ВыберитеЗадачу'] = 'tasks';
relations['ПланОбмена'] = 'exchangePlans';
relations['ВыберитеПланОбмена'] = 'exchangePlans';
relations['ПланВидовХарактеристик'] = 'chartsOfCharacteristicTypes';
relations['ВыберитеПланВидовХарактеристик'] = 'chartsOfCharacteristicTypes';
relations['ПланВидовРасчета'] = 'chartsOfCalculationTypes';
relations['ВыберитеПланВидовРасчета'] = 'chartsOfCalculationTypes';
relations['Константа'] = 'constants';
relations['ВыберитеКонстанту'] = 'constants';
relations['РегистрРасчета'] = 'calcRegs';
let relation = relations[action];
if (relation)
items = this.getSnippetMetadataItems(relation, completionItem);
return items;
}
/**
* Replace standart choice elements with metadata objects
* or other additional transformations
*
* @param {string} snippet code of snippet
* @param {string} completionItem item of suggestion list
*
* @returns {string} formated code of snippet
*/
prepareSnippetCode(snippet) {
prepareSnippetCode(snippet, completionItem) {
let regexp = RegExp('\\${(\\d{1,}):(.*?)}', 'gmi');
let match = null;
while ((match = regexp.exec(snippet)) !== null) {
let action = match[2];
let items = this.getSnippetMetadataItemsByAction(action, completionItem);
if (items.length) {
let text = match[0];
text = text.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
let replace_reg = RegExp(text, 'gmi');
let prev_snippet = snippet;
if (1 < items.length)
snippet = snippet.replace(replace_reg, '${' + match[1] + '|' + items.join() + '|' + '}');
else
snippet = snippet.replace(replace_reg, items[0]);
if (snippet == prev_snippet)
break;
}
}
return snippet;
@ -2979,6 +3114,25 @@ class bslHelper {
}
/**
* Computing completion item its first activation
* at the suggestion list
*
* @param {completionItem} completionItem current item of suggestion list
*
* @returns {completionItem} computed completion item
*/
resolveCompletionItem(completionItem) {
if (completionItem.kind == monaco.languages.CompletionItemKind.Snippet) {
completionItem.guid = this.guid();
completionItem.insertText = this.prepareSnippetCode(completionItem.insertText, completionItem);
}
return completionItem
}
/**
* Fills array of completion for query values
*
@ -4729,7 +4883,7 @@ class bslHelper {
suggestions.push({
label: value.prefix,
kind: monaco.languages.CompletionItemKind.Snippet,
insertText: this.prepareSnippetCode(value.body),
insertText: value.body,
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
detail: key,
documentation: { "value": this.prepareSnippetDocumentation(value.body) }