Skip to content

Commit

Permalink
fix: Make sure the repl check uses the right directory (#4092)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Jan 30, 2025
1 parent 5aeb2c8 commit 90b4464
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 17 deletions.
17 changes: 10 additions & 7 deletions packages/_server/src/handleCheckDocumentRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CSpellSettings } from 'cspell-lib';
import { type CSpellSettings, IssueType } from 'cspell-lib';
import type { TextDocument } from 'vscode-languageserver-textdocument';

import type * as Api from './api.js';
Expand Down Expand Up @@ -29,12 +29,15 @@ export async function handleCheckDocumentRequest(

const results = docVal.checkDocument(options.forceCheck);

const issues: Api.CheckDocumentIssue[] = results.map((issue) => ({
text: issue.text,
range: { start: doc.positionAt(issue.offset), end: doc.positionAt(issue.offset + (issue.length || issue.text.length)) },
suggestions: issue.suggestionsEx,
message: issue.message,
}));
const issues: Api.CheckDocumentIssue[] = results
.filter((issue) => issue.issueType !== IssueType.directive)
.map((issue) => ({
text: issue.text,
range: { start: doc.positionAt(issue.offset), end: doc.positionAt(issue.offset + (issue.length || issue.text.length)) },
suggestions: issue.suggestionsEx,
message: issue.message,
isFlagged: issue.isFlagged,
}));

return { uri, issues };

Expand Down
3 changes: 3 additions & 0 deletions packages/client/src/decorators/traceDecorations.mts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ export class SpellingExclusionsDecorator implements Disposable {
} else {
hoverMessage.appendMarkdown('- $(book) _').appendText(trace.dictName).appendMarkdown('_');
}
if (trace.forbidden) {
hoverMessage.appendMarkdown(' _(Forbidden)_');
}
if (trace.foundWord && trace.foundWord !== word) {
hoverMessage.appendMarkdown(' **').appendText(trace.foundWord.trim()).appendMarkdown('**');
}
Expand Down
32 changes: 27 additions & 5 deletions packages/client/src/repl/cmdCheck.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import { formatPath, relative } from './formatPath.mjs';

const maxPathLen = 60;

async function cmdCheckDocument(prep: CheckDocumentPrep): Promise<void> {
interface CheckResult {
checked: boolean;
issues: number;
}

const skippedCheck: CheckResult = { checked: false, issues: 0 };
const okCheck: CheckResult = { checked: true, issues: 0 };

async function cmdCheckDocument(prep: CheckDocumentPrep): Promise<CheckResult> {
const { uri, options, index, count, result: pResult, startTs, endTs } = prep;
const { output, log, width } = options;

Expand All @@ -22,7 +30,7 @@ async function cmdCheckDocument(prep: CheckDocumentPrep): Promise<void> {

if (result.skipped) {
log(` ${elapsedTime} Skipped`);
return;
return skippedCheck;
}

const lines: string[] = [];
Expand All @@ -35,7 +43,7 @@ async function cmdCheckDocument(prep: CheckDocumentPrep): Promise<void> {

if (!failed) {
output(lines.join('') + '\r' + clearLine(0));
return;
return okCheck;
}

if (result.errors) {
Expand All @@ -49,6 +57,7 @@ async function cmdCheckDocument(prep: CheckDocumentPrep): Promise<void> {
}

log('%s', lines.join('\n'));
return { checked: true, issues: issues?.length || 0 };
}

function countPrefix(index?: number, count?: number): string {
Expand All @@ -61,7 +70,7 @@ function countPrefix(index?: number, count?: number): string {
function formatIssue(uri: string | Uri, issue: API.CheckDocumentIssue): string {
const { range, text, suggestions, isFlagged } = issue;
const pos = `:${range.start.line + 1}:${range.start.character + 1}`;
const message = isFlagged ? 'Flagged word ' : 'Unknown word ';
const message = isFlagged ? 'Flagged word' : 'Unknown word';
const rel = relative(uri);
const sugMsg = suggestions ? ` fix: (${suggestions.map((s) => colors.yellow(s.word)).join(', ')})` : '';
return ` ${colors.green(rel)}${colors.yellow(pos)} - ${message} (${colors.red(text)})${sugMsg}`;
Expand Down Expand Up @@ -102,7 +111,13 @@ const prefetchCount = 10;
export async function cmdCheckDocuments(uris: (string | Uri)[], options: CheckDocumentsOptions): Promise<void> {
const count = uris.length;

options.log(`Checking ${count} documents`);

const pending: CheckDocumentPrep[] = [];
let files = 0;
let filesWithIssues = 0;
let issues = 0;
let skipped = 0;

try {
let index = 0;
Expand All @@ -115,7 +130,11 @@ export async function cmdCheckDocuments(uris: (string | Uri)[], options: CheckDo
try {
const prep = pending.shift();
if (!prep) break;
await cmdCheckDocument(prep);
const r = await cmdCheckDocument(prep);
issues += r.issues;
filesWithIssues += r.issues ? 1 : 0;
files += r.checked ? 1 : 0;
skipped += r.checked ? 0 : 1;
if (index < count) {
const uri = uris[index];
pending.push(prepareCheckDocument(uri, options, index, count));
Expand All @@ -128,6 +147,9 @@ export async function cmdCheckDocuments(uris: (string | Uri)[], options: CheckDo
}
//
Promise.all(pending.map((p) => p.result)).catch((error) => options.error(error));
options.log(
`Checked ${files}/${count} files${skipped ? `, skipped ${skipped},` : ''} with ${issues} issues in ${filesWithIssues} files.`,
);
} catch {
// All errors should have been reported.
return;
Expand Down
41 changes: 37 additions & 4 deletions packages/client/src/repl/cmdTrace.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface TraceWordOptions {
export async function traceWord(word: string, uri: Uri | string, options: TraceWordOptions): Promise<string> {
const { width } = options;
const client = di.get('client');
uri = uri.toString();
if (!uri.endsWith('/')) uri = uri + '/';
const result = await client.serverApi.traceWord({
word,
uri: uri.toString(),
Expand All @@ -41,9 +43,9 @@ export async function traceWord(word: string, uri: Uri | string, options: TraceW

const toTable = asTable.configure({ maxTotalWidth: width });
const filters = [
options.all ? undefined : (trace: { found: boolean; dictEnabled: boolean }) => trace.found || trace.dictEnabled,
options.onlyFound ? (trace: { found: boolean }) => trace.found : undefined,
options.onlyEnabled ? (trace: { dictEnabled: boolean }) => trace.dictEnabled : undefined,
options.all ? undefined : (trace: Trace) => isFound(trace) || trace.dictEnabled,
options.onlyFound ? (trace: Trace) => isFound(trace) : undefined,
options.onlyEnabled ? (trace: Trace) => trace.dictEnabled : undefined,
].filter(isDefined);

const filter = combineFilters(filters);
Expand All @@ -57,7 +59,7 @@ export async function traceWord(word: string, uri: Uri | string, options: TraceW
const dictionary = colors.yellow(line.dictName);
return {
Word: colorWord(line.word, line.foundWord, line.found),
F: line.found ? colors.whiteBright('*') : colors.dim('-'),
F: line.forbidden ? colors.red('!') : line.found ? colors.whiteBright('*') : colors.dim('-'),
Dictionary: line.dictEnabled ? dictionary : colors.dim(dictionary),
};
});
Expand All @@ -80,3 +82,34 @@ function colorWord(word: string, foundWord: string | undefined, found: boolean):

return found ? w : colors.dim(w);
}

interface Trace {
/**
* true if found in the dictionary.
*/
found: boolean;
/**
* The actual word found in the dictionary.
*/
foundWord: string | undefined;
/**
* true if the word is forbidden.
*/
forbidden: boolean;
/**
* true if it is a no-suggest word.
*/
noSuggest: boolean;
/**
* name of the dictionary
*/
dictName: string;
/**
* true if the dictionary is enabled for the languageId (file type).
*/
dictEnabled: boolean;
}

function isFound(trace: Trace): boolean {
return trace.found || trace.forbidden || trace.noSuggest;
}
8 changes: 7 additions & 1 deletion packages/client/src/repl/repl.mts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,13 @@ class Repl implements vscode.Disposable, vscode.Pseudoterminal {
const cfgSearchExclude = vscode.workspace.getConfiguration('search.exclude') as Record<string, boolean>;
const searchExclude = Object.keys(cfgSearchExclude).filter((k) => cfgSearchExclude[k] === true);
const excludePattern = globsToGlob(searchExclude);
const files = await globSearch(pattern, currentDirectory(), excludePattern, undefined, this.#getCancellationTokenForAction());
const files = await globSearch(
pattern,
this.#cwd || currentDirectory(),
excludePattern,
undefined,
this.#getCancellationTokenForAction(),
);

log(eraseLine() + 'Checking...');

Expand Down
1 change: 1 addition & 0 deletions samples/custom-dictionary/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"addWords": true
}
],
"ignorePaths": ["*.dic"],
"dictionaries": ["words", "dict"],
"words": []
}
2 changes: 2 additions & 0 deletions samples/custom-dictionary/dict.dic
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Compaknee
!broadband
!redband

0 comments on commit 90b4464

Please sign in to comment.