Skip to content

Commit 75786f6

Browse files
Fix edge case with leading line ending in 'split_lines'. (#1982)
* Fix edge case with leading line ending in 'split_lines'. * Use dict.fromkeys in NestedCompleter.
1 parent 165258d commit 75786f6

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

Diff for: src/prompt_toolkit/completion/nested.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def from_nested_dict(cls, data: NestedDict) -> NestedCompleter:
6969
elif isinstance(value, dict):
7070
options[key] = cls.from_nested_dict(value)
7171
elif isinstance(value, set):
72-
options[key] = cls.from_nested_dict({item: None for item in value})
72+
options[key] = cls.from_nested_dict(dict.fromkeys(value))
7373
else:
7474
assert value is None
7575
options[key] = None

Diff for: src/prompt_toolkit/formatted_text/utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ def split_lines(
8989
parts = string.split("\n")
9090

9191
for part in parts[:-1]:
92-
if part:
93-
line.append(cast(OneStyleAndTextTuple, (style, part, *mouse_handler)))
92+
line.append(cast(OneStyleAndTextTuple, (style, part, *mouse_handler)))
9493
yield line
9594
line = []
9695

Diff for: tests/test_formatted_text.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def test_split_lines_3():
274274
lines = list(split_lines([("class:a", "\n")]))
275275

276276
assert lines == [
277-
[],
277+
[("class:a", "")],
278278
[("class:a", "")],
279279
]
280280

@@ -284,3 +284,15 @@ def test_split_lines_3():
284284
assert lines == [
285285
[("class:a", "")],
286286
]
287+
288+
289+
def test_split_lines_4():
290+
"Edge cases: inputs starting and ending with newlines."
291+
# -1-
292+
lines = list(split_lines([("class:a", "\nline1\n")]))
293+
294+
assert lines == [
295+
[("class:a", "")],
296+
[("class:a", "line1")],
297+
[("class:a", "")],
298+
]

0 commit comments

Comments
 (0)