Reputation: 21496
I have a Sublime Plugin TextCommand
, which, among other things, sets the view syntax file. I didn't realize this previously, but it seems that the console input is actually a view, so if you have it selected when you run a TextCommand
, it executes on the console input field. How can I prevent my command from running on it?
Here's an example plugin:
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')
If I bring up the console, then click on the input field, and activate the TextCommand using a keyboard shortcut, then the input field of the console is changed to match the Python theme.
To be clear, I don't want this TextCommand to run on the input field of the console at all. How can I detect if that's where my command is running and abort it if that's the case?
Upvotes: 1
Views: 108
Reputation: 21496
Alright, I figured out a solution to this.
Instead of using self.view
, use self.view.window().active_view()
. This will cause your code to run on whichever normal view was most recently active within the currently selected window, which is probably a good assumption of what the user meant to do.
One unusual case you may want to handle: the user could have a window with no open buffers, which does have the console open. It'll be possible to run TextCommand
s, but self.view.window().active_view()
will return None
in that case. You may want to check for and handle that case.
If you'd rather take the safer route and do nothing when the user runs the command while having the cursor in the console input view, you could do something like:
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...
Upvotes: 1