sublime text插件开发

打印 上一主题 下一主题

主题 567|帖子 567|积分 1701

手工开发了一些ST的py插件,记录过程中遇到的一些题目。
ST3/ST4 begin_edit题目

报错:
  1. begin_edit() missing 2 required positional arguments: 'edit_token' and 'cmd'
复制代码
ST3时已经不能直接调view.begin_edit方法了,需要通过runCommand+TextCommand转一手,写法如下:
  1. class ShowEnvVarsInternalCommand(sublime_plugin.TextCommand):
  2.     def run(self, edit, input):
  3.         self.view.set_read_only(False)
  4.         self.view.insert(edit, 0, input)
  5.         self.view.end_edit(edit)
  6.         self.view.set_read_only(True)
  7. class ShowEnvVarsCommand(sublime_plugin.WindowCommand):
  8.     def run(self):
  9.         self.var_names = list(os.environ.keys())
  10.         self.window.show_quick_panel(self.var_names, self.disp_var)
  11.     def disp_var(self, index):
  12.         if index != -1:
  13.             var = self.var_names[index]
  14.             str = '\n'.join(os.environ[var].split(';'))            
  15.             
  16.             output_view = self.window.create_output_panel("env_var")            
  17.             self.window.run_command("show_panel", {"panel": "output.env_var"})
  18.             # 这里在ST2里是直接调用output_view.begin_edit,然后对Edit对象做insert,这里改为调用output_view.run_command方法,把请求转给TextCommand,后者的run方法可以直接拿到Edit对象。
  19.             output_view.run_command('show_env_vars_internal', {"input": str})
  20.             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文档

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

乌市泽哥

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表