streetsidesoftware / cspell

A Spell Checker for Code!
https://cspell.org
MIT License
1.26k stars 103 forks source link

Automatic suggestion fixes #5023

Open aminya opened 11 months ago

aminya commented 11 months ago

Is your feature request related to a problem? Please describe.

It would be great to have a fix command where Cspell automatically takes the first suggestion and replaces the errors with that.

Describe the solution you'd like I have written this Bash script to do that, however, I hope this is integrated into Cspell.

This requires the correct configuration of words to prevent false positive changes, which is needed anyway if a project wants to use Cspell.

#!/bin/bash

# Run cspell and capture output
pnpm i -g cspell || npm i -g cspell
output=$(cspell lint --show-suggestions --no-progress)

# Loop through each line of output
while read -r line; do
    # if line is empty, skip
    if [ -z "$line" ]; then
        continue
    fi

    # Get the file, word, and first suggestion
    file=$(echo "$line" | awk -F ':' '{print $1}')
    word=$(echo "$line" | awk -F '[(|)]' '{print $2}')
    first_suggestion=$(echo "$line" | awk -F 'Suggestions: ' '{print $2}' | awk -F '[[]|[]]' '{print $2}' | awk -F ',' '{print $1}' | sed 's/\*//g')

    # Replace the word with the suggestion (full word)
    sed -i "s/\b$word\b/$first_suggestion/g" "$file"

    echo "Replaced $word with $first_suggestion in $file"
done <<<"$output"

Additional context

I use Cspell for my project to fix spelling errors. However, it takes time for new projects that haven't used Cspell before to fix the errors manually.

Jason3S commented 11 months ago

@aminya,

Thank you for the suggestion. It is on the list of things to improve.

Please see: https://github.com/streetsidesoftware/cspell/issues/4725#issuecomment-1683428016