]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Fix filter/sort order of operations for british spellings
authorepriestley <git@epriestley.com>
Thu, 21 Feb 2013 18:15:20 +0000 (10:15 -0800)
committerepriestley <git@epriestley.com>
Thu, 21 Feb 2013 18:15:20 +0000 (10:15 -0800)
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

src/__phutil_library_map__.php
src/configuration/ArcanistConfiguration.php
src/configuration/__tests__/ArcanistBritishTestCase.php [new file with mode: 0644]

index 80cc0ad2103f57472b71d0130e62a40ab7424361..aaa2721c124230eebec7f29c5ced20ab2d33c38a 100644 (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',
index 0d0f4336a4fcfc3ea03c2072dfa23123b5ae5a24..eed355d3fc7bb5496b1b4aee468480b5af2eab28 100644 (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]);
       }
     }
diff --git a/src/configuration/__tests__/ArcanistBritishTestCase.php b/src/configuration/__tests__/ArcanistBritishTestCase.php
new file mode 100644 (file)
index 0000000..67e219e
--- /dev/null
@@ -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}");
+  }
+
+
+}