如何添加側欄菜單(當我右鍵單擊文件時)僅用於文件或僅用於文件夾?例如,如果我這個代碼添加到"Side Bar.sublime-menu"
:崇高文本中的側欄菜單
{ "caption": "New command", "command": "my_command", "args": {"files": []} }
我會在側欄中的所有文件和文件夾新的選擇。
我如何才能爲文件添加此選項?
如何添加側欄菜單(當我右鍵單擊文件時)僅用於文件或僅用於文件夾?例如,如果我這個代碼添加到"Side Bar.sublime-menu"
:崇高文本中的側欄菜單
{ "caption": "New command", "command": "my_command", "args": {"files": []} }
我會在側欄中的所有文件和文件夾新的選擇。
我如何才能爲文件添加此選項?
在您的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
的來源閱讀,那裏可能還有其他功能可以使用。
最後,請注意,SideBarEnhancements
不再被支持的崇高文字2.請參閱my answer here解釋爲何以及如何解決它,如果你仍然需要在ST2運行它。如果需要,還有一個鏈接可以下載源代碼的ST2兼容zip文件。越來越多的插件正在遷移到僅限ST3的版本,因爲API中的重要增強功能使得兩個版本中的相同功能有時會變得非常痛苦。如果您正在編寫的插件是供公衆使用的,請在釋放它之前確保它與ST2和ST3兼容。
祝你好運!