aider: Added methods to load and save tags cache to .aider.tags.cache file.

# Context:
USER: persist the tags cache to .aider.tags.cache
ASSISTANT: To persist the tags cache to `.aider.tags.cache`, you need to modify the `aider/repomap.py` file. Add two new methods `load_tags_cache` and `save_tags_cache` to the `RepoMap` class, and call them in the `__init__` method and `run_ctags` method, respectively.

Here are the changes you need to make:

```python
aider/repomap.py
<<<<<<< ORIGINAL
    TAGS_CACHE_FILE = ".aider.tags.cache"
=======
    TAGS_CACHE_FILE = ".aider.tags.cache"

    def load_tags_cache(self):
        if os.path.exists(self.TAGS_CACHE_FILE):
            with open(self.TAGS_CACHE_FILE, "r") as f:
                self.TAGS_CACHE = json.load(f)
        else:
            self.TAGS_CACHE = dict()

    def save_tags_cache(self):
        with open(self.TAGS_CACHE_FILE, "w") as f:
            json.dump(self.TAGS_CACHE, f)
>>>>>>> UPDATED
```

```python
aider/repomap.py
<<<<<<< ORIGINAL
    def __init__(self, use_ctags=None, root=None, main_model="gpt-4"):
=======
    def __init__(self, use_ctags=None, root=None, main_model="gpt-4"):
        self.load_tags_cache()
>>>>>>> UPDATED
```

```python
aider/repomap.py
<<<<<<< ORIGINAL
    def run_ctags(self, filename):
        # Check if the file is in the cache and if the modification time has not changed
        file_mtime = os.path.getmtime(filename)
        cache_key = filename
        if cache_key in self.TAGS_CACHE and self.TAGS_CACHE[cache_key]["mtime"] == file_mtime:
            return self.TAGS_CACHE[cache_key]["data"]

        cmd = self.ctags_cmd + [filename]
        output = subprocess.check_output(cmd).decode("utf-8")
        output = output.splitlines()

        data = [json.loads(line) for line in output]

        # Update the cache
        self.TAGS_CACHE[cache_key] = {"mtime": file_mtime, "data": data}
=======
    def run_ctags(self, filename):
        # Check if the file is in the cache and if the modification time has not changed
        file_mtime = os.path.getmtime(filename)
        cache_key = filename
        if cache_key in self.TAGS_CACHE and self.TAGS_CACHE[cache_key]["mtime"] == file_mtime:
            return self.TAGS_CACHE[cache_key]["data"]

        cmd = self.ctags_cmd + [filename]
        output = subprocess.check_output(cmd).decode("utf-8")
        output = output.splitlines()

        data = [json.loads(line) for line in output]

        # Update the cache
        self.TAGS_CACHE[cache_key] = {"mtime": file_mtime, "data": data}
        self.save_tags_cache()
>>>>>>> UPDATED
```

These changes will load the tags cache from the `.aider.tags.cache` file when initializing the `RepoMap` class and save the updated cache to the file after running ctags.
This commit is contained in:
Paul Gauthier 2023-05-30 16:54:36 -07:00
parent 29ca124f5f
commit d1bbb85437

View file

@ -53,11 +53,23 @@ def fname_to_components(fname, with_colon):
class RepoMap:
ctags_cmd = ["ctags", "--fields=+S", "--extras=-F", "--output-format=json"]
IDENT_CACHE_FILE = ".aider.ident.cache"
TAGS_CACHE_FILE = ".aider.tags.cache"
def load_tags_cache(self):
if os.path.exists(self.TAGS_CACHE_FILE):
with open(self.TAGS_CACHE_FILE, "r") as f:
self.TAGS_CACHE = json.load(f)
else:
self.TAGS_CACHE = dict()
def save_tags_cache(self):
with open(self.TAGS_CACHE_FILE, "w") as f:
json.dump(self.TAGS_CACHE, f)
TAGS_CACHE = None
IDENT_CACHE = None
def __init__(self, use_ctags=None, root=None, main_model="gpt-4"):
self.load_tags_cache()
if not root:
root = os.getcwd()
self.root = root
@ -157,6 +169,7 @@ class RepoMap:
# Update the cache
self.TAGS_CACHE[cache_key] = {"mtime": file_mtime, "data": data}
self.save_tags_cache()
return data
def get_tags(self, filename, files=None):