From 5759cb9605331092fa5b4112d5a4acd9699f698e Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Wed, 30 Nov 2022 17:38:00 -0700 Subject: [PATCH 1/4] fix: Display preferred suggestions --- packages/cspell/cspell.json | 1 + packages/cspell/src/emitters/suggestionsEmitter.ts | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/cspell/cspell.json b/packages/cspell/cspell.json index 344be5049eb..fa401c87ff3 100644 --- a/packages/cspell/cspell.json +++ b/packages/cspell/cspell.json @@ -16,6 +16,7 @@ ".cspell.json", ".vscode/**" ], + "flagWords": ["blacklist: denylist", "blackList: denyList", "whitelist: allowlist"], "dictionaryDefinitions": [ { "name": "workspace", diff --git a/packages/cspell/src/emitters/suggestionsEmitter.ts b/packages/cspell/src/emitters/suggestionsEmitter.ts index 50febacb7cf..be12557b03c 100644 --- a/packages/cspell/src/emitters/suggestionsEmitter.ts +++ b/packages/cspell/src/emitters/suggestionsEmitter.ts @@ -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(', '); From 4bf0dfbf9dfe6e3b139ac002291d5f7620c96edc Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Wed, 30 Nov 2022 17:40:36 -0700 Subject: [PATCH 2/4] highlight preferred --- packages/cspell/src/emitters/suggestionsEmitter.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/cspell/src/emitters/suggestionsEmitter.ts b/packages/cspell/src/emitters/suggestionsEmitter.ts index be12557b03c..311fb99a7dd 100644 --- a/packages/cspell/src/emitters/suggestionsEmitter.ts +++ b/packages/cspell/src/emitters/suggestionsEmitter.ts @@ -77,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; } From f6374b259d5bf1ef6101e8096f36f68f67eb324d Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Wed, 30 Nov 2022 17:58:03 -0700 Subject: [PATCH 3/4] format suggestions to emphasize preferred. --- packages/cspell-types/src/CSpellReporter.ts | 15 +++++++++++++++ packages/cspell/src/cli-reporter.ts | 14 +++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/cspell-types/src/CSpellReporter.ts b/packages/cspell-types/src/CSpellReporter.ts index 59dd0cbcb83..19a1560cb7f 100644 --- a/packages/cspell-types/src/CSpellReporter.ts +++ b/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 { /** text surrounding the issue text */ context: TextOffset; @@ -11,6 +22,10 @@ export interface Issue extends Omit { * An optional array of replacement strings. */ suggestions?: string[]; + /** + * An optional array of suggestions. + */ + suggestionsEx?: Suggestion[]; /** * Issues are spelling issues unless otherwise specified. */ diff --git a/packages/cspell/src/cli-reporter.ts b/packages/cspell/src/cli-reporter.ts index e47caf86e32..a76f221b2d1 100644 --- a/packages/cspell/src/cli-reporter.ts +++ b/packages/cspell/src/cli-reporter.ts @@ -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; @@ -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 { raw: string[]; constructor(s: string) { From 2e241e679de9f8d6cc07bcc70f9aff30c461eeb3 Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Thu, 1 Dec 2022 15:37:57 +0100 Subject: [PATCH 4/4] Update app.test.ts.snap --- packages/cspell/src/__snapshots__/app.test.ts.snap | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cspell/src/__snapshots__/app.test.ts.snap b/packages/cspell/src/__snapshots__/app.test.ts.snap index 6672d5a0f7f..1841964ba50 100644 --- a/packages/cspell/src/__snapshots__/app.test.ts.snap +++ b/packages/cspell/src/__snapshots__/app.test.ts.snap @@ -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" `;