Skip to content

Commit

Permalink
fix: add ptw and resolve test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Nov 22, 2023
1 parent 7586c69 commit a3fe20b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ To run the pre-commit hook test:
tox -e py37-hook
```

## `ptw` testing

See configuration in `pyproject.toml` for `[tool.pytest-watcher]`

```sh
pipx install pytest-watcher

ptw .
```

## Publish to PyPi

Either use flit directly:
Expand Down
47 changes: 27 additions & 20 deletions mdformat_mkdocs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,50 +71,56 @@ def add_bullet(self, line: str) -> str:
"""Add bullet to the line."""
match = _RE_INDENT.match(line)
assert match is not None # for pylint
list_match = _RE_LIST_ITEM.match(match["content"])
assert list_match is not None # for pylint

self.this_indent = match["indent"]
self.is_numbered = list_match["bullet"] not in {"-", "*"}
self.is_semantic_indent = True
list_match = _RE_LIST_ITEM.match(match["content"])
self.is_list_match = bool(list_match)

new_bullet = f"{self._number()}." if self.is_numbered else "-"
return f'{new_bullet} {list_match["item"]}'
new_line = line
if list_match:
self.is_numbered = list_match["bullet"] not in {"-", "*"}
new_bullet = f"{self._number()}." if self.is_numbered else "-"
new_line = f'{new_bullet} {list_match["item"]}'
self.is_semantic_indent = True
elif not new_line:
self.is_semantic_indent = False # on line break, use non-semantic indents
return new_line


class _MarkdownIndent:
"""Track Markdown Indent."""

last_indent = ""
counter = 0
lookup: Dict[str, int] = {}
_last_indent = ""
_counter = 0
_lookup: Dict[str, int] = {}

def __init__(self) -> None:
self._lookup = {}

def calculate(self, this_indent: str) -> str:
"""Calculate the new indent."""
if this_indent:
diff = len(this_indent) - len(self.last_indent)
diff = len(this_indent) - len(self._last_indent)
if not diff:
...
elif diff > 0:
self.counter += 1
self.lookup[this_indent] = self.counter
elif this_indent in self.lookup:
self.counter = self.lookup[this_indent]
self._counter += 1
self._lookup[this_indent] = self._counter
elif this_indent in self._lookup:
self._counter = self._lookup[this_indent]
else:
raise NotImplementedError("No indentation")
else:
self.counter = 0
self.last_indent = this_indent
return _DEFAULT_INDENT * self.counter
self._counter = 0
self._last_indent = this_indent
return _DEFAULT_INDENT * self._counter


def _normalize_list(text: str, node: RenderTreeNode, context: RenderContext) -> str:
"""Post-processor to normalize lists."""
eol = "\n"

rendered = ""
md_list = _MarkdownList(increment_number_mode=context.options["mdformat"]["number"])
number_mode = bool(context.options["mdformat"].get("number"))
md_list = _MarkdownList(increment_number_mode=number_mode)
md_indent = _MarkdownIndent()
for line in text.split(eol):
new_line = md_list.add_bullet(line)
Expand All @@ -131,6 +137,7 @@ def _normalize_list(text: str, node: RenderTreeNode, context: RenderContext) ->
):
removed_indents = -1 if md_list.is_numbered else -2
new_indent = new_indent[:removed_indents]

new_line = new_line.replace(f"{FILLER_CHAR} ", "").replace(FILLER_CHAR, "")
rendered += f"{new_indent}{new_line.strip()}{eol}"
return rendered.rstrip()
Expand Down
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ known_first_party = ["mdformat_mkdocs", "tests"]

# Settings for Black compatibility
profile = "black"

[tool.pytest-watcher]
now = true
runner = "tox"
runner_args = ["-e", "py37-recommended"]
patterns = ["*.py"]
ignore_patterns = []
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ skip_missing_interpreters = False

[testenv:py{310}]
extras = test
commands = pytest {posargs} --ff -vv
commands = pytest {posargs} --ff --nf -vv

[testenv:py{310}-cov]
extras = test
commands = pytest --cov={envsitepackagesdir}/mdformat_mkdocs {posargs}

[testenv:py{37}-recommended]
extras = recommended,test
commands = pytest {posargs} --ff -vv
commands = pytest {posargs} --ff --nf -vv --exitfirst

[testenv:py{310}-pre-commit]
extras = dev
Expand All @@ -31,4 +31,4 @@ commands = pre-commit run --config .pre-commit-test.yaml {posargs:--all-files --
max-line-length = 88
max-complexity = 10
# These checks violate PEP8 so let's ignore them
extend-ignore = E203
extend-ignore = E203,W503

0 comments on commit a3fe20b

Please sign in to comment.