在您的 MyCommandCommand 类中,添加一个 is_enabled() 方法。来自ST2 API docs:
如果此时命令能够运行,则返回 true。默认实现总是简单地返回 True。
类似
def is_enabled(self, paths=[]):
self.has_files = False
for path in paths:
if os.path.isdir(path) == False:
self.has_files = True
if self.has_files:
break
return self.has_files
应该可以。 (警告:未经过充分测试!)
还有另一种选择,即依赖现有的SideBarEnhancements 安装,或借用sidebar/SideBarSelection.py 并将其包含在您的源代码中。这样,你可以调用任何一个
from SideBarEnhancements.sidebar.SideBarSelection import SideBarSelection
# if depending on an existing install
或
from .SideBarSelection import SideBarSelection
# if using the file in your own code - probably the best way to go
在您的 .py 文件的顶部。然后,在您的 MyCommandCommand 类中,使用以下内容:
def is_enabled(self, paths = []):
return SideBarSelection(paths).hasFiles()
你会准备好的。
我强烈建议您阅读SideBarEnhancements 的源代码,那里可能还有其他您可以使用的功能。
最后,请注意,Sublime Text 2 不再支持 SideBarEnhancements。如果您仍需要在 ST2 中运行它,请参阅 my answer here 解释为什么以及如何解决它。如果需要,还有一个链接可以下载与 ST2 兼容的源 zip 文件。越来越多的插件正在迁移到仅限 ST3 的版本,因为 API 中的显着增强使得在两个版本中维护相同的功能有时会变得非常痛苦。如果您编写的插件是供公众使用的,请在发布前确保它同时兼容ST2和ST3。
祝你好运!