2020-02-21 01:17:23 +08:00
|
|
|
import { ExtensionContext, CompletionItem, CompletionItemKind, languages, Disposable, workspace, window, commands, TextDocument, Position, Range, Selection, MarkdownString } from "vscode";
|
|
|
|
import { Rule } from './interface';
|
|
|
|
import { COMPLETION_TRIGGER_ID } from './constant';
|
2020-02-21 02:24:15 +08:00
|
|
|
// import { slugify } from 'transliteration';
|
|
|
|
|
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-21 22:50:42 +08:00
|
|
|
const disposable = languages.registerCompletionItemProvider('*', {
|
2020-02-21 01:17:23 +08:00
|
|
|
provideCompletionItems(document, position) {
|
|
|
|
const linePrefix = document.lineAt(position).text.substr(0, position.character);
|
|
|
|
if (!linePrefix.endsWith(COMPLETION_TRIGGER_ID)) return;
|
|
|
|
|
2020-02-21 02:24:15 +08:00
|
|
|
// showQuickPick
|
2020-02-21 15:30:38 +08:00
|
|
|
window.showQuickPick(RULES.map(({ examples, title, rule }, i) => {
|
|
|
|
// const match = title.match(/\((.+)\)/);
|
|
|
|
return {
|
2020-02-21 02:24:15 +08:00
|
|
|
label: title,
|
2020-02-21 15:30:38 +08:00
|
|
|
// description: null !== match ? match[1] : '',
|
|
|
|
rule: String(rule), // 非标准字段, 仅仅为了传值
|
|
|
|
detail: `例如: ${examples.join(' 或 ')}`
|
2020-02-21 01:17:23 +08:00
|
|
|
};
|
2020-02-21 02:24:15 +08:00
|
|
|
}), {
|
|
|
|
placeHolder: '请输入关键词',
|
|
|
|
// onDidSelectItem(item){
|
2020-02-21 15:30:38 +08:00
|
|
|
// console.log(item)
|
2020-02-21 02:24:15 +08:00
|
|
|
// }
|
2020-02-21 15:30:38 +08:00
|
|
|
}).then(item => {
|
|
|
|
if (!item) return
|
|
|
|
insertRule(document, position, item.rule)
|
|
|
|
window.showInformationMessage(item.rule);
|
2020-02-21 02:24:15 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return void 0;
|
2020-02-21 01:17:23 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
resolveCompletionItem(item) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}, '.');
|
|
|
|
context.subscriptions.push(disposable);
|
|
|
|
}
|
|
|
|
|
2020-02-21 02:24:15 +08:00
|
|
|
|
|
|
|
function insertRule(document: TextDocument, position: Position, ruleString: string) {
|
|
|
|
const editor = window.activeTextEditor;
|
|
|
|
if (void 0 === editor) return;
|
|
|
|
editor.edit(editBuilder => {
|
|
|
|
const line = document.lineAt(position);
|
|
|
|
// 起始
|
|
|
|
const startPostion = new Position(line.lineNumber, line.text.indexOf(COMPLETION_TRIGGER_ID));
|
|
|
|
|
2020-02-21 15:30:38 +08:00
|
|
|
// 结束(replace用)
|
|
|
|
const endPostion = new Position(line.lineNumber, startPostion.character + COMPLETION_TRIGGER_ID.length);
|
|
|
|
|
2020-02-21 02:24:15 +08:00
|
|
|
|
|
|
|
// window.showInformationMessage(
|
|
|
|
// '' + startPostion.character
|
2020-02-21 15:30:38 +08:00
|
|
|
// , '' + endPostion.character
|
|
|
|
// );
|
2020-02-21 02:24:15 +08:00
|
|
|
|
|
|
|
editBuilder.replace(new Range(startPostion, endPostion), ruleString);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2020-02-21 15:30:38 +08:00
|
|
|
// 结束(selection用)
|
|
|
|
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
|
|
|
|