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 child_process from 'child_process';
import * as path from 'path'; import * as path from 'path';
import * as timers from 'timers';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { setInterval } from 'timers'; import {StatusDisplay} from './watch_status';
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];
}
}
export class CargoWatchProvider { export class CargoWatchProvider {
private diagnosticCollection?: vscode.DiagnosticCollection; private diagnosticCollection?: vscode.DiagnosticCollection;
private cargoProcess?: child_process.ChildProcess; private cargoProcess?: child_process.ChildProcess;
private outBuffer: string = ""; private outBuffer: string = '';
private statusDisplay?: StatusDisplay; private statusDisplay?: StatusDisplay;
constructor() {
}
public activate(subscriptions: vscode.Disposable[]) { public activate(subscriptions: vscode.Disposable[]) {
subscriptions.push(this); subscriptions.push(this);
this.diagnosticCollection = vscode.languages.createDiagnosticCollection("rustc"); this.diagnosticCollection = vscode.languages.createDiagnosticCollection('rustc');
this.statusDisplay = new StatusDisplay(subscriptions); this.statusDisplay = new StatusDisplay(subscriptions);
// Start the cargo watch with json message // Start the cargo watch with json message
this.cargoProcess = child_process.spawn('cargo', this.cargoProcess = child_process.spawn('cargo',
["watch", "-x", "\"check --message-format json\""], ['watch', '-x', '\"check --message-format json\"'],
{ {
// stdio: ['ignore', 'pipe', 'ignore'], // stdio: ['ignore', 'pipe', 'ignore'],
shell: true, shell: true,
@ -68,11 +31,11 @@ export class CargoWatchProvider {
}); });
this.cargoProcess.stderr.on('data', (s: string) => { 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) => { 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) { private parseLine(line: string) {
if (line.startsWith("[Running")) { if (line.startsWith('[Running')) {
this.diagnosticCollection!.clear(); this.diagnosticCollection!.clear();
this.statusDisplay!.show(); this.statusDisplay!.show();
} }
if (line.startsWith("[Finished running")) { if (line.startsWith('[Finished running')) {
this.statusDisplay!.hide(); this.statusDisplay!.hide();
} }
function getLevel(s: string): vscode.DiagnosticSeverity { function getLevel(s: string): vscode.DiagnosticSeverity {
if (s === "error") if (s === 'error') {
return vscode.DiagnosticSeverity.Error; return vscode.DiagnosticSeverity.Error;
}
if (s.startsWith("warn")) if (s.startsWith('warn')) {
return vscode.DiagnosticSeverity.Warning; return vscode.DiagnosticSeverity.Warning;
}
return vscode.DiagnosticSeverity.Information; return vscode.DiagnosticSeverity.Information;
} }
@ -117,51 +82,51 @@ export class CargoWatchProvider {
} }
// Only handle compiler-message now // Only handle compiler-message now
if (data.reason !== "compiler-message") { if (data.reason !== 'compiler-message') {
return; return;
} }
let spans: any[] = data.message.spans; let spans: any[] = data.message.spans;
spans = spans.filter(o => o.is_primary); spans = spans.filter(o => o.is_primary);
let file_name = null;
// We only handle primary span right now. // We only handle primary span right now.
if (spans.length > 0) { if (spans.length > 0) {
let o = spans[0]; const o = spans[0];
console.log("o", o); const rendered = data.message.rendered;
let rendered = data.message.rendered; const level = getLevel(data.message.level);
let level = getLevel(data.message.level); const range = new vscode.Range(
let range = new vscode.Range(
new vscode.Position(o.line_start - 1, o.column_start - 1), new vscode.Position(o.line_start - 1, o.column_start - 1),
new vscode.Position(o.line_end - 1, o.column_end - 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); const diagnostic = new vscode.Diagnostic(range, rendered, level);
diagnostic.source = 'rustc'; diagnostic.source = 'rustc';
diagnostic.code = data.message.code.code; diagnostic.code = data.message.code.code;
diagnostic.relatedInformation = []; 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); diagnostics.push(diagnostic);
this.diagnosticCollection!.set(fileUrl, diagnostics); 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. // The stdout is not line based, convert it to line based for proceess.
this.outBuffer += chunk; this.outBuffer += chunk;
let eolIndex; let eolIndex = this.outBuffer.indexOf('\n');
while ((eolIndex = this.outBuffer.indexOf('\n')) >= 0) { while (eolIndex >= 0) {
// line includes the EOL // line includes the EOL
const line = this.outBuffer.slice(0, eolIndex + 1); const line = this.outBuffer.slice(0, eolIndex + 1);
this.parseLine(line); this.parseLine(line);
this.outBuffer = this.outBuffer.slice(eolIndex + 1); 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];
}
}