From de5652d674ed35c3298a06a7a5e964ba2c51e176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=81=E5=AE=81?= <383514580@qq.com> Date: Thu, 27 Feb 2020 23:57:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E5=94=A4=E9=86=92=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 17 +++++++++++-- scripts/genCommond.js | 5 ++++ src/extension.ts | 2 ++ src/useMenuCommand.ts | 55 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/useMenuCommand.ts diff --git a/package.json b/package.json index 350026d..1a96302 100644 --- a/package.json +++ b/package.json @@ -37,18 +37,27 @@ "type": "object", "title": "any-rule", "properties": { - "AnyRule.triggerString": { + "any-rule.triggerString": { "type": "string", "default": "@zz", "description": "触发字符串" }, - "AnyRule.supportedLanguages": { + "any-rule.supportedLanguages": { "type": "string", "default": "*, javascript, typescript, javascriptreact, typescriptreact, markdown, jsx, vue, html, json, plaintext, coffeescript", "description": "如果您的文件格式未被支持, 请在此处添加(⚡添加成功后需要重启vscode)" } } }, + "menus": { + "editor/context": [ + { + "when": "editorFocus", + "command": "extension.rule.callByMenu", + "group": "navigation" + } + ] + }, "commands": [ { "command": "extension.rule0", @@ -293,6 +302,10 @@ { "command": "extension.rule60", "title": "$(rocket) zz: java包名" + }, + { + "command": "extension.rule.callByMenu", + "title": "🦕正则大全(61条)" } ] }, diff --git a/scripts/genCommond.js b/scripts/genCommond.js index ec87a02..ccab154 100644 --- a/scripts/genCommond.js +++ b/scripts/genCommond.js @@ -9,6 +9,11 @@ pkg.contributes.commands = RULES.map((rule, index) => ({ title: `$(rocket) zz: ${rule.title}` })); +pkg.contributes.commands.push({ + command: 'extension.rule.callByMenu', + title: `🦕正则大全(${RULES.length}条)` +}); + // console.log(JSON.stringify(pkg)); fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 4), 'utf8'); console.log(chalk.green('🚀 pkg文件修改完毕, 请等待生成vsc包...')); \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 0148e63..dfbe775 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,10 +6,12 @@ import { const RULES: { title: string, rule: RegExp, examples: string[] }[] = require('../packages/www/src/RULES.js'); import useCommand from './useCommand'; import useQuickPick from './useQuickPick'; +import useMenuCommand from './useMenuCommand'; export function activate(context: ExtensionContext) { useCommand(context, RULES); useQuickPick(context, RULES); + useMenuCommand(context, RULES); } export function deactivate() { } diff --git a/src/useMenuCommand.ts b/src/useMenuCommand.ts new file mode 100644 index 0000000..f80df3d --- /dev/null +++ b/src/useMenuCommand.ts @@ -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); +} \ No newline at end of file