Skip to content

Commit

Permalink
fix: Display filenames instead of URI's (#1773)
Browse files Browse the repository at this point in the history
* fix: Display filenames instead of URI's
  When issues are displayed, show the filename, not the URI.
* fix: Fix issue display formatter.
  • Loading branch information
Jason3S committed Sep 26, 2021
1 parent bfd79ac commit 5a9542e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
10 changes: 5 additions & 5 deletions integration-tests/snapshots/php/php-src/snapshot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ Lines:
./Zend/bench.php:174:10 - Unknown word (fibo) -- function fibo_r($n){
./Zend/bench.php:18:10 - Unknown word (simplecall) -- function simplecall() {
./Zend/bench.php:218:10 - Unknown word (heapsort) -- function heapsort_r($n, &$ra) {
./Zend/bench.php:267:10 - Unknown word (mkmatrix) -- function mkmatrix (267s, $cols) {
./Zend/bench.php:278:10 - Unknown word (mmult) -- function mmult (278s, $cols, $m1,
./Zend/bench.php:267:10 - Unknown word (mkmatrix) -- function mkmatrix ($rows, $cols) {
./Zend/bench.php:278:10 - Unknown word (mmult) -- function mmult ($rows, $cols, $m1,
./Zend/bench.php:28:10 - Unknown word (simpleucall) -- function simpleucall() {
./Zend/bench.php:304:10 - Unknown word (nestedloop) -- function nestedloop($n) {
./Zend/bench.php:337:10 - Unknown word (strcat) -- function strcat($n) {
Expand Down Expand Up @@ -275,8 +275,8 @@ Lines:
./build/gen_stub.php:1921:108 - Unknown word (refsect) -- assReference')/db:refentry/db:refsect1[@role='description
./build/gen_stub.php:1921:96 - Unknown word (refentry) -- classReference')/db:refentry/db:refsect1[@role='descriptio
./build/gen_stub.php:2487:47 - Unknown word (conds) -- reprocessorConditions(array &$conds, Stmt $stmt): ?string
./build/gen_stub.php:2492:38 - Unknown word (ifdef) -- if (preg_match('/^#\s*ifdef\s+(.+)$/', ifdef, $matches
./build/gen_stub.php:2494:38 - Unknown word (ifndef) -- if (preg_match('/^#\s*ifndef\s+(.+)$/', ifndef, $matches
./build/gen_stub.php:2492:38 - Unknown word (ifdef) -- if (preg_match('/^#\s*ifdef\s+(.+)$/', $text, $matches
./build/gen_stub.php:2494:38 - Unknown word (ifndef) -- if (preg_match('/^#\s*ifndef\s+(.+)$/', $text, $matches
./build/gen_stub.php:2515:35 - Unknown word (stmts) -- getFileDocComment(array $stmts): ?DocComment {
./build/gen_stub.php:2978:34 - Unknown word (Xmls) -- if (replaceAndCompareXmls($doc, $classSynopsis
./build/gen_stub.php:2989:24 - Unknown word (phpdoc) -- "/<phpdoc:(classref|exceptionref
Expand Down Expand Up @@ -4313,7 +4313,7 @@ Lines:
./ext/xml/xml.c:815:20 - Unknown word (mytype) -- zval *curtag, *mytype, *myval;
./ext/xmlreader/php_xmlreader.c:1100:18 - Unknown word (nodec) -- xmlNode *node, *nodec;
./ext/xmlreader/php_xmlreader.c:710:38 - Unknown word (spcified) -- reader at attribute spcified by name and namespaceURI
./ext/xmlwriter/php_xmlwriter.stub.php:5:10 - Unknown word (xmlwriter) -- function xmlwriter_open_uri(string ./ext/xmlwriter/php_xmlwriter.stub.php
./ext/xmlwriter/php_xmlwriter.stub.php:5:10 - Unknown word (xmlwriter) -- function xmlwriter_open_uri(string $uri
./ext/xsl/xsltprocessor.c:309:20 - Unknown word (sheetp) -- xsltStylesheetPtr sheetp, oldsheetp;
./ext/xsl/xsltprocessor.c:309:28 - Unknown word (oldsheetp) -- xsltStylesheetPtr sheetp, oldsheetp;
./ext/xsl/xsltprocessor.c:311:45 - Unknown word (docu) -- prevExtDtdValue, clone_docu = 0;
Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async function execCheck(context: CheckContext, update: boolean): Promise<CheckR
log(color`* Checking: `);
log(color`* '${name}'`);
log(color`**********************************************\n`);
log(time());
if (!(await checkoutRepositoryAsync(logger, rep.url, rep.path, rep.commit))) {
logger.log('******** fail ********');
return Promise.resolve({ success: false, rep, elapsedTime: 0 });
Expand Down
65 changes: 38 additions & 27 deletions packages/cspell/src/cli-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import * as path from 'path';
import { Options } from './app';
import { URI } from 'vscode-uri';

const templateIssue = `{green $uri}:{yellow $row:$col} - $message ({red $text})`;
const templateIssueWithSuggestions = `{green $uri}:{yellow $row:$col} - $message ({red $text}) Suggestions: {yellow [$suggestions]}`;
const templateIssueWithContext = `{green $uri}:{yellow $row:$col} $padRowCol- $message ({red $text})$padContext -- {gray $contextLeft}{red {underline $text}}{gray $contextRight}`;
const templateIssueWithContextWithSuggestions = `{green $uri}:{yellow $row:$col} $padRowCol- $message ({red $text})$padContext -- {gray $contextLeft}{red {underline $text}}{gray $contextRight}\n\t Suggestions: {yellow [$suggestions]}`;
const templateIssueLegacy = `${chalk.green('$uri')}[$row, $col]: $message: ${chalk.red('$text')}`;
const templateIssue = `{green $filename}:{yellow $row:$col} - $message ({red $text})`;
const templateIssueWithSuggestions = `{green $filename}:{yellow $row:$col} - $message ({red $text}) Suggestions: {yellow [$suggestions]}`;
const templateIssueWithContext = `{green $filename}:{yellow $row:$col} $padRowCol- $message ({red $text})$padContext -- {gray $contextLeft}{red {underline $text}}{gray $contextRight}`;
const templateIssueWithContextWithSuggestions = `{green $filename}:{yellow $row:$col} $padRowCol- $message ({red $text})$padContext -- {gray $contextLeft}{red {underline $text}}{gray $contextRight}\n\t Suggestions: {yellow [$suggestions]}`;
const templateIssueLegacy = `${chalk.green('$filename')}[$row, $col]: $message: ${chalk.red('$text')}`;
const templateIssueWordsOnly = '$text';

interface ReporterIssue extends Issue {
filename: string;
}

function genIssueEmitter(template: string) {
const defaultWidth = 10;
let maxWidth = defaultWidth;
let uri: string | undefined;

return function issueEmitter(issue: Issue) {
return function issueEmitter(issue: ReporterIssue) {
if (uri !== issue.uri) {
maxWidth = defaultWidth;
uri = issue.uri;
Expand Down Expand Up @@ -99,11 +103,13 @@ export function getReporter(options: Options): CSpellReporter {

const root = URI.file(options.root || process.cwd());
const fsPathRoot = root.fsPath;
function relativeIssue(fn: (i: Issue) => void): (i: Issue) => void {
if (!options.relative) return fn;
function relativeIssue(fn: (i: ReporterIssue) => void): (i: Issue) => void {
const fnFilename = options.relative
? (uri: string) => relativeUriFilename(uri, fsPathRoot)
: (uri: string) => URI.parse(uri).fsPath;
return (i: Issue) => {
const r = { ...i };
r.uri = r.uri ? relativeUriFilename(r.uri, fsPathRoot) : r.uri;
const filename = i.uri ? fnFilename(i.uri) : '';
const r = { ...i, filename };
fn(r);
};
}
Expand All @@ -130,11 +136,11 @@ export function getReporter(options: Options): CSpellReporter {
};
}

function formatIssue(templateStr: string, issue: Issue, maxIssueTextWidth: number) {
function formatIssue(templateStr: string, issue: ReporterIssue, maxIssueTextWidth: number) {
function clean(t: string) {
return t.replace(/\s+/, ' ');
}
const { uri = '', row, col, text, context, offset } = issue;
const { uri = '', filename, row, col, text, context, offset } = issue;
const contextLeft = clean(context.text.slice(0, offset - context.offset));
const contextRight = clean(context.text.slice(offset + text.length - context.offset));
const contextFull = clean(context.text);
Expand All @@ -145,21 +151,26 @@ function formatIssue(templateStr: string, issue: Issue, maxIssueTextWidth: numbe
const suggestions = issue.suggestions?.join(', ') || '';
const message = issue.isFlagged ? '{yellow Forbidden word}' : 'Unknown word';
const t = template(templateStr.replace(/\$message/g, message));
return chalk(t)
.replace(/\$\{col\}/g, colText)
.replace(/\$\{row\}/g, rowText)
.replace(/\$\{text\}/g, text)
.replace(/\$\{uri\}/g, uri)
.replace(/\$col/g, colText)
.replace(/\$contextFull/g, contextFull)
.replace(/\$contextLeft/g, contextLeft)
.replace(/\$contextRight/g, contextRight)
.replace(/\$padContext/g, padContext)
.replace(/\$padRowCol/g, padRowCol)
.replace(/\$row/g, rowText)
.replace(/\$suggestions/g, suggestions)
.replace(/\$text/g, text)
.replace(/\$uri/g, uri);
return (
chalk(t)
.replace(/\$\{col\}/g, colText)
.replace(/\$\{filename\}/g, filename)
.replace(/\$\{row\}/g, rowText)
.replace(/\$\{text\}/g, text)
.replace(/\$\{uri\}/g, uri)
.replace(/\$col/g, colText)
.replace(/\$filename/g, filename)
.replace(/\$padContext/g, padContext)
.replace(/\$padRowCol/g, padRowCol)
.replace(/\$row/g, rowText)
.replace(/\$suggestions/g, suggestions)
.replace(/\$text/g, text)
.replace(/\$uri/g, uri)
// Note: context substitution needs to be last to prevent accidental substitution.
.replace(/\$contextFull/g, contextFull)
.replace(/\$contextLeft/g, contextLeft)
.replace(/\$contextRight/g, contextRight)
);
}

class TS extends Array<string> {
Expand Down

0 comments on commit 5a9542e

Please sign in to comment.