Differential D19089 Diff 45754 src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php
Show First 20 Lines • Show All 400 Lines • ▼ Show 20 Lines | foreach ($tokens as $token) { | ||||
$results[] = null; | $results[] = null; | ||||
$evaluate[last_key($results)] = $token; | $evaluate[last_key($results)] = $token; | ||||
} | } | ||||
} | } | ||||
$results = $this->evaluateValues($results); | $results = $this->evaluateValues($results); | ||||
foreach ($evaluate as $result_key => $function) { | foreach ($evaluate as $result_key => $function) { | ||||
$function = self::parseFunction($function); | $function = $this->parseFunction($function); | ||||
if (!$function) { | if (!$function) { | ||||
throw new PhabricatorTypeaheadInvalidTokenException(); | throw new PhabricatorTypeaheadInvalidTokenException(); | ||||
} | } | ||||
$name = $function['name']; | $name = $function['name']; | ||||
$argv = $function['argv']; | $argv = $function['argv']; | ||||
$evaluated_tokens = $this->evaluateFunction($name, array($argv)); | $evaluated_tokens = $this->evaluateFunction($name, array($argv)); | ||||
Show All 36 Lines | public static function isFunctionToken($token) { | ||||
// results immediately, before the user fully types out "members(quack)". | // results immediately, before the user fully types out "members(quack)". | ||||
return (strpos($token, '(') !== false); | return (strpos($token, '(') !== false); | ||||
} | } | ||||
/** | /** | ||||
* @task functions | * @task functions | ||||
*/ | */ | ||||
public function parseFunction($token, $allow_partial = false) { | protected function parseFunction($token, $allow_partial = false) { | ||||
$matches = null; | $matches = null; | ||||
if ($allow_partial) { | if ($allow_partial) { | ||||
$ok = preg_match('/^([^(]+)\((.*?)\)?$/', $token, $matches); | $ok = preg_match('/^([^(]+)\((.*?)\)?\z/', $token, $matches); | ||||
} else { | } else { | ||||
$ok = preg_match('/^([^(]+)\((.*)\)$/', $token, $matches); | $ok = preg_match('/^([^(]+)\((.*)\)\z/', $token, $matches); | ||||
} | } | ||||
if (!$ok) { | if (!$ok) { | ||||
if (!$allow_partial) { | |||||
throw new PhabricatorTypeaheadInvalidTokenException( | |||||
pht( | |||||
'Unable to parse function and arguments for token "%s".', | |||||
$token)); | |||||
} | |||||
return null; | return null; | ||||
} | } | ||||
$function = trim($matches[1]); | $function = trim($matches[1]); | ||||
if (!$this->canEvaluateFunction($function)) { | if (!$this->canEvaluateFunction($function)) { | ||||
if (!$allow_partial) { | |||||
throw new PhabricatorTypeaheadInvalidTokenException( | |||||
pht( | |||||
'This datasource ("%s") can not evaluate the function "%s(...)".', | |||||
get_class($this), | |||||
$function)); | |||||
} | |||||
return null; | return null; | ||||
} | } | ||||
// TODO: There is currently no way to quote characters in arguments, so | |||||
// some characters can't be argument characters. Replace this with a real | |||||
// parser once we get use cases. | |||||
$argv = $matches[2]; | |||||
$argv = trim($argv); | |||||
if (!strlen($argv)) { | |||||
$argv = array(); | |||||
} else { | |||||
$argv = preg_split('/,/', $matches[2]); | |||||
foreach ($argv as $key => $arg) { | |||||
$argv[$key] = trim($arg); | |||||
} | |||||
} | |||||
foreach ($argv as $key => $arg) { | |||||
if (self::isFunctionToken($arg)) { | |||||
$subfunction = $this->parseFunction($arg); | |||||
$results = $this->evaluateFunction( | |||||
$subfunction['name'], | |||||
array($subfunction['argv'])); | |||||
$argv[$key] = head($results); | |||||
} | |||||
} | |||||
return array( | return array( | ||||
'name' => $function, | 'name' => $function, | ||||
'argv' => array(trim($matches[2])), | 'argv' => $argv, | ||||
); | ); | ||||
} | } | ||||
/** | /** | ||||
* @task functions | * @task functions | ||||
*/ | */ | ||||
public function renderFunctionTokens($function, array $argv_list) { | public function renderFunctionTokens($function, array $argv_list) { | ||||
▲ Show 20 Lines • Show All 63 Lines • Show Last 20 Lines |