TextMate Jump To Function Declaration


Many IDEs have the capability to 'jump' to a function declaration within the project you are working in. This is how to do it in TextMate. Assuming you understand bundles the Bash script below should be placed in a Command with output set to Show As Tool Tip, then finally picking the key combination you want.

Once ready simply press the key combination while the Caret is placed over your function. The script below will iterate through PHP related files look for the declaration, then opening a TextMate document at the proper line. When this script fails a tooltip mention so will be displayed.

FUNC="$TM_CURRENT_WORD"
DIR="$TM_PROJECT_DIRECTORY"
OUTPUT=''

FILES=(`find "$DIR" -type f | egrep '\.(module|inc|php|engine|install)$'`)

#
# Look for a function declaration within a files contents.
#
# <file> <function>
#
function lookup_function {
  local line=`nl -b a "$1" | grep 'function '"$2"'(' | awk '{print $1}'`
  if [[ "$line" -gt 0 ]]; then
    mate "$1" -l "$line"
    exit 0
  fi
}

# Iterate files
for (( i=0; i < ${#FILES[*]}; i++)); do
  file="${FILES[${i}]}"
  lookup_function "$file" "$FUNC"
done

# Nothing found
echo 'Function '${FUNC}' was not found within the current project.'

Comments

I had to modify the f() to work with my php (cakephp) projects, and I also updated it to scan only files containing phrases "function" and "php".

FUNC="$TM_CURRENT_WORD"
DIR="$TM_PROJECT_DIRECTORY"
OUTPUT=''

FILES=(`find "$DIR" -type f -not -iname "*.js" | egrep '\.(php|function)$'`)

#
# Look for a function declaration within a files contents.
#
function lookup_function {
local line=`nl -b a "$1" | grep 'function '"$2"'(' | awk '{print $1}'`
if [[ "$line" -gt 0 ]]; then
mate "$1" -l "$line"
exit 0
fi
}

# Iterate files
for (( i=0; i < ${#FILES[*]}; i++)); do
file="${FILES[${i}]}"
lookup_function "$file" "$FUNC"
done

# Nothing found
echo 'Function '${FUNC}' was not found within the current project. '
echo 'Folder searched:' ${DIR}

Dude, this is incredible. Thanks so much. Not having this functionality was the only reason I was considering switching to an IDE.

I was so puzzled to find out TextMate does not have this functionality build in. You should post it to the folks developing TM as it's a must-have feature.
Genius man!

Thanks for sharing this. Huge time-saver.

Thanks, was looking for this! It's a bit slow in big projects, but I'll try to add some caching, so once it found the correct line once, it will be saved in a cache. Thanks!!