向 WindowCommand 添加动态参数 class
Adding DYNAMIC arguments to WindowCommand class
问题
简而言之,以下内容有效,但仅当像这样进行硬编码时才有效。
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)
我的目标是加载插件时,它会读取项目文件夹并根据文件夹查找特定文件。因此,我需要访问外部变量以及 WindowCommandClass
在执行 print(WindowCommandClass)
时,我注意到它用自身填充所有方法,window 变量并且一切正常。
理论上我认为我可以引入如下所示的变量
qualifier="qv"
print(WindowCommandClass.projectFolders(qualifier))
但是,向 class 上的任何方法引入参数似乎会破坏 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
给定一个 class 开始
class GetWindow(sublime_plugin.WindowCommand)
def __init__(self,window):
self.window=window
def results(self)
return("just testing")
我错误地调用了
print(GetWindow().results())
我要做的就是像这样用 class 提供函数。
print(GetWindow(sublime_plugin.WindowCommand).results())
现在要获取原始post中提到的变量,我可以做到这一点
qualifier='qv'
results=SublimeID(sublime.window,qualifier).results()
同时修改 class & 方法以包含变量
def __init__(self,window,qualifier):
self.qualifier=qualifier
def results(self)
qualifer=self.qualifier
# You can now work with this external variable #
问题
简而言之,以下内容有效,但仅当像这样进行硬编码时才有效。
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)
我的目标是加载插件时,它会读取项目文件夹并根据文件夹查找特定文件。因此,我需要访问外部变量以及 WindowCommandClass
在执行 print(WindowCommandClass)
时,我注意到它用自身填充所有方法,window 变量并且一切正常。
理论上我认为我可以引入如下所示的变量
qualifier="qv"
print(WindowCommandClass.projectFolders(qualifier))
但是,向 class 上的任何方法引入参数似乎会破坏 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
给定一个 class 开始
class GetWindow(sublime_plugin.WindowCommand)
def __init__(self,window):
self.window=window
def results(self)
return("just testing")
我错误地调用了
print(GetWindow().results())
我要做的就是像这样用 class 提供函数。
print(GetWindow(sublime_plugin.WindowCommand).results())
现在要获取原始post中提到的变量,我可以做到这一点
qualifier='qv'
results=SublimeID(sublime.window,qualifier).results()
同时修改 class & 方法以包含变量
def __init__(self,window,qualifier):
self.qualifier=qualifier
def results(self)
qualifer=self.qualifier
# You can now work with this external variable #