将sublime中的自定义代码片段snippet 转为vscode可用的代码片段 (cursor可用)

最近接触了ai编辑器cursor 想用起来 所以有此需求 ai编辑器cursor就是用vscode封的 所以和vscode通用

Sublime Text 和 VS Code 都是常用的代码编辑器之一。在 Sublime Text 中,你可以使用代码片段来快速插入常用代码块。而在 VS Code 中,你可以使用智能提示来自动补全代码片段,提高编码效率。

如果你在从 Sublime Text 转换到 VS Code,你可能会想把 Sublime Text 的代码片段迁移到 VS Code 中。为了方便起见,你无需手动创建代码片段,可以使用一个 Python 脚本来自动完成这项工作。

下面是 Python 脚本的主要步骤:

遍历 Sublime Text 的代码片段目录,解析每个代码片段文件的内容。

将每个代码片段的名称、内容和描述转换为 VS Code 所需的格式。

将所有代码片段按照 VS Code 的格式合并成一个 JSON 字符串。

将 JSON 字符串写入到 VS Code 的代码片段目录中的 all_snippets.code-snippets 文件。

这个脚本可以让你快速将 Sublime Text 的代码片段在 VS Code 中使用,并且自动生成代码片段文件,让你不用手动一个一个创建,节省了时间和精力。

import os
import re
import xmltodict
import json

sublime_snippets_dir = "/Users/**/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/snippet"
vscode_snippets_dir = "/Users/**/Library/Application Support/Cursor/User/snippets"

# 如果存在目标文件则删除他
if os.path.exists(os.path.join(vscode_snippets_dir, "all_snippets.code-snippets")):
    os.remove(os.path.join(vscode_snippets_dir, "all_snippets.code-snippets"))

snippets_data = {}
for root, dirs, files in os.walk(sublime_snippets_dir):
    for filename in files:
        if not filename.endswith(".sublime-snippet"):
            continue
        with open(os.path.join(root, filename), "r") as f:
            snippet_content = f.read()
        snippet_dict = xmltodict.parse(
            snippet_content, process_namespaces=True)
        name = snippet_dict["snippet"]["tabTrigger"]
        content = snippet_dict["snippet"]["content"].strip()
        body = [line.strip() for line in content.splitlines() if line.strip()]
        body = [re.sub(r'"', "'", line) for line in body]
        description = "no description yet."
        snippets_data[name] = {
            "prefix": name,
            "body": body,
            "description": description
        }

# 将 snippets_data 转化成 JSON 格式字符串
snippets_str = json.dumps(snippets_data, ensure_ascii=False, indent=4)
# 将双引号替换为单引号
# snippets_str = snippets_str.replace('"', "'")
# 将 \$ 转为 $
snippets_str = snippets_str.replace('\\\$', '$')

# 写入文件
with open(os.path.join(vscode_snippets_dir, "all_snippets.code-snippets"), "w") as f:
    f.write(snippets_str)


print("完成!")

请注意,你需要将 sublime_snippets_dir 和 vscode_snippets_dir 替换为实际的 Sublime Text 和 VS Code 代码片段目录的路径。此外,你还需要使用 xmltodict 库来解析 Sublime Text 代码片段文件。

总的来说,这个脚本非常实用,如果你需要在 Sublime Text 和 VS Code 之间切换,并且希望能够快速迁移代码片段,你可以试试这个脚本。

我自定义的代码块已经上传gitee了 有兴趣可以star下

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值