質問

How can I add side bar menu(when I right click on file) only for files or only for folders? For example if I add this code to "Side Bar.sublime-menu":

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

I will get new option for all files and folders in sidebar.

How I can add this option only for files?

役に立ちましたか?

解決

In your MyCommandCommand class, add an is_enabled() method. From the ST2 API docs:

Returns true if the command is able to be run at this time. The default implementation simply always returns True.

Something like

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

should work. (warning: not well-tested!)

There is another option, and that is to either rely on an existing install of SideBarEnhancements, or to borrow sidebar/SideBarSelection.py and include it in your source. This way, you can just call either

from SideBarEnhancements.sidebar.SideBarSelection import SideBarSelection
# if depending on an existing install

or

from .SideBarSelection import SideBarSelection
# if using the file in your own code - probably the best way to go

at the top of your .py file. Then, in your MyCommandCommand class, use the following:

def is_enabled(self, paths = []):
    return SideBarSelection(paths).hasFiles()

and you'll be all set.

I highly recommend reading through the source of SideBarEnhancements, there may be other features there that you can use as well.

Finally, please be aware that SideBarEnhancements is no longer being supported for Sublime Text 2. Please see my answer here explaining why and how to work around it if you still need to run it in ST2. There's also a link to download an ST2-compatible zip file of the source, if needed. More and more plugins are migrating to ST3-only versions, as there are significant enhancements in the API that make maintaining identical features in both versions a real pain sometimes. If the plugin you are writing is for public use, please make sure it is compatible with both ST2 and ST3 before releasing it.

Good luck!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top