-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagts
executable file
·98 lines (89 loc) · 4.25 KB
/
tagts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash
die() { printf %s "${@+$@$'\n'}" 1>&2 ; exit 1 ; }
see() ( { set -x; } 2>/dev/null ; "$@" )
# keep in sync with %~dp0/tagr !!!
# eXclude .gitignored files per https://github.com/universal-ctags/ctags/issues/218#issuecomment-377717588
TAGS_FIELDS=+K+z+S+l+n
# K Kind of tag as full name
# z Include the "kind:" key in kind field (use k or K) in tags output
# S Signature of routine (e.g. prototype or parameter list)
# l Language of input file containing tag
# n Line number of tag definition
TAGS_EXTRAS=+f
# f Include [a tag record] for the base file name of every input file
TAGS_FNM_BASE=.k_edit_tags
RAW_TAGS="${TAGS_FNM_BASE}.original"
TS_TAGGER_UNIQUE_TAGS="${TAGS_FNM_BASE}.ts_tagger_unique"
CTAGS_CLI=( # https://docs.ctags.io/en/latest/man/ctags.1.html
ctags
-o "${RAW_TAGS}" # specify output file
# --totals=yes # print statistics about input and output
--tag-relative=yes # use paths relative to the tag file
--excmd=number # what's in the `excmd` field: [line] number(s), ex-pattern, or combined (both?) or 'mixed' (either?)
# here need to stick with "number" because "pattern"'s unpredictability will make de-dup'ing vs ts_tagger output impossible.
--fields="$TAGS_FIELDS" # specify extra fields to include in output
--extras="$TAGS_EXTRAS" # include extra tag entries
--langmap=typescript:.ts.tsx # treat .tsx files as ts=typescript
--links=no # don't follow symlinks
# "-<lang>" means ignore that language's source files
--languages=-Markdown,-JSON,-Man,-HTML,-Asciidoc,-Autoconf,-Automake,-CSS
# --verbose # print debug messages
)
# | tee /dev/stderr
#
rg() {
local ts_tagger_ofnm="${TAGS_FNM_BASE}_ts_tags.txt" files="${TAGS_FNM_BASE}_files"
command rg --files > "$files"
addTsOnly() {
if [[ -d "$1" ]]; then
echo "adding $1"
find "$1" -type f -name '*.ts' ! -name '*.d.ts' >> "$files"
fi
}
addAnyTs() {
if [[ -d "$1" ]]; then
echo "adding $1"
find "$1" -type f \( -name '*.ts' \) >> "$files"
fi
}
addAnyTsJs() {
if [[ -d "$1" ]]; then
echo "adding $1"
find "$1" -type f \( -name '*.ts' -o -name '*.js' \) >> "$files"
fi
}
addTsOnly 'node_modules/openai/'
addAnyTsJs 'node_modules/@openai/'
addAnyTs 'node_modules/@anthropic-ai/sdk/'
( # run the following two commands in parallel and wait for both to complete
grep -P '\.tsx?$' "$files" | ts_tagger | LC_ALL=C sort > "$ts_tagger_ofnm" &
"${CTAGS_CLI[@]}" -L "$files" "$@" 2>&1 | grep -vF 'ctags: Notice: ignoring null tag' &
wait
)
local ctags_cnt tts_cnt merged_cnt ts_tagger_unique_tags_cnt uniq_add_cnt
ctags_cnt="$(<"$RAW_TAGS" wc -l)" tts_cnt="$(<"$ts_tagger_ofnm" wc -l)"
echo "tagts: $ctags_cnt tags in ctags output; $RAW_TAGS"
echo "tagts: $tts_cnt tags in ts_tagger output; $ts_tagger_ofnm"
cat "$RAW_TAGS" "$ts_tagger_ofnm" | LC_ALL=C sort -u | tagts_dedup > "$TAGS_FNM_BASE"
# ??? grep -vF 'anonymousObject'
merged_cnt="$(<"$TAGS_FNM_BASE" wc -l)"
grep -vFf "$RAW_TAGS" "$ts_tagger_ofnm" | perl -F'\t' -ane 'print $F[0], "\n"' | LC_ALL=C sort > "$TS_TAGGER_UNIQUE_TAGS"
ts_tagger_unique_tags_cnt="$(<"$TS_TAGGER_UNIQUE_TAGS" wc -l)"
local diffo='' diff=$((ctags_cnt + tts_cnt - merged_cnt))
if (( diff > 0 )); then
uniq_add_cnt=$((merged_cnt - ctags_cnt))
diffo=" (rmvd $diff dup tags; ts_tagger added $uniq_add_cnt unique tags, see $TS_TAGGER_UNIQUE_TAGS"
# if (( ts_tagger_unique_tags_cnt != uniq_add_cnt )); then
# die "FAILURE $ts_tagger_unique_tags_cnt == $uniq_add_cnt $TS_TAGGER_UNIQUE_TAGS"
# fi
fi
echo "tagts: $merged_cnt tags in merged output $TAGS_FNM_BASE$diffo"
if command -v ts_tagger_spot_chk > /dev/null; then ts_tagger_spot_chk "$TAGS_FNM_BASE"; fi
}
all() { see "${CTAGS_CLI[@]}" --quiet=yes "$@" --recurse ; }
one() { see "${CTAGS_CLI[@]}" --quiet=yes "$1" ; }
fxnm=rg # default subcommand
if [[ "$1" ]]; then
fxnm="$1" ; shift
fi
"${fxnm}" "$@"