Skip to content

Improve auto-completion support #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

- New command: `file:import`: - import files into the repository.
- `node:list`: Added `--level` option to rescursively show children nodes and properties
- Autocomplete completes property names in addition to node names in current path

## Improvements

- `session:export:view`: Added `--pretty` option to `session:export:view` command to output formatted XML.
- `session:export:view`: Ask confirmation before overwriting file.

## Bug Fixes
## Bugs

- Aliases: Allow quoted arguments
- Fixed autocomplete segfault
24 changes: 0 additions & 24 deletions src/PHPCR/Shell/Console/Application/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,9 @@ private function autocompleter($text)
{
$info = readline_info();
$text = substr($info['line_buffer'], 0, $info['end']);

$list = $this->application->getHelperSet()->get('phpcr')->getSession()->autocomplete($text);

return $list;

if ($info['point'] !== $info['end']) {
return false;
}

// task name?
if (false === strpos($text, ' ') || !$text) {
return array_keys($this->application->all());
}

// options and arguments?
try {
$command = $this->application->find(substr($text, 0, strpos($text, ' ')));
} catch (\Exception $e) {
return false;
}

$list = array('--help');
foreach ($command->getDefinition()->getOptions() as $option) {
$list[] = '--' . $option->getName();
}

return $list;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/PHPCR/Shell/Console/Application/ShellApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ private function configureFormatter(OutputFormatter $formatter)
$style = new OutputFormatterStyle(null, null, array('bold'));
$formatter->setStyle('node', $style);

$style = new OutputFormatterStyle('blue', null, array('bold'));
$formatter->setStyle('templatenode', $style);

$style = new OutputFormatterStyle('blue', null, array());
$formatter->setStyle('templateproperty', $style);

$style = new OutputFormatterStyle(null, null, array());
$formatter->setStyle('property', $style);

Expand Down
48 changes: 48 additions & 0 deletions src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use PHPCR\PropertyType;

class NodeListCommand extends Command
{
Expand All @@ -24,8 +25,14 @@ protected function configure()
$this->addOption('properties', null, InputOption::VALUE_NONE, 'List only the properties of this node');
$this->addOption('filter', 'f', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Optional filter to apply');
$this->addOption('level', 'L', InputOption::VALUE_REQUIRED, 'Depth of tree to show');
$this->addOption('no-template', 'T', InputOption::VALUE_REQUIRED, 'Do not show template nodes and properties');
$this->setHelp(<<<HERE
List both or one of the children and properties of this node.

Multiple levels can be shown by using the <info>--level</info> option.

The <info>node:list</info> command also shows template nodes and properties as defined a nodes node-type.
These can be suppressed using the <info>--no-template</info> option.
HERE
);
}
Expand Down Expand Up @@ -72,9 +79,20 @@ private function renderChildren($currentNode, $table, $spacers)
{
$children = $currentNode->getNodes($this->filters ? : null);

$nodeType = $currentNode->getPrimaryNodeType();
$childNodeDefinitions = $nodeType->getDeclaredChildNodeDefinitions();
$childNodeNames = array();
foreach ($childNodeDefinitions as $childNodeDefinition) {
$childNodeNames[$childNodeDefinition->getName()] = $childNodeDefinition;
}

$i = 0;
foreach ($children as $child) {
$i++;
if (isset($childNodeNames[$child->getName()])) {
unset($childNodeNames[$child->getName()]);
}

$isLast = count($children) === $i;

$table->addRow(array(
Expand All @@ -94,20 +112,50 @@ private function renderChildren($currentNode, $table, $spacers)
$this->renderNode($child, $table, $newSpacers);
}
}

// render empty schematic children
foreach ($childNodeNames as $childNodeName => $childNodeDefinition) {
// @todo: Determine and show cardinality, 1..*, *..*, 0..1, etc.
$table->addRow(array(
'<templatenode>' . implode('', $spacers) . '@' . $childNodeName . '</templatenode>',
implode('|', $childNodeDefinition->getRequiredPrimaryTypeNames()),
'',
));
}
}

private function renderProperties($currentNode, $table, $spacers)
{
$properties = $currentNode->getProperties($this->filters ? : null);

$nodeType = $currentNode->getPrimaryNodeType();
$propertyDefinitions = $nodeType->getDeclaredPropertyDefinitions();

$propertyNames = array();
foreach ($propertyDefinitions as $name => $propertyDefinition) {
$propertyNames[$propertyDefinition->getName()] = $propertyDefinition;
}

$i = 0;
foreach ($properties as $name => $property) {
$i++;
if (isset($propertyNames[$name])) {
unset($propertyNames[$name]);
}

$table->addRow(array(
'<property>' . implode('', $spacers). $name . '</property>',
'<property-type>' . $this->formatter->getPropertyTypeName($property->getType()) . '</property-type>',
$this->textHelper->truncate($this->formatter->formatValue($property), 55),
));
}

foreach ($propertyNames as $propertyName => $property) {
$table->addRow(array(
'<templateproperty>' . implode('', $spacers). '@' . $propertyName . '</templateproperty>',
'<property-type>' . strtoupper(PropertyType::nameFromValue($property->getRequiredType())) . '</property-type>',
''
));
}
}
}
47 changes: 11 additions & 36 deletions src/PHPCR/Shell/PhpcrSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,43 +48,18 @@ public function setCwd($cwd)
*/
public function autocomplete($text)
{
// get last string
if (!preg_match('&^(.+) &', $text, $matches)) {
return false;
}

$path = $matches[1];

if (substr($path, 0, 1) == '/') {
$parentPath = PathHelper::getParentPath($path);
try {
$node = $this->getNode($parentPath);
$list = array();
foreach ($node->getNodes() as $path => $node) {
$list[] = substr($parentPath, 1) . '/' . $path;
}

return $list;
} catch (PathNotFoundException $e) {
return false;
}
} else {
$cwd = $this->getCwd();
try {
$node = $this->getNode($cwd);
$list = array();
foreach ($node->getNodes() as $path => $node) {
if ($this->getCwd() == '/') {
$list[] = $path;
} else {
$list[] = $path;
}
}

return $list;
} catch (PathNotFoundException $e) {
return false;
// return autocompletions for current path
$cwd = $this->getCwd();
try {
$node = $this->getNode($cwd);
$list = (array) $node->getNodeNames();
foreach ($node->getProperties() as $name => $v) {
$list[] = $name;
}

return $list;
} catch (PathNotFoundException $e) {
return false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/PHPCR/Shell/Resources/config.dist/alias.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exit: shell:exit

# Node commands
ls: node:list {arg1}
sl: node:clone {arg1} {arg2} # symlink, as in ln -s
ln: node:clone {arg1} {arg2} # symlink, as in ln -s
cp: node:copy {arg1} {arg2}
cat: node:property:show {arg1}
touch: node:property:set {arg1} {arg2} {arg3}
Expand Down