Skip to content

Commit

Permalink
fix: catch error on nonexistant kernel init (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas authored Mar 25, 2024
1 parent 23d8afc commit 2fffc76
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions rplugin/python3/molten/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def _on_cursor_moved(self, scrolled=False) -> None:
for m in molten_kernels:
m.on_cursor_moved(scrolled)

def _initialize_buffer(self, kernel_name: str, shared=False) -> MoltenKernel:
def _initialize_buffer(self, kernel_name: str, shared=False) -> MoltenKernel | None:
assert self.canvas is not None
if shared: # use an existing molten kernel, for a new neovim buffer
molten = self.molten_kernels.get(kernel_name)
Expand All @@ -185,21 +185,27 @@ def _initialize_buffer(self, kernel_name: str, shared=False) -> MoltenKernel:
if self.molten_kernels.get(kernel_name) is not None:
kernel_id = f"{kernel_name}_{len(self.molten_kernels)}"

molten = MoltenKernel(
self.nvim,
self.canvas,
self.highlight_namespace,
self.extmark_namespace,
self.nvim.current.buffer,
self.options,
kernel_name,
kernel_id,
)
try:
molten = MoltenKernel(
self.nvim,
self.canvas,
self.highlight_namespace,
self.extmark_namespace,
self.nvim.current.buffer,
self.options,
kernel_name,
kernel_id,
)

self.add_kernel(self.nvim.current.buffer, kernel_id, molten)
molten._doautocmd("MoltenInitPost")
self.add_kernel(self.nvim.current.buffer, kernel_id, molten)
molten._doautocmd("MoltenInitPost")

return molten
return molten
except:
notify_error(
self.nvim,
f"Could not initialize kernel named '{kernel_name}'."
)

def add_kernel(self, buffer: Buffer, kernel_id: str, kernel: MoltenKernel):
"""Add a new MoltenKernel to be tracked by Molten.
Expand Down Expand Up @@ -817,10 +823,10 @@ def command_load(self, args) -> None:
kernel_name = data["kernel"]

molten = self._initialize_buffer(kernel_name, shared=shared)
if molten:
load(self.nvim, molten, self.nvim.current.buffer, data)

load(self.nvim, molten, self.nvim.current.buffer, data)

self._update_interface()
self._update_interface()
except MoltenIOError as err:
if molten is not None:
self._deinit_buffer([molten])
Expand Down Expand Up @@ -865,7 +871,7 @@ def function_molten_tick(self, _: Any) -> None:
m.tick()

@pynvim.function("MoltenTickInput", sync=False) # type: ignore
@nvimui #type: ignore
@nvimui # type: ignore
def function_molten_tick_input(self, _: Any) -> None:
self._initialize_if_necessary()

Expand All @@ -877,7 +883,7 @@ def function_molten_tick_input(self, _: Any) -> None:
m.tick_input()

@pynvim.function("MoltenSendStdin", sync=False) # type: ignore
@nvimui #type: ignore
@nvimui # type: ignore
def function_molten_send_stdin(self, args: Tuple[str, str]) -> None:
molten_kernels = self._get_current_buf_kernels(False)
if molten_kernels is None:
Expand Down

0 comments on commit 2fffc76

Please sign in to comment.