mirror of
https://github.com/Aider-AI/aider
synced 2026-05-11 09:27:01 +02:00
36d15d5f370d9e071571fcc27da7d6fe6c19aec7
self.fnames.
# Context:
USER: have prompt_toolkit autocomplete based on the contents of the files in self.fnames
ASSISTANT: To add autocompletion based on the contents of the files in `self.fnames`, you can use the `Completer` class from `prompt_toolkit`. Here's how to implement it:
1. Import the necessary classes and functions from `prompt_toolkit`.
2. Create a custom completer class that inherits from `Completer`.
3. Override the `get_completions` method to provide completions based on the contents of the files.
4. Use the custom completer when calling the `prompt` function.
Here are the changes to the code:
```python
coder.py
<<<<<<< ORIGINAL
from prompt_toolkit import prompt
=======
from prompt_toolkit import prompt
from prompt_toolkit.completion import Completer, Completion
>>>>>>> UPDATED
```
```python
coder.py
<<<<<<< ORIGINAL
class Coder:
fnames = dict()
last_modified = 0
repo = None
=======
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:
fnames = dict()
last_modified = 0
repo = None
>>>>>>> UPDATED
```
```python
coder.py
<<<<<<< ORIGINAL
line = prompt("> ")
=======
completer = FileContentCompleter(self.fnames)
line = prompt("> ", completer=completer)
>>>>>>> UPDATED
```
These changes will add autocompletion based on the contents of the files in `self.fnames`.
Description
Mirrored from GitHub
Languages
Python
80.1%
CSS
4%
Shell
4%
Scheme
3.9%
JavaScript
3.7%
Other
4.3%