Skip to content

Commit

Permalink
Make AI suggestions show as multiple lines instead of one line with ^J's
Browse files Browse the repository at this point in the history
  • Loading branch information
asmeurer committed Sep 26, 2024
1 parent f794da9 commit bb4b2e6
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions mypython/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
HighlightMatchingBracketProcessor, Processor)
from prompt_toolkit.layout.utils import explode_text_fragments
from prompt_toolkit.application import get_app
from prompt_toolkit.formatted_text import fragment_list_width, to_formatted_text

from pyflakes.checker import Checker
from pyflakes.messages import (UnusedImport, UnusedVariable, UndefinedName,
Expand Down Expand Up @@ -305,26 +306,49 @@ def apply_transformation(self, transformation_input):

class AppendAIAutoSuggestion(Processor):
"""
Based on prompt_toolkit.layout.processors.AppendAutoSuggest but is not
Based on prompt_toolkit.layout.processors.Autosuggestion but is not
limited to just inserting the suggestion at the end.
"""

def __init__(self, style: str = "class:auto-suggestion") -> None:
self.style = style

def apply_transformation(self, ti):
buffer = ti.buffer_control.buffer
def apply_transformation(self, transformation_input):
buffer = transformation_input.buffer_control.buffer
app = get_app()

source_to_display = ti.source_to_display
source_to_display = transformation_input.source_to_display
column = source_to_display(buffer.document.cursor_position_col)
text_before_cursor = buffer.document.text_before_cursor
if buffer.ai_suggestions:
suggestion = buffer.ai_suggestions[buffer.ai_suggestion_index]

terminal_size = app.output.get_size().columns
wrap_width = terminal_size - prompt_width(buffer)
suggestion = replace_newlines_with_spaces(text_before_cursor + suggestion, wrap_width)[column:]
else:
suggestion = ""

column = source_to_display(buffer.document.cursor_position_col)

fragments = list(ti.fragments)
if ti.lineno == buffer.document.cursor_position_row:
fragments = list(transformation_input.fragments)
if transformation_input.lineno == buffer.document.cursor_position_row:
fragments.insert(column, (self.style, suggestion))

return Transformation(fragments=fragments)

def prompt_width(buffer):
return fragment_list_width(to_formatted_text(buffer.session.message))

def replace_newlines_with_spaces(text, column_width):
lines = text.split('\n')
result = ''
for i, line in enumerate(lines):
line_length = len(line)
# Calculate padding to reach the next multiple of column_width
if not line:
padding = column_width
else:
padding = (column_width - (line_length % column_width)) % column_width
result += line
if i != len(lines) - 1: # Avoid adding padding after the last line
result += ' ' * padding
return result

0 comments on commit bb4b2e6

Please sign in to comment.