Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output in build panel + Sublime build execution #54

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Hermes - Excecute Block.sublime-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"selector": "source.python",
"target": "hermes_execute_block_build"
}
4 changes: 4 additions & 0 deletions Hermes - Excecute Cell.sublime-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"selector": "source.python",
"target": "hermes_execute_cell_build"
}
2 changes: 1 addition & 1 deletion Hermes.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// # <codecell>
"complete": true, // Whether use Hermes' autocomplete powered by Jupyter kernel.
"complete_timeout": 0.5, // Timeout to get completion (in seconds).
"inline_output": false, // Set to true to show output in current view, like Jupyter
"output_style": "default", // Set to 'inline' to show output in current view, like Jupyter, set to 'panel' to show output in Sublime's output panel
"ssh_servers": {
// List ssh servers.
// "server_name_shown_in_menu": { // You can use arbitary name here.
Expand Down
27 changes: 22 additions & 5 deletions hermes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from sublime_plugin import (
TextCommand,
EventListener,
ViewEventListener
ViewEventListener,
WindowCommand
)
from .kernel import KernelConnection

Expand Down Expand Up @@ -93,11 +94,11 @@ def connect_kernel(cls, buffer_id, lang, kernel_id):
"""Connect view to kernel."""
kernel = HermesKernelManager.get_kernel(kernel_id)
cls.view_kernel_table[buffer_id] = kernel
inline_output = (
output_style = (
sublime
.load_settings("Hermes.sublime-settings")
.get("inline_output"))
if not inline_output:
.get("output_style"))
if output_style == "default":
kernel.activate_view()

@classmethod
Expand Down Expand Up @@ -786,8 +787,16 @@ def _execute_cell(view, region: sublime.Region, *, logger=HERMES_LOGGER):
logger.info(log_info_msg)


class HermesExecuteBlockBuild(WindowCommand):
"""Execute code block as build."""
def run(self, *, logger=HERMES_LOGGER):
view = sublime.active_window().active_view()
text_command = HermesExecuteBlock(view)
text_command.run(edit=None, logger=logger)


class HermesExecuteBlock(TextCommand):
"""Execute code."""
"""Execute code block."""

def is_enabled(self, *, logger=HERMES_LOGGER):
try:
Expand All @@ -804,6 +813,14 @@ def run(self, edit, *, logger=HERMES_LOGGER):
_execute_block(self.view, logger=logger)


class HermesExecuteCellBuild(WindowCommand):
"""Execute code cell as build."""
def run(self, *, logger=HERMES_LOGGER):
view = sublime.active_window().active_view()
text_command = HermesExecuteCell(view)
text_command.run(edit=None, logger=logger)


class HermesExecuteCell(TextCommand):
"""Execute code cell."""

Expand Down
28 changes: 22 additions & 6 deletions kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ def __init__(
self._connection_name = connection_name
self._execution_state = 'unknown'
self._init_receivers()
self.window = sublime.active_window()
self._panel_name = 'Hermes: {}'.format(
connection_name if connection_name is not None else kernel_id
)
self.panel = self.window.create_output_panel(
self._panel_name, unlisted=False
)

def __del__(self):
self._shell_msg_receiver.shutdown()
Expand Down Expand Up @@ -328,10 +335,10 @@ def execution_state(self):
return self._execution_state

@property
def _show_inline_output(self):
def _output_style(self):
return (sublime
.load_settings("Hermes.sublime-settings")
.get("inline_output"))
.get("output_style"))

def activate_view(self):
"""Activate view to show the output of kernel."""
Expand Down Expand Up @@ -397,7 +404,10 @@ def _write_out_execution_count(self, execution_count) -> None:
self._write_text_to_view("\nOut[{}]: ".format(execution_count))

def _write_text_to_view(self, text: str) -> None:
if self._show_inline_output:
if self._output_style == "panel":
self._write_text_to_panel(text)
return
elif self._output_style == "inline":
return
self.activate_view()
view = self.get_view()
Expand All @@ -409,7 +419,7 @@ def _write_text_to_view(self, text: str) -> None:
view.show(view.size())

def _write_phantom(self, content: str):
if self._show_inline_output:
if self._output_style == "inline":
return
self.activate_view()
file_size = self.get_view().size()
Expand All @@ -421,8 +431,14 @@ def _write_phantom(self, content: str):
sublime.LAYOUT_BLOCK)
self._logger.info("Created phantom {}".format(content))

def _write_text_to_panel(self, text: str) -> None:
self.window.run_command(
'show_panel', {'panel': 'output.{}'.format(self._panel_name)}
)
self.panel.run_command('append', {'characters': text, "scroll_to_end": True})

def _write_inline_html_phantom(self, content: str, region: sublime.Region, view: sublime.View):
if self._show_inline_output:
if self._output_style == "inline":
# region = self._inline_view.sel()[-1]
id = HERMES_FIGURE_PHANTOMS + datetime.now().isoformat()
html = TEXT_PHANTOM.format(content=content)
Expand All @@ -435,7 +451,7 @@ def _write_inline_html_phantom(self, content: str, region: sublime.Region, view:
self._logger.info("Created inline phantom {}".format(html))

def _write_inline_image_phantom(self, data: str, region: sublime.Region, view: sublime.View):
if self._show_inline_output:
if self._output_style == "inline":
# region = self._inline_view.sel()[-1]
id = HERMES_FIGURE_PHANTOMS + datetime.now().isoformat()
html = IMAGE_PHANTOM.format(data=data)
Expand Down