diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index a2f8fc0cc..19bef991e 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -167,13 +167,13 @@ class Coder: self.find_common_root() if main_model.use_repo_map and self.repo and self.gpt_prompts.repo_content_prefix: - rm_io = io if self.verbose else None self.repo_map = RepoMap( map_tokens, self.root, self.main_model, - rm_io, + io, self.gpt_prompts.repo_content_prefix, + self.verbose, ) if self.repo_map.use_ctags: diff --git a/aider/io.py b/aider/io.py index ff3cab386..f001d63f5 100644 --- a/aider/io.py +++ b/aider/io.py @@ -141,7 +141,7 @@ class InputOutput: return f.read() except (FileNotFoundError, UnicodeError) as e: self.tool_error(str(e)) - return None + return def get_input(self, root, rel_fnames, addable_rel_fnames, commands): if self.pretty: diff --git a/aider/repomap.py b/aider/repomap.py index fab94ec27..df739c8f9 100644 --- a/aider/repomap.py +++ b/aider/repomap.py @@ -74,8 +74,10 @@ class RepoMap: main_model=models.GPT4, io=None, repo_content_prefix=None, + verbose=False, ): self.io = io + self.verbose = verbose if not root: root = os.getcwd() @@ -130,7 +132,7 @@ class RepoMap: files_listing = self.get_ranked_tags_map(chat_files, other_files) if files_listing: num_tokens = self.token_count(files_listing) - if self.io: + if self.verbose: self.io.tool_output(f"ctags map: {num_tokens/1024:.1f} k-tokens") ctags_msg = " with selected ctags info" return files_listing, ctags_msg @@ -138,7 +140,7 @@ class RepoMap: files_listing = self.get_simple_files_map(other_files) ctags_msg = "" num_tokens = self.token_count(files_listing) - if self.io: + if self.verbose: self.io.tool_output(f"simple map: {num_tokens/1024:.1f} k-tokens") if num_tokens < self.max_map_tokens: return files_listing, ctags_msg @@ -198,7 +200,7 @@ class RepoMap: with tempfile.TemporaryDirectory() as tempdir: hello_py = os.path.join(tempdir, "hello.py") - with open(hello_py, "w") as f: + with open(hello_py, "w", encoding="utf-8") as f: f.write("def hello():\n print('Hello, world!')\n") self.run_ctags(hello_py) except FileNotFoundError: @@ -237,10 +239,8 @@ class RepoMap: return idents def get_name_identifiers_uncached(self, fname): - try: - with open(fname, "r") as f: - content = f.read() - except UnicodeDecodeError: + content = self.io.read_text(fname) + if content is None: return list() try: diff --git a/tests/test_repomap.py b/tests/test_repomap.py index cba69e2b1..03aa410f8 100644 --- a/tests/test_repomap.py +++ b/tests/test_repomap.py @@ -3,6 +3,7 @@ import tempfile import unittest from unittest.mock import patch +from aider.io import InputOutput from aider.repomap import RepoMap @@ -21,7 +22,8 @@ class TestRepoMap(unittest.TestCase): with open(os.path.join(temp_dir, file), "w") as f: f.write("") - repo_map = RepoMap(root=temp_dir) + io = InputOutput() + repo_map = RepoMap(root=temp_dir, io=io) other_files = [os.path.join(temp_dir, file) for file in test_files] result = repo_map.get_repo_map([], other_files) @@ -65,7 +67,8 @@ print(my_function(3, 4)) with open(os.path.join(temp_dir, test_file3), "w") as f: f.write(file_content3) - repo_map = RepoMap(root=temp_dir) + io = InputOutput() + repo_map = RepoMap(root=temp_dir, io=io) other_files = [ os.path.join(temp_dir, test_file1), os.path.join(temp_dir, test_file2), @@ -83,7 +86,7 @@ print(my_function(3, 4)) def test_check_for_ctags_failure(self): with patch("subprocess.run") as mock_run: mock_run.side_effect = Exception("ctags not found") - repo_map = RepoMap() + repo_map = RepoMap(io=InputOutput()) self.assertFalse(repo_map.has_ctags) def test_check_for_ctags_success(self): @@ -100,7 +103,7 @@ print(my_function(3, 4)) b' status = main()$/", "kind": "variable"}' ), ] - repo_map = RepoMap() + repo_map = RepoMap(io=InputOutput()) self.assertTrue(repo_map.has_ctags) def test_get_repo_map_without_ctags(self): @@ -120,7 +123,7 @@ print(my_function(3, 4)) with open(os.path.join(temp_dir, file), "w") as f: f.write("") - repo_map = RepoMap(root=temp_dir) + repo_map = RepoMap(root=temp_dir, io=InputOutput()) repo_map.has_ctags = False # force it off other_files = [os.path.join(temp_dir, file) for file in test_files]