Fix tslint error

This commit is contained in:
Edwin Cheng 2019-03-31 20:51:17 +08:00
parent b3683df0cd
commit ac8f35019b
2 changed files with 65 additions and 63 deletions

View File

@ -1,61 +1,24 @@
import * as child_process from 'child_process';
import * as path from 'path';
import * as timers from 'timers';
import * as vscode from 'vscode';
import { setInterval } from 'timers';
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
class StatusDisplay {
private i = 0;
private statusBarItem: vscode.StatusBarItem;
private timer?: NodeJS.Timeout;
constructor(subscriptions: vscode.Disposable[]) {
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 10);
subscriptions.push(this.statusBarItem);
this.statusBarItem.hide();
}
public show() {
this.timer = this.timer || setInterval(() => {
this.statusBarItem!.text = "cargo check " + this.frame();
}, 300);
this.statusBarItem!.show();
}
public hide() {
if(this.timer) {
clearInterval(this.timer);
this.timer = undefined;
}
this.statusBarItem!.hide();
}
frame() {
return spinnerFrames[this.i = ++this.i % spinnerFrames.length];
}
}
import {StatusDisplay} from './watch_status';
export class CargoWatchProvider {
private diagnosticCollection?: vscode.DiagnosticCollection;
private cargoProcess?: child_process.ChildProcess;
private outBuffer: string = "";
private statusDisplay? : StatusDisplay;
constructor() {
}
private outBuffer: string = '';
private statusDisplay?: StatusDisplay;
public activate(subscriptions: vscode.Disposable[]) {
subscriptions.push(this);
this.diagnosticCollection = vscode.languages.createDiagnosticCollection("rustc");
this.diagnosticCollection = vscode.languages.createDiagnosticCollection('rustc');
this.statusDisplay = new StatusDisplay(subscriptions);
// Start the cargo watch with json message
this.cargoProcess = child_process.spawn('cargo',
["watch", "-x", "\"check --message-format json\""],
['watch', '-x', '\"check --message-format json\"'],
{
// stdio: ['ignore', 'pipe', 'ignore'],
shell: true,
@ -68,11 +31,11 @@ export class CargoWatchProvider {
});
this.cargoProcess.stderr.on('data', (s: string) => {
console.error('Error on cargo watch : ' + s);
// console.error('Error on cargo watch : ' + s);
});
this.cargoProcess.on('error', (err: Error) => {
console.error('Error on spawn cargo process : ' + err);
// console.error('Error on spawn cargo process : ' + err);
});
}
@ -87,22 +50,24 @@ export class CargoWatchProvider {
}
}
parseLine(line: string) {
if (line.startsWith("[Running")) {
private parseLine(line: string) {
if (line.startsWith('[Running')) {
this.diagnosticCollection!.clear();
this.statusDisplay!.show();
}
if (line.startsWith("[Finished running")) {
if (line.startsWith('[Finished running')) {
this.statusDisplay!.hide();
}
function getLevel(s: string): vscode.DiagnosticSeverity {
if (s === "error")
if (s === 'error') {
return vscode.DiagnosticSeverity.Error;
}
if (s.startsWith("warn"))
if (s.startsWith('warn')) {
return vscode.DiagnosticSeverity.Warning;
}
return vscode.DiagnosticSeverity.Information;
}
@ -117,51 +82,51 @@ export class CargoWatchProvider {
}
// Only handle compiler-message now
if (data.reason !== "compiler-message") {
if (data.reason !== 'compiler-message') {
return;
}
let spans: any[] = data.message.spans;
spans = spans.filter(o => o.is_primary);
let file_name = null;
// We only handle primary span right now.
if (spans.length > 0) {
let o = spans[0];
const o = spans[0];
console.log("o", o);
let rendered = data.message.rendered;
let level = getLevel(data.message.level);
let range = new vscode.Range(
const rendered = data.message.rendered;
const level = getLevel(data.message.level);
const range = new vscode.Range(
new vscode.Position(o.line_start - 1, o.column_start - 1),
new vscode.Position(o.line_end - 1, o.column_end - 1)
);
file_name = path.join(vscode.workspace.rootPath!, o.file_name);
const fileName = path.join(vscode.workspace.rootPath!, o.file_name);
const diagnostic = new vscode.Diagnostic(range, rendered, level);
diagnostic.source = 'rustc';
diagnostic.code = data.message.code.code;
diagnostic.relatedInformation = [];
let fileUrl = vscode.Uri.file(file_name!);
const fileUrl = vscode.Uri.file(fileName!);
let diagnostics: vscode.Diagnostic[] = [...(this.diagnosticCollection!.get(fileUrl) || [])];
const diagnostics: vscode.Diagnostic[] = [...(this.diagnosticCollection!.get(fileUrl) || [])];
diagnostics.push(diagnostic);
this.diagnosticCollection!.set(fileUrl, diagnostics);
}
}
processOutput(chunk: string) {
private processOutput(chunk: string) {
// The stdout is not line based, convert it to line based for proceess.
this.outBuffer += chunk;
let eolIndex;
while ((eolIndex = this.outBuffer.indexOf('\n')) >= 0) {
let eolIndex = this.outBuffer.indexOf('\n');
while (eolIndex >= 0) {
// line includes the EOL
const line = this.outBuffer.slice(0, eolIndex + 1);
this.parseLine(line);
this.outBuffer = this.outBuffer.slice(eolIndex + 1);
eolIndex = this.outBuffer.indexOf('\n');
}
}

View File

@ -0,0 +1,37 @@
import * as timers from 'timers';
import * as vscode from 'vscode';
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
export class StatusDisplay {
private i = 0;
private statusBarItem: vscode.StatusBarItem;
private timer?: NodeJS.Timeout;
constructor(subscriptions: vscode.Disposable[]) {
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 10);
subscriptions.push(this.statusBarItem);
this.statusBarItem.hide();
}
public show() {
this.timer = this.timer || setInterval(() => {
this.statusBarItem!.text = 'cargo check ' + this.frame();
}, 300);
this.statusBarItem!.show();
}
public hide() {
if (this.timer) {
clearInterval(this.timer);
this.timer = undefined;
}
this.statusBarItem!.hide();
}
private frame() {
return spinnerFrames[this.i = ++this.i % spinnerFrames.length];
}
}