Skip to content

Commit

Permalink
fix: Display preferred suggestions (#3886)
Browse files Browse the repository at this point in the history
* Display preferred suggestions
* highlight preferred
* format suggestions to emphasize preferred.
  • Loading branch information
Jason3S committed Dec 1, 2022
1 parent 27b0fa8 commit 8b25077
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
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;
}

0 comments on commit 8b25077

Please sign in to comment.