From 02e450f354ccd978c90425929c635139210843a3 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 2 Apr 2019 14:43:02 +0800 Subject: Add cargo-watch.check-arguments --- editors/code/src/commands/cargo_watch.ts | 78 ++++++++++++++++++++------------ editors/code/src/commands/line_buffer.ts | 16 +++++++ 2 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 editors/code/src/commands/line_buffer.ts (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/cargo_watch.ts b/editors/code/src/commands/cargo_watch.ts index 9864ce01a..fb8fcaeb3 100644 --- a/editors/code/src/commands/cargo_watch.ts +++ b/editors/code/src/commands/cargo_watch.ts @@ -3,9 +3,9 @@ import * as path from 'path'; import * as vscode from 'vscode'; import { Server } from '../server'; import { terminate } from '../utils/processes'; +import { LineBuffer } from './line_buffer'; import { StatusDisplay } from './watch_status'; - export class CargoWatchProvider { private diagnosticCollection?: vscode.DiagnosticCollection; private cargoProcess?: child_process.ChildProcess; @@ -23,33 +23,44 @@ export class CargoWatchProvider { this.outputChannel = vscode.window.createOutputChannel( 'Cargo Watch Trace' ); - + + let args = '"check --message-format json'; + if (Server.config.cargoWatchOptions.checkArguments.length > 0) { + // Excape the double quote string: + args += ' ' + Server.config.cargoWatchOptions.checkArguments; + } + args += '"'; + // Start the cargo watch with json message this.cargoProcess = child_process.spawn( 'cargo', - ['watch', '-x', '\"check --message-format json\"'], + ['watch', '-x', args], { stdio: ['ignore', 'pipe', 'pipe'], cwd: vscode.workspace.rootPath, - windowsVerbatimArguments: true, + windowsVerbatimArguments: true } ); + const stdoutData = new LineBuffer(); this.cargoProcess.stdout.on('data', (s: string) => { - this.processOutput(s, (line) => { + stdoutData.processOutput(s, line => { this.logInfo(line); this.parseLine(line); }); }); + const stderrData = new LineBuffer(); this.cargoProcess.stderr.on('data', (s: string) => { - this.processOutput(s, (line) => { - this.logError('Error on cargo-watch : {\n' + line + '}\n' ); + stderrData.processOutput(s, line => { + this.logError('Error on cargo-watch : {\n' + line + '}\n'); }); }); this.cargoProcess.on('error', (err: Error) => { - this.logError('Error on cargo-watch process : {\n' + err.message + '}\n'); + this.logError( + 'Error on cargo-watch process : {\n' + err.message + '}\n' + ); }); this.logInfo('cargo-watch started.'); @@ -66,7 +77,7 @@ export class CargoWatchProvider { terminate(this.cargoProcess); } - if(this.outputChannel) { + if (this.outputChannel) { this.outputChannel.dispose(); } } @@ -74,13 +85,16 @@ export class CargoWatchProvider { private logInfo(line: string) { if (Server.config.cargoWatchOptions.trace === 'verbose') { this.outputChannel!.append(line); - } + } } private logError(line: string) { - if (Server.config.cargoWatchOptions.trace === 'error' || Server.config.cargoWatchOptions.trace === 'verbose' ) { + if ( + Server.config.cargoWatchOptions.trace === 'error' || + Server.config.cargoWatchOptions.trace === 'verbose' + ) { this.outputChannel!.append(line); - } + } } private parseLine(line: string) { @@ -105,12 +119,32 @@ export class CargoWatchProvider { return vscode.DiagnosticSeverity.Information; } + interface ErrorSpan { + line_start: number; + line_end: number; + column_start: number; + column_end: number; + } + + interface ErrorMessage { + reason: string; + message: { + spans: ErrorSpan[]; + rendered: string; + level: string; + code?: { + code: string; + }; + }; + } + // cargo-watch itself output non json format // Ignore these lines - let data = null; + let data: ErrorMessage; try { data = JSON.parse(line.trim()); } catch (error) { + this.logError(`Fail to pass to json : { ${error} }`); return; } @@ -137,7 +171,9 @@ export class CargoWatchProvider { const diagnostic = new vscode.Diagnostic(range, rendered, level); diagnostic.source = 'rustc'; - diagnostic.code = data.message.code.code; + diagnostic.code = data.message.code + ? data.message.code.code + : undefined; diagnostic.relatedInformation = []; const fileUrl = vscode.Uri.file(fileName!); @@ -150,18 +186,4 @@ export class CargoWatchProvider { this.diagnosticCollection!.set(fileUrl, diagnostics); } } - - private processOutput(chunk: string, cb: (line: string) => void ) { - // The stdout is not line based, convert it to line based for proceess. - this.outBuffer += chunk; - let eolIndex = this.outBuffer.indexOf('\n'); - while (eolIndex >= 0) { - // line includes the EOL - const line = this.outBuffer.slice(0, eolIndex + 1); - cb(line); - this.outBuffer = this.outBuffer.slice(eolIndex + 1); - - eolIndex = this.outBuffer.indexOf('\n'); - } - } } diff --git a/editors/code/src/commands/line_buffer.ts b/editors/code/src/commands/line_buffer.ts new file mode 100644 index 000000000..fb5b9f7f2 --- /dev/null +++ b/editors/code/src/commands/line_buffer.ts @@ -0,0 +1,16 @@ +export class LineBuffer { + private outBuffer: string = ''; + + public processOutput(chunk: string, cb: (line: string) => void) { + this.outBuffer += chunk; + let eolIndex = this.outBuffer.indexOf('\n'); + while (eolIndex >= 0) { + // line includes the EOL + const line = this.outBuffer.slice(0, eolIndex + 1); + cb(line); + this.outBuffer = this.outBuffer.slice(eolIndex + 1); + + eolIndex = this.outBuffer.indexOf('\n'); + } + } +} -- cgit v1.2.3