Skip to content

Commit 91b8273

Browse files
committed
Minimal changes to allow multiline suggestions.
I'm trying to be the least invasive; But the idea is to allow access to previous lines when rendering a line in order to shift them down; and display multiline sugestions. With this I can add minimal modification into IPython that allows multiline suggestion (typically would be created by an LLM – while not a big fan, there is a lot of requests for it. I'm going to assume there is no willingness tochange the current API for transformers to get at least a ranges of lines instead of lines, it would be easier to handle. I do not include any update to the transformers we use in IPython to display those completions; but once battled tested a bit more, I'm happy to contribute it as well.
1 parent a2a1230 commit 91b8273

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/prompt_toolkit/layout/controls.py

Lines changed: 14 additions & 3 deletions
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

src/prompt_toolkit/layout/processors.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import re
1212
from abc import ABCMeta, abstractmethod
13-
from typing import TYPE_CHECKING, Callable, Hashable, cast
13+
from typing import TYPE_CHECKING, Callable, Hashable, cast, Optional
1414

1515
from prompt_toolkit.application.current import get_app
1616
from prompt_toolkit.cache import SimpleCache
@@ -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: Optional[Callable[[int], StyleAndTextTuples]] = 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)