Skip to content

Commit fcbd844

Browse files
author
Constantin Gahr
committed
added 'Clear All Cells' command
1 parent c8d9d9f commit fcbd844

File tree

3 files changed

+81
-22
lines changed

3 files changed

+81
-22
lines changed

Default.sublime-commands

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
"caption": "Helium: Get Object Inspection",
4343
"command": "helium_get_object_inspection"
4444
},
45+
{
46+
"caption": "Helium: Clear All Cells",
47+
"command": "helium_clear_all_cells"
48+
},
4549
{
4650
"caption": "Helium: Settings",
4751
"command": "edit_settings", "args":

helium.py

+54-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import sublime
1717
from sublime_plugin import EventListener, TextCommand, ViewEventListener
1818

19-
from .lib.kernel import KernelConnection
19+
from .lib.kernel import MAX_PHANTOMS, KernelConnection
2020
from .lib.utils import add_path, chain_callbacks, get_cell
2121

2222
with add_path(os.path.join(os.path.dirname(__file__), "lib/client")):
@@ -334,6 +334,7 @@ class HeliumStartKernel(TextCommand):
334334
def run(self, edit, *, logger=HELIUM_LOGGER):
335335
"""Command definition."""
336336
_start_kernel(sublime.active_window(), self.view)
337+
self.view.run_command("helium_clear_all_cells")
337338

338339

339340
# TODO: Make this an enum
@@ -482,6 +483,7 @@ class HeliumConnectKernel(TextCommand):
482483
def run(self, edit, *, logger=HELIUM_LOGGER):
483484
"""Command definition."""
484485
_connect_kernel(sublime.active_window(), self.view, logger=logger)
486+
self.view.run_command("helium_clear_all_cells")
485487

486488

487489
@chain_callbacks
@@ -574,6 +576,7 @@ def is_visible(self, *, logger=HELIUM_LOGGER):
574576
def run(self, edit, *, logger=HELIUM_LOGGER):
575577
"""Command definition."""
576578
_restart_kernel(sublime.active_window(), self.view, logger=logger)
579+
self.view.run_command("helium_clear_all_cells")
577580

578581

579582
@chain_callbacks
@@ -801,6 +804,56 @@ def run(self, edit, move_cursor=False, *, logger=HELIUM_LOGGER):
801804
sublime.set_timeout(lambda: self.view.show(pt), 500)
802805

803806

807+
class HeliumClearAllCells(TextCommand):
808+
"""Clear all phantoms"""
809+
810+
def is_enabled(self, *, logger=HELIUM_LOGGER):
811+
try:
812+
kernel = ViewManager.get_kernel_for_view(self.view.buffer_id())
813+
814+
# if view has an attached kernel, return its status
815+
return kernel.is_alive()
816+
except KeyError:
817+
# if view doesn't have an attached kernel, check if view was created by
818+
# kernel
819+
parent_view = self._get_parent_view()
820+
return parent_view is not None
821+
822+
def is_visible(self, *, logger=HELIUM_LOGGER):
823+
return self.is_enabled()
824+
825+
def run(self, edit, *, logger=HELIUM_LOGGER):
826+
# get correct kernel
827+
try:
828+
kernel = ViewManager.get_kernel_for_view(self.view.buffer_id())
829+
except KeyError:
830+
view = self._get_parent_view()
831+
kernel = ViewManager.get_kernel_for_view(view.buffer_id())
832+
833+
def cb():
834+
if kernel._show_inline_output:
835+
for k in range(MAX_PHANTOMS):
836+
self.view.erase_phantom_by_id(k)
837+
else:
838+
kernel.get_view().close()
839+
kernel.activate_view()
840+
841+
# clear the old phantoms async
842+
sublime.set_timeout_async(cb, 0)
843+
844+
def _get_parent_view(self) -> sublime.View:
845+
for window in sublime.windows():
846+
for view in window.views():
847+
try:
848+
kernel = ViewManager.get_kernel_for_view(view.buffer_id())
849+
except KeyError:
850+
continue
851+
852+
if kernel.get_view() == self.view:
853+
return view
854+
return None
855+
856+
804857
class StatusBar(object):
805858
"""Status Bar with animation.
806859

lib/kernel.py

+23-21
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
MSG_TYPE_STATUS = "status"
3737

3838
HELIUM_FIGURE_PHANTOMS = "helium_figure_phantoms"
39+
MAX_PHANTOMS = 65536
3940

4041
# Used as key of status bar.
4142
KERNEL_STATUS_KEY = "helium_kernel_status"
@@ -422,17 +423,7 @@ def _write_inline_html_phantom(
422423
id = HELIUM_FIGURE_PHANTOMS + datetime.now().isoformat()
423424

424425
html = TEXT_PHANTOM.format(content=content)
425-
int_id = view.add_phantom(
426-
id,
427-
region,
428-
html,
429-
sublime.LAYOUT_BLOCK,
430-
on_navigate=lambda href, id=id, view=view: self._erase_phantom(
431-
id, view=view
432-
),
433-
)
434-
self.phantoms[id] = int_id
435-
self._logger.info("Created inline phantom {}".format(html))
426+
self._add_phantom(view, id, region, html)
436427

437428
def _write_inline_image_phantom(
438429
self, data: str, region: sublime.Region, view: sublime.View
@@ -458,17 +449,28 @@ def _write_inline_image_phantom(
458449

459450
html = IMAGE_PHANTOM.format(data=data, width=width, height=height)
460451

461-
int_id = view.add_phantom(
462-
id,
463-
region,
464-
html,
465-
sublime.LAYOUT_BLOCK,
466-
on_navigate=lambda href, id=id, view=view: self._erase_phantom(
467-
id, view=view
468-
),
452+
self._add_phantom(view, id, region, html)
453+
454+
def _add_phantom(
455+
self, view: sublime.View, id: str, region: sublime.Region, html: str
456+
):
457+
int_id = view.add_phantom(
458+
id,
459+
region,
460+
html,
461+
sublime.LAYOUT_BLOCK,
462+
on_navigate=lambda href, id=id, view=view: self._erase_phantom(
463+
id, view=view
464+
),
465+
)
466+
self.phantoms[id] = int_id
467+
468+
if int_id > MAX_PHANTOMS:
469+
sublime.message_dialog(
470+
"Please close and reopen your current tab. "
471+
+ "Otherwise the 'Clear All Cells' command might not work as intended."
469472
)
470-
self.phantoms[id] = int_id
471-
self._logger.info("Created inline phantom image")
473+
self._logger.info("Created inline phantom image")
472474

473475
def _clear_phantoms_in_region(self, region: sublime.Region, view: sublime.View):
474476
_, cell = get_cell(view, region, logger="")

0 commit comments

Comments
 (0)