#!/usr/bin/env bash

function header() {
	tput setaf 3
	tput bold
	echo ------------------------------------
	echo "$1"
	echo ------------------------------------
	tput sgr0
}

function info() {
	echo "$1"
}

function note() {
	tput setaf 2
	echo "$1"
	tput sgr0
}

function grep_tree() {
	tput setaf 6
	tree-grepper --query php "$1" | tee -a "$QUICKFIX"
	tput sgr0
}

function grep_fn_def() {
	declare l=""
	for n in "$@";do
		l="$l \"$n\""
	done

	note "Query finds function declarations of names $l"
	grep_tree "((function_definition (name) @name) (#any-of? @name $l))";
}

function grep_fn_call() {
	declare l=""
	for n in "$@";do
		l="$l \"$n\""
	done

	note "Query finds function calls of names $l"
	grep_tree "(function_call_expression
		function: (name) @name
		(#any-of? @name $l))
	"
}

function greplike_fn_call() {
	note "Query finds function calls of name like $1"
	grep_tree "(
		(function_call_expression function: (name) @name)
		(#match? @name \"$1\")
	)";
}

function greplike_constant() {
	note "Query finds constant symbols of name like $1"
	grep_tree "((name) @name (#match? @name \"$1\"))"
}

function grep_constant() {
	declare l=""
	for n in "$@";do
		l="$l \"$n\""
	done

	note "Query finds constant symbols of names $l"
	grep_tree "((name) @name (#any-of? @name $l))"
}

function grep_fn_call_2args() {
	declare l=""
	for n in "$@";do
		l="$l \"$n\""
	done

	note "Query finds function calls of names $l with two arguments"
	grep_tree "(function_call_expression function: (name) @name arguments: (arguments (_) (_)) (#any-of? @name $l))"
}

function grep_fn_call_3args() {
	declare l=""
	for n in "$@";do
		l="$l \"$n\""
	done

	note "Query finds function calls of names $l with three arguments"
	grep_tree "(function_call_expression function: (name) @name arguments: (arguments (_) (_) (_)) (#any-of? @name $l))"
}
