代码之家  ›  专栏  ›  技术社区  ›  john c. j.

“view.window().run_command”与“view.run_command”的区别

  •  1
  • john c. j.  · 技术社区  · 6 年前

    两者之间的实际区别是什么 view.window().run_command(...) view.run_command(...) 是吗?

    下面是同一个插件的两个版本,有两个小改动:

    (保存时它会将制表符转换为空格。你需要 "expand_tabs_on_save": true 在首选项中)。

    一:

    # https://coderwall.com/p/zvyg7a/convert-tabs-to-spaces-on-file-save
    import sublime, sublime_plugin, os
    
    class ExpandTabsOnSave(sublime_plugin.EventListener):
      def on_pre_save(self, view):
        if view.settings().get('expand_tabs_on_save') == 1:
          view.window().run_command('expand_tabs')
    

    二:

    # https://github.com/bubenkoff/ExpandTabsOnSave-SublimeText
    import sublime_plugin # <---------- `sublime` and `os` removed
    
    class ExpandTabsOnSave(sublime_plugin.EventListener):
      def on_pre_save(self, view):
        if view.settings().get('expand_tabs_on_save') == 1:
          view.run_command('expand_tabs') # <--------- `window()` removed
    

    随着这些变化,它的行为发生了什么变化?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Keith Hall    6 年前

    在Sublime文本中,命令可以定义为在 Application 水平( ApplicationCommand ),一个 Window 水平( WindowCommand ),或 View 水平( TextCommand )中。

    通常只有 文本命令 s修改缓冲区,或只影响当前缓冲区的设置, 窗口命令 当前窗口布局或其他相关设置,以及 应用程序命令 全球偏好等。

    以我的经验,执行 窗口命令 在一个 查看 对象什么也不做。例子:

    view.run_command('set_layout', {"cells": [[0, 0, 1, 1], [1, 0, 2, 1]], "cols": [0.0, 0.5, 1.0], "rows": [0.0, 1.0]})
    

    But executing a TextCommand on a Window object implicitly targets that window's currently active view. 当从st控制台执行时,它将影响st控制台的输入文本区域。

    window.run_command('insert', { 'characters': 'testing123' })
    

    所以,答案是,根据命令的类型以及 查看 您想在上执行的命令是否为活动命令。


    import 删除后,由于插件中未使用这些导入,因此没有任何效果。