Skip to content

Commit 8ea3306

Browse files
authored
Allow multiline suggestions. (#1948)
Added `TransformationInput.get_line` for multiline suggestions.
1 parent a2a1230 commit 8ea3306

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Diff for: src/prompt_toolkit/layout/controls.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,11 @@ def _create_get_processed_line_func(
667667

668668
merged_processor = merge_processors(input_processors)
669669

670-
def transform(lineno: int, fragments: StyleAndTextTuples) -> _ProcessedLine:
670+
def transform(
671+
lineno: int,
672+
fragments: StyleAndTextTuples,
673+
get_line: Callable[[int], StyleAndTextTuples],
674+
) -> _ProcessedLine:
671675
"Transform the fragments for a given line number."
672676

673677
# Get cursor position at this line.
@@ -679,7 +683,14 @@ def source_to_display(i: int) -> int:
679683

680684
transformation = merged_processor.apply_transformation(
681685
TransformationInput(
682-
self, document, lineno, source_to_display, fragments, width, height
686+
self,
687+
document,
688+
lineno,
689+
source_to_display,
690+
fragments,
691+
width,
692+
height,
693+
get_line,
683694
)
684695
)
685696

@@ -697,7 +708,7 @@ def get_processed_line(i: int) -> _ProcessedLine:
697708
try:
698709
return cache[i]
699710
except KeyError:
700-
processed_line = transform(i, get_line(i))
711+
processed_line = transform(i, get_line(i), get_line)
701712
cache[i] = processed_line
702713
return processed_line
703714

Diff for: src/prompt_toolkit/layout/processors.py

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class TransformationInput:
8686
previous processors into account.)
8787
:param fragments: List of fragments that we can transform. (Received from the
8888
previous processor.)
89+
:param get_line: Optional ; a callable that returns the fragments of another
90+
line in the current buffer; This can be used to create processors capable
91+
of affecting transforms across multiple lines.
8992
"""
9093

9194
def __init__(
@@ -97,6 +100,7 @@ def __init__(
97100
fragments: StyleAndTextTuples,
98101
width: int,
99102
height: int,
103+
get_line: Callable[[int], StyleAndTextTuples] | None = None,
100104
) -> None:
101105
self.buffer_control = buffer_control
102106
self.document = document
@@ -105,6 +109,7 @@ def __init__(
105109
self.fragments = fragments
106110
self.width = width
107111
self.height = height
112+
self.get_line = get_line
108113

109114
def unpack(
110115
self,
@@ -987,6 +992,7 @@ def source_to_display(i: int) -> int:
987992
fragments,
988993
ti.width,
989994
ti.height,
995+
ti.get_line,
990996
)
991997
)
992998
fragments = transformation.fragments

0 commit comments

Comments
 (0)