功能
用户可以通过自定义按钮的方式来粘贴一些自定义代码片段,
从而帮助记忆一些非常用的代码API。
使用方法
使用前需要配置两个变量
Custom HTML Path
: 指向一个本地html文件,用于展示按钮
Custom Map Path
:指向一个本地json文件,其中配置了按钮的snippets.
由于json文件并不支持多行字符串,所以推荐使用脚本工具生成这两个文件;例如下面的Python脚本
"""将文件中的代码片段生成一个json文件和html文件,为VS Code的button snippets插件服务
"""
import json
class Button:
def __init__(self, name, id, filename):
self.name = name
self.id = id
self.snippets = self.__read__(filename)
def __str__(self):
return f'<button id="{self.id}">{self.name}</button>\n'
def __read__(self, filename):
with open(filename, encoding='utf-8', mode='r') as f:
return f.read()
# 在这里配置按钮的名字、id和文件地址,文件内为点击按钮要粘贴的代码
buttons = [
Button('按钮一', 'btn1', './snippets/按钮一.txt'),
Button('按钮二', 'btn2', './snippets/按钮二.txt'),
Button('按钮三', 'btn3', './snippets/按钮三.txt')
]
header = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Webview Demo</title>
<link rel="stylesheet" href="vscode.css">
</head>
<body>"""
footer = """
<script src="index.js"></script>
</body>
</html>
"""
html = header
for b in buttons:
html += str(b)
html += footer
with open('./index.html', encoding='utf-8', mode='w') as f:
f.write(html)
print('./index.html 已写出')
snippets = {"buttons":[]}
for b in buttons:
snippets["buttons"].append({
"name": b.name,
"id": b.id,
"snippets": b.snippets
})
with open('./button-snippets.map.json', encoding='utf-8', mode='w') as f:
json.dump(snippets, f, ensure_ascii=False, indent=4)
print('./button-snippets.map.json 已写出')
print('done')
非正式发布版本
If you have any suggestions, please send an email to litchi.code@foxmail.com.