aider-ce 0.87.2.dev9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of aider-ce might be problematic. Click here for more details.

Files changed (264) hide show
  1. aider/__init__.py +20 -0
  2. aider/__main__.py +4 -0
  3. aider/_version.py +34 -0
  4. aider/analytics.py +258 -0
  5. aider/args.py +1014 -0
  6. aider/args_formatter.py +228 -0
  7. aider/change_tracker.py +133 -0
  8. aider/coders/__init__.py +36 -0
  9. aider/coders/architect_coder.py +48 -0
  10. aider/coders/architect_prompts.py +40 -0
  11. aider/coders/ask_coder.py +9 -0
  12. aider/coders/ask_prompts.py +35 -0
  13. aider/coders/base_coder.py +3013 -0
  14. aider/coders/base_prompts.py +87 -0
  15. aider/coders/chat_chunks.py +64 -0
  16. aider/coders/context_coder.py +53 -0
  17. aider/coders/context_prompts.py +75 -0
  18. aider/coders/editblock_coder.py +657 -0
  19. aider/coders/editblock_fenced_coder.py +10 -0
  20. aider/coders/editblock_fenced_prompts.py +143 -0
  21. aider/coders/editblock_func_coder.py +141 -0
  22. aider/coders/editblock_func_prompts.py +27 -0
  23. aider/coders/editblock_prompts.py +177 -0
  24. aider/coders/editor_diff_fenced_coder.py +9 -0
  25. aider/coders/editor_diff_fenced_prompts.py +11 -0
  26. aider/coders/editor_editblock_coder.py +9 -0
  27. aider/coders/editor_editblock_prompts.py +21 -0
  28. aider/coders/editor_whole_coder.py +9 -0
  29. aider/coders/editor_whole_prompts.py +12 -0
  30. aider/coders/help_coder.py +16 -0
  31. aider/coders/help_prompts.py +46 -0
  32. aider/coders/navigator_coder.py +2711 -0
  33. aider/coders/navigator_legacy_prompts.py +338 -0
  34. aider/coders/navigator_prompts.py +530 -0
  35. aider/coders/patch_coder.py +706 -0
  36. aider/coders/patch_prompts.py +161 -0
  37. aider/coders/search_replace.py +757 -0
  38. aider/coders/shell.py +37 -0
  39. aider/coders/single_wholefile_func_coder.py +102 -0
  40. aider/coders/single_wholefile_func_prompts.py +27 -0
  41. aider/coders/udiff_coder.py +429 -0
  42. aider/coders/udiff_prompts.py +117 -0
  43. aider/coders/udiff_simple.py +14 -0
  44. aider/coders/udiff_simple_prompts.py +25 -0
  45. aider/coders/wholefile_coder.py +144 -0
  46. aider/coders/wholefile_func_coder.py +134 -0
  47. aider/coders/wholefile_func_prompts.py +27 -0
  48. aider/coders/wholefile_prompts.py +70 -0
  49. aider/commands.py +1946 -0
  50. aider/copypaste.py +72 -0
  51. aider/deprecated.py +126 -0
  52. aider/diffs.py +128 -0
  53. aider/dump.py +29 -0
  54. aider/editor.py +147 -0
  55. aider/exceptions.py +107 -0
  56. aider/format_settings.py +26 -0
  57. aider/gui.py +545 -0
  58. aider/help.py +163 -0
  59. aider/help_pats.py +19 -0
  60. aider/history.py +178 -0
  61. aider/io.py +1257 -0
  62. aider/linter.py +304 -0
  63. aider/llm.py +47 -0
  64. aider/main.py +1297 -0
  65. aider/mcp/__init__.py +94 -0
  66. aider/mcp/server.py +119 -0
  67. aider/mdstream.py +243 -0
  68. aider/models.py +1344 -0
  69. aider/onboarding.py +428 -0
  70. aider/openrouter.py +129 -0
  71. aider/prompts.py +56 -0
  72. aider/queries/tree-sitter-language-pack/README.md +7 -0
  73. aider/queries/tree-sitter-language-pack/arduino-tags.scm +5 -0
  74. aider/queries/tree-sitter-language-pack/c-tags.scm +9 -0
  75. aider/queries/tree-sitter-language-pack/chatito-tags.scm +16 -0
  76. aider/queries/tree-sitter-language-pack/clojure-tags.scm +7 -0
  77. aider/queries/tree-sitter-language-pack/commonlisp-tags.scm +122 -0
  78. aider/queries/tree-sitter-language-pack/cpp-tags.scm +15 -0
  79. aider/queries/tree-sitter-language-pack/csharp-tags.scm +26 -0
  80. aider/queries/tree-sitter-language-pack/d-tags.scm +26 -0
  81. aider/queries/tree-sitter-language-pack/dart-tags.scm +92 -0
  82. aider/queries/tree-sitter-language-pack/elisp-tags.scm +5 -0
  83. aider/queries/tree-sitter-language-pack/elixir-tags.scm +54 -0
  84. aider/queries/tree-sitter-language-pack/elm-tags.scm +19 -0
  85. aider/queries/tree-sitter-language-pack/gleam-tags.scm +41 -0
  86. aider/queries/tree-sitter-language-pack/go-tags.scm +42 -0
  87. aider/queries/tree-sitter-language-pack/java-tags.scm +20 -0
  88. aider/queries/tree-sitter-language-pack/javascript-tags.scm +88 -0
  89. aider/queries/tree-sitter-language-pack/lua-tags.scm +34 -0
  90. aider/queries/tree-sitter-language-pack/matlab-tags.scm +10 -0
  91. aider/queries/tree-sitter-language-pack/ocaml-tags.scm +115 -0
  92. aider/queries/tree-sitter-language-pack/ocaml_interface-tags.scm +98 -0
  93. aider/queries/tree-sitter-language-pack/pony-tags.scm +39 -0
  94. aider/queries/tree-sitter-language-pack/properties-tags.scm +5 -0
  95. aider/queries/tree-sitter-language-pack/python-tags.scm +14 -0
  96. aider/queries/tree-sitter-language-pack/r-tags.scm +21 -0
  97. aider/queries/tree-sitter-language-pack/racket-tags.scm +12 -0
  98. aider/queries/tree-sitter-language-pack/ruby-tags.scm +64 -0
  99. aider/queries/tree-sitter-language-pack/rust-tags.scm +60 -0
  100. aider/queries/tree-sitter-language-pack/solidity-tags.scm +43 -0
  101. aider/queries/tree-sitter-language-pack/swift-tags.scm +51 -0
  102. aider/queries/tree-sitter-language-pack/udev-tags.scm +20 -0
  103. aider/queries/tree-sitter-languages/README.md +23 -0
  104. aider/queries/tree-sitter-languages/c-tags.scm +9 -0
  105. aider/queries/tree-sitter-languages/c_sharp-tags.scm +46 -0
  106. aider/queries/tree-sitter-languages/cpp-tags.scm +15 -0
  107. aider/queries/tree-sitter-languages/dart-tags.scm +91 -0
  108. aider/queries/tree-sitter-languages/elisp-tags.scm +8 -0
  109. aider/queries/tree-sitter-languages/elixir-tags.scm +54 -0
  110. aider/queries/tree-sitter-languages/elm-tags.scm +19 -0
  111. aider/queries/tree-sitter-languages/go-tags.scm +30 -0
  112. aider/queries/tree-sitter-languages/hcl-tags.scm +77 -0
  113. aider/queries/tree-sitter-languages/java-tags.scm +20 -0
  114. aider/queries/tree-sitter-languages/javascript-tags.scm +88 -0
  115. aider/queries/tree-sitter-languages/kotlin-tags.scm +27 -0
  116. aider/queries/tree-sitter-languages/matlab-tags.scm +10 -0
  117. aider/queries/tree-sitter-languages/ocaml-tags.scm +115 -0
  118. aider/queries/tree-sitter-languages/ocaml_interface-tags.scm +98 -0
  119. aider/queries/tree-sitter-languages/php-tags.scm +26 -0
  120. aider/queries/tree-sitter-languages/python-tags.scm +12 -0
  121. aider/queries/tree-sitter-languages/ql-tags.scm +26 -0
  122. aider/queries/tree-sitter-languages/ruby-tags.scm +64 -0
  123. aider/queries/tree-sitter-languages/rust-tags.scm +60 -0
  124. aider/queries/tree-sitter-languages/scala-tags.scm +65 -0
  125. aider/queries/tree-sitter-languages/typescript-tags.scm +41 -0
  126. aider/reasoning_tags.py +82 -0
  127. aider/repo.py +621 -0
  128. aider/repomap.py +988 -0
  129. aider/report.py +200 -0
  130. aider/resources/__init__.py +3 -0
  131. aider/resources/model-metadata.json +699 -0
  132. aider/resources/model-settings.yml +2046 -0
  133. aider/run_cmd.py +132 -0
  134. aider/scrape.py +284 -0
  135. aider/sendchat.py +61 -0
  136. aider/special.py +203 -0
  137. aider/tools/__init__.py +26 -0
  138. aider/tools/command.py +58 -0
  139. aider/tools/command_interactive.py +53 -0
  140. aider/tools/delete_block.py +120 -0
  141. aider/tools/delete_line.py +112 -0
  142. aider/tools/delete_lines.py +137 -0
  143. aider/tools/extract_lines.py +276 -0
  144. aider/tools/grep.py +171 -0
  145. aider/tools/indent_lines.py +155 -0
  146. aider/tools/insert_block.py +211 -0
  147. aider/tools/list_changes.py +51 -0
  148. aider/tools/ls.py +49 -0
  149. aider/tools/make_editable.py +46 -0
  150. aider/tools/make_readonly.py +29 -0
  151. aider/tools/remove.py +48 -0
  152. aider/tools/replace_all.py +77 -0
  153. aider/tools/replace_line.py +125 -0
  154. aider/tools/replace_lines.py +160 -0
  155. aider/tools/replace_text.py +125 -0
  156. aider/tools/show_numbered_context.py +101 -0
  157. aider/tools/tool_utils.py +313 -0
  158. aider/tools/undo_change.py +60 -0
  159. aider/tools/view.py +13 -0
  160. aider/tools/view_files_at_glob.py +65 -0
  161. aider/tools/view_files_matching.py +103 -0
  162. aider/tools/view_files_with_symbol.py +121 -0
  163. aider/urls.py +17 -0
  164. aider/utils.py +454 -0
  165. aider/versioncheck.py +113 -0
  166. aider/voice.py +187 -0
  167. aider/waiting.py +221 -0
  168. aider/watch.py +318 -0
  169. aider/watch_prompts.py +12 -0
  170. aider/website/Gemfile +8 -0
  171. aider/website/_includes/blame.md +162 -0
  172. aider/website/_includes/get-started.md +22 -0
  173. aider/website/_includes/help-tip.md +5 -0
  174. aider/website/_includes/help.md +24 -0
  175. aider/website/_includes/install.md +5 -0
  176. aider/website/_includes/keys.md +4 -0
  177. aider/website/_includes/model-warnings.md +67 -0
  178. aider/website/_includes/multi-line.md +22 -0
  179. aider/website/_includes/python-m-aider.md +5 -0
  180. aider/website/_includes/recording.css +228 -0
  181. aider/website/_includes/recording.md +34 -0
  182. aider/website/_includes/replit-pipx.md +9 -0
  183. aider/website/_includes/works-best.md +1 -0
  184. aider/website/_sass/custom/custom.scss +103 -0
  185. aider/website/docs/config/adv-model-settings.md +2260 -0
  186. aider/website/docs/config/aider_conf.md +548 -0
  187. aider/website/docs/config/api-keys.md +90 -0
  188. aider/website/docs/config/dotenv.md +493 -0
  189. aider/website/docs/config/editor.md +127 -0
  190. aider/website/docs/config/mcp.md +95 -0
  191. aider/website/docs/config/model-aliases.md +104 -0
  192. aider/website/docs/config/options.md +890 -0
  193. aider/website/docs/config/reasoning.md +210 -0
  194. aider/website/docs/config.md +44 -0
  195. aider/website/docs/faq.md +384 -0
  196. aider/website/docs/git.md +76 -0
  197. aider/website/docs/index.md +47 -0
  198. aider/website/docs/install/codespaces.md +39 -0
  199. aider/website/docs/install/docker.md +57 -0
  200. aider/website/docs/install/optional.md +100 -0
  201. aider/website/docs/install/replit.md +8 -0
  202. aider/website/docs/install.md +115 -0
  203. aider/website/docs/languages.md +264 -0
  204. aider/website/docs/legal/contributor-agreement.md +111 -0
  205. aider/website/docs/legal/privacy.md +104 -0
  206. aider/website/docs/llms/anthropic.md +77 -0
  207. aider/website/docs/llms/azure.md +48 -0
  208. aider/website/docs/llms/bedrock.md +132 -0
  209. aider/website/docs/llms/cohere.md +34 -0
  210. aider/website/docs/llms/deepseek.md +32 -0
  211. aider/website/docs/llms/gemini.md +49 -0
  212. aider/website/docs/llms/github.md +111 -0
  213. aider/website/docs/llms/groq.md +36 -0
  214. aider/website/docs/llms/lm-studio.md +39 -0
  215. aider/website/docs/llms/ollama.md +75 -0
  216. aider/website/docs/llms/openai-compat.md +39 -0
  217. aider/website/docs/llms/openai.md +58 -0
  218. aider/website/docs/llms/openrouter.md +78 -0
  219. aider/website/docs/llms/other.md +111 -0
  220. aider/website/docs/llms/vertex.md +50 -0
  221. aider/website/docs/llms/warnings.md +10 -0
  222. aider/website/docs/llms/xai.md +53 -0
  223. aider/website/docs/llms.md +54 -0
  224. aider/website/docs/more/analytics.md +127 -0
  225. aider/website/docs/more/edit-formats.md +116 -0
  226. aider/website/docs/more/infinite-output.md +159 -0
  227. aider/website/docs/more-info.md +8 -0
  228. aider/website/docs/recordings/auto-accept-architect.md +31 -0
  229. aider/website/docs/recordings/dont-drop-original-read-files.md +35 -0
  230. aider/website/docs/recordings/index.md +21 -0
  231. aider/website/docs/recordings/model-accepts-settings.md +69 -0
  232. aider/website/docs/recordings/tree-sitter-language-pack.md +80 -0
  233. aider/website/docs/repomap.md +112 -0
  234. aider/website/docs/scripting.md +100 -0
  235. aider/website/docs/troubleshooting/aider-not-found.md +24 -0
  236. aider/website/docs/troubleshooting/edit-errors.md +76 -0
  237. aider/website/docs/troubleshooting/imports.md +62 -0
  238. aider/website/docs/troubleshooting/models-and-keys.md +54 -0
  239. aider/website/docs/troubleshooting/support.md +79 -0
  240. aider/website/docs/troubleshooting/token-limits.md +96 -0
  241. aider/website/docs/troubleshooting/warnings.md +12 -0
  242. aider/website/docs/troubleshooting.md +11 -0
  243. aider/website/docs/usage/browser.md +57 -0
  244. aider/website/docs/usage/caching.md +49 -0
  245. aider/website/docs/usage/commands.md +133 -0
  246. aider/website/docs/usage/conventions.md +119 -0
  247. aider/website/docs/usage/copypaste.md +121 -0
  248. aider/website/docs/usage/images-urls.md +48 -0
  249. aider/website/docs/usage/lint-test.md +118 -0
  250. aider/website/docs/usage/modes.md +211 -0
  251. aider/website/docs/usage/not-code.md +179 -0
  252. aider/website/docs/usage/notifications.md +87 -0
  253. aider/website/docs/usage/tips.md +79 -0
  254. aider/website/docs/usage/tutorials.md +30 -0
  255. aider/website/docs/usage/voice.md +121 -0
  256. aider/website/docs/usage/watch.md +294 -0
  257. aider/website/docs/usage.md +102 -0
  258. aider/website/share/index.md +101 -0
  259. aider_ce-0.87.2.dev9.dist-info/METADATA +543 -0
  260. aider_ce-0.87.2.dev9.dist-info/RECORD +264 -0
  261. aider_ce-0.87.2.dev9.dist-info/WHEEL +5 -0
  262. aider_ce-0.87.2.dev9.dist-info/entry_points.txt +3 -0
  263. aider_ce-0.87.2.dev9.dist-info/licenses/LICENSE.txt +202 -0
  264. aider_ce-0.87.2.dev9.dist-info/top_level.txt +1 -0
aider/repomap.py ADDED
@@ -0,0 +1,988 @@
1
+ import math
2
+ import os
3
+ import shutil
4
+ import sqlite3
5
+ import sys
6
+ import time
7
+ import warnings
8
+ from collections import Counter, defaultdict, namedtuple
9
+ from importlib import resources
10
+ from pathlib import Path
11
+
12
+ from diskcache import Cache
13
+ from grep_ast import TreeContext, filename_to_lang
14
+ from pygments.lexers import guess_lexer_for_filename
15
+ from pygments.token import Token
16
+ from tqdm import tqdm
17
+
18
+ from aider.dump import dump
19
+ from aider.special import filter_important_files
20
+ from aider.tools.tool_utils import ToolError
21
+ from aider.waiting import Spinner
22
+
23
+ # tree_sitter is throwing a FutureWarning
24
+ warnings.simplefilter("ignore", category=FutureWarning)
25
+ from grep_ast.tsl import USING_TSL_PACK, get_language, get_parser # noqa: E402
26
+
27
+
28
+ # Define the Tag namedtuple with a default for specific_kind to maintain compatibility
29
+ # with cached entries that might have been created with the old definition
30
+ class TagBase(
31
+ namedtuple(
32
+ "TagBase",
33
+ "rel_fname fname line name kind specific_kind start_line end_line start_byte end_byte",
34
+ )
35
+ ):
36
+ __slots__ = ()
37
+
38
+ def __new__(
39
+ cls,
40
+ rel_fname,
41
+ fname,
42
+ line,
43
+ name,
44
+ kind,
45
+ specific_kind=None,
46
+ start_line=None,
47
+ end_line=None,
48
+ start_byte=None,
49
+ end_byte=None,
50
+ ):
51
+ # Provide a default value for specific_kind to handle old cached objects
52
+ return super(TagBase, cls).__new__(
53
+ cls,
54
+ rel_fname,
55
+ fname,
56
+ line,
57
+ name,
58
+ kind,
59
+ specific_kind,
60
+ start_line,
61
+ end_line,
62
+ start_byte,
63
+ end_byte,
64
+ )
65
+
66
+
67
+ Tag = TagBase
68
+
69
+
70
+ SQLITE_ERRORS = (sqlite3.OperationalError, sqlite3.DatabaseError, OSError)
71
+
72
+
73
+ CACHE_VERSION = 5
74
+ if USING_TSL_PACK:
75
+ CACHE_VERSION = 7
76
+
77
+ UPDATING_REPO_MAP_MESSAGE = "Updating repo map"
78
+
79
+
80
+ class RepoMap:
81
+ TAGS_CACHE_DIR = f".aider.tags.cache.v{CACHE_VERSION}"
82
+
83
+ warned_files = set()
84
+
85
+ # Define kinds that typically represent definitions across languages
86
+ # Used by NavigatorCoder to filter tags for the symbol outline
87
+ definition_kinds = {
88
+ "class",
89
+ "struct",
90
+ "enum",
91
+ "interface",
92
+ "trait", # Structure definitions
93
+ "function",
94
+ "method",
95
+ "constructor", # Function/method definitions
96
+ "module",
97
+ "namespace", # Module/namespace definitions
98
+ "constant",
99
+ "variable", # Top-level/class variable definitions (consider refining)
100
+ "type", # Type definitions
101
+ # Add more based on tree-sitter queries if needed
102
+ }
103
+
104
+ def __init__(
105
+ self,
106
+ map_tokens=1024,
107
+ map_cache_dir=".",
108
+ main_model=None,
109
+ io=None,
110
+ repo_content_prefix=None,
111
+ verbose=False,
112
+ max_context_window=None,
113
+ map_mul_no_files=8,
114
+ refresh="auto",
115
+ max_code_line_length=100,
116
+ ):
117
+ self.io = io
118
+ self.verbose = verbose
119
+ self.refresh = refresh
120
+
121
+ self.map_cache_dir = map_cache_dir
122
+ self.root = os.getcwd()
123
+
124
+ self.load_tags_cache()
125
+ self.cache_threshold = 0.95
126
+
127
+ self.max_map_tokens = map_tokens
128
+ self.map_mul_no_files = map_mul_no_files
129
+ self.max_context_window = max_context_window
130
+
131
+ self.max_code_line_length = max_code_line_length
132
+
133
+ self.repo_content_prefix = repo_content_prefix
134
+
135
+ self.main_model = main_model
136
+
137
+ self.tree_cache = {}
138
+ self.tree_context_cache = {}
139
+ self.map_cache = {}
140
+ self.map_processing_time = 0
141
+ self.last_map = None
142
+
143
+ if self.verbose:
144
+ self.io.tool_output(
145
+ f"RepoMap initialized with map_mul_no_files: {self.map_mul_no_files}"
146
+ )
147
+ self.io.tool_output(f"RepoMap initialized with map_cache_dir: {self.map_cache_dir}")
148
+ self.io.tool_output(f"RepoMap assumes repo root is: {self.root}")
149
+
150
+ def token_count(self, text):
151
+ len_text = len(text)
152
+ if len_text < 200:
153
+ return self.main_model.token_count(text)
154
+
155
+ lines = text.splitlines(keepends=True)
156
+ num_lines = len(lines)
157
+ step = num_lines // 100 or 1
158
+ lines = lines[::step]
159
+ sample_text = "".join(lines)
160
+ sample_tokens = self.main_model.token_count(sample_text)
161
+ est_tokens = sample_tokens / len(sample_text) * len_text
162
+ return est_tokens
163
+
164
+ def get_repo_map(
165
+ self,
166
+ chat_files,
167
+ other_files,
168
+ mentioned_fnames=None,
169
+ mentioned_idents=None,
170
+ force_refresh=False,
171
+ ):
172
+ if self.max_map_tokens <= 0:
173
+ return
174
+ if not other_files:
175
+ return
176
+ if not mentioned_fnames:
177
+ mentioned_fnames = set()
178
+ if not mentioned_idents:
179
+ mentioned_idents = set()
180
+
181
+ max_map_tokens = self.max_map_tokens
182
+
183
+ # With no files in the chat, give a bigger view of the entire repo
184
+ padding = 4096
185
+ if max_map_tokens and self.max_context_window:
186
+ target = min(
187
+ int(max_map_tokens * self.map_mul_no_files),
188
+ self.max_context_window - padding,
189
+ )
190
+ else:
191
+ target = 0
192
+ if not chat_files and self.max_context_window and target > 0:
193
+ max_map_tokens = target
194
+
195
+ try:
196
+ files_listing = self.get_ranked_tags_map(
197
+ chat_files,
198
+ other_files,
199
+ max_map_tokens,
200
+ mentioned_fnames,
201
+ mentioned_idents,
202
+ force_refresh,
203
+ )
204
+ except RecursionError:
205
+ self.io.tool_error("Disabling repo map, git repo too large?")
206
+ self.max_map_tokens = 0
207
+ return
208
+
209
+ if not files_listing:
210
+ return
211
+
212
+ if self.verbose:
213
+ num_tokens = self.token_count(files_listing)
214
+ self.io.tool_output(f"Repo-map: {num_tokens / 1024:.1f} k-tokens")
215
+
216
+ if chat_files:
217
+ other = "other "
218
+ else:
219
+ other = ""
220
+
221
+ if self.repo_content_prefix:
222
+ repo_content = self.repo_content_prefix.format(other=other)
223
+ else:
224
+ repo_content = ""
225
+
226
+ repo_content += files_listing
227
+
228
+ return repo_content
229
+
230
+ def get_rel_fname(self, fname):
231
+ try:
232
+ return os.path.relpath(fname, self.root)
233
+ except ValueError:
234
+ # Issue #1288: ValueError: path is on mount 'C:', start on mount 'D:'
235
+ # Just return the full fname.
236
+ return fname
237
+
238
+ def tags_cache_error(self, original_error=None):
239
+ """Handle SQLite errors by trying to recreate cache, falling back to dict if needed"""
240
+
241
+ if self.verbose and original_error:
242
+ self.io.tool_warning(f"Tags cache error: {str(original_error)}")
243
+
244
+ if isinstance(getattr(self, "TAGS_CACHE", None), dict):
245
+ return
246
+
247
+ path = Path(self.map_cache_dir) / self.TAGS_CACHE_DIR
248
+
249
+ # Try to recreate the cache
250
+ try:
251
+ # Delete existing cache dir
252
+ if path.exists():
253
+ shutil.rmtree(path)
254
+
255
+ # Try to create new cache
256
+ new_cache = Cache(path)
257
+
258
+ # Test that it works
259
+ test_key = "test"
260
+ new_cache[test_key] = "test"
261
+ _ = new_cache[test_key]
262
+ del new_cache[test_key]
263
+
264
+ # If we got here, the new cache works
265
+ self.TAGS_CACHE = new_cache
266
+ return
267
+
268
+ except SQLITE_ERRORS as e:
269
+ # If anything goes wrong, warn and fall back to dict
270
+ self.io.tool_warning(
271
+ f"Unable to use tags cache at {path}, falling back to memory cache"
272
+ )
273
+ if self.verbose:
274
+ self.io.tool_warning(f"Cache recreation error: {str(e)}")
275
+
276
+ self.TAGS_CACHE = dict()
277
+
278
+ def load_tags_cache(self):
279
+ path = Path(self.map_cache_dir) / self.TAGS_CACHE_DIR
280
+ try:
281
+ self.TAGS_CACHE = Cache(path)
282
+ except SQLITE_ERRORS as e:
283
+ self.tags_cache_error(e)
284
+
285
+ def save_tags_cache(self):
286
+ pass
287
+
288
+ def get_mtime(self, fname):
289
+ try:
290
+ return os.path.getmtime(fname)
291
+ except FileNotFoundError:
292
+ self.io.tool_warning(f"File not found error: {fname}")
293
+
294
+ def get_tags(self, fname, rel_fname):
295
+ # Check if the file is in the cache and if the modification time has not changed
296
+ file_mtime = self.get_mtime(fname)
297
+ if file_mtime is None:
298
+ return []
299
+
300
+ cache_key = fname
301
+ try:
302
+ val = self.TAGS_CACHE.get(cache_key) # Issue #1308
303
+ except SQLITE_ERRORS as e:
304
+ self.tags_cache_error(e)
305
+ val = self.TAGS_CACHE.get(cache_key)
306
+
307
+ if val is not None and val.get("mtime") == file_mtime:
308
+ try:
309
+ # Get the cached data
310
+ data = self.TAGS_CACHE[cache_key]["data"]
311
+
312
+ # Let our Tag class handle compatibility with old cache formats
313
+ # No need for special handling as TagBase.__new__ will supply default specific_kind
314
+
315
+ return data
316
+ except SQLITE_ERRORS as e:
317
+ self.tags_cache_error(e)
318
+ return self.TAGS_CACHE[cache_key]["data"]
319
+ except (TypeError, AttributeError) as e:
320
+ # If we hit an error related to missing fields in old cached Tag objects,
321
+ # force a cache refresh for this file
322
+ if self.verbose:
323
+ self.io.tool_warning(f"Cache format error for {fname}, refreshing: {e}")
324
+ # Return empty list to trigger cache refresh
325
+ return []
326
+
327
+ # miss!
328
+ data = list(self.get_tags_raw(fname, rel_fname))
329
+
330
+ # Update the cache
331
+ try:
332
+ self.TAGS_CACHE[cache_key] = {"mtime": file_mtime, "data": data}
333
+ self.save_tags_cache()
334
+ except SQLITE_ERRORS as e:
335
+ self.tags_cache_error(e)
336
+ self.TAGS_CACHE[cache_key] = {"mtime": file_mtime, "data": data}
337
+
338
+ return data
339
+
340
+ def get_symbol_definition_location(self, file_path, symbol_name):
341
+ """
342
+ Finds the unique definition location (start/end line) for a symbol in a file.
343
+
344
+ Args:
345
+ file_path (str): The relative path to the file.
346
+ symbol_name (str): The name of the symbol to find.
347
+
348
+ Returns:
349
+ tuple: (start_line, end_line) (0-based) if a unique definition is found.
350
+
351
+ Raises:
352
+ ToolError: If the symbol is not found, not unique, or not a definition.
353
+ """
354
+ abs_path = self.io.root_abs_path(file_path) # Assuming io has this helper or similar
355
+ rel_path = self.get_rel_fname(abs_path) # Ensure we use consistent relative path
356
+
357
+ tags = self.get_tags(abs_path, rel_path)
358
+ if not tags:
359
+ raise ToolError(f"Symbol '{symbol_name}' not found in '{file_path}' (no tags).")
360
+
361
+ definitions = []
362
+ for tag in tags:
363
+ # Check if it's a definition and the name matches
364
+ if tag.kind == "def" and tag.name == symbol_name:
365
+ # Ensure we have valid location info
366
+ if tag.start_line is not None and tag.end_line is not None and tag.start_line >= 0:
367
+ definitions.append(tag)
368
+
369
+ if not definitions:
370
+ # Check if it exists as a non-definition tag
371
+ non_defs = [tag for tag in tags if tag.name == symbol_name and tag.kind != "def"]
372
+ if non_defs:
373
+ raise ToolError(
374
+ f"Symbol '{symbol_name}' found in '{file_path}', but not as a unique definition"
375
+ f" (found as {non_defs[0].kind})."
376
+ )
377
+ else:
378
+ raise ToolError(f"Symbol '{symbol_name}' definition not found in '{file_path}'.")
379
+
380
+ if len(definitions) > 1:
381
+ # Provide more context about ambiguity if possible
382
+ lines = sorted([d.start_line + 1 for d in definitions]) # 1-based for user message
383
+ raise ToolError(
384
+ f"Symbol '{symbol_name}' is ambiguous in '{file_path}'. Found definitions on lines:"
385
+ f" {', '.join(map(str, lines))}."
386
+ )
387
+
388
+ # Unique definition found
389
+ definition_tag = definitions[0]
390
+ return definition_tag.start_line, definition_tag.end_line
391
+ # Check if the file is in the cache and if the modification time has not changed
392
+
393
+ def get_tags_raw(self, fname, rel_fname):
394
+ lang = filename_to_lang(fname)
395
+ if not lang:
396
+ return
397
+
398
+ try:
399
+ language = get_language(lang)
400
+ parser = get_parser(lang)
401
+ except Exception as err:
402
+ print(f"Skipping file {fname}: {err}")
403
+ return
404
+
405
+ query_scm = get_scm_fname(lang)
406
+ if not query_scm.exists():
407
+ return
408
+ query_scm = query_scm.read_text()
409
+
410
+ code = self.io.read_text(fname)
411
+ if not code:
412
+ return
413
+ tree = parser.parse(bytes(code, "utf-8"))
414
+
415
+ # Run the tags queries
416
+ query = language.query(query_scm)
417
+ captures = query.captures(tree.root_node)
418
+
419
+ saw = set()
420
+ if USING_TSL_PACK:
421
+ all_nodes = []
422
+ for tag, nodes in captures.items():
423
+ all_nodes += [(node, tag) for node in nodes]
424
+ else:
425
+ all_nodes = list(captures)
426
+
427
+ for node, tag in all_nodes:
428
+ if tag.startswith("name.definition."):
429
+ kind = "def"
430
+ elif tag.startswith("name.reference."):
431
+ kind = "ref"
432
+ else:
433
+ continue
434
+
435
+ saw.add(kind)
436
+
437
+ # Extract specific kind from the tag, e.g., 'function' from 'name.definition.function'
438
+ specific_kind = tag.split(".")[-1] if "." in tag else None
439
+
440
+ result = Tag(
441
+ rel_fname=rel_fname,
442
+ fname=fname,
443
+ name=node.text.decode("utf-8"),
444
+ kind=kind,
445
+ specific_kind=specific_kind,
446
+ line=node.start_point[0], # Legacy line number
447
+ start_line=node.start_point[0],
448
+ end_line=node.end_point[0],
449
+ start_byte=node.start_byte,
450
+ end_byte=node.end_byte,
451
+ )
452
+
453
+ yield result
454
+
455
+ if "ref" in saw:
456
+ return
457
+ if "def" not in saw:
458
+ return
459
+
460
+ # We saw defs, without any refs
461
+ # Some tags files only provide defs (cpp, for example)
462
+ # Use pygments to backfill refs
463
+
464
+ try:
465
+ lexer = guess_lexer_for_filename(fname, code)
466
+ except Exception: # On Windows, bad ref to time.clock which is deprecated?
467
+ # self.io.tool_error(f"Error lexing {fname}")
468
+ return
469
+
470
+ tokens = list(lexer.get_tokens(code))
471
+ tokens = [token[1] for token in tokens if token[0] in Token.Name]
472
+
473
+ for token in tokens:
474
+ yield Tag(
475
+ rel_fname=rel_fname,
476
+ fname=fname,
477
+ name=token,
478
+ kind="ref",
479
+ specific_kind="name", # Default for pygments fallback
480
+ line=-1, # Pygments doesn't give precise locations easily
481
+ start_line=-1,
482
+ end_line=-1,
483
+ start_byte=-1,
484
+ end_byte=-1,
485
+ )
486
+
487
+ def get_ranked_tags(
488
+ self, chat_fnames, other_fnames, mentioned_fnames, mentioned_idents, progress=None
489
+ ):
490
+ import networkx as nx
491
+
492
+ defines = defaultdict(set)
493
+ references = defaultdict(list)
494
+ definitions = defaultdict(set)
495
+
496
+ personalization = dict()
497
+
498
+ fnames = set(chat_fnames).union(set(other_fnames))
499
+ chat_rel_fnames = set()
500
+
501
+ fnames = sorted(fnames)
502
+
503
+ # Default personalization for unspecified files is 1/num_nodes
504
+ # https://networkx.org/documentation/stable/_modules/networkx/algorithms/link_analysis/pagerank_alg.html#pagerank
505
+ personalize = 100 / len(fnames)
506
+
507
+ try:
508
+ cache_size = len(self.TAGS_CACHE)
509
+ except SQLITE_ERRORS as e:
510
+ self.tags_cache_error(e)
511
+ cache_size = len(self.TAGS_CACHE)
512
+
513
+ if len(fnames) - cache_size > 100:
514
+ self.io.tool_output(
515
+ "Initial repo scan can be slow in larger repos, but only happens once."
516
+ )
517
+ fnames = tqdm(fnames, desc="Scanning repo")
518
+ showing_bar = True
519
+ else:
520
+ showing_bar = False
521
+
522
+ for fname in fnames:
523
+ if self.verbose:
524
+ self.io.tool_output(f"Processing {fname}")
525
+ if progress and not showing_bar:
526
+ progress(f"{UPDATING_REPO_MAP_MESSAGE}: {fname}")
527
+
528
+ try:
529
+ file_ok = Path(fname).is_file()
530
+ except OSError:
531
+ file_ok = False
532
+
533
+ if not file_ok:
534
+ if fname not in self.warned_files:
535
+ self.io.tool_warning(f"Repo-map can't include {fname}")
536
+ self.io.tool_output(
537
+ "Has it been deleted from the file system but not from git?"
538
+ )
539
+ self.warned_files.add(fname)
540
+ continue
541
+
542
+ # dump(fname)
543
+ rel_fname = self.get_rel_fname(fname)
544
+ current_pers = 0.0 # Start with 0 personalization score
545
+
546
+ if fname in chat_fnames:
547
+ current_pers += personalize
548
+ chat_rel_fnames.add(rel_fname)
549
+
550
+ if rel_fname in mentioned_fnames:
551
+ # Use max to avoid double counting if in chat_fnames and mentioned_fnames
552
+ current_pers = max(current_pers, personalize)
553
+
554
+ # Check path components against mentioned_idents
555
+ path_obj = Path(rel_fname)
556
+ path_components = set(path_obj.parts)
557
+ basename_with_ext = path_obj.name
558
+ basename_without_ext, _ = os.path.splitext(basename_with_ext)
559
+ components_to_check = path_components.union({basename_with_ext, basename_without_ext})
560
+
561
+ matched_idents = components_to_check.intersection(mentioned_idents)
562
+ if matched_idents:
563
+ # Add personalization *once* if any path component matches a mentioned ident
564
+ current_pers += personalize
565
+
566
+ if current_pers > 0:
567
+ personalization[rel_fname] = current_pers # Assign the final calculated value
568
+
569
+ tags = list(self.get_tags(fname, rel_fname))
570
+ if tags is None:
571
+ continue
572
+
573
+ for tag in tags:
574
+ if tag.kind == "def":
575
+ defines[tag.name].add(rel_fname)
576
+ key = (rel_fname, tag.name)
577
+ definitions[key].add(tag)
578
+
579
+ elif tag.kind == "ref":
580
+ references[tag.name].append(rel_fname)
581
+
582
+ ##
583
+ # dump(defines)
584
+ # dump(references)
585
+ # dump(personalization)
586
+
587
+ if not references:
588
+ references = dict((k, list(v)) for k, v in defines.items())
589
+
590
+ idents = set(defines.keys()).intersection(set(references.keys()))
591
+
592
+ G = nx.MultiDiGraph()
593
+
594
+ # Add a small self-edge for every definition that has no references
595
+ # Helps with tree-sitter 0.23.2 with ruby, where "def greet(name)"
596
+ # isn't counted as a def AND a ref. tree-sitter 0.24.0 does.
597
+ for ident in defines.keys():
598
+ if ident in references:
599
+ continue
600
+ for definer in defines[ident]:
601
+ G.add_edge(definer, definer, weight=0.1, ident=ident)
602
+
603
+ for ident in idents:
604
+ if progress:
605
+ progress(f"{UPDATING_REPO_MAP_MESSAGE}: {ident}")
606
+
607
+ definers = defines[ident]
608
+
609
+ mul = 1.0
610
+
611
+ is_snake = ("_" in ident) and any(c.isalpha() for c in ident)
612
+ is_kebab = ("-" in ident) and any(c.isalpha() for c in ident)
613
+ is_camel = any(c.isupper() for c in ident) and any(c.islower() for c in ident)
614
+ if ident in mentioned_idents:
615
+ mul *= 10
616
+ if (is_snake or is_kebab or is_camel) and len(ident) >= 8:
617
+ mul *= 10
618
+ if ident.startswith("_"):
619
+ mul *= 0.1
620
+ if len(defines[ident]) > 5:
621
+ mul *= 0.1
622
+
623
+ for referencer, num_refs in Counter(references[ident]).items():
624
+ for definer in definers:
625
+ # dump(referencer, definer, num_refs, mul)
626
+ # if referencer == definer:
627
+ # continue
628
+
629
+ use_mul = mul
630
+ if referencer in chat_rel_fnames:
631
+ use_mul *= 50
632
+
633
+ # scale down so high freq (low value) mentions don't dominate
634
+ num_refs = math.sqrt(num_refs)
635
+
636
+ G.add_edge(referencer, definer, weight=use_mul * num_refs, ident=ident)
637
+
638
+ if not references:
639
+ pass
640
+
641
+ if personalization:
642
+ pers_args = dict(personalization=personalization, dangling=personalization)
643
+ else:
644
+ pers_args = dict()
645
+
646
+ try:
647
+ ranked = nx.pagerank(G, weight="weight", **pers_args)
648
+ except ZeroDivisionError:
649
+ # Issue #1536
650
+ try:
651
+ ranked = nx.pagerank(G, weight="weight")
652
+ except ZeroDivisionError:
653
+ return []
654
+
655
+ # distribute the rank from each source node, across all of its out edges
656
+ ranked_definitions = defaultdict(float)
657
+ for src in G.nodes:
658
+ if progress:
659
+ progress(f"{UPDATING_REPO_MAP_MESSAGE}: {src}")
660
+
661
+ src_rank = ranked[src]
662
+ total_weight = sum(data["weight"] for _src, _dst, data in G.out_edges(src, data=True))
663
+ # dump(src, src_rank, total_weight)
664
+ for _src, dst, data in G.out_edges(src, data=True):
665
+ data["rank"] = src_rank * data["weight"] / total_weight
666
+ ident = data["ident"]
667
+ ranked_definitions[(dst, ident)] += data["rank"]
668
+
669
+ ranked_tags = []
670
+ ranked_definitions = sorted(
671
+ ranked_definitions.items(), reverse=True, key=lambda x: (x[1], x[0])
672
+ )
673
+
674
+ # dump(ranked_definitions)
675
+
676
+ for (fname, ident), rank in ranked_definitions:
677
+ # print(f"{rank:.03f} {fname} {ident}")
678
+ if fname in chat_rel_fnames:
679
+ continue
680
+ ranked_tags += list(definitions.get((fname, ident), []))
681
+
682
+ rel_other_fnames_without_tags = set(self.get_rel_fname(fname) for fname in other_fnames)
683
+
684
+ fnames_already_included = set(rt[0] for rt in ranked_tags)
685
+
686
+ top_rank = sorted([(rank, node) for (node, rank) in ranked.items()], reverse=True)
687
+ for rank, fname in top_rank:
688
+ if fname in rel_other_fnames_without_tags:
689
+ rel_other_fnames_without_tags.remove(fname)
690
+ if fname not in fnames_already_included:
691
+ ranked_tags.append((fname,))
692
+
693
+ for fname in rel_other_fnames_without_tags:
694
+ ranked_tags.append((fname,))
695
+
696
+ return ranked_tags
697
+
698
+ def get_ranked_tags_map(
699
+ self,
700
+ chat_fnames,
701
+ other_fnames=None,
702
+ max_map_tokens=None,
703
+ mentioned_fnames=None,
704
+ mentioned_idents=None,
705
+ force_refresh=False,
706
+ ):
707
+ # Create a cache key
708
+ cache_key = [
709
+ tuple(sorted(chat_fnames)) if chat_fnames else None,
710
+ tuple(sorted(other_fnames)) if other_fnames else None,
711
+ max_map_tokens,
712
+ ]
713
+
714
+ if self.refresh == "auto":
715
+ cache_key += [
716
+ tuple(sorted(mentioned_fnames)) if mentioned_fnames else None,
717
+ tuple(sorted(mentioned_idents)) if mentioned_idents else None,
718
+ ]
719
+ cache_key = tuple(cache_key)
720
+
721
+ use_cache = False
722
+ if not force_refresh:
723
+ if self.refresh == "manual" and self.last_map:
724
+ return self.last_map
725
+
726
+ if self.refresh == "always":
727
+ use_cache = False
728
+ elif self.refresh == "files":
729
+ use_cache = True
730
+ elif self.refresh == "auto":
731
+ use_cache = self.map_processing_time > 1.0
732
+
733
+ # Check if the result is in the cache
734
+ if use_cache and cache_key in self.map_cache:
735
+ return self.map_cache[cache_key]
736
+
737
+ # If not in cache or force_refresh is True, generate the map
738
+ start_time = time.time()
739
+ result = self.get_ranked_tags_map_uncached(
740
+ chat_fnames, other_fnames, max_map_tokens, mentioned_fnames, mentioned_idents
741
+ )
742
+ end_time = time.time()
743
+ self.map_processing_time = end_time - start_time
744
+
745
+ # Store the result in the cache
746
+ self.map_cache[cache_key] = result
747
+ self.last_map = result
748
+
749
+ return result
750
+
751
+ def get_ranked_tags_map_uncached(
752
+ self,
753
+ chat_fnames,
754
+ other_fnames=None,
755
+ max_map_tokens=None,
756
+ mentioned_fnames=None,
757
+ mentioned_idents=None,
758
+ ):
759
+ if not other_fnames:
760
+ other_fnames = list()
761
+ if not max_map_tokens:
762
+ max_map_tokens = self.max_map_tokens
763
+ if not mentioned_fnames:
764
+ mentioned_fnames = set()
765
+ if not mentioned_idents:
766
+ mentioned_idents = set()
767
+
768
+ spin = Spinner(UPDATING_REPO_MAP_MESSAGE)
769
+
770
+ ranked_tags = self.get_ranked_tags(
771
+ chat_fnames,
772
+ other_fnames,
773
+ mentioned_fnames,
774
+ mentioned_idents,
775
+ progress=spin.step,
776
+ )
777
+
778
+ other_rel_fnames = sorted(set(self.get_rel_fname(fname) for fname in other_fnames))
779
+ special_fnames = filter_important_files(other_rel_fnames)
780
+ ranked_tags_fnames = set(tag[0] for tag in ranked_tags)
781
+ special_fnames = [fn for fn in special_fnames if fn not in ranked_tags_fnames]
782
+ special_fnames = [(fn,) for fn in special_fnames]
783
+
784
+ ranked_tags = special_fnames + ranked_tags
785
+
786
+ spin.step()
787
+
788
+ num_tags = len(ranked_tags)
789
+ lower_bound = 0
790
+ upper_bound = num_tags
791
+ best_tree = None
792
+ best_tree_tokens = 0
793
+
794
+ chat_rel_fnames = set(self.get_rel_fname(fname) for fname in chat_fnames)
795
+
796
+ self.tree_cache = dict()
797
+
798
+ middle = min(int(max_map_tokens // 25), num_tags)
799
+ while lower_bound <= upper_bound:
800
+ # dump(lower_bound, middle, upper_bound)
801
+
802
+ if middle > 1500:
803
+ show_tokens = f"{middle / 1000.0:.1f}K"
804
+ else:
805
+ show_tokens = str(middle)
806
+ spin.step(f"{UPDATING_REPO_MAP_MESSAGE}: {show_tokens} tokens")
807
+
808
+ tree = self.to_tree(ranked_tags[:middle], chat_rel_fnames)
809
+ num_tokens = self.token_count(tree)
810
+
811
+ pct_err = abs(num_tokens - max_map_tokens) / max_map_tokens
812
+ ok_err = 0.15
813
+ if (num_tokens <= max_map_tokens and num_tokens > best_tree_tokens) or pct_err < ok_err:
814
+ best_tree = tree
815
+ best_tree_tokens = num_tokens
816
+
817
+ if pct_err < ok_err:
818
+ break
819
+
820
+ if num_tokens < max_map_tokens:
821
+ lower_bound = middle + 1
822
+ else:
823
+ upper_bound = middle - 1
824
+
825
+ middle = int((lower_bound + upper_bound) // 2)
826
+
827
+ spin.end()
828
+ return best_tree
829
+
830
+ tree_cache = dict()
831
+
832
+ def render_tree(self, abs_fname, rel_fname, lois):
833
+ mtime = self.get_mtime(abs_fname)
834
+ key = (rel_fname, tuple(sorted(lois)), mtime)
835
+
836
+ if key in self.tree_cache:
837
+ return self.tree_cache[key]
838
+
839
+ if (
840
+ rel_fname not in self.tree_context_cache
841
+ or self.tree_context_cache[rel_fname]["mtime"] != mtime
842
+ ):
843
+ code = self.io.read_text(abs_fname) or ""
844
+ if not code.endswith("\n"):
845
+ code += "\n"
846
+
847
+ context = TreeContext(
848
+ rel_fname,
849
+ code,
850
+ color=False,
851
+ line_number=False,
852
+ child_context=False,
853
+ last_line=False,
854
+ margin=0,
855
+ mark_lois=False,
856
+ loi_pad=0,
857
+ # header_max=30,
858
+ show_top_of_file_parent_scope=False,
859
+ )
860
+ self.tree_context_cache[rel_fname] = {"context": context, "mtime": mtime}
861
+
862
+ context = self.tree_context_cache[rel_fname]["context"]
863
+ context.lines_of_interest = set()
864
+ context.add_lines_of_interest(lois)
865
+ context.add_context()
866
+ res = context.format()
867
+ self.tree_cache[key] = res
868
+ return res
869
+
870
+ def to_tree(self, tags, chat_rel_fnames):
871
+ if not tags:
872
+ return ""
873
+
874
+ cur_fname = None
875
+ cur_abs_fname = None
876
+ lois = None
877
+ output = ""
878
+
879
+ # add a bogus tag at the end so we trip the this_fname != cur_fname...
880
+ dummy_tag = (None,)
881
+ for tag in sorted(tags) + [dummy_tag]:
882
+ this_rel_fname = tag[0]
883
+ if this_rel_fname in chat_rel_fnames:
884
+ continue
885
+
886
+ # ... here ... to output the final real entry in the list
887
+ if this_rel_fname != cur_fname:
888
+ if lois is not None:
889
+ output += "\n"
890
+ output += cur_fname + ":\n"
891
+
892
+ # truncate long lines, in case we get minified js or something else crazy
893
+ output += truncate_long_lines(
894
+ self.render_tree(cur_abs_fname, cur_fname, lois), self.max_code_line_length
895
+ )
896
+
897
+ lois = None
898
+ elif cur_fname:
899
+ output += "\n" + cur_fname + "\n"
900
+ if type(tag) is Tag:
901
+ lois = []
902
+ cur_abs_fname = tag.fname
903
+ cur_fname = this_rel_fname
904
+
905
+ if lois is not None:
906
+ lois.append(tag.line)
907
+
908
+ return output
909
+
910
+
911
+ def truncate_long_lines(text, max_length):
912
+ return "\n".join([line[:max_length] for line in text.splitlines()]) + "\n"
913
+
914
+
915
+ def find_src_files(directory):
916
+ if not os.path.isdir(directory):
917
+ return [directory]
918
+
919
+ src_files = []
920
+ for root, dirs, files in os.walk(directory):
921
+ for file in files:
922
+ src_files.append(os.path.join(root, file))
923
+ return src_files
924
+
925
+
926
+ def get_scm_fname(lang):
927
+ # Load the tags queries
928
+ if USING_TSL_PACK:
929
+ subdir = "tree-sitter-language-pack"
930
+ try:
931
+ path = resources.files(__package__).joinpath(
932
+ "queries",
933
+ subdir,
934
+ f"{lang}-tags.scm",
935
+ )
936
+ if path.exists():
937
+ return path
938
+ except KeyError:
939
+ pass
940
+
941
+ # Fall back to tree-sitter-languages
942
+ subdir = "tree-sitter-languages"
943
+ try:
944
+ return resources.files(__package__).joinpath(
945
+ "queries",
946
+ subdir,
947
+ f"{lang}-tags.scm",
948
+ )
949
+ except KeyError:
950
+ return
951
+
952
+
953
+ def get_supported_languages_md():
954
+ from grep_ast.parsers import PARSERS
955
+
956
+ res = """
957
+ | Language | File extension | Repo map | Linter |
958
+ |:--------:|:--------------:|:--------:|:------:|
959
+ """
960
+ data = sorted((lang, ex) for ex, lang in PARSERS.items())
961
+
962
+ for lang, ext in data:
963
+ fn = get_scm_fname(lang)
964
+ repo_map = "✓" if Path(fn).exists() else ""
965
+ linter_support = "✓"
966
+ res += f"| {lang:20} | {ext:20} | {repo_map:^8} | {linter_support:^6} |\n"
967
+
968
+ res += "\n"
969
+
970
+ return res
971
+
972
+
973
+ if __name__ == "__main__":
974
+ fnames = sys.argv[1:]
975
+
976
+ chat_fnames = []
977
+ other_fnames = []
978
+ for fname in sys.argv[1:]:
979
+ if Path(fname).is_dir():
980
+ chat_fnames += find_src_files(fname)
981
+ else:
982
+ chat_fnames.append(fname)
983
+
984
+ rm = RepoMap(root=".")
985
+ repo_map = rm.get_ranked_tags_map(chat_fnames, other_fnames)
986
+
987
+ dump(len(repo_map))
988
+ print(repo_map)