Skip to content

Commit c9c699d

Browse files
committed
Merge pull request #75 from gchaincl/master
Add autocompletion for defined functions and constants
2 parents 0ec2421 + 8a6886c commit c9c699d

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

lib/Boris/Autocompletion.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/* vim: set shiftwidth=2 expandtab softtabstop=2: */
4+
5+
namespace Boris;
6+
7+
class Autocompletion {
8+
private $symbols = [];
9+
10+
public function __construct() {
11+
}
12+
13+
public function complete($status)
14+
{
15+
$chunk = substr($status['line'], 0, $status['cursor']);
16+
17+
/**
18+
* Verify if we are in a variable context
19+
* Take the last word and check if the first character is a '$'
20+
*/
21+
$tokens = preg_split('/[^a-zA-Z_\$]/', $chunk);
22+
$current = end($tokens);
23+
if($current && $current[0] == '$') {
24+
return $this->getVariables();
25+
} else {
26+
return $this->getSymbols();
27+
}
28+
}
29+
30+
private function getSymbols() {
31+
if(empty($this->symbols)) {
32+
$this->symbols = call_user_func_array(
33+
"array_merge", array_values(get_defined_functions())
34+
);
35+
36+
$this->symbols = array_merge(
37+
$this->symbols, array_keys(get_defined_constants())
38+
);
39+
}
40+
41+
return $this->symbols;
42+
}
43+
44+
private function getVariables() {
45+
/**
46+
* TODO: Find a way to access EvalWorker context/scope to access vars
47+
*/
48+
return array('this', 'container');
49+
}
50+
51+
}

lib/Boris/ReadlineClient.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public function start($prompt, $historyFile) {
4848
$buf = '';
4949
$lineno = 1;
5050

51+
$auto = new Autocompletion();
52+
readline_completion_function(function($string, $index) use ($auto) {
53+
$info = readline_info();
54+
return $auto->complete(array(
55+
'line' => substr($info['line_buffer'], 0, $info['end']),
56+
'cursor' => $info['point']
57+
));
58+
});
59+
5160
for (;;) {
5261
$this->_clear = false;
5362
$line = readline(

0 commit comments

Comments
 (0)