gpt4 book ai didi

python - 代码段无法在 ST3 中静默插入

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:40 27 4
gpt4 key购买 nike

我一直在关注this , this , this , thisthis在 ST3 中创建我自己的模板。因此,当我创建一个新文件时,我会使用模板内容预加载该文件。我读过我应该用一个片段来做到这一点。

我创建了一个文件,并尝试在新的 view 中应用一个 snippet,但是没有任何反应,我没有得到任何反馈(即使我输入错误也没有片段名称)。我的代码片段在我的插件文件夹中:

C:\Users\...\AppData\Roaming\Sublime Text 3\Packages\MyPlugin\templ.sublime-snippet

我的多次尝试如下:

def on_done(...):
...
open(path, 'a').close()
new_view = self.window.open_file(path)

# These seem to work, so the view seems valid
print(new_view.file_name())
print(new_view.line_height())

# Attempt 1
template = """<snippet>
<content><![CDATA[
Hello, \${1:this} is a \${2:snippet}.
]]></content>
</snippet>
"""
new_view.run_command("insert_snippet", {"contents": template})

# Attempt 2: from the user folder. Nothing happens
new_view.run_command("insert_snippet", { "name": "Packages/User/templ.sublime-snippet" })

# Attempt 3: from current (?) folder just in case
new_view.run_command("insert_snippet", {"name" : "templ.sublime-snippet"})

# Attempt 4, from the actual plugin folder
new_view.run_command("insert_snippet", {"name" : "Packages/MyPlugin/templ.sublime-snippet"})

# Attempt 5, absolute directory
new_view.run_command("insert_snippet", {"name" : "C:\\Users\\nerea\\AppData\\Roaming\\Sublime Text 3\\Packages\\MyPlugin\\templ.sublime-snippet"})

# Attempt 6, to check if I mistook the path if I would get any error
new_view.run_command("insert_snippet", {"name" : "Packages/User/nonexisting.sublime-snippet"})

# Attempt 7, nothing either
new_view.run_command("insert_snippet", {"contents" : "<snippet><content><![CDATA[Hello, ${1:this} is a ${2:snippet}.]]></content></snippet>"})

# This does nothing either. I supposed that should do something
new_view.run_command("insert", "wawawiwa")

最佳答案

问题是当您执行insert_snippet 命令时, View 还没有完成加载您指定的文件。我建议试试这个:

    new_view = self.window.open_file(path)
def do_insert():
if not new_view.is_loading():
new_view.run_command("insert_snippet", { "name": "Packages/MyPlugin/templ.sublime-snippet" })
sublime.set_timeout_async(do_insert, 10)
do_insert()

如果您使用 contents 参数调用 insert_snippet,API 只需要片段内容 - 即 Hello, ${1:this} 是一个 $ {2:snippet}。 而不是周围的 XML。

请注意,在代码段内容中的美元前面有一个反斜杠将导致 ST 按字面意思处理美元,而不是描绘制表符/编辑点。

关于python - 代码段无法在 ST3 中静默插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55674928/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com