【问题标题】:Side bar menu in sublime text崇高文本中的侧栏菜单
【发布时间】:2014-03-27 10:34:03
【问题描述】:

如何为文件或文件夹添加侧边栏菜单(当我右键单击文件时)?例如,如果我将此代码添加到 "Side Bar.sublime-menu":

{ "caption": "New command", "command": "my_command", "args": {"files": []} }

我将为侧边栏中的所有文件和文件夹获得新选项。

如何仅为文件添加此选项?

【问题讨论】:

    标签: sublimetext2 sublimetext sublime-text-plugin


    【解决方案1】:

    在您的 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。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 2015-04-27
      相关资源
      最近更新 更多