@@ -37,50 +37,52 @@ def _gitignore():
37
37
return ignored
38
38
39
39
40
- def _ctags ():
40
+ def _ctags () -> str :
41
41
"""Generate ctags for current project."""
42
42
43
43
ignored = _gitignore ()
44
44
ignored_str = " " .join ([f"--exclude='{ i } '" for i in ignored ])
45
45
46
46
shell = get_shell ()
47
47
cmd = f"ctags -R --output-format=json { ignored_str } --fields=+l+n --languages=python --python-kinds=-iv -f -"
48
- print (cmd )
49
48
ret , ctags , _ = shell .run_command (cmd )
50
49
assert ret == 0
51
50
52
- print ( "ctags:" )
51
+ output = [ "ctags:" ]
53
52
tags = []
54
53
for line in ctags .splitlines ():
55
54
try :
56
55
tags .append (json .loads (line ))
57
56
except json .JSONDecodeError :
58
- print ( " failed to parse: " , line )
57
+ output += [ " failed to parse: { line}" ]
59
58
break
60
59
61
60
files = {tag ["path" ] for tag in tags }
62
61
for file in sorted (files ):
63
62
filetags = [tag for tag in tags if tag ["path" ] == file ]
64
- print ( f" { file } " )
63
+ output += [ str ( file )]
65
64
level = 0
66
65
for tag in sorted (filetags , key = lambda x : x ["line" ]):
67
66
if tag ["kind" ] == "class" :
68
- print ( level * " " + f" class { tag ['name' ]} :{ tag ['line' ]} " )
67
+ output += [ level * " " + f" class { tag ['name' ]} :{ tag ['line' ]} " ]
69
68
level += 1
70
69
elif tag ["kind" ] == "function" :
71
70
level = 0
72
- print ( level * " " + f" def { tag ['name' ]} :{ tag ['line' ]} " )
71
+ output += [ level * " " + f" def { tag ['name' ]} :{ tag ['line' ]} " ]
73
72
elif tag ["kind" ] == "variable" :
74
73
level = 0
75
- print ( level * " " + f" { tag ['name' ]} :{ tag ['line' ]} " )
74
+ output += [ level * " " + f" { tag ['name' ]} :{ tag ['line' ]} " ]
76
75
elif tag ["kind" ] == "unknown" :
77
76
# import a as b
78
77
pass
79
78
else :
80
- print (level * " " + f" { tag ['kind' ]} { tag ['name' ]} :{ tag ['line' ]} " )
79
+ output += [
80
+ level * " " + f" { tag ['kind' ]} { tag ['name' ]} :{ tag ['line' ]} "
81
+ ]
81
82
82
- return ctags
83
+ return " \n " . join ( output )
83
84
84
85
85
86
if __name__ == "__main__" :
86
- assert _ctags ()
87
+ output = _ctags ()
88
+ print (output )
0 commit comments