aider: Added try-except block to handle ClassNotFound exception in guess_lexer_for_filename function.

# Context:
USER: Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/gauthier/Projects/aider/aider/main.py", line 74, in <module>
    status = main()
             ^^^^^^
  File "/Users/gauthier/Projects/aider/aider/main.py", line 70, in main
    coder.run()
  File "/Users/gauthier/Projects/aider/aider/coder.py", line 185, in run
    self.run_loop()
  File "/Users/gauthier/Projects/aider/aider/coder.py", line 200, in run_loop
    inp = get_input(self.history_file, self.abs_fnames, self.commands)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gauthier/Projects/aider/aider/getinput.py", line 92, in get_input
    completer_instance = FileContentCompleter(fnames, commands)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gauthier/Projects/aider/aider/getinput.py", line 24, in __init__
    lexer = guess_lexer_for_filename(fname, content)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gauthier/Projects/aider/.venv/lib/python3.11/site-packages/pygments/lexers/__init__.py", line 282, in guess_lexer_for_filename
    raise ClassNotFound('no lexer for filename %r found' % fn)
pygments.util.ClassNotFound: no lexer for filename '.flake8' found

ASSISTANT: It seems that the `guess_lexer_for_filename` function from Pygments is raising a `ClassNotFound` exception because it cannot find a lexer for the '.flake8' file. To fix this issue, you can add a try-except block around the `guess_lexer_for_filename` function call to handle the exception and continue processing other files.

Here's the change you need to make in the `aider/getinput.py` file:

```python
aider/getinput.py
<<<<<<< ORIGINAL
            lexer = guess_lexer_for_filename(fname, content)
=======
            try:
                lexer = guess_lexer_for_filename(fname, content)
            except ClassNotFound:
                continue
>>>>>>> UPDATED
```

This change will catch the `ClassNotFound` exception and continue processing the remaining files without raising an error.
This commit is contained in:
Paul Gauthier 2023-05-10 21:19:03 -07:00
parent a4c58cf106
commit 22b151fdd0

View file

@ -21,7 +21,10 @@ class FileContentCompleter(Completer):
for fname in fnames:
with open(fname, "r") as f:
content = f.read()
lexer = guess_lexer_for_filename(fname, content)
try:
lexer = guess_lexer_for_filename(fname, content)
except ClassNotFound:
continue
tokens = list(lexer.get_tokens(content))
self.words.update(token[1] for token in tokens if token[0] in Token.Name)