Skip to content

Instantly share code, notes, and snippets.

@tubaman
Created August 29, 2019 18:47
Show Gist options
  • Save tubaman/07b0a587874416d102345638cdfb4136 to your computer and use it in GitHub Desktop.
Save tubaman/07b0a587874416d102345638cdfb4136 to your computer and use it in GitHub Desktop.
Build ctags/cscope automatically
#!/bin/bash
set -o noglob
function addfiles {
NAME="$1"
find $EXCLUDE -name "$NAME" >> cscope.files
}
function autoignore {
NAME="$1"
if ! [ -e .xrefignore ] || ! grep -q "$NAME" .xrefignore; then
if [ -d "$NAME" ]; then
echo "$NAME/**" >> .xrefignore
fi
else if [ -e "$NAME" ]; then
echo "$NAME" >> .xrefignore
fi
fi
}
# auto ignore certain things
#autoignore './node_modules'
# build exclude params
if [ -e .xrefignore ]; then
EXCLUDE=""
for path in $(cat .xrefignore); do
EXCLUDE="$EXCLUDE -not -path $path"
done
fi
NAMES=""
for name in "*.py" "*.cpp" "*.c" "*.h" "*.js" "*.java" "*.html" "*.proto" "*.php" "*.sh"; do
if [ -z "$NAMES" ]; then
NAMES="-name $name"
else
NAMES="$NAMES -o -name $name"
fi
done
NAMES="( $NAMES )"
find $EXCLUDE $NAMES > cscope.files
sort cscope.files > cscope.files_sorted
mv cscope.files_sorted cscope.files
# configure ctags to handle javascript
cat << EOF > .ctags
--langdef=js
--langmap=js:.js
--regex-js=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\{/\1/,object/
--regex-js=/([A-Za-z0-9._$()]+)[ \t]*[:=][ \t]*function[ \t]*\(/\1/,function/
--regex-js=/function[ \t]+([A-Za-z0-9._$]+)[ \t]*\(([^)])\)/\1/,function/
--regex-js=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\[/\1/,array/
--regex-js=/([^= ]+)[ \t]*=[ \t]*[^"]'[^']*/\1/,string/
--regex-js=/([^= ]+)[ \t]*=[ \t]*[^']"[^"]*/\1/,string/
--langdef=protobuf
--langmap=protobuf:.proto
--regex-protobuf=/message ([A-Za-z0-9._$]+)[ \t]*\{/\1/,object/
--PHP-kinds=+cf
-h .php
--regex-PHP=/abstract class ([^ ]*)/\1/c/
--regex-PHP=/interface ([^ ]*)/\1/c/
--regex-PHP=/(public |static |abstract |protected |private )+function ([^ (]*)/\2/f/
EOF
# tell git to ignore this stuff
if [ -e .git/info/exclude ]; then
if ! grep -q .xrefignore .git/info/exclude; then
echo ".xrefignore" >> .git/info/exclude
fi
if ! grep -q .ctags .git/info/exclude; then
echo ".ctags" >> .git/info/exclude
fi
if ! grep -q tags .git/info/exclude; then
echo "tags" >> .git/info/exclude
fi
if ! grep -q cscope.out .git/info/exclude; then
echo "cscope.out" >> .git/info/exclude
fi
if ! grep -q cscope.files .git/info/exclude; then
echo "cscope.files" >> .git/info/exclude
fi
fi
rm -f tags
xargs ctags -f tags --append < cscope.files
cscope -i cscope.files -f cscope.out -b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment