Skip to content

Commit c2e02f8

Browse files
committed
Added autocomplete support
1 parent 0e3f944 commit c2e02f8

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/PHPCR/Shell/Console/Application/Shell.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ private function autocompleter($text)
106106
$info = readline_info();
107107
$text = substr($info['line_buffer'], 0, $info['end']);
108108

109+
$list = $this->application->getHelperSet()->get('phpcr')->getSession()->autocomplete($text);
110+
return $list;
111+
112+
109113
if ($info['point'] !== $info['end']) {
110-
return true;
114+
return false;
111115
}
112116

113117
// task name?
@@ -119,12 +123,12 @@ private function autocompleter($text)
119123
try {
120124
$command = $this->application->find(substr($text, 0, strpos($text, ' ')));
121125
} catch (\Exception $e) {
122-
return true;
126+
return false;
123127
}
124128

125129
$list = array('--help');
126130
foreach ($command->getDefinition()->getOptions() as $option) {
127-
$list[] = '--'.$option->getName();
131+
$list[] = '--' . $option->getName();
128132
}
129133

130134
return $list;

src/PHPCR/Shell/Console/Application/ShellApplication.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public function __construct(SessionInterface $session)
4545
->setName('ls')
4646
->setDescription('Alias for dump')
4747
);
48-
$this->get('ls')
49-
->getDefinition()->getArgument('identifier')->setDefault(null);
48+
$ls = $this->get('ls');
49+
$ls->getDefinition()->getArgument('identifier')->setDefault(null);
50+
5051
$this->add($this->wrap(new NodeMoveCommand())
5152
->setName('mv')
5253
);

src/PHPCR/Shell/PhpcrSession.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use PHPCR\SessionInterface;
66
use PHPCR\CredentialsInterface;
7+
use PHPCR\Util\PathHelper;
8+
use PHPCR\PathNotFoundException;
79

810
class PhpcrSession implements SessionInterface
911
{
@@ -20,6 +22,48 @@ public function setCwd($cwd)
2022
$this->cwd = $cwd;
2123
}
2224

25+
public function autocomplete($text)
26+
{
27+
// get last string
28+
if (!preg_match('&^(.+) &', $text, $matches)) {
29+
return false;
30+
}
31+
32+
$path = $matches[1];
33+
34+
if (substr($path, 0, 1) == '/') {
35+
$parentPath = PathHelper::getParentPath($path);
36+
try {
37+
$node = $this->getNode($parentPath);
38+
$list = array();
39+
foreach ($node->getNodes() as $path => $node) {
40+
$list[] = substr($parentPath, 1) . '/' . $path;
41+
}
42+
43+
return $list;
44+
} catch (PathNotFoundException $e) {
45+
return false;
46+
}
47+
} else {
48+
$cwd = $this->getCwd();
49+
try {
50+
$node = $this->getNode($cwd);
51+
$list = array();
52+
foreach ($node->getNodes() as $path => $node) {
53+
if ($this->getCwd() == '/') {
54+
$list[] = $path;
55+
} else {
56+
$list[] = $path;
57+
}
58+
}
59+
60+
return $list;
61+
} catch (PathNotFoundException $e) {
62+
return false;
63+
}
64+
}
65+
}
66+
2367
public function chdir($path)
2468
{
2569
$cwd = $this->getCwd();

0 commit comments

Comments
 (0)