我是如何在代码里做表情包的

前几天,我一边在 Reddit 上做梗图,一边抽空写代码。突然灵光一闪:为什么不能把这两件事合二为一呢?在代码里做表情包完全可行——只要在注释里放 ASCII 字符画表情包就行了。不过手动操作实在太费时间了:
-
找到合适的表情包模板
-
把它下载下来
-
转换成 ASCII 字符画格式
-
粘贴到 IDE 里
-
加上注释符号
试了几次之后,我决定自己写一个 VS Code 插件来加速整个流程,把上面所有步骤浓缩成一步:按下键盘快捷键。
插件地址:https://marketplace.visualstudio.com/items?itemName=llldar.vsc-memer
GitHub 地址:https://github.com/llldar/vsc-memer
起步
要开发一个 VS Code 插件,首先需要 yo 和 generator-code,通过以下命令安装:
npm install -g yo generator-code
安装完成后,运行:
yo code
命令行会以交互方式依次提示你填写若干选项,照着一步步选就行。
走完这些步骤后,你就得到了一个可运行的 hello-world VS Code 插件模板。
具体实现
我计划为这个插件支持 4 个命令:
-
insertRandomMeme
-
copyRandomMeme
-
insertMeme
-
copyMeme
为了实现这些功能,需要大量用到 VS Code 的插件 API。
要在编辑器中插入文本,首先需要获取当前的选区位置,然后把内容插入到对应位置:
editor?.selections.forEach((v) =>
edit.replace(v, comment(getRandomMeme())) );
有一点需要注意:VS Code 在插入文本后会默认保持选中刚插入的内容,因此需要手动清空选区来取消选中状态:
editor.selection = new vscode.Selection(
editor.selection.end,
editor.selection.end );
将文本复制到剪贴板非常简单,直接调用 VS Code 的内置 API 即可,复制完别忘了给用户弹出一条提示:
vscode.env.clipboard.writeText(comment(getRandomMeme()));
vscode.window.showInformationMessage('Random meme copied to clipboard');
插入随机表情包很简单,但如果想让用户自己挑选要插入哪个表情包,就需要接收用户输入。好在 VS Code 提供了 QuickPick API,可以让用户从列表中进行选择:
const options = Object.keys(memes).map((label) => ({
label
}
));
const quickPick = vscode.window.createQuickPick();
quickPick.items = options;
当用户触发命令时,展示 QuickPick 列表:
quickPick.show();
在回调函数中接收用户的选择,选中后隐藏列表:
quickPick.onDidChangeSelection(([{
label
}
]) => {
vscode.env.clipboard.writeText(
comment(memes[label as keyof typeof memes])
);
vscode.window.showInformationMessage('Random meme copied to clipboard');
quickPick.hide();
}
);
还有一件事需要考虑:表情包必须包裹在注释里,而不同编程语言的注释语法各不相同。我们需要在 package.json 里添加配置项,允许用户自行定制注释风格:
"configuration": [
{
"title": "Meme maker",
"properties": {
"vsc-memer.commentStyle": {
"type": "string",
"default": "line",
"enum": [
"line",
"block"
],
"enumDescriptions": [
"line comment style, like //",
"block comment style, like /* */"
],
"description": "Type of comment style"
},
"vsc-memer.lineComment": {
"type": "string",
"default": "//",
"description": "Symbol of line comments the generated meme should be wrapped in"
},
"vsc-memer.blockCommentLeft": {
"type": "string",
"default": "/*",
"description": "Symbol of left side of block comments the generated meme should be wrapped in"
},
"vsc-memer.blockCommentRight": {
"type": "string",
"default": "*/",
"description": "Symbol of right side of block comments the generated meme should be wrapped in"
}
}
}
],
注意这些配置必须放在 contributes 字段下面,否则不会生效。
加上注释符号本身并不难,但还有一个细节要处理:Windows 和 macOS/Linux 的换行符不同,而且 VS Code 本身还可以覆盖系统默认设置,这就让事情变得有点复杂了。
首先我们需要读取 VS Code 的 files.eol 配置,如果它不是 auto,就直接采用该配置指定的换行符。
如果它是 auto,则通过 Node.js 的 os.EOL API 获取当前操作系统的默认换行符。
所有功能都实现完毕后,别忘了把这几个命令注册到插件上下文中:
context.subscriptions.push(insertRandomMeme);
context.subscriptions.push(copyRandomMeme);
context.subscriptions.push(insertMeme);
context.subscriptions.push(copyMeme);
调试
调试非常直观,直接按 F5,就能在当前宿主 VS Code 窗口中启动一个子窗口来进行插件调试。
发布
代码写完后,需要用另一个工具 vsce 来发布插件,先全局安装它:
npm i -g vsce
此外,你还需要一个 Azure DevOps 账号,并在 VS Code 插件市场中创建一个发布者(Publisher)。
同时还需要在 Azure DevOps 中生成一个个人访问令牌(Personal Access Token),并为其授予管理 VS Code Marketplace 以及所有可访问组织的权限。
更多细节可以参考官方文档。
拿到个人访问令牌之后,运行以下命令登录:
vsce login <your_publisher_name>
接着就可以直接发布了:
vsce publish
推广
好酒也怕巷子深,优秀的插件同样需要推广。既然这是一个做表情包的插件,我决定主要在 Reddit 上用梗图来宣传它。如果你有自己的 Discord 服务器、Twitter 粉丝群或者 YouTube 频道,也都可以拿来推广。
分享文章
互动