1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-10 08:52:39 +01:00

Fix filter/sort order of operations for british spellings

Summary: Currently, if we match "alnd" to "land" and "amend" equally (distance 2), we drop "land" with the other part of the rule. Stop doing that.

Test Plan:
Unit tests. Also:

```
$ arc alnd
Usage Exception: Unknown command 'alnd'. Try 'arc help'.

Did you mean:
    amend
    land
```

Reviewers: vrana

Reviewed By: vrana

CC: aran

Differential Revision: https://secure.phabricator.com/D5033
This commit is contained in:
epriestley 2013-02-21 10:15:20 -08:00
parent 5e7a458053
commit e96db54125
3 changed files with 59 additions and 3 deletions

View file

@ -23,6 +23,7 @@ phutil_register_library_map(array(
'ArcanistBaseXHPASTLinter' => 'lint/linter/ArcanistBaseXHPASTLinter.php',
'ArcanistBookmarkWorkflow' => 'workflow/ArcanistBookmarkWorkflow.php',
'ArcanistBranchWorkflow' => 'workflow/ArcanistBranchWorkflow.php',
'ArcanistBritishTestCase' => 'configuration/__tests__/ArcanistBritishTestCase.php',
'ArcanistBrowseWorkflow' => 'workflow/ArcanistBrowseWorkflow.php',
'ArcanistBundle' => 'parser/ArcanistBundle.php',
'ArcanistBundleTestCase' => 'parser/__tests__/ArcanistBundleTestCase.php',
@ -181,6 +182,7 @@ phutil_register_library_map(array(
'ArcanistBaseXHPASTLinter' => 'ArcanistLinter',
'ArcanistBookmarkWorkflow' => 'ArcanistFeatureWorkflow',
'ArcanistBranchWorkflow' => 'ArcanistFeatureWorkflow',
'ArcanistBritishTestCase' => 'ArcanistTestCase',
'ArcanistBrowseWorkflow' => 'ArcanistBaseWorkflow',
'ArcanistBundleTestCase' => 'ArcanistTestCase',
'ArcanistCallConduitWorkflow' => 'ArcanistBaseWorkflow',

View file

@ -152,7 +152,7 @@ class ArcanistConfiguration {
// We haven't found a real command, alias, or unique prefix. Try similar
// spellings.
$corrected = $this->correctCommandSpelling($command, $all, 2);
$corrected = self::correctCommandSpelling($command, $all, 2);
if (count($corrected) == 1) {
$console->writeErr(
pht(
@ -191,7 +191,7 @@ class ArcanistConfiguration {
return array_keys($is_prefix);
}
private function correctCommandSpelling(
public static function correctCommandSpelling(
$command,
array $options,
$max_distance) {
@ -204,7 +204,21 @@ class ArcanistConfiguration {
asort($distances);
$best = min($max_distance, reset($distances));
foreach ($distances as $option => $distance) {
if ($distance > $best || strlen($option) <= 2 * $distance) {
if ($distance > $best) {
unset($distances[$option]);
}
}
// Before filtering, check if we have multiple equidistant matches and
// return them if we do. This prevents us from, e.g., matching "alnd" with
// both "land" and "amend", then dropping "land" for being too short, and
// incorrectly completing to "amend".
if (count($distances) > 1) {
return array_keys($distances);
}
foreach ($distances as $option => $distance) {
if (strlen($option) <= 2 * $distance) {
unset($distances[$option]);
}
}

View file

@ -0,0 +1,40 @@
<?php
final class ArcanistBritishTestCase extends ArcanistTestCase {
public function testCompletion() {
$this->assertCompletion(
array('land', 'amend'),
'alnd',
array('land', 'amend'));
$this->assertCompletion(
array('branch'),
'brnach',
array('branch', 'browse'));
$this->assertCompletion(
array(),
'test',
array('list', 'unit'));
}
private function assertCompletion($expect, $input, $commands) {
$result = ArcanistConfiguration::correctCommandSpelling(
$input,
$commands,
2);
sort($result);
sort($expect);
$commands = implode(', ', $commands);
$this->assertEqual(
$expect,
$result,
"Correction of {$input} against: {$commands}");
}
}