sublime text插件开发

手工开发了一些ST的py插件,记录过程中遇到的一些问题。

ST3/ST4 begin_edit问题

报错:

复制代码
begin_edit() missing 2 required positional arguments: 'edit_token' and 'cmd'

ST3时已经不能直接调view.begin_edit方法了,需要通过runCommand+TextCommand转一手,写法如下:

python 复制代码
class ShowEnvVarsInternalCommand(sublime_plugin.TextCommand):
    def run(self, edit, input):
        self.view.set_read_only(False)
        self.view.insert(edit, 0, input)
        self.view.end_edit(edit)
        self.view.set_read_only(True)


class ShowEnvVarsCommand(sublime_plugin.WindowCommand):
    def run(self):
        self.var_names = list(os.environ.keys())
        self.window.show_quick_panel(self.var_names, self.disp_var)

    def disp_var(self, index):
        if index != -1:
            var = self.var_names[index]
            str = '\n'.join(os.environ[var].split(';'))             
            
            output_view = self.window.create_output_panel("env_var")            
            self.window.run_command("show_panel", {"panel": "output.env_var"})
            # 这里在ST2里是直接调用output_view.begin_edit,然后对Edit对象做insert,这里改为调用output_view.run_command方法,把请求转给TextCommand,后者的run方法可以直接拿到Edit对象。
            output_view.run_command('show_env_vars_internal', {"input": str})
            self.window.focus_view(output_view)

sublime.set_timeout的用途

用于在工作线程里访问ST的界面,因只有主线程才能刷新ST界面,set_timeout相当于"工作线程向主线程发消息"。

exec命令

通过查看exec.py源码,得知:

exec是异步的,也就是说,ST主线程不会等待exec完成才返回,相反,它立刻返回,而exec进程的输出被异步(通过起线程的方式)的追加到output.exec窗口里。因此,当你run_command后立刻从output.exec里拿结果,只会得到空。

为何ST主线程不会等待呢?因为这是一个GUI程序,一旦主线程等待,界面就会僵死。

ST插件API说明

ST4的API文档

相关推荐
冬奇Lab14 小时前
【Cursor进阶实战·06】MCP生态:让AI突破编辑器边界
人工智能·编辑器·ai编程
环黄金线HHJX.15 小时前
【MCP: Tuan编程 + Qt架构 + QoS - 量子-经典混合计算管理控制平台】
ide·人工智能·qt·编辑器·量子计算
我送炭你添花16 小时前
Pelco KBD300A 模拟器:07+1.宏脚本编辑器与模板库管理实现细节
python·自动化·编辑器·运维开发
智源研究院官方账号1 天前
众智FlagOS 1.6发布,以统一架构推动AI硬件、软件技术生态创新发展
数据库·人工智能·算法·架构·编辑器·硬件工程·开源软件
咬人喵喵2 天前
SVG 答题类互动模板汇总(共 16 种/来自 E2 编辑器)
编辑器·svg·e2 编辑器
漫步星河2 天前
unityEditor Note 编辑器笔记本
编辑器
咬人喵喵2 天前
16 类春节核心 SVG 交互方案拆解(E2 编辑器实战)
前端·css·编辑器·交互·svg
lixzest2 天前
Vim 快捷键速查表
linux·编辑器·vim
环黄金线HHJX.2 天前
MCP: Tuan编程 + Qt架构 + QoS - 量子-经典混合计算管理控制平台
开发语言·qt·算法·编辑器·量子计算
全干工程师—3 天前
在liunx下使用VScode检测到 #include 错误。请更新 includePath问题的解决方法
ide·vscode·编辑器