Skip to content

Commit

Permalink
Add code frame in vti diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Nov 10, 2020
1 parent 335fea7 commit 05567e8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions vti/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"author": "Pine Wu <octref@gmail.com>",
"license": "MIT",
"dependencies": {
"@babel/code-frame": "^7.10.4",
"chalk": "^4.1.0",
"glob": "^7.1.2",
"vls": "^0.5.5",
Expand All @@ -20,6 +21,7 @@
"watch": "tsc --watch"
},
"devDependencies": {
"@types/babel__code-frame": "^7.0.2",
"@types/glob": "^7.1.3",
"typescript": "^4.0.5"
}
Expand Down
20 changes: 19 additions & 1 deletion vti/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { URI } from 'vscode-uri';
import * as glob from 'glob';
import * as path from 'path';
import * as chalk from 'chalk';
import { codeFrameColumns, SourceLocation } from '@babel/code-frame';
import { Range } from 'vscode-languageclient';

class NullLogger implements Logger {
error(_message: string): void {}
Expand Down Expand Up @@ -71,6 +73,19 @@ async function prepareClientConnection(workspaceUri: URI) {
return clientConnection;
}

function range2Location (range: Range): SourceLocation {
return {
start: {
line: range.start.line + 1,
column: range.start.character + 1
},
end: {
line: range.end.line + 1,
column: range.end.character + 1
}
};
}

async function getDiagnostics(workspaceUri: URI) {
const clientConnection = await prepareClientConnection(workspaceUri);

Expand All @@ -89,12 +104,13 @@ async function getDiagnostics(workspaceUri: URI) {
let errCount = 0;

for (const absFilePath of absFilePaths) {
const fileText = fs.readFileSync(absFilePath, 'utf-8');
await clientConnection.sendNotification(DidOpenTextDocumentNotification.type, {
textDocument: {
languageId: 'vue',
uri: URI.file(absFilePath).toString(),
version: 1,
text: fs.readFileSync(absFilePath, 'utf-8')
text: fileText
}
});

Expand All @@ -111,9 +127,11 @@ async function getDiagnostics(workspaceUri: URI) {
res.forEach(d => {
if (d.severity === DiagnosticSeverity.Error) {
console.log(`${chalk.red('Error')}: ${d.message.trim()}`);
console.log(codeFrameColumns(fileText, range2Location(d.range)));
errCount++;
} else {
console.log(`${chalk.yellow('Warn')} : ${d.message.trim()}`);
console.log(codeFrameColumns(fileText, range2Location(d.range)));
}
});
console.log('');
Expand Down
7 changes: 6 additions & 1 deletion vti/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# yarn lockfile v1


"@babel/code-frame@^7.0.0":
"@babel/code-frame@^7.0.0", "@babel/code-frame@^7.10.4":
version "7.10.4"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.10.4.tgz#168da1a36e90da68ae8d49c0f1b48c7c6249213a"
integrity sha512-vG6SvB6oYEhvgisZNFRmRCUkLz11c7rp+tbNTynGqc6mS1d5ATd/sGyV6W0KZZnXRKMTzZDRgQT3Ou9jhpAfUg==
Expand Down Expand Up @@ -237,6 +237,11 @@
dependencies:
defer-to-connect "^1.0.1"

"@types/babel__code-frame@^7.0.2":
version "7.0.2"
resolved "https://registry.yarnpkg.com/@types/babel__code-frame/-/babel__code-frame-7.0.2.tgz#e0c0f1648cbc09a9d4e5b4ed2ae9a6f7c8f5aeb0"
integrity sha512-imO+jT/yjOKOAS5GQZ8SDtwiIloAGGr6OaZDKB0V5JVaSfGZLat5K5/ZRtyKW6R60XHV3RHYPTFfhYb+wDKyKg==

"@types/eslint-visitor-keys@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
Expand Down

0 comments on commit 05567e8

Please sign in to comment.