Skip to content

Commit

Permalink
Allow multiline suggestions. (#1948)
Browse files Browse the repository at this point in the history
Added `TransformationInput.get_line` for multiline suggestions.
  • Loading branch information
Carreau authored Jan 1, 2025
1 parent a2a1230 commit 8ea3306
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/prompt_toolkit/layout/controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,11 @@ def _create_get_processed_line_func(

merged_processor = merge_processors(input_processors)

def transform(lineno: int, fragments: StyleAndTextTuples) -> _ProcessedLine:
def transform(
lineno: int,
fragments: StyleAndTextTuples,
get_line: Callable[[int], StyleAndTextTuples],
) -> _ProcessedLine:
"Transform the fragments for a given line number."

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

transformation = merged_processor.apply_transformation(
TransformationInput(
self, document, lineno, source_to_display, fragments, width, height
self,
document,
lineno,
source_to_display,
fragments,
width,
height,
get_line,
)
)

Expand All @@ -697,7 +708,7 @@ def get_processed_line(i: int) -> _ProcessedLine:
try:
return cache[i]
except KeyError:
processed_line = transform(i, get_line(i))
processed_line = transform(i, get_line(i), get_line)
cache[i] = processed_line
return processed_line

Expand Down
6 changes: 6 additions & 0 deletions src/prompt_toolkit/layout/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class TransformationInput:
previous processors into account.)
:param fragments: List of fragments that we can transform. (Received from the
previous processor.)
:param get_line: Optional ; a callable that returns the fragments of another
line in the current buffer; This can be used to create processors capable
of affecting transforms across multiple lines.
"""

def __init__(
Expand All @@ -97,6 +100,7 @@ def __init__(
fragments: StyleAndTextTuples,
width: int,
height: int,
get_line: Callable[[int], StyleAndTextTuples] | None = None,
) -> None:
self.buffer_control = buffer_control
self.document = document
Expand All @@ -105,6 +109,7 @@ def __init__(
self.fragments = fragments
self.width = width
self.height = height
self.get_line = get_line

def unpack(
self,
Expand Down Expand Up @@ -987,6 +992,7 @@ def source_to_display(i: int) -> int:
fragments,
ti.width,
ti.height,
ti.get_line,
)
)
fragments = transformation.fragments
Expand Down

0 comments on commit 8ea3306

Please sign in to comment.