Skip to content

Suggestion: lookup for a function inside project directory #71

@feekApp

Description

@feekApp

Hi,
herewith a bash script in order to lookup for a function definition inside a project folder.
Using this script you can directly jump to a function!
Regards,
Feek

#!/bin/bash
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"

function_name="$TM_CURRENT_WORD"
project_dir="$TM_PROJECT_DIRECTORY"

# find php-related files in project directory, result is a string
files_string=`find "$project_dir" -type f | egrep '\.(module|inc|php|engine|install)$'`

# place multi-line sting in a array, works for file names with spaces!
files_array=()
while read -r line; do
   files_array+=("$line")
done <<< "$files_string"

#
# Lookup for a function declaration inside the file content.
# multi space allowed in function declaration
# <file> <function>
#
function lookup_function {
  local line=`nl -b a "$1" | grep 'function\\s\+'"$2"'\\s*(' | awk '{print $1}'`
  if [[ "$line" -gt 0 ]]; then
    mate "$1" -l "$line"
    exit 0
  fi
}

# Iterate the array with files, works for files with spaces!
for (( i=0; i < ${#files_array[*]}; i++)); do
  file="${files_array[${i}]}"
  lookup_function "$file" "$function_name"
done

# Nothing found
echo 'NOTE: Function '${function_name}' was not found within the current project directory.'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions