Skip to content

Commit 65efbac

Browse files
committed
IndentationInfo.apply_relative_indents
Improved handling of lines without the relative indent prefix (@n:); Fixes 'apply-relative-indents' test.
1 parent a49b826 commit 65efbac

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/text_manipulation/indentation_kit.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
from collections.abc import Sequence
1919
from math import gcd
2020
from typing import NamedTuple
21+
import re
2122

2223
from .cst_kit import IdentifierFinder
2324

2425
from .line_kit import get_line_indent_count, extract_indentation
2526

27+
relative_indent_prefix = re.compile(r'^\s*@(-?\d+):(.*)')
28+
29+
2630
class IndentationInfo(NamedTuple):
2731
"""
2832
A class to represent and manage indentation information.
@@ -276,9 +280,9 @@ def adjust_line(line: str) -> str:
276280
return new_indent + line.lstrip()
277281
return adjust_line
278282

279-
def apply_relative_indents(self, content: str | Sequence[str], context_indent_count: int = 0) -> list[str]:
283+
def apply_relative_indents(self, content: str | Sequence[str], reference_indent_count: int = 0) -> list[str]:
280284
"""
281-
Apply relative indentation based on annotations in the content.
285+
Apply relative indentation based on optional annotations in the content.
282286
283287
This method processes the input content, interpreting special annotations
284288
to apply relative indentation. It uses '@' followed by a number to indicate
@@ -287,7 +291,7 @@ def apply_relative_indents(self, content: str | Sequence[str], context_indent_co
287291
Args:
288292
content (str | Sequence[str]): The content to process. Can be a string
289293
or a sequence of strings.
290-
context_indent_count (int, optional): The base indentation count of the
294+
reference_indent_count (int, optional): The base indentation count of the
291295
context. Defaults to 0.
292296
293297
Returns:
@@ -312,23 +316,23 @@ def apply_relative_indents(self, content: str | Sequence[str], context_indent_co
312316
[' def example():', ' print('Hello')', ' if True:', ' print('World')']
313317
"""
314318
# TODO Always send str?
315-
lines = [l.lstrip() for l in content.splitlines() if l.strip()] if isinstance(content, str) else content
316-
317-
context_indent_level = self.char_count_to_level(context_indent_count)
319+
lines = [l for l in content.strip('\n').splitlines()] if isinstance(content, str) else content
320+
reference_indent_level = self.char_count_to_level(reference_indent_count)
318321
for i in range(len(lines)):
319322
line = lines[i]
320-
parts = line.split(':', 1)
321-
if len(parts) == 2 and parts[0].startswith('@'):
322-
relative_indent_level = int(parts[0][1:])
323-
absolute_indent_level = context_indent_level + relative_indent_level
324-
assert absolute_indent_level >= 0, (
325-
f"Final indentation for line `{line.strip()}` cannot be negative "
326-
f"({absolute_indent_level})"
327-
)
328-
lines[i] = self.level_to_chars(absolute_indent_level) + parts[1].lstrip()
329-
else:
330-
absolute_indent_level = context_indent_level
331-
lines[i] = self.level_to_chars(absolute_indent_level) + line.lstrip()
323+
match relative_indent_prefix.match(line):
324+
case re.Match() as m:
325+
relative_indent_level, line = m.groups()
326+
relative_indent_level = int(relative_indent_level)
327+
line = line.lstrip()
328+
case _:
329+
relative_indent_level = self.char_count_to_level(get_line_indent_count(line))
330+
absolute_indent_level = reference_indent_level + relative_indent_level
331+
assert absolute_indent_level >= 0, (
332+
f"Final indent level for line `{line.strip()}` cannot be negative "
333+
f"({absolute_indent_level})"
334+
)
335+
lines[i] = self.level_to_chars(absolute_indent_level) + line.lstrip()
332336

333337
return lines
334338

0 commit comments

Comments
 (0)