Skip to content

Commit

Permalink
fix: remove orphaned cells (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas authored Dec 10, 2023
1 parent aede385 commit b500515
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 3 additions & 1 deletion rplugin/python3/molten/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,9 @@ def command_export(self, args, bang: bool) -> None:
if len(args) > 1:
kernel = args[1]
else:
self.kernel_check(f"MoltenExportOutput{'!' if bang else ''}", path, buf, kernel_last=True)
self.kernel_check(
f"MoltenExportOutput{'!' if bang else ''}", path, buf, kernel_last=True
)
return

kernels = self._get_current_buf_kernels(True)
Expand Down
3 changes: 3 additions & 0 deletions rplugin/python3/molten/code_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def clear_interface(self, highlight_namespace):
self.end.lineno + 1,
)

def empty(self) -> bool:
return self.end <= self.begin

def get_text(self, nvim: Nvim) -> str:
assert self.begin.bufno == self.end.bufno

Expand Down
16 changes: 13 additions & 3 deletions rplugin/python3/molten/moltenbuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
from molten.runtime import JupyterRuntime


# Handles a Single Kernel that can be attached to multiple buffers
# Other MoltenKernels can be attached to the same buffers
class MoltenKernel:
"""Handles a Single Kernel that can be attached to multiple buffers
Other MoltenKernels can be attached to the same buffers"""

nvim: Nvim
canvas: Canvas
highlight_namespace: int
Expand All @@ -26,8 +27,8 @@ class MoltenKernel:

runtime: JupyterRuntime

# name unique to this specific jupyter runtime. Only used within Molten. Human Readable
kernel_id: str
"""name unique to this specific jupyter runtime. Only used within Molten. Human Readable"""

outputs: Dict[CodeCell, OutputBuffer]
current_output: Optional[CodeCell]
Expand Down Expand Up @@ -210,6 +211,14 @@ def delete_cell(self) -> None:
del self.outputs[self.selected_cell]
self.selected_cell = None

def clear_empty_spans(self) -> None:
for span in list(self.outputs.keys()):
if span.empty():
self.outputs[span].clear_float_win()
self.outputs[span].clear_virt_output(span.bufno)
del self.outputs[span]
span.clear_interface(self.highlight_namespace)

def update_interface(self) -> None:
buffer_numbers = [buf.number for buf in self.buffers]
if self.nvim.current.buffer.number not in buffer_numbers:
Expand All @@ -219,6 +228,7 @@ def update_interface(self) -> None:
return

self.updating_interface = True
self.clear_empty_spans()
new_selected_cell = self._get_selected_span()

# Clear the cell we just left
Expand Down

0 comments on commit b500515

Please sign in to comment.