1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-10 08:52:39 +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) ->setReplaceCost(3)
->setTransposeCost(2); ->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(); $distances = array();
$commandv = str_split($command); $inputv = str_split($input);
foreach ($options as $option) { foreach ($options as $option) {
$optionv = str_split($option); $optionv = str_split($option);
$matrix->setSequences($optionv, $commandv); $matrix->setSequences($optionv, $inputv);
$distances[$option] = $matrix->getEditDistance(); $distances[$option] = $matrix->getEditDistance();
} }

View file

@ -2,44 +2,44 @@
final class ArcanistBritishTestCase extends ArcanistTestCase { final class ArcanistBritishTestCase extends ArcanistTestCase {
public function testCompletion() { public function testCommandCompletion() {
$this->assertCompletion( $this->assertCommandCompletion(
array('land'), array('land'),
'alnd', 'alnd',
array('land', 'amend')); array('land', 'amend'));
$this->assertCompletion( $this->assertCommandCompletion(
array('branch'), array('branch'),
'brnach', 'brnach',
array('branch', 'browse')); array('branch', 'browse'));
$this->assertCompletion( $this->assertCommandCompletion(
array(), array(),
'test', 'test',
array('list', 'unit')); array('list', 'unit'));
$this->assertCompletion( $this->assertCommandCompletion(
array('list'), array('list'),
'lists', 'lists',
array('list')); array('list'));
$this->assertCompletion( $this->assertCommandCompletion(
array('diff'), array('diff'),
'dfif', 'dfif',
array('diff')); array('diff'));
$this->assertCompletion( $this->assertCommandCompletion(
array('unit'), array('unit'),
'uint', 'uint',
array('unit', 'lint', 'list')); array('unit', 'lint', 'list'));
$this->assertCompletion( $this->assertCommandCompletion(
array('list', 'lint'), array('list', 'lint'),
'nilt', 'nilt',
array('unit', 'lint', 'list')); array('unit', 'lint', 'list'));
} }
private function assertCompletion($expect, $input, $commands) { private function assertCommandCompletion($expect, $input, $commands) {
$result = ArcanistConfiguration::correctCommandSpelling( $result = ArcanistConfiguration::correctCommandSpelling(
$input, $input,
$commands, $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)) { } else if (!strncmp($arg, '--', 2)) {
$arg_key = substr($arg, 2); $arg_key = substr($arg, 2);
if (!array_key_exists($arg_key, $spec)) { 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( throw new ArcanistUsageException(
"Unknown argument '{$arg_key}'. Try 'arc help'."); "Unknown argument '{$arg_key}'. Try 'arc help'.");
} }
}
} else if (!strncmp($arg, '-', 1)) { } else if (!strncmp($arg, '-', 1)) {
$arg_key = substr($arg, 1); $arg_key = substr($arg, 1);
if (empty($short_to_long_map[$arg_key])) { if (empty($short_to_long_map[$arg_key])) {