Is it possible to have a custom context menu items for an output panel.
It is not a problem for an ordinary view but I see no way of achieving it for the output panel.
Thank you
Context menu for output panel
yes, this is possible:
import sublime
import sublime_plugin
class ExampleOutputPanelCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World!")
def is_visible(self):
panel_name = self.view.window().active_panel()
if panel_name and panel_name.startswith('output.'):
panel = self.view.window().find_output_panel(panel_name[len('output.'):])
return panel is not None and panel.id() == self.view.id()
else:
return False
and then in Context.sublime-menu
:
[
{ "command": "example_output_panel", "caption": "Example output panel command" },
]
Thank you, but unfortunately this approach doesn’t work. You code assumes that is_visible is called for all views including output panels. But it’s not the case. At least with ST3. is_visible
is never called for output panel.
Even simplifying your code like this doesn’t help:
class ExampleOutputPanelCommand(sublime_plugin.TextCommand):
def is_visible(self):
print('is_visible is called')
return True
"Example output panel command"
indeed displayed in the ordinary view. But for the output panel the editor always displays the built in context menu:
Sorry, didn’t get the notification about your response.
Yes you are right. You are using built-in panel output.exec
and indeed this panel does work with the custom context menu.
However if one creates a custom output panel then ST3 does’t process context menu at all. The code below shows that is_visible
is only called for output.exec
but not for any other panel (e.g. console
, output.my_panel
).
I was hoping that I have missed something but now it looks like a defect or an intended behavior:
class ExampleOutputPanelCommand(sublime_plugin.TextCommand):
def run(self, edit):
window = sublime.active_window()
panel = window.create_output_panel('my_panel')
panel.run_command("append", {"characters": "Hello World"})
window.run_command('show_panel', {'panel':'output.my_panel'})
def is_visible(self):
print(self.view.window().active_panel())
return True
ah I see - apparently you need to assign a syntax first, then the normal context menu items will appear:
class ExampleOutputPanelCommand(sublime_plugin.TextCommand):
def run(self, edit):
window = sublime.active_window()
panel = window.create_output_panel('my_panel')
panel.run_command("append", {"characters": "Hello World"})
panel.assign_syntax('Packages/Text/Plain text.tmLanguage')
window.run_command('show_panel', {'panel':'output.my_panel'})
def is_visible(self):
print(self.view.window().active_panel())
return True
Excellen! Txs.
Who would expect that? But… may be it is a common ST way that I just didn’t know about.
Much appreciated…
.