Skip to content

Minimal changes to allow multiline suggestions. #1948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading