From cceb7c3bef2a4a5f793f10afb8a739e4b584bc69 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 19 May 2023 08:31:00 -0700 Subject: [PATCH] Refactor ctags.py to print tags with indentation. --- aider/ctags.py | 53 +++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/aider/ctags.py b/aider/ctags.py index 51ced3a9f..5cb05b31d 100644 --- a/aider/ctags.py +++ b/aider/ctags.py @@ -1,26 +1,26 @@ +import os import json import sys +from aider.dump import dump def print_tags_info(filename): - last = None tags = sorted(get_tags(filename)) + last = [None] * len(tags[0]) + tab = ' ' for tag in tags: - if last is None: - show = tag - else: - show = [] - for lst, tg in zip(last, tag): - if lst == tg: - show.append("") - else: - show.append(tg) - - if not show[-1]: - show = show[:-1] - show = "\t".join(show) - print(show) + tag = list(tag) + common_prefix = [ + tag_i for tag_i,last_i in zip(tag,last) + if tag_i == last_i + ] + num_common = len(common_prefix) + indent = tab * num_common + rest = tag[num_common:] + for item in rest: + print(indent + item) + indent += tab last = tag @@ -28,11 +28,24 @@ def get_tags(filename): with open(filename, "r") as tags_file: for line in tags_file: tag = json.loads(line) - scope = tag.get("scope", "(top)") - kind = tag.get("kind", "") - name = tag.get("name", "") - signature = tag.get("signature", "") - yield (scope, kind, name, signature) + path = tag.get("path") + scope = tag.get("scope") + kind = tag.get("kind") + name = tag.get("name") + signature = tag.get("signature") + + last = name + if signature: + last += ' ' + signature + + path = os.path.relpath(path, os.getcwd()) + + res = [] + if scope: + res.append(scope) + res += [kind, last] + + yield res if __name__ == "__main__":