Skip to content

Commit

Permalink
Enhance Parser to Support # %% with Additional Comments (#127)
Browse files Browse the repository at this point in the history
* refactor(parser): support # %% with additional comments
* fix(parser): import re for regex matching
* refactor(parser): support # %% with additional comments
* fix: correct cell separator detection for magic commands
* docs: add comments for '# %% Cell Title' feature
* test: add tests for '# %% Cell Title' in test_buffer.py
  • Loading branch information
ESSO0428 committed Jul 16, 2024
1 parent 5900c1a commit d93164f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lua/jupynium/cells.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ 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
-- Treat '# %% Cell Title' as a code cell
-- But not magic commands such as '# %%timeit'.
-- See: https://github.com/kiyoon/jupynium.nvim/pull/127
elseif vim.fn.trim(string.sub(line, 1, 5)) == "# %%" then
return "cell separator: code"
elseif vim.startswith(line, "# ---") then
return "metadata"
Expand Down
5 changes: 4 additions & 1 deletion src/jupynium/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ 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.strip() == "# %%":
# Treat '# %% Cell Title' as a code cell
# But not magic commands such as '# %%timeit'.
# See: https://github.com/kiyoon/jupynium.nvim/pull/127
elif line[:5].strip() == "# %%":
num_rows_per_cell.append(num_rows_this_cell)
num_rows_this_cell = 1
cell_types.append("code")
Expand Down
16 changes: 16 additions & 0 deletions tests/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def test_buffer_1():
assert buffer.cell_types == ["header", "code"]


def test_buffer_with_cell_title_1():
buffer = JupyniumBuffer(["a", "b", "c", "# %% Cell Title", "d", "e", "f"])
assert buffer.num_rows_per_cell == [3, 4]
assert buffer.cell_types == ["header", "code"]


def test_magic_command_1():
"""
Everything else except magic commands should be preserved after __init__() or fully_analysed_buf().
Expand All @@ -21,6 +27,16 @@ def test_magic_command_1():
assert code_cell_content == "%time\ne\nf"


def test_double_magic_command_1():
"""
Everything else except double magic commands should be preserved after __init__() or fully_analyse_buf().
"""
lines = ["a", "b", "c", "# %% Cell Title", "# %%timeit", "e", "f"]
buffer = JupyniumBuffer(lines)
code_cell_content = buffer.get_cell_text(1)
assert code_cell_content == "%%timeit\ne\nf"


def test_buffer_markdown():
buffer = JupyniumBuffer(["a", "b", "c", "# %% [md]", "d", "# %%", "f"])
assert buffer.num_rows_per_cell == [3, 2, 2]
Expand Down

0 comments on commit d93164f

Please sign in to comment.