]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Make spell check linter patching
authorvrana <jakubv@fb.com>
Mon, 10 Sep 2012 23:09:44 +0000 (16:09 -0700)
committervrana <jakubv@fb.com>
Mon, 10 Sep 2012 23:21:58 +0000 (16:21 -0700)
Test Plan:
Wrote "Posible" in the linter, let it change to "Possible".
New unit test.

Reviewers: jack, epriestley

Reviewed By: epriestley

CC: aran, Korvin
Differential Revision: https://secure.phabricator.com/D3469

src/lint/linter/ArcanistSpellingLinter.php
src/lint/linter/__tests__/ArcanistSpellingLinterTestCase.php

index 7d0432384b2d201bd0622be7238ff1ee03531886..bfd81f6af13c2a9427561bdeafe36b367057b73e 100644 (file)
@@ -111,6 +111,8 @@ final class ArcanistSpellingLinter extends ArcanistLinter {
       if ($next === false) {
         return;
       }
+      $original = substr($text, $next, strlen($word));
+      $replacement = self::fixLetterCase($correct_word, $original);
       $this->raiseLintAtOffset(
         $next,
         $severity,
@@ -118,7 +120,9 @@ final class ArcanistSpellingLinter extends ArcanistLinter {
           "Possible spelling error. You wrote '%s', but did you mean '%s'?",
           $word,
           $correct_word
-        )
+        ),
+        $original,
+        $replacement
       );
       $pos = $next + 1;
     }
@@ -137,6 +141,8 @@ final class ArcanistSpellingLinter extends ArcanistLinter {
       return;
     }
     foreach ($matches[0] as $match) {
+      $original = $match[0];
+      $replacement = self::fixLetterCase($correct_word, $original);
       $this->raiseLintAtOffset(
         $match[1],
         $severity,
@@ -144,8 +150,24 @@ final class ArcanistSpellingLinter extends ArcanistLinter {
           "Possible spelling error. You wrote '%s', but did you mean '%s'?",
           $word,
           $correct_word
-        )
+        ),
+        $original,
+        $replacement
       );
     }
   }
+
+  public static function fixLetterCase($string, $case) {
+    if ($case == strtolower($case)) {
+      return strtolower($string);
+    }
+    if ($case == strtoupper($case)) {
+      return strtoupper($string);
+    }
+    if ($case == ucwords(strtolower($case))) {
+      return ucwords(strtolower($string));
+    }
+    return null;
+  }
+
 }
index 7afe118e0053bf8bf09821db06de40e2bba363e0..0cd4c3e45d7855aaf2f80606fa2c2c4166951794 100644 (file)
@@ -35,4 +35,19 @@ final class ArcanistSpellingLinterTestCase extends ArcanistLinterTestCase {
       $working_copy);
   }
 
+  public function testFixLetterCase() {
+    $tests = array(
+      'tst' => 'test',
+      'Tst' => 'Test',
+      'TST' => 'TEST',
+      'tSt' => null,
+    );
+    foreach ($tests as $case => $expect) {
+      foreach (array('test', 'TEST') as $string) {
+        $result = ArcanistSpellingLinter::fixLetterCase($string, $case);
+        $this->assertEqual($expect, $result, $case);
+      }
+    }
+  }
+
 }