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

fix: Display preferred suggestions #3886

Merged
merged 4 commits into from Dec 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/cspell-types/src/CSpellReporter.ts
@@ -1,5 +1,16 @@
import { TextDocumentOffset, TextOffset } from './TextOffset';

export interface Suggestion {
/**
* Word to suggest.
*/
word: string;
/**
* `true` - if this suggestion can be an automatic fix.
*/
isPreferred?: boolean;
}

export interface Issue extends Omit<TextDocumentOffset, 'doc'> {
/** text surrounding the issue text */
context: TextOffset;
Expand All @@ -11,6 +22,10 @@ export interface Issue extends Omit<TextDocumentOffset, 'doc'> {
* An optional array of replacement strings.
*/
suggestions?: string[];
/**
* An optional array of suggestions.
*/
suggestionsEx?: Suggestion[];
/**
* Issues are spelling issues unless otherwise specified.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/cspell/cspell.json
Expand Up @@ -16,6 +16,7 @@
".cspell.json",
".vscode/**"
],
"flagWords": ["blacklist: denylist", "blackList: denyList", "whitelist: allowlist"],
"dictionaryDefinitions": [
{
"name": "workspace",
Expand Down
6 changes: 3 additions & 3 deletions packages/cspell/src/__snapshots__/app.test.ts.snap
Expand Up @@ -1343,9 +1343,9 @@ not-in-any-dictionary - workspace* ../../cspell-dict.txt"
exports[`Validate cli app typos Expect Error: [Function CheckFailed] 1`] = `[]`;

exports[`Validate cli app typos Expect Error: [Function CheckFailed] 2`] = `
"log ./fixtures/features/typos/test.md:5:3 - Forbidden word (blacklist) Suggestions: [denylist, blacklists, backlist, blacklist's, blacklisted]
log ./fixtures/features/typos/test.md:6:3 - Forbidden word (whitelist) Suggestions: [allowlist, whitelists, whitelisted, whitefish, whitest]
log ./fixtures/features/typos/test.md:7:3 - Forbidden word (rad) Suggestions: [cool, rda, rads, raid, rand]
"log ./fixtures/features/typos/test.md:5:3 - Forbidden word (blacklist) Suggestions: [denylist*, blacklists, backlist, blacklist's, blacklisted]
log ./fixtures/features/typos/test.md:6:3 - Forbidden word (whitelist) Suggestions: [allowlist*, whitelists, whitelisted, whitefish, whitest]
log ./fixtures/features/typos/test.md:7:3 - Forbidden word (rad) Suggestions: [cool*, rda, rads, raid, rand]
error CSpell: Files checked: 2, Issues found: 3 in 1 files"
`;

Expand Down
14 changes: 13 additions & 1 deletion packages/cspell/src/cli-reporter.ts
Expand Up @@ -198,7 +198,7 @@ function formatIssue(templateStr: string, issue: ReporterIssue, maxIssueTextWidt
const rowText = row.toString();
const colText = col.toString();
const padRowCol = ' '.repeat(Math.max(1, 8 - (rowText.length + colText.length)));
const suggestions = issue.suggestions?.join(', ') || '';
const suggestions = formatSuggestions(issue);
const msg = issue.message || (issue.isFlagged ? 'Forbidden word' : 'Unknown word');
const message = issue.isFlagged ? `{yellow ${msg}}` : msg;

Expand All @@ -221,6 +221,18 @@ function formatIssue(templateStr: string, issue: ReporterIssue, maxIssueTextWidt
return substitute(chalk(t), substitutions);
}

function formatSuggestions(issue: Issue): string {
if (issue.suggestionsEx) {
return issue.suggestionsEx
.map((sug) => (sug.isPreferred ? chalk.italic(chalk.bold(sug.word)) + '*' : sug.word))
.join(', ');
}
if (issue.suggestions) {
return issue.suggestions.join(', ');
}
return '';
}

class TS extends Array<string> {
raw: string[];
constructor(s: string) {
Expand Down
10 changes: 9 additions & 1 deletion packages/cspell/src/emitters/suggestionsEmitter.ts
Expand Up @@ -48,7 +48,14 @@ export function emitSuggestionResult(result: TimedSuggestionsForWordResult, opti
for (const sug of mappedSugs) {
const { cost, dictionaries, w } = sug;
const padding = ' '.repeat(padWidth(w, maxWidth));
const forbid = sug.forbidden ? chalk.red('X') : ' ';
const forbid =
sug.forbidden && sug.isPreferred
? chalk.red('*')
: sug.forbidden
? chalk.red('X')
: sug.isPreferred
? chalk.yellow('*')
: ' ';
const ignore = sug.noSuggest ? chalk.yellow('N') : ' ';
const strCost = padLeft(cost.toString(10), 4);
const dicts = dictionaries.map((n) => chalk.gray(n)).join(', ');
Expand All @@ -70,5 +77,6 @@ function formatWordSingle(s: SuggestedWord): string {
let word = formatWord(s.word, s);
word = s.forbidden ? word + chalk.red(' X') : word;
word = s.noSuggest ? word + chalk.yellow(' Not suggested.') : word;
word = s.isPreferred ? chalk.yellow(word + ' *') : word;
return word;
}