Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!--
Copyright (C) 2026 Intel Corporation
SPDX-License-Identifier: Apache-2.0
-->

### Fixed

- **`dokimi memory` reads a paragraph of many lines in time linear in its lines** — the memory lint joined the blanked paragraph above for every line and scanned it again, so `"word `a` word\n" * 2000` took 0.49 s, 3.7 times what 1000 lines took. It now keeps the start of the paragraph and the offsets it needs as it goes, with the same output.
85 changes: 85 additions & 0 deletions dokimi/dokimi/memory/blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright (C) 2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

"""The block structure of a Markdown line, as ``memory.store`` blanks the code in an index: fences, lists, paragraph breaks."""

from __future__ import annotations

import re

FENCE = re.compile(r" {0,3}(`{3,}|~{3,})")
_LIST_ITEM = re.compile(r" *(?:[-*+]|\d{1,9}[.)])(?:[ \t]|$)")
_BLOCK_START = re.compile(r" {0,3}(?:#{1,6}(?:[ \t]|$)|>)")


def fence_opener(line: str) -> str:
"""The fence marker that ``line`` opens, or ``""``: an info string holds no backtick."""
m = FENCE.match(line)
if m and not (m.group(1)[0] == "`" and "`" in line[m.end():]):
return m.group(1)
return ""


def closes_fence(line: str, fence: str) -> bool:
"""Whether ``line`` closes the fence ``fence``: the same character, at least as long, nothing after it."""
m = FENCE.match(line)
return bool(m and m.group(1)[0] == fence[0] and len(m.group(1)) >= len(fence) and not line[m.end():].strip())


def span_end(line: str, span: str) -> int:
"""The offset just past the backtick run ``span`` that closes a code span on ``line``, or -1."""
closer = re.search(r"(?<!`)" + span + r"(?!`)", line)
return closer.end() if closer else -1


def indented_code(lines: list[str], i: int, in_list: bool, code: bool) -> tuple[bool, bool, bool]:
"""``(in_list, code, hidden)`` after line ``i``: an indented line outside a list item, after a blank line or code, is code."""
line = lines[i]
if not line.strip():
return in_list, code, False
prev_blank = i == 0 or not lines[i - 1].strip()
if _LIST_ITEM.match(line) and (in_list or not line.startswith((" ", "\t")) or not (prev_blank or code)):
in_list = True
elif line[0] not in " \t" and (prev_blank or _BLOCK_START.match(line)):
in_list = False
hidden = not in_list and line.startswith((" ", "\t")) and (prev_blank or code)
return in_list, hidden, hidden


def paragraph_break(line: str) -> bool:
"""Whether ``line`` ends the paragraph before it: blank, a fence, a list item, a heading or a quote."""
return not line.strip() or bool(fence_opener(line) or _LIST_ITEM.match(line) or _BLOCK_START.match(line))


def close_ahead(lines: list[str]) -> list[bool]:
"""For each line, whether a ``-->`` follows it in the same paragraph."""
ahead = [False] * len(lines) # mutation-ok[flip-bool]: the loop sets every row
seen = False
for i in range(len(lines) - 1, -1, -1):
ahead[i] = seen
if paragraph_break(lines[i]):
seen = False
elif "-->" in lines[i]:
seen = True
return ahead


def span_closes_ahead(lines: list[str], i: int, run: str) -> bool:
"""Whether a backtick run of the length of ``run`` closes the code span it opens on line ``i``, within its paragraph."""
closer = re.compile(r"(?<!`)" + run + r"(?!`)")
for line in lines[i + 1:]:
if paragraph_break(line):
return False
if closer.search(line):
return True
return False


def resume_comment(line: str, comment: str, opener: str) -> tuple[str | None, str, str]:
"""``(line, comment, opener)`` for a line met inside a comment; ``line`` is None while the comment holds all of it."""
if comment == "inline" and paragraph_break(line): # a blank line, a fence or a block start ends it
return line, "", opener
end = line.find("-->")
if end < 0:
return None, comment, opener
return " " * (end + 3) + line[end + 3:], "", "" # the text after the close is prose, not a fence
Loading
Loading