Skip to content

Instantly share code, notes, and snippets.

@Xion
Last active June 4, 2016 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xion/9215741 to your computer and use it in GitHub Desktop.
Save Xion/9215741 to your computer and use it in GitHub Desktop.
List "tags" (TODO etc.) found in inline comments
#!/bin/sh
# List code "tags" (TODO et al.) present in source files within given directory
#
# :author: Karol Kuczmarski "Xion"
# :license: Public Domain
TAGS="TODO FIXME XXX"
HASH_COMMENT_LANGS="pl py rb sh"
SLASH_COMMENT_LANGS="c cc cpp cs d h hh hpp go java js rs"
COMMON_GREP_OPTIONS="--recursive --binary-files=without-match --devices=skip"
GREP_AFTER=3
main() {
local dir="${1-.}"
# use pcregrep-based function if available; otherwise fallback to regular grep
local ls_tags
if command -v pcregrep >/dev/null 2>&1 ; then
ls_tags=pcregrep_tag
else
echo >&2 "pcregrep not available -- using regular grep with --after=$GREP_AFTER"
ls_tags=grep_tag
fi
for tag in $TAGS; do
for lang in $HASH_COMMENT_LANGS; do
$ls_tags "#" "$tag" "$lang" "$dir"
done
for lang in $SLASH_COMMENT_LANGS; do
$ls_tags "//" "$tag" "$lang" "$dir"
done
done
}
grep_tag() {
local prefix="$1" ; local tag="$2" ; local lang="$3" ; local dir="$4"
grep $COMMON_GREP_OPTIONS
--include="*.$lang" --exclude-dir=".*" \
--after="$GREP_AFTER" --line-number \
"$prefix\s*$tag\b" \
"$dir"
}
pcregrep_tag() {
local prefix="$1" ; local tag="$2" ; local lang="$3" ; local dir="$4"
(
export PCREGREP_COLOR="1;32" # green text
pcregrep $COMMON_GREP_OPTIONS \
--include=".*\.$lang" --exclude-dir="\..*" \
--line-number --color --multiline \
"$prefix\\s*$tag\\b.*\n?([ \t]*$prefix.*\n)*" \
"$dir"
)
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment