aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/cargo_watch.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/commands/cargo_watch.ts')
-rw-r--r--editors/code/src/commands/cargo_watch.ts102
1 files changed, 58 insertions, 44 deletions
diff --git a/editors/code/src/commands/cargo_watch.ts b/editors/code/src/commands/cargo_watch.ts
index 32bd38a1c..1d939e28c 100644
--- a/editors/code/src/commands/cargo_watch.ts
+++ b/editors/code/src/commands/cargo_watch.ts
@@ -7,44 +7,55 @@ import { terminate } from '../utils/processes';
7import { LineBuffer } from './line_buffer'; 7import { LineBuffer } from './line_buffer';
8import { StatusDisplay } from './watch_status'; 8import { StatusDisplay } from './watch_status';
9 9
10export class CargoWatchProvider { 10export function registerCargoWatchProvider(
11 private diagnosticCollection?: vscode.DiagnosticCollection; 11 subscriptions: vscode.Disposable[]
12 private cargoProcess?: child_process.ChildProcess; 12): CargoWatchProvider | undefined {
13 private outBuffer: string = ''; 13 let cargoExists = false;
14 private statusDisplay?: StatusDisplay; 14 const cargoTomlFile = path.join(vscode.workspace.rootPath!, 'Cargo.toml');
15 private outputChannel?: vscode.OutputChannel; 15 // Check if the working directory is valid cargo root path
16 16 try {
17 public activate(subscriptions: vscode.Disposable[]) { 17 if (fs.existsSync(cargoTomlFile)) {
18 let cargoExists = false; 18 cargoExists = true;
19 const cargoTomlFile = path.join( 19 }
20 vscode.workspace.rootPath!, 20 } catch (err) {
21 'Cargo.toml' 21 cargoExists = false;
22 }
23
24 if (!cargoExists) {
25 vscode.window.showErrorMessage(
26 `Couldn\'t find \'Cargo.toml\' in ${cargoTomlFile}`
22 ); 27 );
23 // Check if the working directory is valid cargo root path 28 return;
24 try { 29 }
25 if (fs.existsSync(cargoTomlFile)) {
26 cargoExists = true;
27 }
28 } catch (err) {
29 cargoExists = false;
30 }
31 30
32 if (!cargoExists) { 31 const provider = new CargoWatchProvider();
33 vscode.window.showErrorMessage( 32 subscriptions.push(provider);
34 `Couldn\'t find \'Cargo.toml\' in ${cargoTomlFile}` 33 return provider;
35 ); 34}
36 return;
37 }
38 35
39 subscriptions.push(this); 36export class CargoWatchProvider implements vscode.Disposable {
37 private readonly diagnosticCollection: vscode.DiagnosticCollection;
38 private readonly statusDisplay: StatusDisplay;
39 private readonly outputChannel: vscode.OutputChannel;
40 private cargoProcess?: child_process.ChildProcess;
41
42 constructor() {
40 this.diagnosticCollection = vscode.languages.createDiagnosticCollection( 43 this.diagnosticCollection = vscode.languages.createDiagnosticCollection(
41 'rustc' 44 'rustc'
42 ); 45 );
43 46 this.statusDisplay = new StatusDisplay();
44 this.statusDisplay = new StatusDisplay(subscriptions);
45 this.outputChannel = vscode.window.createOutputChannel( 47 this.outputChannel = vscode.window.createOutputChannel(
46 'Cargo Watch Trace' 48 'Cargo Watch Trace'
47 ); 49 );
50 }
51
52 public start() {
53 if (this.cargoProcess) {
54 vscode.window.showInformationMessage(
55 'Cargo Watch is already running'
56 );
57 return;
58 }
48 59
49 let args = 'check --message-format json'; 60 let args = 'check --message-format json';
50 if (Server.config.cargoWatchOptions.checkArguments.length > 0) { 61 if (Server.config.cargoWatchOptions.checkArguments.length > 0) {
@@ -95,25 +106,28 @@ export class CargoWatchProvider {
95 this.logInfo('cargo-watch started.'); 106 this.logInfo('cargo-watch started.');
96 } 107 }
97 108
98 public dispose(): void { 109 public stop() {
99 if (this.diagnosticCollection) {
100 this.diagnosticCollection.clear();
101 this.diagnosticCollection.dispose();
102 }
103
104 if (this.cargoProcess) { 110 if (this.cargoProcess) {
105 this.cargoProcess.kill(); 111 this.cargoProcess.kill();
106 terminate(this.cargoProcess); 112 terminate(this.cargoProcess);
113 this.cargoProcess = undefined;
114 } else {
115 vscode.window.showInformationMessage('Cargo Watch is not running');
107 } 116 }
117 }
108 118
109 if (this.outputChannel) { 119 public dispose(): void {
110 this.outputChannel.dispose(); 120 this.stop();
111 } 121
122 this.diagnosticCollection.clear();
123 this.diagnosticCollection.dispose();
124 this.outputChannel.dispose();
125 this.statusDisplay.dispose();
112 } 126 }
113 127
114 private logInfo(line: string) { 128 private logInfo(line: string) {
115 if (Server.config.cargoWatchOptions.trace === 'verbose') { 129 if (Server.config.cargoWatchOptions.trace === 'verbose') {
116 this.outputChannel!.append(line); 130 this.outputChannel.append(line);
117 } 131 }
118 } 132 }
119 133
@@ -122,18 +136,18 @@ export class CargoWatchProvider {
122 Server.config.cargoWatchOptions.trace === 'error' || 136 Server.config.cargoWatchOptions.trace === 'error' ||
123 Server.config.cargoWatchOptions.trace === 'verbose' 137 Server.config.cargoWatchOptions.trace === 'verbose'
124 ) { 138 ) {
125 this.outputChannel!.append(line); 139 this.outputChannel.append(line);
126 } 140 }
127 } 141 }
128 142
129 private parseLine(line: string) { 143 private parseLine(line: string) {
130 if (line.startsWith('[Running')) { 144 if (line.startsWith('[Running')) {
131 this.diagnosticCollection!.clear(); 145 this.diagnosticCollection.clear();
132 this.statusDisplay!.show(); 146 this.statusDisplay.show();
133 } 147 }
134 148
135 if (line.startsWith('[Finished running')) { 149 if (line.startsWith('[Finished running')) {
136 this.statusDisplay!.hide(); 150 this.statusDisplay.hide();
137 } 151 }
138 152
139 function getLevel(s: string): vscode.DiagnosticSeverity { 153 function getLevel(s: string): vscode.DiagnosticSeverity {
@@ -193,7 +207,7 @@ export class CargoWatchProvider {
193 207
194 // The format of the package_id is "{name} {version} ({source_id})", 208 // The format of the package_id is "{name} {version} ({source_id})",
195 // https://github.com/rust-lang/cargo/blob/37ad03f86e895bb80b474c1c088322634f4725f5/src/cargo/core/package_id.rs#L53 209 // https://github.com/rust-lang/cargo/blob/37ad03f86e895bb80b474c1c088322634f4725f5/src/cargo/core/package_id.rs#L53
196 this.statusDisplay!.packageName = msg.package_id.split(' ')[0]; 210 this.statusDisplay.packageName = msg.package_id.split(' ')[0];
197 } else if (data.reason === 'compiler-message') { 211 } else if (data.reason === 'compiler-message') {
198 const msg = data.message as RustDiagnostic; 212 const msg = data.message as RustDiagnostic;
199 213