【问题标题】:Sublime Text 3: Simple plug-in that changes color theme depending on remote hostSublime Text 3:根据远程主机改变颜色主题的简单插件
【发布时间】:2018-04-18 10:18:57
【问题描述】:

设置:我使用 Sublime Text 3 (ST),并且我经常使用 RemoteSubl 在不同的远程工作区中打开 2-3 个不同的 Sublime + iTerm2 会话。

使用简单的批处理脚本,我已将 iTerm2 设置为在我 ssh 进入不同的主机时更改颜色(通过激活不同的 iTerm 用户)。

我想知道 RemoteSubl 是否也可以这样做?这样当我从特定的主机/IP/端口打开某些东西时,Sublime 会以不同的配色方案打开,具体取决于主机/IP/端口。


解决方案尝试:到目前为止,这是我尝试构建一个在主机为remote_host 时更改配色方案的小插件。

import sublime
import sublime_plugin


class ExampleCommand(sublime_plugin.TextCommand):

    def run(self, view):

        try:

            host = view.settings().get('remote_subl.host')

            print(host)

            if host == 'remote_host':

                view.settings().set(
                    'color_scheme',
                    'Packages/Color Scheme - Default/Mariana.tmTheme')

                print(view.settings().get('color_scheme'))

        except:
            print("Not on remote_host")
            pass

问题:在控制台中使用 view.settings().get('remote_subl.host') 时,它工作正常,并返回 remote_host。但是,当运行脚本view.run_command('example') 时,我得到“不在 remote_host”打印,表明 try 循环由于某种原因而失败。


根据 Keiths 的建议

import sublime
import sublime_plugin


class ExampleCommand(sublime_plugin.TextCommand):

    def run(self, view):
        view = self.view
        host = view.settings().get('remote_subl.host', None)

        print(host)

        if host:

            view.settings().set(
                'color_scheme',
                'Packages/Color Scheme - Default/Mariana.tmTheme')

            print(view.settings().get('color_scheme'))

        if host is None:

            view.settings().set(
                'color_scheme',
                'Packages/Color Scheme - Default/Monokai.tmTheme')

            print(view.settings().get('color_scheme'))

【问题讨论】:

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


    【解决方案1】:

    view 不是传递给TextCommandrun 方法的参数。相反,它是self 上的一个属性。将其更改为以下内容应该可以:

    import sublime
    import sublime_plugin
    
    
    class ExampleCommand(sublime_plugin.TextCommand):
    
        def run(self, edit):
            view = self.view
    
            try:
    
                host = view.settings().get('remote_subl.host')
    
                print(host)
    
                if host == 'dsintmain':
    
                    view.settings().set(
                        'color_scheme',
                        'Packages/Color Scheme - Default/Mariana.tmTheme')
    
                    print(view.settings().get('color_scheme'))
    
            except:
                print("Not on remote_host")
                pass
    

    我还建议打印发生的异常以帮助将来调试此类事情。更好的是,与其期望在正常使用中出现异常,不如为settings 上的get 方法提供一个默认值(即None)并完全删除异常处理。

    host = view.settings().get('remote_subl.host', None)
    

    这样,如果确实出现问题,您将在 ST 控制台中看到回溯。

    【讨论】:

    • 太棒了。您能否指出我将如何在打开远程工作区时自动运行我的小插件的文档?
    • 您可以在EventListener 类中使用类似on_load_async 的东西来检测文件何时加载并检查是否设置了设置。一个例子是 this gist 基于您的原始插件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    相关资源
    最近更新 更多