向WindowCommand类添加DYNAMIC参数

问题描述

问题

简而言之,下面的方法有效,但仅当这样进行硬编码时。

class GetWindowCommand(sublime_plugin.WindowCommand):

    #NEED VARIABLE INSTEAD OF THE HARDCODED "qv" string
    sublime.active_window().run_command("get_window",{"qualifier": "qv"})
    
    def __init__(self,window):
        self.window=window

    def run(self,qualifier):
        self.projectFolders(qualifier)

    def projectFolders(self,qualifier):
        print(qualifier)

我的目标是在加载插件时,它会读取项目文件夹并根据文件夹查找特定文件。因此,我需要访问外部变量AS以及WindowCommandClass

在进行print(WindowCommandClass)时,我注意到它会使用self,window变量填充所有方法,并且一切正常。

从理论上讲,我认为可以引入如下所示的变量

qualifier="qv"
print(WindowCommandClass.projectFolders(qualifier))

但是,向该类上的任何方法引入参数似乎会破坏WindowCommandClass的self和window参数。我只使用了python和sublime text api几天,所以我不知道是否缺少一些小东西或尝试不可能的事情。有什么想法吗?

解决方法

您的问题并未清楚问题所在。但是,请看一下这个示例以及我在其中所做的注释,也许会有所帮助。

class GetWindowCommand(sublime_plugin.WindowCommand):

    # The following call should not be anywhere in your plugin at all
    # unless for some reason you want to restart your plugin:
    # sublime.active_window().run_command("get_window",{"qualifier": "qv"})

    def run(self,qualifier=None):
        """ Called by Sublime Text when the plugin is run. """

        # self.window is set by Sublime - do NOT set it in a __init__() method.

        # The 'qualifier' variable will hold whatever the 'qualifier' arg
        # was when the plugin was launched,e.g. 'foo' in my example below,# or None if the plugin was started without the 'qualifier' arg set.

        project_data = self.window.project_data()
        if not project_data:
            return

        project_folders = project_data.get("folders",[])
        if not project_folders:
            print("No project folders have been set.")
            return

        for folder in project_folders:
            print(folder)

您可以通过在用户密钥文件中分配密钥绑定来启动插件:

{ "keys": ["ctrl+f0"],"command": "get_window","args": {"qualifier": "foo" } },
,

万一有人希望将值传递给Sublime Text 3插件中的类

给出一个开始的课程

class GetWindow(sublime_plugin.WindowCommand)

    def __init__(self,window):
        self.window=window

    def results(self)
        return("just testing")

我打错了电话

print(GetWindow().results())

我要做的就是为此类提供函数。

print(GetWindow(sublime_plugin.WindowCommand).results())

现在要获取原始帖子中提到的变量,我可以做到

qualifier='qv'
results=SublimeID(sublime.window,qualifier).results()

还修改类和方法以包含变量

def __init__(self,window,qualifier):
    self.qualifier=qualifier

def results(self)
    qualifer=self.qualifier
    # You can now work with this external variable #

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...