UNPKG

codess

Version:

vscode代码片段管理器,通过 本地包(本地某个文件夹) 或官网的 远程包(代码片段集合) 生成本地项目的 vscode 代码片段配置。

151 lines (150 loc) 6.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toSnippetConfig = toSnippetConfig; exports.handleBody = handleBody; exports.parseSnippet = parseSnippet; const ArrayUtils_1 = require("./yayaluoya-tool/ArrayUtils"); const scopeConfig_1 = require("./config/scopeConfig"); function toSnippetConfig(snippets, prefixs = '') { let jsonSnippets = []; let f = (list = [], prefixs = []) => { if (!list || list.length <= 0) { return; } list.forEach((_) => { var _a, _b, _c; _.scope = (_a = _.scope) === null || _a === void 0 ? void 0 : _a.filter(Boolean); switch (_.type) { case 'group': f(_.children, [...prefixs, _.prefix]); break; case 'snippet': { _.body && jsonSnippets.push({ name: _.name || ([...prefixs, _.prefix].filter(Boolean).join('/') + ' ' + (((_b = _.scope) === null || _b === void 0 ? void 0 : _b.join('|')) || '')).trim(), scope: ((_c = _.scope) === null || _c === void 0 ? void 0 : _c.join(',')) || '', prefix: [...prefixs, _.prefix].filter(Boolean).join('>'), body: handleBody(_.body), description: _.description, isFileTemplate: _.isFileTemplate, }); break; } } }); }; f(ArrayUtils_1.ArrayUtils.arraify(snippets), [prefixs].filter(Boolean)); let jsonSnippets_ = jsonSnippets.map((item, i) => { let key = item.name; let count = jsonSnippets.filter((_, i1) => { return i1 < i && _.name == key; }).length; if (count > 0) { key = key + ' ' + count; } return Object.assign(Object.assign({}, item), { key, name: undefined }); }); jsonSnippets_.sort((a, b) => { if (a.key > b.key) { return 1; } if (a.key < b.key) { return -1; } return 0; }); return jsonSnippets_.reduce((a, b, i) => { a[b.key] = Object.assign(Object.assign({}, b), { key: undefined }); return a; }, {}); } function handleBody(body) { return body.split(/\n\r|\r\n|\n|\r/g); } const prefixReg = /#{1,2}\s+(.*)/; const nameReg = /#{3,}\s+(.*)/; const codeReg = /(```|~~~)([^~`\r\n]*)(?:\r?\n|\r)([\s\S]*?)(?:\r?\n|\r)\1/; const codeRegLine = /(```)(.*?)\1/; const codeRegLine2 = /(``) (.*?) \1/; const codeRegLine3 = /(`)(.*?)\1/; const annotationReg = /<!--[\s\S]*?-->/; function parseSnippet(pItem, str, languageSuffix = '.md') { str = str.trim(); pItem.type = 'snippet'; if (languageSuffix == '.md') { let snippets = []; let onItemPrefix = pItem.prefix; let description = {}; let names = {}; while (str.length > 0) { let searchCodeReg; if (str.search(prefixReg) == 0) { let match = str.match(prefixReg); onItemPrefix = match[1].trim(); str = str.slice(match[0].length); } else if ((str.search(codeReg) == 0 && (searchCodeReg = codeReg)) || (str.search(codeRegLine) == 0 && (searchCodeReg = codeRegLine)) || (str.search(codeRegLine2) == 0 && (searchCodeReg = codeRegLine2)) || (str.search(codeRegLine3) == 0 && (searchCodeReg = codeRegLine3))) { let match = str.match(searchCodeReg); switch (true) { case searchCodeReg == codeReg: snippets.push({ type: 'snippet', scope: match[2] .split(/,|\s+/) .filter(Boolean) .map((_) => (0, scopeConfig_1.suffixToScope)(_)), prefix: onItemPrefix, body: match[3], }); break; case searchCodeReg == codeRegLine || searchCodeReg == codeRegLine2 || searchCodeReg == codeRegLine3: snippets.push({ type: 'snippet', prefix: onItemPrefix, body: match[2], }); break; } str = str.slice(match[0].length); } else if (str.search(nameReg) == 0) { let match = str.match(nameReg); names[onItemPrefix] = match[1].trim(); str = str.slice(match[0].length); } else if (str.search(annotationReg) == 0) { let match = str.match(annotationReg); str = str.slice(match[0].length); } else { description[onItemPrefix] || (description[onItemPrefix] = ''); description[onItemPrefix] += str[0]; str = str.slice(1); } } snippets = snippets.filter((_) => !!_.body); snippets.forEach((_) => { _.name = (names[_.prefix] || '').trim(); _.description = (description[_.prefix] || '').trim(); if (pItem.prefix && _.prefix != pItem.prefix) { _.prefix = pItem.prefix + (/^[^\u4E00-\u9FFFa-zA-Z0-9]/.test(_.prefix) ? '' : '-') + _.prefix; } }); return { snippets, description: description[pItem.prefix] || '' }; } else { pItem.body = str; pItem.scope = [(0, scopeConfig_1.suffixToScope)(languageSuffix)].filter(Boolean); pItem.isFileTemplate = true; return { snippets: [pItem] }; } }