mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-24 22:34:59 +00:00
Refactored linting process and error handling in commands.
This commit is contained in:
parent
deb13c060c
commit
9c28bbc98e
5 changed files with 64 additions and 29 deletions
|
@ -82,11 +82,20 @@ class Coder:
|
|||
)
|
||||
|
||||
if not main_model:
|
||||
if from_coder:
|
||||
main_model = from_coder.main_model
|
||||
else:
|
||||
main_model = models.Model(models.DEFAULT_MODEL_NAME)
|
||||
|
||||
if edit_format is None:
|
||||
if from_coder:
|
||||
edit_format = from_coder.edit_format
|
||||
else:
|
||||
edit_format = main_model.edit_format
|
||||
|
||||
if not io and from_coder:
|
||||
io = from_coder.io
|
||||
|
||||
if from_coder:
|
||||
use_kwargs = dict(from_coder.original_kwargs) # copy orig kwargs
|
||||
|
||||
|
@ -125,6 +134,9 @@ class Coder:
|
|||
|
||||
return res
|
||||
|
||||
def clone(self, **kwargs):
|
||||
return Coder.create(from_coder=self, **kwargs)
|
||||
|
||||
def get_announcements(self):
|
||||
lines = []
|
||||
lines.append(f"Aider v{__version__}")
|
||||
|
|
|
@ -153,40 +153,51 @@ class Commands:
|
|||
commit_message = args.strip()
|
||||
self.coder.repo.commit(message=commit_message)
|
||||
|
||||
def cmd_lint(self, args):
|
||||
def cmd_lint(self, args="", fnames=None):
|
||||
"Commit, run the linter on all dirty files, fix problems and commit again"
|
||||
|
||||
if not self.coder.repo:
|
||||
self.io.tool_error("No git repository found.")
|
||||
return
|
||||
|
||||
if not self.coder.repo.is_dirty():
|
||||
self.io.tool_error("No more changes to commit.")
|
||||
if not fnames:
|
||||
fnames = self.coder.repo.get_dirty_files()
|
||||
|
||||
if not fnames:
|
||||
self.io.tool_error("No dirty files to lint.")
|
||||
return
|
||||
|
||||
fnames = self.coder.repo.get_dirty_files()
|
||||
linted = False
|
||||
lint_coder = None
|
||||
for fname in fnames:
|
||||
try:
|
||||
errors = self.coder.linter.lint(fname, cmd=args)
|
||||
linted = True
|
||||
except FileNotFoundError as err:
|
||||
self.io.tool_error(f"Unable to lint {fname}")
|
||||
self.io.tool_error(str(err))
|
||||
continue
|
||||
|
||||
if errors:
|
||||
if not errors:
|
||||
continue
|
||||
|
||||
# Commit everything before we start fixing lint errors
|
||||
if self.coder.repo.is_dirty():
|
||||
self.cmd_commit("")
|
||||
|
||||
self.io.tool_error(errors)
|
||||
|
||||
abs_file_path = self.coder.abs_root_path(fname)
|
||||
self.coder.abs_fnames.add(abs_file_path)
|
||||
self.coder.run(errors)
|
||||
if not lint_coder:
|
||||
lint_coder = self.coder.clone(
|
||||
# Clear the chat history, fnames
|
||||
cur_messages=[],
|
||||
done_messages=[],
|
||||
fnames=None,
|
||||
)
|
||||
|
||||
if linted and self.coder.repo.is_dirty():
|
||||
lint_coder.add_rel_fname(fname)
|
||||
lint_coder.run(errors)
|
||||
lint_coder.abs_fnames = set()
|
||||
|
||||
if lint_coder and self.coder.repo.is_dirty():
|
||||
self.cmd_commit("")
|
||||
|
||||
def cmd_clear(self, args):
|
||||
|
|
|
@ -325,7 +325,7 @@ class InputOutput:
|
|||
|
||||
return res
|
||||
|
||||
def tool_error(self, message, strip=True):
|
||||
def tool_error(self, message="", strip=True):
|
||||
self.num_error_outputs += 1
|
||||
|
||||
if message.strip():
|
||||
|
|
|
@ -13,7 +13,6 @@ from tree_sitter_languages import get_parser # noqa: E402
|
|||
warnings.simplefilter("ignore", category=FutureWarning)
|
||||
|
||||
|
||||
|
||||
class Linter:
|
||||
def __init__(self, encoding="utf-8", root=None):
|
||||
self.encoding = encoding
|
||||
|
@ -22,9 +21,14 @@ class Linter:
|
|||
self.languages = dict(
|
||||
python=self.py_lint,
|
||||
)
|
||||
self.all_lint_cmd = None
|
||||
|
||||
def set_linter(self, lang, cmd):
|
||||
if lang:
|
||||
self.languages[lang] = cmd
|
||||
return
|
||||
|
||||
self.all_lint_cmd = cmd
|
||||
|
||||
def get_rel_fname(self, fname):
|
||||
if self.root:
|
||||
|
@ -66,6 +70,9 @@ class Linter:
|
|||
lang = filename_to_lang(fname)
|
||||
if not lang:
|
||||
return
|
||||
if self.all_lint_cmd:
|
||||
cmd = self.all_lint_cmd
|
||||
else:
|
||||
cmd = self.languages.get(lang)
|
||||
|
||||
if callable(cmd):
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import configparser
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
@ -181,14 +182,18 @@ def parse_lint_cmds(lint_cmds, io):
|
|||
err = False
|
||||
res = dict()
|
||||
for lint_cmd in lint_cmds:
|
||||
if re.match(r"^[a-z]+:.*", lint_cmd):
|
||||
pieces = lint_cmd.split(":")
|
||||
lang = pieces[0]
|
||||
cmd = lint_cmd[len(lang) + 1 :]
|
||||
|
||||
lang = lang.strip()
|
||||
else:
|
||||
lang = None
|
||||
cmd = lint_cmd
|
||||
|
||||
cmd = cmd.strip()
|
||||
|
||||
if lang and cmd:
|
||||
if cmd:
|
||||
res[lang] = cmd
|
||||
else:
|
||||
io.tool_error(f'Unable to parse --lint-cmd "{lint_cmd}"')
|
||||
|
@ -383,11 +388,11 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F
|
|||
return
|
||||
|
||||
if args.commit:
|
||||
coder.commands.cmd_commit("")
|
||||
coder.commands.cmd_commit()
|
||||
return
|
||||
|
||||
if args.lint:
|
||||
coder.commands.cmd_lint("")
|
||||
coder.commands.cmd_lint(fnames=fnames)
|
||||
return
|
||||
|
||||
if args.test:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue