2020-02-23 01:11:22 +08:00
|
|
|
import { ExtensionContext, workspace, languages, window, TextDocument, Position, Range, Selection } from "vscode";
|
2020-02-21 01:17:23 +08:00
|
|
|
import { Rule } from './interface';
|
2020-02-21 02:24:15 +08:00
|
|
|
// import { slugify } from 'transliteration';
|
2020-02-22 17:16:39 +08:00
|
|
|
import insertLog from './insertLog';
|
|
|
|
import showResultMessage from './showResultMessage';
|
2020-02-21 02:24:15 +08:00
|
|
|
|
2020-02-21 01:17:23 +08:00
|
|
|
export default function (context: ExtensionContext, RULES: Rule[]) {
|
2020-02-21 02:24:15 +08:00
|
|
|
// commands.registerCommand('functions.insertRegex', insertRule);
|
2020-02-23 01:11:22 +08:00
|
|
|
// 不确定是不是都兼容"*", 保守
|
|
|
|
const { supportedLanguages, triggerStringEnd } = getConfig();
|
|
|
|
const disposable = languages.registerCompletionItemProvider(supportedLanguages.split(','), {
|
2020-02-21 01:17:23 +08:00
|
|
|
provideCompletionItems(document, position) {
|
2020-02-23 01:11:22 +08:00
|
|
|
const { triggerString } = getConfig();
|
|
|
|
// 如果为空表示关闭
|
2020-02-23 01:22:28 +08:00
|
|
|
if (!triggerString) return [];
|
2020-02-23 01:11:22 +08:00
|
|
|
|
2020-02-21 01:17:23 +08:00
|
|
|
const linePrefix = document.lineAt(position).text.substr(0, position.character);
|
2020-02-23 01:22:28 +08:00
|
|
|
if (!linePrefix.endsWith(triggerString)) return [];
|
2020-02-21 01:17:23 +08:00
|
|
|
|
2020-02-23 01:22:28 +08:00
|
|
|
// 滞后执行
|
2020-02-23 01:11:22 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
// showQuickPick
|
2020-02-23 01:22:28 +08:00
|
|
|
window.showQuickPick(RULES.map(({ examples, title, rule }) => {
|
2020-02-23 01:11:22 +08:00
|
|
|
// const match = title.match(/\((.+)\)/);
|
|
|
|
return {
|
|
|
|
label: title,
|
|
|
|
// description: null !== match ? match[1] : '',
|
|
|
|
rule: String(rule), // 非标准字段, 仅仅为了传值
|
|
|
|
detail: `例如: ${examples.join(' 或 ')}`
|
|
|
|
};
|
|
|
|
}), {
|
|
|
|
placeHolder: '请输入关键词',
|
|
|
|
// onDidSelectItem(item){
|
|
|
|
// console.log(item)
|
|
|
|
// }
|
|
|
|
}).then(item => {
|
|
|
|
if (!item) return
|
|
|
|
insertRule(document, position, item.rule);
|
2020-02-22 01:08:31 +08:00
|
|
|
|
2020-02-23 01:11:22 +08:00
|
|
|
// 日志
|
|
|
|
insertLog({
|
|
|
|
rule: item.rule,
|
|
|
|
title: item.label,
|
|
|
|
method: 'QuickPick'
|
|
|
|
});
|
|
|
|
showResultMessage(item.label);
|
2020-02-22 01:08:31 +08:00
|
|
|
});
|
2020-02-23 01:33:01 +08:00
|
|
|
}, 10)
|
2020-02-23 01:22:28 +08:00
|
|
|
return [];
|
2020-02-21 01:17:23 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
resolveCompletionItem(item) {
|
|
|
|
return item;
|
|
|
|
}
|
2020-02-23 01:11:22 +08:00
|
|
|
}, triggerStringEnd);
|
2020-02-21 01:17:23 +08:00
|
|
|
context.subscriptions.push(disposable);
|
|
|
|
}
|
|
|
|
|
2020-02-21 02:24:15 +08:00
|
|
|
|
|
|
|
function insertRule(document: TextDocument, position: Position, ruleString: string) {
|
2020-02-23 01:11:22 +08:00
|
|
|
const { triggerString } = getConfig();
|
|
|
|
|
2020-02-21 02:24:15 +08:00
|
|
|
const editor = window.activeTextEditor;
|
|
|
|
if (void 0 === editor) return;
|
|
|
|
editor.edit(editBuilder => {
|
|
|
|
const line = document.lineAt(position);
|
|
|
|
|
2020-02-23 01:11:22 +08:00
|
|
|
// 起始, "zz."前面的位置
|
|
|
|
const startPostion = position.translate(0, -triggerString.length);
|
2020-02-21 15:30:38 +08:00
|
|
|
|
2020-02-23 01:11:22 +08:00
|
|
|
editBuilder.replace(new Range(startPostion, position), ruleString);
|
2020-02-21 02:24:15 +08:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2020-02-23 01:11:22 +08:00
|
|
|
// 全选正则字符串
|
2020-02-21 15:30:38 +08:00
|
|
|
const endPostion = new Position(line.lineNumber, startPostion.character + ruleString.length);
|
2020-02-21 02:24:15 +08:00
|
|
|
editor.selection = new Selection(startPostion, endPostion);
|
|
|
|
}, 0);
|
2020-02-21 01:17:23 +08:00
|
|
|
});
|
|
|
|
}
|
2020-02-21 02:24:15 +08:00
|
|
|
|
2020-02-23 01:11:22 +08:00
|
|
|
// 获取配置
|
|
|
|
function getConfig() {
|
|
|
|
const configuration = workspace.getConfiguration();
|
|
|
|
const { triggerString, supportedLanguages = '*' } = configuration.AnyRule;
|
|
|
|
const { length } = triggerString;
|
|
|
|
const triggerStringStart = triggerString.substr(0, length - 1);
|
|
|
|
const triggerStringEnd = triggerString.substr(-1);
|
|
|
|
|
|
|
|
return {
|
|
|
|
triggerStringStart, triggerStringEnd, triggerString, supportedLanguages
|
|
|
|
}
|
|
|
|
}
|