Skip to content

Commit

Permalink
fix: correct cell separator detection for magic commands
Browse files Browse the repository at this point in the history
This commit corrects the cell separator detection that was broken
in the previous commit. The previous commit allowed lines starting
with "# %%" followed by additional comments to be treated as cell
separators. However, this broke the functionality for magic commands
like "# %%timeit", which should not be treated as cell separators.

The fix involves checking whether there is a space after "# %%" before
treating it as a cell separator. This is done by changing the line
separator check in Lua files to `vim.fn.trim(string.sub(line, 1, 5)) == "# %%"`.

Additionally, it was discovered that `line.startswith("# %%")` in Python
files also misidentified magic commands as cell separators when syncing
to Jupyter. Therefore, this has been corrected to `line[:5].strip() == "# %%"`.

This change ensures that magic commands are correctly recognized and
not treated as cell separators.
  • Loading branch information
ESSO0428 committed Jul 13, 2024
1 parent 5d01a25 commit b2547fe
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lua/jupynium/cells.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function M.line_type(line)
return "empty"
elseif vim.startswith(line, "# %% [md]") or vim.startswith(line, "# %% [markdown]") then
return "cell separator: markdown"
elseif vim.fn.trim(line) == "# %%" then
elseif vim.fn.trim(string.sub(line, 1, 5)) == "# %%" then
return "cell separator: code"
elseif vim.startswith(line, "# ---") then
return "metadata"
Expand Down
2 changes: 1 addition & 1 deletion src/jupynium/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def full_analyse_buf(self, header_cell_type: str = "header"):
num_rows_per_cell.append(num_rows_this_cell)
num_rows_this_cell = 1
cell_types.append("markdown")
elif line.startswith("# %%"):
elif line[:5].strip() == "# %%":
num_rows_per_cell.append(num_rows_this_cell)
num_rows_this_cell = 1
cell_types.append("code")
Expand Down

0 comments on commit b2547fe

Please sign in to comment.