feat: 增加通过菜单唤醒列表

This commit is contained in:
宁宁 2020-02-27 23:57:07 +08:00
parent 37657b15d2
commit de5652d674
4 changed files with 77 additions and 2 deletions

View File

@ -37,18 +37,27 @@
"type": "object", "type": "object",
"title": "any-rule", "title": "any-rule",
"properties": { "properties": {
"AnyRule.triggerString": { "any-rule.triggerString": {
"type": "string", "type": "string",
"default": "@zz", "default": "@zz",
"description": "触发字符串" "description": "触发字符串"
}, },
"AnyRule.supportedLanguages": { "any-rule.supportedLanguages": {
"type": "string", "type": "string",
"default": "*, javascript, typescript, javascriptreact, typescriptreact, markdown, jsx, vue, html, json, plaintext, coffeescript", "default": "*, javascript, typescript, javascriptreact, typescriptreact, markdown, jsx, vue, html, json, plaintext, coffeescript",
"description": "如果您的文件格式未被支持, 请在此处添加(⚡添加成功后需要重启vscode)" "description": "如果您的文件格式未被支持, 请在此处添加(⚡添加成功后需要重启vscode)"
} }
} }
}, },
"menus": {
"editor/context": [
{
"when": "editorFocus",
"command": "extension.rule.callByMenu",
"group": "navigation"
}
]
},
"commands": [ "commands": [
{ {
"command": "extension.rule0", "command": "extension.rule0",
@ -293,6 +302,10 @@
{ {
"command": "extension.rule60", "command": "extension.rule60",
"title": "$(rocket) zz: java包名" "title": "$(rocket) zz: java包名"
},
{
"command": "extension.rule.callByMenu",
"title": "🦕正则大全(61条)"
} }
] ]
}, },

View File

@ -9,6 +9,11 @@ pkg.contributes.commands = RULES.map((rule, index) => ({
title: `$(rocket) zz: ${rule.title}` title: `$(rocket) zz: ${rule.title}`
})); }));
pkg.contributes.commands.push({
command: 'extension.rule.callByMenu',
title: `🦕正则大全(${RULES.length}条)`
});
// console.log(JSON.stringify(pkg)); // console.log(JSON.stringify(pkg));
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 4), 'utf8'); fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 4), 'utf8');
console.log(chalk.green('🚀 pkg文件修改完毕, 请等待生成vsc包...')); console.log(chalk.green('🚀 pkg文件修改完毕, 请等待生成vsc包...'));

View File

@ -6,10 +6,12 @@ import {
const RULES: { title: string, rule: RegExp, examples: string[] }[] = require('../packages/www/src/RULES.js'); const RULES: { title: string, rule: RegExp, examples: string[] }[] = require('../packages/www/src/RULES.js');
import useCommand from './useCommand'; import useCommand from './useCommand';
import useQuickPick from './useQuickPick'; import useQuickPick from './useQuickPick';
import useMenuCommand from './useMenuCommand';
export function activate(context: ExtensionContext) { export function activate(context: ExtensionContext) {
useCommand(context, RULES); useCommand(context, RULES);
useQuickPick(context, RULES); useQuickPick(context, RULES);
useMenuCommand(context, RULES);
} }
export function deactivate() { } export function deactivate() { }

55
src/useMenuCommand.ts Normal file
View File

@ -0,0 +1,55 @@
import { window, commands, Range, ExtensionContext } from "vscode";
import { Rule } from './interface';
import insertLog from './insertLog';
import showResultMessage from './showResultMessage';
export default function (context: ExtensionContext, RULES: Rule[]) {
const disposable = commands.registerCommand(`extension.rule.callByMenu`, () => {
// showQuickPick
window.showQuickPick(RULES.map(({ examples, title, rule }) => {
// 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
const editor = window.activeTextEditor;
if (editor) {
const ruleString = String(item.rule);
const title = item.label;
const { selections } = editor;
editor.edit(editBuilder => {
selections.forEach(selection => {
const { start, end } = selection;
const range = new Range(start, end);
editBuilder.replace(range, ruleString);
});
});
// 日志
insertLog({
rule: ruleString,
title,
method: 'Menu'
});
showResultMessage(title, ruleString);
}
});
});
context.subscriptions.push(disposable);
}