【问题标题】:How to get content of each active tab in column of Sublime Text 3?如何获取 Sublime Text 3 列中每个活动选项卡的内容?
【发布时间】:2015-08-20 15:58:27
【问题描述】:

我正在为 Sublime 编写一个插件,能够通过self.view 获取活动视图的内容。但是,如果我在不同的列中有两个打开的文件,如何通过SublimeText3 API 在每个窗口中获取活动选项卡的内容(或至少window.id)?是否应该通过sublime.Window类的views()方法来完成?

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        print (self.view.id())

-> 有效

class TestCommand(sublime_plugin.WindowCommand):
    def run(self, edit):
        print (self.window.views())

->当我运行view.run_command('test')时,它不起作用并且控制台中没有错误

非常感谢任何建议。

【问题讨论】:

    标签: python python-3.x sublimetext3 sublimetext sublime-text-plugin


    【解决方案1】:

    如果您查看链接的文档,self.window.views() 会返回一个视图列表。视图是对象,不能打印。试试这个:

    class TestCommand(sublime_plugin.WindowCommand):
        def run(self):
            print([view.id() for view in self.window.views()])
    

    这将打印窗口中每个视图的唯一 ID。您可以根据需要替换 sublime.View 的任何方法。

    要从 Sublime 的控制台运行 WindowCommands,请使用

    window.run_command("command_name")
    

    TextCommand 是通过访问view 对象来运行的:

    view.run_command("command_name")
    

    【讨论】:

    • 嗨,马特,它不起作用 :( 运行时同样没有输出 >>> view.run_command('test')
    • 我知道了,需要运行窗口而不是查看 >>> window.run_command('test') 并从运行参数中删除 self
    • @PaulSerikov 很高兴我能提供帮助。如果这个answer 解决了您的问题,请考虑accepting it,通过单击其左侧的复选标记/打勾,将其变为绿色。这标志着问题的解决令您满意,并向您和回答者奖励reputation
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    相关资源
    最近更新 更多