1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-25 16:22:42 +01:00

Respect British spelling also in arguments

Summary: These are the errors I really do.

Test Plan:
  $ arc diff --no-lint
  (Assuming '--no-lint' is the British spelling of '--nolint'.)
  $ arc diff --reviewer a
  (Assuming '--reviewer' is the British spelling of '--reviewers'.)

New unit test.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D5185
This commit is contained in:
vrana 2013-03-01 16:07:16 -08:00
parent 4af7c865aa
commit ebe464b6f0
3 changed files with 86 additions and 13 deletions

View file

@ -209,11 +209,34 @@ class ArcanistConfiguration {
->setReplaceCost(3)
->setTransposeCost(2);
return self::correctSpelling($command, $options, $matrix, $max_distance);
}
public static function correctArgumentSpelling($command, array $options) {
$max_distance = 1;
// We are stricter with arguments - we allow only one inserted or deleted
// character. It is mainly to handle cases like --no-lint versus --nolint
// or --reviewer versus --reviewers.
$matrix = id(new PhutilEditDistanceMatrix())
->setInsertCost(1)
->setDeleteCost(1)
->setReplaceCost(10);
return self::correctSpelling($command, $options, $matrix, $max_distance);
}
public static function correctSpelling(
$input,
array $options,
PhutilEditDistanceMatrix $matrix,
$max_distance) {
$distances = array();
$commandv = str_split($command);
$inputv = str_split($input);
foreach ($options as $option) {
$optionv = str_split($option);
$matrix->setSequences($optionv, $commandv);
$matrix->setSequences($optionv, $inputv);
$distances[$option] = $matrix->getEditDistance();
}

View file

@ -2,44 +2,44 @@
final class ArcanistBritishTestCase extends ArcanistTestCase {
public function testCompletion() {
$this->assertCompletion(
public function testCommandCompletion() {
$this->assertCommandCompletion(
array('land'),
'alnd',
array('land', 'amend'));
$this->assertCompletion(
$this->assertCommandCompletion(
array('branch'),
'brnach',
array('branch', 'browse'));
$this->assertCompletion(
$this->assertCommandCompletion(
array(),
'test',
array('list', 'unit'));
$this->assertCompletion(
$this->assertCommandCompletion(
array('list'),
'lists',
array('list'));
$this->assertCompletion(
$this->assertCommandCompletion(
array('diff'),
'dfif',
array('diff'));
$this->assertCompletion(
$this->assertCommandCompletion(
array('unit'),
'uint',
array('unit', 'lint', 'list'));
$this->assertCompletion(
$this->assertCommandCompletion(
array('list', 'lint'),
'nilt',
array('unit', 'lint', 'list'));
}
private function assertCompletion($expect, $input, $commands) {
private function assertCommandCompletion($expect, $input, $commands) {
$result = ArcanistConfiguration::correctCommandSpelling(
$input,
$commands,
@ -57,4 +57,42 @@ final class ArcanistBritishTestCase extends ArcanistTestCase {
}
public function testArgumentCompletion() {
$this->assertArgumentCompletion(
array('nolint'),
'no-lint',
array('nolint', 'nounit'));
$this->assertArgumentCompletion(
array('reviewers'),
'reviewer',
array('reviewers', 'cc'));
$this->assertArgumentCompletion(
array(),
'onlint',
array('nolint'));
$this->assertArgumentCompletion(
array(),
'nolind',
array('nolint'));
}
private function assertArgumentCompletion($expect, $input, $arguments) {
$result = ArcanistConfiguration::correctArgumentSpelling(
$input,
$arguments);
sort($result);
sort($expect);
$arguments = implode(', ', $arguments);
$this->assertEqual(
$expect,
$result,
"Correction of {$input} against: {$arguments}");
}
}

View file

@ -636,9 +636,21 @@ abstract class ArcanistBaseWorkflow extends Phobject {
} else if (!strncmp($arg, '--', 2)) {
$arg_key = substr($arg, 2);
if (!array_key_exists($arg_key, $spec)) {
$corrected = ArcanistConfiguration::correctArgumentSpelling(
$arg_key,
array_keys($spec));
if (count($corrected) == 1) {
PhutilConsole::getConsole()->writeErr(
pht(
"(Assuming '%s' is the British spelling of '%s'.)",
'--'.$arg_key,
'--'.head($corrected))."\n");
$arg_key = head($corrected);
} else {
throw new ArcanistUsageException(
"Unknown argument '{$arg_key}'. Try 'arc help'.");
}
}
} else if (!strncmp($arg, '-', 1)) {
$arg_key = substr($arg, 1);
if (empty($short_to_long_map[$arg_key])) {