Skip to content

Commit

Permalink
Add trace command
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Apr 6, 2024
1 parent 9d44dbe commit 3820703
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
9 changes: 6 additions & 3 deletions packages/client/src/repl/args.mts
Expand Up @@ -123,7 +123,7 @@ export class Application {

constructor(
public name: string,
public description: string,
public description: string = '',
public usage: string = '',
) {}

Expand Down Expand Up @@ -187,9 +187,12 @@ export class Application {

#formatApplicationHeader(width: number) {
const lines = [];
lines.push(this.name, '', ...splitIntoLines(this.description, width), '');
lines.push(this.name, '');
if (this.description) {
lines.push(...splitIntoLines(this.description, width), '');
}
if (this.usage) {
lines.push('', 'Usage:', '', ...splitIntoLines(this.usage, width), '');
lines.push('Usage:', ...splitIntoLines(this.usage, width), '');
}
return lines.join('\n');
}
Expand Down
31 changes: 31 additions & 0 deletions packages/client/src/repl/cmdTrace.mts
@@ -0,0 +1,31 @@
import type { Uri } from 'vscode';

import * as di from '../di.js';

export async function traceWord(word: string, uri: Uri | string, _width: number): Promise<string> {
const client = di.get('client');
const result = await client.serverApi.traceWord({ word, uri: uri.toString() });

if (!result) {
return 'No trace results';
}

const lines: string[] = [];

lines.push(`Trace: "${result.word}"`);

if (result.errors) {
lines.push('Errors:');
lines.push(` ${result.errors}`);
}

for (const trace of result.traces || []) {
lines.push(`${trace.word}: ${trace.found ? 'found' : 'not found'}`);
for (const line of trace.traces) {
const w = line.foundWord || line.word;
lines.push(`${w} ${line.dictName} ${line.found ? 'found' : 'not found'}`);
}
}

return lines.join('\n');
}
26 changes: 21 additions & 5 deletions packages/client/src/repl/repl.mts
Expand Up @@ -7,6 +7,7 @@ import * as vscode from 'vscode';
import { clearScreen, crlf, green, red, yellow } from './ansiUtils.mjs';
import { Application, Command, defArg } from './args.mjs';
import { cmdLs } from './cmdLs.mjs';
import { traceWord } from './cmdTrace.mjs';
import { consoleDebug } from './consoleDebug.mjs';
import { emitterToReadStream, emitterToWriteStream } from './emitterToWriteStream.mjs';
import type { DirEntry } from './fsUtils.mjs';
Expand Down Expand Up @@ -100,7 +101,7 @@ class Repl implements vscode.Disposable, vscode.Pseudoterminal {

#getApplication(): Application {
if (this.#application) return this.#application;
const app = new Application('repl', 'CSpell REPL', 'Usage: ????');
const app = new Application('CSpell REPL');
const cmdCheck = new Command(
'check',
'Spell check the files matching the globs.',
Expand All @@ -120,9 +121,9 @@ class Repl implements vscode.Disposable, vscode.Pseudoterminal {
const cmdTrace = new Command(
'trace',
'Trace which dictionaries contain the word.',
{ ...defArg('word', 'string', 'The word to trace.') },
{ ...defArg('word', 'string', 'The word to trace.', true) },
{},
(args) => this.log('Tracing... %o', args),
(args) => this.#cmdTrace(args.args),
);

const cmdPwd = new Command('pwd', 'Print the current working directory.', {}, {}, () => this.#cmdPwd());
Expand All @@ -143,8 +144,12 @@ class Repl implements vscode.Disposable, vscode.Pseudoterminal {
(args) => this.#cmdLs(args.args),
);

const cmdEnv = new Command('env', 'Show the environment.', { ...defArg('filter', 'string[]', 'Optional filter.') }, {}, (args) =>
this.#cmdEnv(args.args.filter),
const cmdEnv = new Command(
'env',
'Show environment variables.',
{ ...defArg('filter', 'string[]', 'Optional filter.') },
{},
(args) => this.#cmdEnv(args.args.filter),
);

const cmdExit = new Command('exit', 'Exit the REPL.', {}, {}, () => {
Expand Down Expand Up @@ -331,6 +336,17 @@ class Repl implements vscode.Disposable, vscode.Pseudoterminal {
}
}

async #cmdTrace(args: { word?: string | undefined }) {
consoleDebug('Repl.cmdTrace %o', args);
const { word } = args;
if (!word) {
this.log('No word specified.');
return;
}
const result = await traceWord(word, vscode.window.activeTextEditor?.document.uri || this.#cwd, this.#dimensions?.columns || 80);
this.log('%s', result);
}

async #cmdLs(args: { paths?: string[] | undefined }) {
consoleDebug('Repl.cmdLs %o', args);

Expand Down

0 comments on commit 3820703

Please sign in to comment.