![](/img/trans.png)
[英]Sublime Plugin: Share object between TextCommand and EventListener?
[英]Prevent Sublime Plugin TextCommand from running on console?
我有一個Sublime插件TextCommand
,它可以設置視圖語法文件。 我以前沒有意識到這一點,但似乎控制台輸入實際上是一個視圖,因此,如果在運行TextCommand
時選擇了TextCommand
,它將在控制台輸入字段上執行。 如何防止我的命令在其上運行?
這是一個示例插件:
from sublime_plugin import TextCommand
class ExampleCommand(TextCommand):
def run(self, edit):
if 'Python' not in self.view.settings().get('syntax'):
self.view.set_syntax_file('Packages/Python/Python.tmLanguage')
如果調出控制台,請單擊輸入字段,然后使用鍵盤快捷鍵激活TextCommand,然后控制台的輸入字段將更改為與Python主題匹配。
明確地說,我根本不希望此TextCommand在控制台的輸入字段上運行。 如何檢測我的命令是否正在運行,並在這種情況下中止運行?
好吧,我想出了一個解決方案。
代替使用self.view
,請使用self.view.window().active_view()
。 這將使您的代碼在當前所選窗口中最近處於活動狀態的普通視圖上運行,這可能是對用戶意圖的一個很好的假設。
您可能想處理一種不常見的情況:用戶可能有一個沒有打開緩沖區的窗口,但確實打開了控制台。 可以運行TextCommand
,但是在這種情況下, TextCommand
self.view.window().active_view()
將返回None
。 您可能要檢查並處理這種情況。
如果您寧願走更安全的路線,而在光標進入控制台輸入視圖時用戶運行命令時不執行任何操作,則可以執行以下操作:
from sublime_plugin import TextCommand
class ExampleCommand(TextCommand):
def run(self, edit):
# If the user attempted to run the command while having a non-activatable
# view selected, such as the console input view, don't run anything.
if self.view.id() != self.view.window().active_view().id():
return
# Otherwise continue running the command as normal...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.