Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,25 @@ In order to make it easier to get started with Peck, we've included a few preset

- `laravel`

### Languages

While by default `peck` verfies the spelling by using GNU Aspell's `en_US` language dictionary, you can optionally specify a different (installed) language to be passed using the "language" key in the configuration:

```json
{
"preset": "laravel",
"language": "en_US",
"ignore": {
"words": [
"config",
"namespace"
]
}
}
```

You can see a full list of available dictionaries using the command `aspell dump dicts`.

## Command Options

The behaviour of `peck` can be modified with the following command options:
Expand Down
17 changes: 17 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ final class Config
*/
private const JSON_CONFIGURATION_NAME = 'peck.json';

/**
* The default language passed to Aspell.
*/
private const DEFAULT_LANGUAGE = 'en_US';

/**
* The instance of the configuration.
*/
Expand All @@ -35,6 +40,7 @@ public function __construct(
public array $whitelistedWords = [],
public array $whitelistedPaths = [],
public ?string $preset = null,
public ?string $language = null,
) {
$this->whitelistedWords = array_map(strtolower(...), $whitelistedWords);
}
Expand Down Expand Up @@ -87,6 +93,7 @@ public static function instance(): self
/**
* @var array{
* preset?: string,
* language?: string,
* ignore?: array{
* words?: array<int, string>,
* paths?: array<int, string>
Expand All @@ -99,6 +106,7 @@ public static function instance(): self
$jsonAsArray['ignore']['words'] ?? [],
$jsonAsArray['ignore']['paths'] ?? [],
$jsonAsArray['preset'] ?? null,
$jsonAsArray['language'] ?? null,
);
}

Expand Down Expand Up @@ -154,6 +162,14 @@ public function isWordIgnored(string $word): bool
]);
}

/**
* Retrieves the configured language or the default
*/
public function getLanguage(): string
{
return $this->language ?? self::DEFAULT_LANGUAGE;
}

/**
* Save the configuration to the file.
*/
Expand All @@ -163,6 +179,7 @@ private function persist(): void

file_put_contents($filePath, json_encode([
...$this->preset !== null ? ['preset' => $this->preset] : [],
...$this->language !== null ? ['language' => $this->language] : [],
'ignore' => [
'words' => $this->whitelistedWords,
'paths' => $this->whitelistedPaths,
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Spellcheckers/Aspell.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function createProcess(): Process
'utf-8',
'-a',
'--ignore-case',
'--lang=en_US',
'--lang='.$this->config->getLanguage(),
]);

$process->setTimeout(0);
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Peck\Config;
use Peck\Support\ProjectPath;

it('should have a default configuration', function (): void {
$config = Config::instance();
Expand Down Expand Up @@ -59,3 +60,37 @@
'tests',
]);
});

describe('language', function (): void {
beforeEach(function (): void {
$configPath = '/temp.json';
$this->jsonPath = ProjectPath::get().$configPath;
Config::flush();
Config::resolveConfigFilePathUsing(fn (): string => $configPath);
$this->exampleConfig = [
'preset' => 'laravel',
'ignore' => [
'words' => [
'config',
],
],
];
});

it('should default to `en_US` when the language flag is not set in the json file', function (): void {
file_put_contents($this->jsonPath, json_encode($this->exampleConfig));
$config = Config::instance();
expect($config->getLanguage())->toBe('en_US');
});

it('should read the language flag from the json file', function (): void {
$this->exampleConfig['language'] = 'en_GB';
file_put_contents($this->jsonPath, json_encode($this->exampleConfig));
$config = Config::instance();
expect($config->getLanguage())->toBe('en_GB');
});

afterEach(function (): void {
unlink($this->jsonPath);
});
});