From 1fb42b4817a965a09f7181d3f6e35b15d3dcb724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=81=E5=AE=81?= <383514580@qq.com> Date: Sat, 22 Feb 2020 17:16:39 +0800 Subject: [PATCH] =?UTF-8?q?reafactor:=20=E6=8A=BD=E8=B1=A1=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E6=A1=86=E5=92=8C=E4=B8=80=E4=BA=9B=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=89=80=E9=9C=80=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 186 ++++++++-------------- scripts/genCommond.js | 17 +- src/extension.ts | 4 +- src/{inserLog.ts => insertLog.ts} | 19 ++- src/shared.ts | 16 ++ src/showResultMessage.ts | 32 ++++ src/useCommand.ts | 15 +- src/{useCompletion.ts => useQuickPick.ts} | 20 +-- 8 files changed, 141 insertions(+), 168 deletions(-) rename src/{inserLog.ts => insertLog.ts} (51%) create mode 100644 src/shared.ts create mode 100644 src/showResultMessage.ts rename src/{useCompletion.ts => useQuickPick.ts} (75%) diff --git a/package.json b/package.json index d2baf5e..55233bf 100644 --- a/package.json +++ b/package.json @@ -19,67 +19,6 @@ "url": "git+https://github.com/any86/any-rule.git" }, "activationEvents": [ - "onCommand:extension.rule0", - "onCommand:extension.rule1", - "onCommand:extension.rule2", - "onCommand:extension.rule3", - "onCommand:extension.rule4", - "onCommand:extension.rule5", - "onCommand:extension.rule6", - "onCommand:extension.rule7", - "onCommand:extension.rule8", - "onCommand:extension.rule9", - "onCommand:extension.rule10", - "onCommand:extension.rule11", - "onCommand:extension.rule12", - "onCommand:extension.rule13", - "onCommand:extension.rule14", - "onCommand:extension.rule15", - "onCommand:extension.rule16", - "onCommand:extension.rule17", - "onCommand:extension.rule18", - "onCommand:extension.rule19", - "onCommand:extension.rule20", - "onCommand:extension.rule21", - "onCommand:extension.rule22", - "onCommand:extension.rule23", - "onCommand:extension.rule24", - "onCommand:extension.rule25", - "onCommand:extension.rule26", - "onCommand:extension.rule27", - "onCommand:extension.rule28", - "onCommand:extension.rule29", - "onCommand:extension.rule30", - "onCommand:extension.rule31", - "onCommand:extension.rule32", - "onCommand:extension.rule33", - "onCommand:extension.rule34", - "onCommand:extension.rule35", - "onCommand:extension.rule36", - "onCommand:extension.rule37", - "onCommand:extension.rule38", - "onCommand:extension.rule39", - "onCommand:extension.rule40", - "onCommand:extension.rule41", - "onCommand:extension.rule42", - "onCommand:extension.rule43", - "onCommand:extension.rule44", - "onCommand:extension.rule45", - "onCommand:extension.rule46", - "onCommand:extension.rule47", - "onCommand:extension.rule48", - "onCommand:extension.rule49", - "onCommand:extension.rule50", - "onCommand:extension.rule51", - "onCommand:extension.rule52", - "onCommand:extension.rule53", - "onCommand:extension.rule54", - "onCommand:extension.rule55", - "onCommand:extension.rule56", - "onCommand:extension.rule57", - "onCommand:extension.rule58", - "onCommand:extension.rule59", - "onCommand:extension.rule60", "*" ], "main": "./out/extension.js", @@ -87,247 +26,247 @@ "commands": [ { "command": "extension.rule0", - "title": "zz: 火车车次" + "title": "$(rocket) zz: 火车车次" }, { "command": "extension.rule1", - "title": "zz: 手机机身码(IMEI)" + "title": "$(rocket) zz: 手机机身码(IMEI)" }, { "command": "extension.rule2", - "title": "zz: 必须带端口号的网址(或ip)" + "title": "$(rocket) zz: 必须带端口号的网址(或ip)" }, { "command": "extension.rule3", - "title": "zz: 网址(支持端口和\"?+参数\"和\"#+参数)" + "title": "$(rocket) zz: 网址(支持端口和\"?+参数\"和\"#+参数)" }, { "command": "extension.rule4", - "title": "zz: 统一社会信用代码" + "title": "$(rocket) zz: 统一社会信用代码" }, { "command": "extension.rule5", - "title": "zz: 迅雷链接" + "title": "$(rocket) zz: 迅雷链接" }, { "command": "extension.rule6", - "title": "zz: ed2k链接(宽松匹配)" + "title": "$(rocket) zz: ed2k链接(宽松匹配)" }, { "command": "extension.rule7", - "title": "zz: 磁力链接(宽松匹配)" + "title": "$(rocket) zz: 磁力链接(宽松匹配)" }, { "command": "extension.rule8", - "title": "zz: 子网掩码" + "title": "$(rocket) zz: 子网掩码" }, { "command": "extension.rule9", - "title": "zz: linux\"文件夹\"路径" + "title": "$(rocket) zz: linux\"文件夹\"路径" }, { "command": "extension.rule10", - "title": "zz: linux\"文件\"路径" + "title": "$(rocket) zz: linux\"文件\"路径" }, { "command": "extension.rule11", - "title": "zz: window下\"文件夹\"路径" + "title": "$(rocket) zz: window\"文件夹\"路径" }, { "command": "extension.rule12", - "title": "zz: window下\"文件\"路径" + "title": "$(rocket) zz: window下\"文件\"路径" }, { "command": "extension.rule13", - "title": "zz: A股代码" + "title": "$(rocket) zz: 股票代码(A股)" }, { "command": "extension.rule14", - "title": "zz: 大于等于0, 小于等于150, 支持小数位出现5, 如145.5, 用于判断考卷分数" + "title": "$(rocket) zz: 大于等于0, 小于等于150, 支持小数位出现5, 如145.5, 用于判断考卷分数" }, { "command": "extension.rule15", - "title": "zz: html注释" + "title": "$(rocket) zz: html注释" }, { "command": "extension.rule16", - "title": "zz: md5格式(32位)" + "title": "$(rocket) zz: md5格式(32位)" }, { "command": "extension.rule17", - "title": "zz: 版本号格式必须为X.Y.Z" + "title": "$(rocket) zz: 版本号格式必须为X.Y.Z" }, { "command": "extension.rule18", - "title": "zz: 视频链接地址(视频格式可按需增删)" + "title": "$(rocket) zz: 视频链接地址(视频格式可按需增删)" }, { "command": "extension.rule19", - "title": "zz: 图片链接地址(图片格式可按需增删)" + "title": "$(rocket) zz: 图片链接地址(图片格式可按需增删)" }, { "command": "extension.rule20", - "title": "zz: 24小时制时间(HH:mm:ss)" + "title": "$(rocket) zz: 24小时制时间(HH:mm:ss)" }, { "command": "extension.rule21", - "title": "zz: 12小时制时间(hh:mm:ss)" + "title": "$(rocket) zz: 12小时制时间(hh:mm:ss)" }, { "command": "extension.rule22", - "title": "zz: base64格式" + "title": "$(rocket) zz: base64格式" }, { "command": "extension.rule23", - "title": "zz: 数字/货币金额(支持负数、千分位分隔符)" + "title": "$(rocket) zz: 数字/货币金额(支持负数、千分位分隔符)" }, { "command": "extension.rule24", - "title": "zz: 数字/货币金额 (只支持正数、不支持校验千分位分隔符)" + "title": "$(rocket) zz: 数字/货币金额 (只支持正数、不支持校验千分位分隔符)" }, { "command": "extension.rule25", - "title": "zz: 银行卡号(10到30位, 覆盖对公/私账户, 参考[微信支付](https://pay.weixin.qq.com/wiki/doc/api/xiaowei.php?chapter=22_1))" + "title": "$(rocket) zz: 银行卡号(10到30位, 覆盖对公/私账户, 参考[微信支付](https://pay.weixin.qq.com/wiki/doc/api/xiaowei.php?chapter=22_1))" }, { "command": "extension.rule26", - "title": "zz: 中文姓名" + "title": "$(rocket) zz: 中文姓名" }, { "command": "extension.rule27", - "title": "zz: 英文姓名" + "title": "$(rocket) zz: 英文姓名" }, { "command": "extension.rule28", - "title": "zz: 新能源车牌号" + "title": "$(rocket) zz: 车牌号(新能源)" }, { "command": "extension.rule29", - "title": "zz: 非新能源车牌号" + "title": "$(rocket) zz: 车牌号(非新能源)" }, { "command": "extension.rule30", - "title": "zz: 车牌号(新能源+非新能源)" + "title": "$(rocket) zz: 车牌号(新能源+非新能源)" }, { "command": "extension.rule31", - "title": "zz: 中国手机号(严谨), 根据工信部2019年最新公布的手机号段" + "title": "$(rocket) zz: 手机号中国(严谨), 根据工信部2019年最新公布的手机号段" }, { "command": "extension.rule32", - "title": "zz: 中国手机号(宽松), 只要是13,14,15,16,17,18,19开头即可" + "title": "$(rocket) zz: 手机号中国(宽松), 只要是13,14,15,16,17,18,19开头即可" }, { "command": "extension.rule33", - "title": "zz: 中国手机号(最宽松), 只要是1开头即可, 如果你的手机号是用来接收短信, 优先建议选择这一条" + "title": "$(rocket) zz: 手机号中国(最宽松), 只要是1开头即可, 如果你的手机号是用来接收短信, 优先建议选择这一条" }, { "command": "extension.rule34", - "title": "zz: 日期" + "title": "$(rocket) zz: 日期" }, { "command": "extension.rule35", - "title": "zz: 邮箱地址(email)" + "title": "$(rocket) zz: email(邮箱)" }, { "command": "extension.rule36", - "title": "zz: 国内座机电话,如: 0341-86091234" + "title": "$(rocket) zz: 座机电话(国内),如: 0341-86091234" }, { "command": "extension.rule37", - "title": "zz: 一代身份证号(15位数字)" + "title": "$(rocket) zz: 身份证号(1代,15位数字)" }, { "command": "extension.rule38", - "title": "zz: 二代身份证号(18位数字),最后一位是校验位,可能为数字或字符X" + "title": "$(rocket) zz: 身份证号(2代,18位数字),最后一位是校验位,可能为数字或字符X" }, { "command": "extension.rule39", - "title": "zz: 身份证号, 支持1/2代(15位/18位数字)" + "title": "$(rocket) zz: 身份证号, 支持1/2代(15位/18位数字)" }, { "command": "extension.rule40", - "title": "zz: 护照(包含香港、澳门)" + "title": "$(rocket) zz: 护照(包含香港、澳门)" }, { "command": "extension.rule41", - "title": "zz: 帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线组合" + "title": "$(rocket) zz: 帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线组合" }, { "command": "extension.rule42", - "title": "zz: 纯中文/汉字" + "title": "$(rocket) zz: 中文/汉字" }, { "command": "extension.rule43", - "title": "zz: 是否小数" + "title": "$(rocket) zz: 小数" }, { "command": "extension.rule44", - "title": "zz: 电话(座机)" + "title": "$(rocket) zz: 数字" }, { "command": "extension.rule45", - "title": "zz: 纯数字" + "title": "$(rocket) zz: html标签(宽松匹配)" }, { "command": "extension.rule46", - "title": "zz: 是否html标签(宽松匹配)" + "title": "$(rocket) zz: qq号格式正确" }, { "command": "extension.rule47", - "title": "zz: 是否qq号格式正确" + "title": "$(rocket) zz: 数字和字母组成" }, { "command": "extension.rule48", - "title": "zz: 是否由数字和字母组成" + "title": "$(rocket) zz: 英文字母" }, { "command": "extension.rule49", - "title": "zz: 纯英文字母" + "title": "$(rocket) zz: 小写英文字母组成" }, { "command": "extension.rule50", - "title": "zz: 纯小写英文字母组成" + "title": "$(rocket) zz: 大写英文字母" }, { "command": "extension.rule51", - "title": "zz: 纯大写英文字母" + "title": "$(rocket) zz: 密码强度校验,最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符" }, { "command": "extension.rule52", - "title": "zz: 密码强度校验,最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符" + "title": "$(rocket) zz: 用户名校验,4到16位(字母,数字,下划线,减号)" }, { "command": "extension.rule53", - "title": "zz: 用户名校验,4到16位(字母,数字,下划线,减号)" + "title": "$(rocket) zz: ip-v4" }, { "command": "extension.rule54", - "title": "zz: ip-v4" + "title": "$(rocket) zz: ip-v6" }, { "command": "extension.rule55", - "title": "zz: ip-v6" + "title": "$(rocket) zz: 16进制颜色" }, { "command": "extension.rule56", - "title": "zz: 16进制颜色" + "title": "$(rocket) zz: 微信号,6至20位,以字母开头,字母,数字,减号,下划线" }, { "command": "extension.rule57", - "title": "zz: 微信号,6至20位,以字母开头,字母,数字,减号,下划线" + "title": "$(rocket) zz: 邮政编码(中国)" }, { "command": "extension.rule58", - "title": "zz: 中国邮政编码" + "title": "$(rocket) zz: 中文和数字" }, { "command": "extension.rule59", - "title": "zz: 只包含中文和数字" + "title": "$(rocket) zz: 不能包含字母" }, { "command": "extension.rule60", - "title": "zz: 不能包含字母" + "title": "$(rocket) zz: java包名" } ] }, @@ -369,7 +308,6 @@ } }, "dependencies": { - "axios": "^0.19.2", "transliteration": "^2.1.8" } -} +} \ No newline at end of file diff --git a/scripts/genCommond.js b/scripts/genCommond.js index f5346c5..ec87a02 100644 --- a/scripts/genCommond.js +++ b/scripts/genCommond.js @@ -3,17 +3,12 @@ const pkg = require('../package.json'); const fs = require('fs'); const chalk = require('chalk'); -pkg.contributes.commands = []; -pkg.activationEvents = []; -RULES.forEach((rule, index) => { - pkg.contributes.commands.push({ - command: 'extension.rule' + index, - title: `zz: ${rule.title}` - }); - - pkg.activationEvents.push('*'); -}) +pkg.activationEvents = ['*']; +pkg.contributes.commands = RULES.map((rule, index) => ({ + command: 'extension.rule' + index, + title: `$(rocket) zz: ${rule.title}` +})); // console.log(JSON.stringify(pkg)); fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 4), 'utf8'); -console.log(chalk.green('🚀 pkg文件修改完毕, 请等待生成vsc包...')); +console.log(chalk.green('🚀 pkg文件修改完毕, 请等待生成vsc包...')); \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index eb58e94..0148e63 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -5,11 +5,11 @@ import { } from 'vscode'; const RULES: { title: string, rule: RegExp, examples: string[] }[] = require('../packages/www/src/RULES.js'); import useCommand from './useCommand'; -import useCompletion from './useCompletion'; +import useQuickPick from './useQuickPick'; export function activate(context: ExtensionContext) { useCommand(context, RULES); - useCompletion(context, RULES); + useQuickPick(context, RULES); } export function deactivate() { } diff --git a/src/inserLog.ts b/src/insertLog.ts similarity index 51% rename from src/inserLog.ts rename to src/insertLog.ts index 48ae696..5ee7109 100644 --- a/src/inserLog.ts +++ b/src/insertLog.ts @@ -1,5 +1,6 @@ import axios from 'axios'; - +import {version ,env} from 'vscode'; +import {getCodeLanguage,getExtensionVersion} from './shared'; const http = axios.create({ baseURL: 'https://leancloud.cn:443/1.1/classes/', timeout: 1000, @@ -9,15 +10,19 @@ const http = axios.create({ "Content-Type": "application/json" } }); - -export default function ({ vscodeVersion, extensionVersion, language, rule, title, method }: Record) { +/** + * 插入日志 + */ +export default function ({ rule, title, method }: { rule: string, title: string, method: string }) { + const {language,machineId} = env; http.post('https://leancloud.cn:443/1.1/classes/Log', { - vscodeVersion, + vscodeVersion: version, + codeLanguage: getCodeLanguage(), language, + machineId, rule, title, - extensionVersion, + extensionVersion:getExtensionVersion(), method }); -}; - +}; \ No newline at end of file diff --git a/src/shared.ts b/src/shared.ts new file mode 100644 index 0000000..c6bdbb1 --- /dev/null +++ b/src/shared.ts @@ -0,0 +1,16 @@ +import { extensions, window} from 'vscode'; + +/** + * 获取编程语言 + */ +export function getCodeLanguage():string{ + return window.activeTextEditor ? window.activeTextEditor.document.languageId as string : ''; +}; + +/** + * 获取插件版本号 + */ +export function getExtensionVersion():string|void { + const e = extensions.getExtension('russell.any-rule'); + return e && e.packageJSON.version; +} \ No newline at end of file diff --git a/src/showResultMessage.ts b/src/showResultMessage.ts new file mode 100644 index 0000000..3d96e6b --- /dev/null +++ b/src/showResultMessage.ts @@ -0,0 +1,32 @@ +import { extensions, window, version, env, Uri, languages } from 'vscode'; +import { getCodeLanguage, getExtensionVersion } from './shared' +import { URL } from 'url'; + +const BUTTON_FEEDBACK = '🚀反馈问题'; +const BUTTON_DIAGRAMMATIZE = '🦕图解正则'; +const BUTTON_CANCEL = '关闭'; + +export default function (title: string): void { + // window.setStatusBarMessage(`已插入正则: "${title}", 点击查看更多🔥`) + window.showInformationMessage(`已插入正则: "${title}"`, BUTTON_FEEDBACK,BUTTON_CANCEL).then(value => { + if (BUTTON_FEEDBACK === value) { + const URL = Uri.parse(genGithubIssueURL(title)); + env.openExternal(URL); + } + }); +} +// gihub issue模板 +function genGithubIssueURL(title: string): string { + const BASE_URL = 'https://github.com/any86/any-rule/issues/new'; + const TITLE = `title=[vscode feedback] ${title}`; +// const BODY = `body=### vscode version +// ${version} +// ### extension version +// ${getExtensionVersion()} +// ### code language +// ${getCodeLanguage()} +// ### comment +// 请留言... +// `; + return BASE_URL + '?' + TITLE; +} diff --git a/src/useCommand.ts b/src/useCommand.ts index 3d93a2f..c370868 100644 --- a/src/useCommand.ts +++ b/src/useCommand.ts @@ -1,6 +1,7 @@ import {window,version,commands,Range,ExtensionContext,extensions} from "vscode"; import { Rule } from './interface'; -import inserLog from './inserLog'; +import insertLog from './insertLog'; +import showResultMessage from './showResultMessage'; export default function (context: ExtensionContext, RULES: Rule[]) { RULES.forEach(({ title, rule }, index) => { @@ -16,19 +17,15 @@ export default function (context: ExtensionContext, RULES: Rule[]) { editBuilder.replace(range, String(rule)); }); }); - + // 日志 - const language = window.activeTextEditor ? window.activeTextEditor.document.languageId as string : ''; - const e = extensions.getExtension('russell.any-rule') - inserLog({ - vscodeVersion: version, - language, + insertLog({ rule: String(rule), title, - extensionVersion: e && e.packageJSON.version, method: 'Command' }); - window.showInformationMessage(`已插入正则: ${title}`); + + showResultMessage(title); } else { window.showWarningMessage('any-rule: 只有在编辑文本的时候才可以使用!'); } diff --git a/src/useCompletion.ts b/src/useQuickPick.ts similarity index 75% rename from src/useCompletion.ts rename to src/useQuickPick.ts index 118784b..5cd2354 100644 --- a/src/useCompletion.ts +++ b/src/useQuickPick.ts @@ -1,8 +1,9 @@ -import { ExtensionContext, version, CompletionItemKind, languages, Disposable, Extension, window, commands, TextDocument, Position, Range, Selection, MarkdownString, extensions } from "vscode"; +import { ExtensionContext, version, CompletionItemKind, languages, env, Extension, window, commands, TextDocument, Position, Range, Selection, MarkdownString, extensions, Uri } from "vscode"; import { Rule } from './interface'; import { COMPLETION_TRIGGER_ID } from './constant'; // import { slugify } from 'transliteration'; -import inserLog from './inserLog'; +import insertLog from './insertLog'; +import showResultMessage from './showResultMessage'; export default function (context: ExtensionContext, RULES: Rule[]) { // commands.registerCommand('functions.insertRegex', insertRule); @@ -30,24 +31,13 @@ export default function (context: ExtensionContext, RULES: Rule[]) { if (!item) return insertRule(document, position, item.rule); - const language = window.activeTextEditor ? window.activeTextEditor.document.languageId as string : ''; - // 日志 - const e = extensions.getExtension('russell.any-rule') - inserLog({ - vscodeVersion: version, - language, + insertLog({ rule: item.rule, title: item.label, - extensionVersion: e && e.packageJSON.version, method: 'QuickPick' }); - - // https://github.com/any86/any-rule/issues/new?title=%E6%9D%A5%E8%87%AAvscode%E7%9A%84%E5%8F%8D%E9%A6%88(1.4.2%20-%20java) - window.showInformationMessage(`已插入正则: "${item.label}"`, '🦕图解正则', '🚀反馈问题', version, language).then(value => { - - // window.showInformationMessage(); - }); + showResultMessage(item.label); }); return void 0;