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
这些变化导致它的行为发生了什么变化?
最佳答案
在 Sublime Text 中,命令可以定义为在 Application
级别 ( ApplicationCommand
)、Window
级别 ( WindowCommand
) 或 级别进行操作>查看
级别(TextCommand
)。
通常只有 TextCommand
修改缓冲区或仅影响当前缓冲区的设置,WindowCommand
当前窗口布局或其他相关设置,以及 ApplicationCommand
的全局偏好等。
根据我的经验,在 View
对象上执行 WindowCommand
不会执行任何操作。示例:
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' })
因此,答案是,根据命令的类型以及您要执行命令的View
是否处于事件状态,可能会有所不同。
就删除的导入
而言,没有任何影响,因为这些导入未在插件中使用。
关于sublimetext3 - "view.window().run_command"和 "view.run_command"之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51042670/