aboutsummaryrefslogtreecommitdiff
path: root/editors/code
diff options
context:
space:
mode:
authorIgor Matuszewski <[email protected]>2019-03-18 19:50:52 +0000
committerIgor Matuszewski <[email protected]>2019-03-18 19:50:52 +0000
commitafe9cea6402ec7d2deb129e823c3a6d19aa2c906 (patch)
tree22ac2d96c8f34f2991aebfb07f27a755b4dd359d /editors/code
parent9f1ae658dbb0d091bd384efbab93d622e5fff49f (diff)
Ask the user to install and start cargo watch
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/src/extension.ts66
1 files changed, 61 insertions, 5 deletions
diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts
index 941beba18..d5b496b1b 100644
--- a/editors/code/src/extension.ts
+++ b/editors/code/src/extension.ts
@@ -1,11 +1,15 @@
1import { exec, spawn } from 'child_process';
2import * as util from 'util';
1import * as vscode from 'vscode'; 3import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 4import * as lc from 'vscode-languageclient';
3 5
4import * as commands from './commands'; 6import * as commands from './commands';
7import { autoCargoWatchTask, createTask } from './commands/runnables';
5import { SyntaxTreeContentProvider } from './commands/syntaxTree'; 8import { SyntaxTreeContentProvider } from './commands/syntaxTree';
6import * as events from './events'; 9import * as events from './events';
7import * as notifications from './notifications'; 10import * as notifications from './notifications';
8import { Server } from './server'; 11import { Server } from './server';
12import { TextDecoder } from 'util';
9 13
10export function activate(context: vscode.ExtensionContext) { 14export function activate(context: vscode.ExtensionContext) {
11 function disposeOnDeactivation(disposable: vscode.Disposable) { 15 function disposeOnDeactivation(disposable: vscode.Disposable) {
@@ -89,11 +93,11 @@ export function activate(context: vscode.ExtensionContext) {
89 const allNotifications: Iterable< 93 const allNotifications: Iterable<
90 [string, lc.GenericNotificationHandler] 94 [string, lc.GenericNotificationHandler]
91 > = [ 95 > = [
92 [ 96 [
93 'rust-analyzer/publishDecorations', 97 'rust-analyzer/publishDecorations',
94 notifications.publishDecorations.handle 98 notifications.publishDecorations.handle
95 ] 99 ]
96 ]; 100 ];
97 const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); 101 const syntaxTreeContentProvider = new SyntaxTreeContentProvider();
98 102
99 // The events below are plain old javascript events, triggered and handled by vscode 103 // The events below are plain old javascript events, triggered and handled by vscode
@@ -119,6 +123,9 @@ export function activate(context: vscode.ExtensionContext) {
119 context.subscriptions 123 context.subscriptions
120 ); 124 );
121 125
126 // Attempts to run `cargo watch`, which provides inline diagnostics on save
127 askToCargoWatch();
128
122 // Start the language server, finally! 129 // Start the language server, finally!
123 Server.start(allNotifications); 130 Server.start(allNotifications);
124} 131}
@@ -129,3 +136,52 @@ export function deactivate(): Thenable<void> {
129 } 136 }
130 return Server.client.stop(); 137 return Server.client.stop();
131} 138}
139
140async function askToCargoWatch() {
141 const watch = await vscode.window.showInformationMessage(
142 'Start watching changes with cargo? (Executes `cargo watch`, provides inline diagnostics)',
143 'yes',
144 'no'
145 );
146 if (watch === 'no') {
147 return;
148 }
149
150 const { stderr } = await util.promisify(exec)('cargo watch --version').catch(e => e);
151 if (stderr.includes('no such subcommand: `watch`')) {
152 const msg = 'The `cargo-watch` subcommand is not installed. Install? (takes ~1-2 minutes)';
153 const install = await vscode.window.showInformationMessage(msg, 'yes', 'no');
154 if (install === 'no') {
155 return;
156 }
157
158 try {
159 // await vscode.tasks.executeTask(createTask({label: '', bin: 'cargo', args: ['install', 'cargo-watch'], env: {}}));
160
161 const channel = vscode.window.createOutputChannel('cargo-watch');
162 channel.show(false);
163 const sup = spawn('cargo', ['install', 'cargo-watch']);
164 sup.stderr.on('data', chunk => {
165 const output = new TextDecoder().decode(chunk);
166 channel.append(output);
167 });
168 await new Promise((resolve, reject) => {
169 sup.on('close', (code, signal) => {
170 if (code === 0) {
171 resolve(code);
172 } else {
173 reject(code);
174 }
175 });
176 });
177 channel.dispose();
178 } catch (err) {
179 vscode.window.showErrorMessage(
180 `Couldn't install \`cargo-watch\`: ${err.message}`
181 );
182 return;
183 }
184 }
185
186 vscode.tasks.executeTask(autoCargoWatchTask);
187}