This commit is contained in:
Will McGugan 2023-07-28 09:37:01 +01:00
parent 7a15d62e44
commit 29dfa0e5c8
2 changed files with 52 additions and 18 deletions

View file

@ -817,7 +817,6 @@ class Text(JupyterMixin):
"""
if "\t" not in self.plain:
return
pos = 0
if tab_size is None:
tab_size = self.tab_size
assert tab_size is not None
@ -825,21 +824,25 @@ class Text(JupyterMixin):
append = result.append
for line in self.split("\n", include_separator=True):
parts = line.split("\t", include_separator=True)
tab_parts: list[Text] = []
for part in parts:
if part.plain.endswith("\t"):
part._text[-1] = part._text[-1][:-1] + " "
pos += part.cell_len
tab_remainder = pos % tab_size
if tab_remainder:
spaces = tab_size - (pos % tab_size)
part.extend_style(spaces)
pos += spaces
else:
pos += part.cell_len
tab_parts.append(part)
append(Text("").join(tab_parts))
cell_position = 0
if "\t" not in line.plain:
append(line)
else:
parts = line.split("\t", include_separator=True)
tab_parts: list[Text] = []
for part in parts:
if part.plain.endswith("\t"):
part._text[-1] = part._text[-1][:-1] + " "
cell_position += part.cell_len
tab_remainder = cell_position % tab_size
if tab_remainder:
spaces = tab_size - (cell_position % tab_size)
part.extend_style(spaces)
cell_position += spaces
else:
cell_position += part.cell_len
tab_parts.append(part)
append(Text("").join(tab_parts))
self._text = [result.plain]
self._length = len(self.plain)

View file

@ -608,6 +608,7 @@ def test_tabs_to_spaces():
("\t", 4, " ", []),
("\tbar", 4, " bar", []),
("foo\tbar", 4, "foo bar", []),
("foo\nbar\nbaz", 4, "foo\nbar\nbaz", []),
(
"[bold]foo\tbar",
4,
@ -617,7 +618,15 @@ def test_tabs_to_spaces():
Span(4, 7, "bold"),
],
),
("[bold]\tbar", 4, " bar", [Span(0, 4, "bold"), Span(4, 7, "bold")]),
(
"[bold]\tbar",
4,
" bar",
[
Span(0, 4, "bold"),
Span(4, 7, "bold"),
],
),
(
"\t[bold]bar",
4,
@ -626,6 +635,28 @@ def test_tabs_to_spaces():
Span(4, 7, "bold"),
],
),
(
"[red]foo\tbar\n[green]egg\tbaz",
8,
"foo bar\negg baz",
[
Span(0, 8, "red"),
Span(8, 12, "red"),
Span(12, 20, "red"),
Span(12, 20, "green"),
Span(20, 23, "red"),
Span(20, 23, "green"),
],
),
(
"[bold]💩\t💩",
8,
"💩 💩",
[
Span(0, 7, "bold"),
Span(7, 8, "bold"),
],
),
],
)
def test_tabs_to_spaces_spans(
@ -634,7 +665,7 @@ def test_tabs_to_spaces_spans(
"""Test spans are correct after expand_tabs"""
text = Text.from_markup(markup)
text.expand_tabs(tab_size)
print(expected_spans)
print(text._spans)
assert text.plain == expected_text
assert text._spans == expected_spans