aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--editors/code/src/commands/cargo_watch.ts91
-rw-r--r--editors/code/src/commands/watch_status.ts37
2 files changed, 65 insertions, 63 deletions
diff --git a/editors/code/src/commands/cargo_watch.ts b/editors/code/src/commands/cargo_watch.ts
index 55a1909cb..c6ce6ba06 100644
--- a/editors/code/src/commands/cargo_watch.ts
+++ b/editors/code/src/commands/cargo_watch.ts
@@ -1,61 +1,24 @@
1import * as child_process from 'child_process'; 1import * as child_process from 'child_process';
2import * as path from 'path'; 2import * as path from 'path';
3import * as timers from 'timers';
3import * as vscode from 'vscode'; 4import * as vscode from 'vscode';
4import { setInterval } from 'timers'; 5import {StatusDisplay} from './watch_status';
5
6const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
7
8class StatusDisplay {
9 private i = 0;
10 private statusBarItem: vscode.StatusBarItem;
11 private timer?: NodeJS.Timeout;
12
13 constructor(subscriptions: vscode.Disposable[]) {
14 this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 10);
15 subscriptions.push(this.statusBarItem);
16 this.statusBarItem.hide();
17 }
18
19 public show() {
20 this.timer = this.timer || setInterval(() => {
21 this.statusBarItem!.text = "cargo check " + this.frame();
22 }, 300);
23
24 this.statusBarItem!.show();
25 }
26
27 public hide() {
28 if(this.timer) {
29 clearInterval(this.timer);
30 this.timer = undefined;
31 }
32
33 this.statusBarItem!.hide();
34 }
35
36 frame() {
37 return spinnerFrames[this.i = ++this.i % spinnerFrames.length];
38 }
39}
40 6
41export class CargoWatchProvider { 7export class CargoWatchProvider {
42 private diagnosticCollection?: vscode.DiagnosticCollection; 8 private diagnosticCollection?: vscode.DiagnosticCollection;
43 private cargoProcess?: child_process.ChildProcess; 9 private cargoProcess?: child_process.ChildProcess;
44 private outBuffer: string = ""; 10 private outBuffer: string = '';
45 private statusDisplay? : StatusDisplay; 11 private statusDisplay?: StatusDisplay;
46
47 constructor() {
48 }
49 12
50 public activate(subscriptions: vscode.Disposable[]) { 13 public activate(subscriptions: vscode.Disposable[]) {
51 subscriptions.push(this); 14 subscriptions.push(this);
52 this.diagnosticCollection = vscode.languages.createDiagnosticCollection("rustc"); 15 this.diagnosticCollection = vscode.languages.createDiagnosticCollection('rustc');
53 16
54 this.statusDisplay = new StatusDisplay(subscriptions); 17 this.statusDisplay = new StatusDisplay(subscriptions);
55 18
56 // Start the cargo watch with json message 19 // Start the cargo watch with json message
57 this.cargoProcess = child_process.spawn('cargo', 20 this.cargoProcess = child_process.spawn('cargo',
58 ["watch", "-x", "\"check --message-format json\""], 21 ['watch', '-x', '\"check --message-format json\"'],
59 { 22 {
60 // stdio: ['ignore', 'pipe', 'ignore'], 23 // stdio: ['ignore', 'pipe', 'ignore'],
61 shell: true, 24 shell: true,
@@ -68,11 +31,11 @@ export class CargoWatchProvider {
68 }); 31 });
69 32
70 this.cargoProcess.stderr.on('data', (s: string) => { 33 this.cargoProcess.stderr.on('data', (s: string) => {
71 console.error('Error on cargo watch : ' + s); 34 // console.error('Error on cargo watch : ' + s);
72 }); 35 });
73 36
74 this.cargoProcess.on('error', (err: Error) => { 37 this.cargoProcess.on('error', (err: Error) => {
75 console.error('Error on spawn cargo process : ' + err); 38 // console.error('Error on spawn cargo process : ' + err);
76 }); 39 });
77 } 40 }
78 41
@@ -87,22 +50,24 @@ export class CargoWatchProvider {
87 } 50 }
88 } 51 }
89 52
90 parseLine(line: string) { 53 private parseLine(line: string) {
91 if (line.startsWith("[Running")) { 54 if (line.startsWith('[Running')) {
92 this.diagnosticCollection!.clear(); 55 this.diagnosticCollection!.clear();
93 this.statusDisplay!.show(); 56 this.statusDisplay!.show();
94 } 57 }
95 58
96 if (line.startsWith("[Finished running")) { 59 if (line.startsWith('[Finished running')) {
97 this.statusDisplay!.hide(); 60 this.statusDisplay!.hide();
98 } 61 }
99 62
100 function getLevel(s: string): vscode.DiagnosticSeverity { 63 function getLevel(s: string): vscode.DiagnosticSeverity {
101 if (s === "error") 64 if (s === 'error') {
102 return vscode.DiagnosticSeverity.Error; 65 return vscode.DiagnosticSeverity.Error;
66 }
103 67
104 if (s.startsWith("warn")) 68 if (s.startsWith('warn')) {
105 return vscode.DiagnosticSeverity.Warning; 69 return vscode.DiagnosticSeverity.Warning;
70 }
106 71
107 return vscode.DiagnosticSeverity.Information; 72 return vscode.DiagnosticSeverity.Information;
108 } 73 }
@@ -117,51 +82,51 @@ export class CargoWatchProvider {
117 } 82 }
118 83
119 // Only handle compiler-message now 84 // Only handle compiler-message now
120 if (data.reason !== "compiler-message") { 85 if (data.reason !== 'compiler-message') {
121 return; 86 return;
122 } 87 }
123 88
124 let spans: any[] = data.message.spans; 89 let spans: any[] = data.message.spans;
125 spans = spans.filter(o => o.is_primary); 90 spans = spans.filter(o => o.is_primary);
126 let file_name = null;
127 91
128 // We only handle primary span right now. 92 // We only handle primary span right now.
129 if (spans.length > 0) { 93 if (spans.length > 0) {
130 let o = spans[0]; 94 const o = spans[0];
131 95
132 console.log("o", o); 96 const rendered = data.message.rendered;
133 let rendered = data.message.rendered; 97 const level = getLevel(data.message.level);
134 let level = getLevel(data.message.level); 98 const range = new vscode.Range(
135 let range = new vscode.Range(
136 new vscode.Position(o.line_start - 1, o.column_start - 1), 99 new vscode.Position(o.line_start - 1, o.column_start - 1),
137 new vscode.Position(o.line_end - 1, o.column_end - 1) 100 new vscode.Position(o.line_end - 1, o.column_end - 1)
138 ); 101 );
139 102
140 file_name = path.join(vscode.workspace.rootPath!, o.file_name); 103 const fileName = path.join(vscode.workspace.rootPath!, o.file_name);
141 const diagnostic = new vscode.Diagnostic(range, rendered, level); 104 const diagnostic = new vscode.Diagnostic(range, rendered, level);
142 105
143 diagnostic.source = 'rustc'; 106 diagnostic.source = 'rustc';
144 diagnostic.code = data.message.code.code; 107 diagnostic.code = data.message.code.code;
145 diagnostic.relatedInformation = []; 108 diagnostic.relatedInformation = [];
146 109
147 let fileUrl = vscode.Uri.file(file_name!); 110 const fileUrl = vscode.Uri.file(fileName!);
148 111
149 let diagnostics: vscode.Diagnostic[] = [...(this.diagnosticCollection!.get(fileUrl) || [])]; 112 const diagnostics: vscode.Diagnostic[] = [...(this.diagnosticCollection!.get(fileUrl) || [])];
150 diagnostics.push(diagnostic); 113 diagnostics.push(diagnostic);
151 114
152 this.diagnosticCollection!.set(fileUrl, diagnostics); 115 this.diagnosticCollection!.set(fileUrl, diagnostics);
153 } 116 }
154 } 117 }
155 118
156 processOutput(chunk: string) { 119 private processOutput(chunk: string) {
157 // The stdout is not line based, convert it to line based for proceess. 120 // The stdout is not line based, convert it to line based for proceess.
158 this.outBuffer += chunk; 121 this.outBuffer += chunk;
159 let eolIndex; 122 let eolIndex = this.outBuffer.indexOf('\n');
160 while ((eolIndex = this.outBuffer.indexOf('\n')) >= 0) { 123 while (eolIndex >= 0) {
161 // line includes the EOL 124 // line includes the EOL
162 const line = this.outBuffer.slice(0, eolIndex + 1); 125 const line = this.outBuffer.slice(0, eolIndex + 1);
163 this.parseLine(line); 126 this.parseLine(line);
164 this.outBuffer = this.outBuffer.slice(eolIndex + 1); 127 this.outBuffer = this.outBuffer.slice(eolIndex + 1);
128
129 eolIndex = this.outBuffer.indexOf('\n');
165 } 130 }
166 } 131 }
167 132
diff --git a/editors/code/src/commands/watch_status.ts b/editors/code/src/commands/watch_status.ts
new file mode 100644
index 000000000..0943e8533
--- /dev/null
+++ b/editors/code/src/commands/watch_status.ts
@@ -0,0 +1,37 @@
1import * as timers from 'timers';
2import * as vscode from 'vscode';
3
4const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
5
6export class StatusDisplay {
7 private i = 0;
8 private statusBarItem: vscode.StatusBarItem;
9 private timer?: NodeJS.Timeout;
10
11 constructor(subscriptions: vscode.Disposable[]) {
12 this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 10);
13 subscriptions.push(this.statusBarItem);
14 this.statusBarItem.hide();
15 }
16
17 public show() {
18 this.timer = this.timer || setInterval(() => {
19 this.statusBarItem!.text = 'cargo check ' + this.frame();
20 }, 300);
21
22 this.statusBarItem!.show();
23 }
24
25 public hide() {
26 if (this.timer) {
27 clearInterval(this.timer);
28 this.timer = undefined;
29 }
30
31 this.statusBarItem!.hide();
32 }
33
34 private frame() {
35 return spinnerFrames[this.i = ++this.i % spinnerFrames.length];
36 }
37} \ No newline at end of file