mirror of
https://github.com/Aider-AI/aider.git
synced 2025-06-01 02:05:00 +00:00
refactored into getinput
This commit is contained in:
parent
6419e6c390
commit
82cf653d73
3 changed files with 64 additions and 59 deletions
27
coder.py
27
coder.py
|
@ -4,10 +4,6 @@
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import traceback
|
import traceback
|
||||||
from prompt_toolkit import prompt
|
|
||||||
from prompt_toolkit.completion import Completer, Completion
|
|
||||||
from prompt_toolkit.history import FileHistory
|
|
||||||
from prompt_toolkit.styles import Style
|
|
||||||
|
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
from rich.prompt import Confirm, Prompt
|
from rich.prompt import Confirm, Prompt
|
||||||
|
@ -17,6 +13,8 @@ from rich.text import Text
|
||||||
from rich.markdown import Markdown
|
from rich.markdown import Markdown
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from getinput import get_input
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
@ -30,25 +28,6 @@ import prompts
|
||||||
openai.api_key = os.getenv("OPENAI_API_KEY")
|
openai.api_key = os.getenv("OPENAI_API_KEY")
|
||||||
|
|
||||||
|
|
||||||
class FileContentCompleter(Completer):
|
|
||||||
def __init__(self, fnames):
|
|
||||||
self.fnames = fnames
|
|
||||||
|
|
||||||
def get_completions(self, document, complete_event):
|
|
||||||
text = document.text_before_cursor
|
|
||||||
words = text.split()
|
|
||||||
if not words:
|
|
||||||
return
|
|
||||||
|
|
||||||
last_word = words[-1]
|
|
||||||
for fname in self.fnames:
|
|
||||||
with open(fname, "r") as f:
|
|
||||||
content = f.read()
|
|
||||||
for word in content.split():
|
|
||||||
if word.startswith(last_word):
|
|
||||||
yield Completion(word, start_position=-len(last_word))
|
|
||||||
|
|
||||||
|
|
||||||
class Coder:
|
class Coder:
|
||||||
fnames = dict()
|
fnames = dict()
|
||||||
last_modified = 0
|
last_modified = 0
|
||||||
|
@ -180,7 +159,7 @@ class Coder:
|
||||||
else:
|
else:
|
||||||
print()
|
print()
|
||||||
|
|
||||||
inp = utils.get_input(self.history_file, self.fnames)
|
inp = get_input(self.history_file, self.fnames)
|
||||||
if inp is None:
|
if inp is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
61
getinput.py
Normal file
61
getinput.py
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
from prompt_toolkit.styles import Style
|
||||||
|
|
||||||
|
from prompt_toolkit import prompt
|
||||||
|
from prompt_toolkit.completion import Completer, Completion
|
||||||
|
from prompt_toolkit.history import FileHistory
|
||||||
|
|
||||||
|
|
||||||
|
class FileContentCompleter(Completer):
|
||||||
|
def __init__(self, fnames):
|
||||||
|
self.fnames = fnames
|
||||||
|
|
||||||
|
def get_completions(self, document, complete_event):
|
||||||
|
text = document.text_before_cursor
|
||||||
|
words = text.split()
|
||||||
|
if not words:
|
||||||
|
return
|
||||||
|
|
||||||
|
last_word = words[-1]
|
||||||
|
for fname in self.fnames:
|
||||||
|
with open(fname, "r") as f:
|
||||||
|
content = f.read()
|
||||||
|
for word in content.split():
|
||||||
|
if word.startswith(last_word):
|
||||||
|
yield Completion(word, start_position=-len(last_word))
|
||||||
|
|
||||||
|
|
||||||
|
def get_input(history_file, fnames):
|
||||||
|
inp = ""
|
||||||
|
multiline_input = False
|
||||||
|
|
||||||
|
style = Style.from_dict({"": "green"})
|
||||||
|
|
||||||
|
while True:
|
||||||
|
completer_instance = FileContentCompleter(fnames)
|
||||||
|
if multiline_input:
|
||||||
|
show = ". "
|
||||||
|
else:
|
||||||
|
show = "> "
|
||||||
|
|
||||||
|
try:
|
||||||
|
line = prompt(
|
||||||
|
show,
|
||||||
|
completer=completer_instance,
|
||||||
|
history=FileHistory(history_file),
|
||||||
|
style=style,
|
||||||
|
)
|
||||||
|
except EOFError:
|
||||||
|
return
|
||||||
|
if line.strip() == "{" and not multiline_input:
|
||||||
|
multiline_input = True
|
||||||
|
continue
|
||||||
|
elif line.strip() == "}" and multiline_input:
|
||||||
|
break
|
||||||
|
elif multiline_input:
|
||||||
|
inp += line + "\n"
|
||||||
|
else:
|
||||||
|
inp = line
|
||||||
|
break
|
||||||
|
|
||||||
|
print()
|
||||||
|
return inp
|
35
utils.py
35
utils.py
|
@ -107,38 +107,3 @@ def do_replace(fname, before_text, after_text):
|
||||||
|
|
||||||
fname.write_text(new_content)
|
fname.write_text(new_content)
|
||||||
return True
|
return True
|
||||||
def get_input(history_file, fnames):
|
|
||||||
inp = ""
|
|
||||||
multiline_input = False
|
|
||||||
|
|
||||||
style = Style.from_dict({'': 'green'})
|
|
||||||
|
|
||||||
while True:
|
|
||||||
completer_instance = FileContentCompleter(fnames)
|
|
||||||
if multiline_input:
|
|
||||||
show = ". "
|
|
||||||
else:
|
|
||||||
show = "> "
|
|
||||||
|
|
||||||
try:
|
|
||||||
line = prompt(
|
|
||||||
show,
|
|
||||||
completer=completer_instance,
|
|
||||||
history=FileHistory(history_file),
|
|
||||||
style=style,
|
|
||||||
)
|
|
||||||
except EOFError:
|
|
||||||
return
|
|
||||||
if line.strip() == "{" and not multiline_input:
|
|
||||||
multiline_input = True
|
|
||||||
continue
|
|
||||||
elif line.strip() == "}" and multiline_input:
|
|
||||||
break
|
|
||||||
elif multiline_input:
|
|
||||||
inp += line + "\n"
|
|
||||||
else:
|
|
||||||
inp = line
|
|
||||||
break
|
|
||||||
|
|
||||||
print()
|
|
||||||
return inp
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue