Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract _build_row_lengths_offsets function #117

Merged
merged 3 commits into from
May 10, 2024
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
34 changes: 15 additions & 19 deletions src/ssort/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
from ssort._statements import Statement


def _build_row_lengths_offsets(text):
# Build an index of row lengths and start offsets to enable fast string
# indexing using ast row/column coordinates.
row_lengths = []
row_offsets = [0]
for offset, char in enumerate(text):
if char == "\n":
row_lengths.append(offset - row_offsets[-1])
row_offsets.append(offset + 1)
row_lengths.append(len(text) - row_offsets[-1])
return row_lengths, row_offsets


def _find_start(node):
if (
isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef))
Expand All @@ -29,17 +42,8 @@ def split(
nodes,
next_row=0,
next_col=0,
indent=0,
):
# Build an index of row lengths and start offsets to enable fast string
# indexing using ast row/column coordinates.
row_lengths = []
row_offsets = [0]
for offset, char in enumerate(root_text):
if char == "\n":
row_lengths.append(offset - row_offsets[-1])
row_offsets.append(offset + 1)
row_lengths.append(len(root_text) - row_offsets[-1])
row_lengths, row_offsets = _build_row_lengths_offsets(root_text)

nodes = iter(nodes)

Expand Down Expand Up @@ -106,15 +110,7 @@ def split_class(statement):
text = statement.text
text_padded = statement.text_padded()

# Build an index of row lengths and start offsets to enable fast string
# indexing using ast row/column coordinates.
row_lengths = []
row_offsets = [0]
for offset, char in enumerate(text_padded):
if char == "\n":
row_lengths.append(offset - row_offsets[-1])
row_offsets.append(offset + 1)
row_lengths.append(len(text_padded) - row_offsets[-1])
_, row_offsets = _build_row_lengths_offsets(text_padded)

tokens = iter(generate_tokens(StringIO(text_padded).readline))

Expand Down
4 changes: 4 additions & 0 deletions tests/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
def _nodes_types(
node_type: type[ast.AST] = ast.AST,
) -> Iterable[type[ast.AST]]:
# coverage package adds a coverage.parser.NodeList subclass
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just naughty. Why on earth does it do that?

if node_type.__module__ != "ast":
return

# Skip deprecated AST nodes.
if issubclass(node_type, _deprecated_node_types):
return
Expand Down
34 changes: 34 additions & 0 deletions tests/test_split.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import sys

import pytest

from ssort._parsing import parse, split_class

type_parameter_syntax = pytest.mark.skipif(
sys.version_info < (3, 12),
reason="type parameter syntax was introduced in python 3.12",
)


def _split_text(source):
return [
Expand Down Expand Up @@ -117,13 +126,38 @@ def test_split_class_decorators():
assert actual == expected


def test_split_class_decorator_single_line():
actual = _split_class("@decorator()\nclass A: key: int")
expected = "@decorator()\nclass A:", [" key: int"]
assert actual == expected


def test_split_class_leading_comment():
actual = _split_class("# Comment.\nclass A:\n pass")
expected = "# Comment.\nclass A:", [" pass"]
assert actual == expected


def test_split_class_leading_comment_single_line():
actual = _split_class("# Comment.\nclass A: pass")
expected = "# Comment.\nclass A:", [" pass"]
assert actual == expected


def test_split_class_multiple():
actual = _split_class("def a():\n pass\n\nclass A:\n pass")
expected = "\nclass A:", [" pass"]
assert actual == expected


def test_split_class_with_bases():
actual = _split_class("class A(\n B,\n): pass")
expected = "class A(\n B,\n):", [" pass"]
assert actual == expected


@type_parameter_syntax
def test_split_class_with_type_params():
actual = _split_class("class A[\n B,\n]: pass")
expected = "class A[\n B,\n]:", [" pass"]
assert actual == expected
Loading