Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document using extra dicts with pre-commit #207

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 6 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,17 @@ This repository enables using [cspell](https://github.com/streetsidesoftware/csp
# .pre-commit-config.yaml
repos:
- repo: https://github.com/streetsidesoftware/cspell-cli
rev: v6.2.0
rev: v6.7.0
hooks:
- id: cspell
```

## Setup Custom Dictionary
## How-To Guides and Configuration Examples

To use a custom dictionary with the `pre-commit` hook, create either a `cspell.config.yaml` or `cspell.json` file in your project's root directory.

`cspell.config.yaml`

```yaml
dictionaryDefinitions:
- name: myWords
path: ./path/to/cSpell_dict.txt
addWords: true
dictionaries:
- myWords
```

`cSpell.json`

```json
{
"dictionaryDefinitions": [
{
"name": "myWords",
"path": "./path/to/cSpell_dict.txt",
"addWords": true
}
],
"dictionaries": ["myWords"]
}
```

If you installed the [Code Spell Checker extension](https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker) for VS Code, this can be done automatically from the command palette by running "Spell: Create a CSpell configuration file".
* [Use Dictionaries from `cspell-dicts`](docs/use-dictionaries-from-cspell-dicts.md)
* [Use a Custom Dictionary](docs/use-a-custom-dictionary.md)
* [Use an Extra Dictionary Based on the Folder or Filename](docs/file-or-folder-based-overrides.md)
* [Example `pre-commit` Setup for French](docs/pre-commit-example-setup-for-french.md)

## Install from GitHub

Expand Down
168 changes: 168 additions & 0 deletions docs/file-or-folder-based-overrides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Use an Extra Dictionary Based on the Folder or Filename
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are a few approaches to document this. A couple come to mind:

  • A walk through style.
  • A reference style.

I think either approach is fine.

Based upon how you wrote it so far, I'm guessing you are leaning towards a Walk Through Style.
My suggestion is to use a single short example file. No need to show too much. We just want
to demonstrate the issue to be solved.

Walk Though Style

A walk through is more like telling a story:

  1. Show the problem you are trying to solve

    • lots of spelling issues in bash code blocks.
    • Screen shots:
      image or image
  2. Show some ways to solve it.

    • In Document: <!--- cspell:ignore esac ---> or <!--- cspell:words esac --->
    • In Document: <!--- cspell:dictionaries bash --->
    • Using overrides
    • Using languageSettings
    • Add words to cspell.json: { "words": ["esac"] }
  3. Pick one and explain your choice.

  4. Summarize / conclusions.

Useful things:

  • Some screen shot could be useful.
  • Keep configuration short.
  • The should be a logical flow.

Reference Style

In a reference style, it is more like a list of examples.

  • Enable the bash dictionary using overrides
  • Enable the bash dictionary using languageSettings
  • Enable the bash dictionary using in-document settings.

Tools

  • list of dictionaries:
    cspell-cli trace word
  • cli based visual check:
    cspell-cli check docs/file-or-folder-based-overrides.md

<!--- cspell:ignore esac getopts shopt --->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: it is also possible to enable a dictionary in a file if adding words becomes too long.

<!--- cspell:dictionaries bash --->


For example, using Bash dictionary with Markdown or text files, by filename (the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about flipping the logic:

By default the Bash dictionary is only applied to files of type schellscript. In this example we will show how to enable the Bash dictionary based upon a file glob using overrides.

default is to only apply from within VSCode for files of type `shellscript`).

**Note**: This adds the Bash dictionary to the list of dictionaries against
which to check the files, it does not check only against the Bash dictionary.

This is the same for use with `pre-commit` as with other methods of invoking
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, pre-commit works exactly the same. I think it is fine to mention that in the summary.

`cspell`, namely configuring an `overrides` section on your `cspell.json`
or equivalent file.

The [current overrides documentation on the
website](https://cspell.org/configuration/overrides/) covers a different type of
scenario, so here is an example of selecting adding a dictionary to apply to
specific folders.

## Using Bash dictionary with Markdown or text files by filename

Given a `cspell.json` as follows:


``` json
{
"overrides": [
{
"filename": ["**/bash_examples/**", "**/bash_docs/**.md"],
"dictionaries": [
"bash"
]
}
],
"version": "0.2"
}
```

Add the following code as `docs/bash_docs/a-silly-bash-code-block.md`

```` markdown
## Sample code block

```sh
if grep -r "something" .; then
echo "Found"
fi

case "a string with 3" in
with)
echo "Why?"
;;
esac

. ./.bash_aliases

# We should use getopts

shopt -p
```

## And another topic

Words, many words.
````

Add the following file as `examples/bash_examples/a-silly-bash-script`

``` markdown
#!/bin/bash

if grep -r "something" .; then
echo "Found"
fi

case "a string with 3" in
with)
echo "Why?"
;;
esac

. ./.bash_aliases

# We should use getopts

shopt -p
```

And add the following code as `docs/about.md`

```` markdown
# Just a rather eccentric bash and markdown example for documentation purposes

## Sample code block

```sh
if grep -r "something" .; then
echo "Found"
fi

case "a string with 3" in
with)
echo "Why?"
;;
esac

. ./.bash_aliases

# We should use getopts

shopt -p
```

## And another topic

Words, many words.
````

When `cspell` is invoked only `docs/about.md` should show errors.

## Invoking CSpell

### Via [`pre-commit`](https://pre-commit.com)

#### Prerequisites

* Project folder initialized as a git repository via `git init` or as part of a
git repository cloned via `git clone`
* ['pre-commit' installed](https://pre-commit.com/#install)
* A `.pre-commit-config.yaml` such as:

```yaml
fail_fast: true
minimum_pre_commit_version: 2.18.1

repos:
- repo: "https://github.com/streetsidesoftware/cspell-cli"
rev: v6.7.0
hooks:
- id: cspell
```

#### Triggering CSpell using `pre-commit`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move the pre-commit directions into it own file: Using cspell-cli with pre-comit.


* Stage all files

```
git add --all .
```

* Check all files in `git` against `pre-commit` hooks

``` bash
pre-commit run --all-files
```

* OR commit the changes

``` bash
git commit
```

### Via Command line

* Execute

``` bash
cspell '**'
```
148 changes: 148 additions & 0 deletions docs/pre-commit-example-setup-for-french.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# Example `pre-commit` setup for French
<!--- cspell:ignore Voici nous avons française reforme --->

## Common configuration

### `pre-commit-config.yaml` configuration

Extend the `pre-commit` hook config from the [README.md](../README.md) with
`additional_dependencies`. For example:

```yaml
# .pre-commit-config.yaml
repos:
- repo: https://github.com/streetsidesoftware/cspell-cli
rev: v6.7.0
hooks:
- id: cspell
additional_dependencies:
- "@cspell/dict-fr-fr"
- "@cspell/dict-fr-reforme"

```
Comment on lines +6 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!


For a complete list of available dictionaries,
see: <https://github.com/streetsidesoftware/cspell-dicts>.

## Using French as the locale

Use a `cspell.json` such as the following:

```json
{
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
"import": [
"@cspell/dict-fr-fr/cspell-ext.json",
"@cspell/dict-fr-reforme/cspell-ext.json"
],
"language": "fr",
"version": "0.2"
}
```

A file such as `mots-française.md` containing:

```markdown
# Testing french in Markdown

## Les mots

Voici, nous avons les mots française.
```

When `cspell` is invoked `mots-française.md` should not show errors

## Applying to specific files

And to apply those dictionaries to files with the `.md` extension (Markdown),
use a `cspell.json` such as:

```json
{
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
"import": [
"@cspell/dict-fr-fr/cspell-ext.json",
"@cspell/dict-fr-reforme/cspell-ext.json"
],
"overrides": [
{
"filename": "**/*.md",
"language": "fr,fr-fr,fr-90"
}
],
"version": "0.2"
}
```

And the following, as `mots-française.md`:

``` markdown
# Testing french in Markdown

## Les mots

Voici, nous avons les mots française
```

as again as `mots-française.err`:
Copy link
Contributor

@Jason3S Jason3S Aug 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to repeat the example or show errors.

At most, a directory tree showing which files will use French and which ones will not.


``` markdown
# Testing french in Markdown

## Les mots

Voici, nous avons les mots française
```

When `cspell` is invoked `mots-française.md` should not show errors, but
`mots-française.err` should.

## Invoking CSpell
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a list of references to other pages.


### Via [`pre-commit`](https://pre-commit.com)

#### Prerequisites

* Project folder initialized as a git repository via `git init` or as part of a
git repository cloned via `git clone`
* ['pre-commit' installed](https://pre-commit.com/#install)
* A `.pre-commit-config.yaml` such as:

```yaml
fail_fast: true
minimum_pre_commit_version: 2.18.1

repos:
- repo: "https://github.com/streetsidesoftware/cspell-cli"
rev: v6.7.0
hooks:
- id: cspell
```

#### Triggering CSpell using `pre-commit`

* Stage all files

```
git add --all .
```

* Check all files in `git` against `pre-commit` hooks

``` bash
pre-commit run --all-files
```

* OR commit the changes

``` bash
git commit
```

### Via Command line

* Execute

``` bash
cspell '**'
```
32 changes: 32 additions & 0 deletions docs/use-a-custom-dictionary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Setup Custom Dictionary

To use a custom dictionary with the `pre-commit` hook, create either a `cspell.config.yaml` or `cspell.json` file in your project's root directory.

`cspell.config.yaml`

```yaml
dictionaryDefinitions:
- name: myWords
path: ./path/to/cSpell_dict.txt
addWords: true
dictionaries:
- myWords
```

`cSpell.json`

```json
{
"dictionaryDefinitions": [
{
"name": "myWords",
"path": "./path/to/cSpell_dict.txt",
"addWords": true
}
],
"dictionaries": ["myWords"]
}
```

If you installed the [Code Spell Checker extension](https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker) for VS Code, this can be done automatically from the command palette by running "Spell: Create a CSpell configuration file".

Loading