From 311fc1d6e10cee5f1803401fd75507062b8df315 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Tue, 30 May 2023 16:50:56 -0700 Subject: [PATCH] aider: Persist the `IDENT_CACHE` to `.aider.ident.cache` whenever it is updated. # Context: USER: persist the `IDENT_CACHE` to .aider.ident.cache whenever it is updated ASSISTANT: To persist the `IDENT_CACHE` to `.aider.ident.cache` whenever it is updated, we need to modify the `get_name_identifiers` method in the `RepoMap` class. Here are the changes: ```python aider/repomap.py <<<<<<< ORIGINAL def get_name_identifiers(self, fname, uniq=True): file_mtime = os.path.getmtime(fname) cache_key = fname if cache_key in self.IDENT_CACHE and self.IDENT_CACHE[cache_key]["mtime"] == file_mtime: idents = self.IDENT_CACHE[cache_key]["data"] else: idents = self.get_name_identifiers_uncached(fname) self.IDENT_CACHE[cache_key] = {"mtime": file_mtime, "data": idents} ======= import json class RepoMap: # ... IDENT_CACHE_FILE = ".aider.ident.cache" def __init__(self, use_ctags=None, root=None, main_model="gpt-4"): # ... self.load_ident_cache() def load_ident_cache(self): if os.path.exists(self.IDENT_CACHE_FILE): with open(self.IDENT_CACHE_FILE, "r") as f: self.IDENT_CACHE = json.load(f) else: self.IDENT_CACHE = dict() def save_ident_cache(self): with open(self.IDENT_CACHE_FILE, "w") as f: json.dump(self.IDENT_CACHE, f) def get_name_identifiers(self, fname, uniq=True): file_mtime = os.path.getmtime(fname) cache_key = fname if cache_key in self.IDENT_CACHE and self.IDENT_CACHE[cache_key]["mtime"] == file_mtime: idents = self.IDENT_CACHE[cache_key]["data"] else: idents = self.get_name_identifiers_uncached(fname) self.IDENT_CACHE[cache_key] = {"mtime": file_mtime, "data": idents} self.save_ident_cache() >>>>>>> UPDATED ``` This will load the `IDENT_CACHE` from the `.aider.ident.cache` file when the `RepoMap` class is initialized and save the cache to the file whenever it is updated. --- aider/repomap.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/aider/repomap.py b/aider/repomap.py index 2712d1be8..1472d3a30 100644 --- a/aider/repomap.py +++ b/aider/repomap.py @@ -206,6 +206,27 @@ class RepoMap: return False return True +import json + +class RepoMap: + # ... + IDENT_CACHE_FILE = ".aider.ident.cache" + + def __init__(self, use_ctags=None, root=None, main_model="gpt-4"): + # ... + self.load_ident_cache() + + def load_ident_cache(self): + if os.path.exists(self.IDENT_CACHE_FILE): + with open(self.IDENT_CACHE_FILE, "r") as f: + self.IDENT_CACHE = json.load(f) + else: + self.IDENT_CACHE = dict() + + def save_ident_cache(self): + with open(self.IDENT_CACHE_FILE, "w") as f: + json.dump(self.IDENT_CACHE, f) + def get_name_identifiers(self, fname, uniq=True): file_mtime = os.path.getmtime(fname) cache_key = fname @@ -214,6 +235,7 @@ class RepoMap: else: idents = self.get_name_identifiers_uncached(fname) self.IDENT_CACHE[cache_key] = {"mtime": file_mtime, "data": idents} + self.save_ident_cache() if uniq: idents = set(idents)