From: epriestley Date: Thu, 21 Feb 2013 18:15:20 +0000 (-0800) Subject: Fix filter/sort order of operations for british spellings X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=e96db5412553c8324eda2d004b65ad39082a84c8;p=arcanist.git 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 --- diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 80cc0ad2..aaa2721c 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -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', diff --git a/src/configuration/ArcanistConfiguration.php b/src/configuration/ArcanistConfiguration.php index 0d0f4336..eed355d3 100644 --- a/src/configuration/ArcanistConfiguration.php +++ b/src/configuration/ArcanistConfiguration.php @@ -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]); } } diff --git a/src/configuration/__tests__/ArcanistBritishTestCase.php b/src/configuration/__tests__/ArcanistBritishTestCase.php new file mode 100644 index 00000000..67e219e2 --- /dev/null +++ b/src/configuration/__tests__/ArcanistBritishTestCase.php @@ -0,0 +1,40 @@ +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}"); + } + + +}