aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
authorIgor Matuszewski <[email protected]>2019-03-18 21:30:23 +0000
committerIgor Matuszewski <[email protected]>2019-03-18 21:30:23 +0000
commit60cac299640912ad1ad75644bfa0088d7ba6e367 (patch)
tree50aab7bd4a56995fa965ebd4b53d70506a8b9b4d /editors
parent5c3cc8c95f392c523bb638d78e0217780d6e8476 (diff)
Separate out the interactive cargo watch procedure
Diffstat (limited to 'editors')
-rw-r--r--editors/code/src/commands/runnables.ts68
-rw-r--r--editors/code/src/extension.ts66
2 files changed, 70 insertions, 64 deletions
diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts
index 23fd280b4..285afaaf6 100644
--- a/editors/code/src/commands/runnables.ts
+++ b/editors/code/src/commands/runnables.ts
@@ -1,5 +1,8 @@
1import { exec } 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';
5
3import { Server } from '../server'; 6import { Server } from '../server';
4 7
5interface RunnablesParams { 8interface RunnablesParams {
@@ -33,7 +36,7 @@ interface CargoTaskDefinition extends vscode.TaskDefinition {
33 env?: { [key: string]: string }; 36 env?: { [key: string]: string };
34} 37}
35 38
36export function createTask(spec: Runnable): vscode.Task { 39function createTask(spec: Runnable): vscode.Task {
37 const TASK_SOURCE = 'Rust'; 40 const TASK_SOURCE = 'Rust';
38 const definition: CargoTaskDefinition = { 41 const definition: CargoTaskDefinition = {
39 type: 'cargo', 42 type: 'cargo',
@@ -143,3 +146,66 @@ export const autoCargoWatchTask: vscode.Task = {
143 runOn: 2 // RunOnOptions.folderOpen 146 runOn: 2 // RunOnOptions.folderOpen
144 } as unknown) as vscode.RunOptions 147 } as unknown) as vscode.RunOptions
145}; 148};
149
150/**
151 * Interactively asks the user whether we should run `cargo check` in order to
152 * provide inline diagnostics; the user is met with a series of dialog boxes
153 * that, when accepted, allow us to `cargo install cargo-watch` and then run it.
154 */
155export async function interactivelyStartCargoWatch() {
156 const execAsync = util.promisify(exec);
157
158 const watch = await vscode.window.showInformationMessage(
159 'Start watching changes with cargo? (Executes `cargo watch`, provides inline diagnostics)',
160 'yes',
161 'no'
162 );
163 if (watch === 'no') {
164 return;
165 }
166
167 const { stderr } = await execAsync('cargo watch --version').catch(e => e);
168 if (stderr.includes('no such subcommand: `watch`')) {
169 const msg =
170 'The `cargo-watch` subcommand is not installed. Install? (takes ~1-2 minutes)';
171 const install = await vscode.window.showInformationMessage(
172 msg,
173 'yes',
174 'no'
175 );
176 if (install === 'no') {
177 return;
178 }
179
180 const label = 'install-cargo-watch';
181 const taskFinished = new Promise((resolve, reject) => {
182 let disposable = vscode.tasks.onDidEndTask(({ execution }) => {
183 if (execution.task.name === label) {
184 disposable.dispose();
185 resolve();
186 }
187 });
188 });
189
190 vscode.tasks.executeTask(
191 createTask({
192 label,
193 bin: 'cargo',
194 args: ['install', 'cargo-watch'],
195 env: {}
196 })
197 );
198 await taskFinished;
199 const { stderr } = await execAsync('cargo watch --version').catch(
200 e => e
201 );
202 if (stderr !== '') {
203 vscode.window.showErrorMessage(
204 `Couldn't install \`cargo-\`watch: ${stderr}`
205 );
206 return;
207 }
208 }
209
210 vscode.tasks.executeTask(autoCargoWatchTask);
211}
diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts
index f915a5023..2e13c87de 100644
--- a/editors/code/src/extension.ts
+++ b/editors/code/src/extension.ts
@@ -1,10 +1,8 @@
1import { exec } from 'child_process';
2import * as util from 'util';
3import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
4import * as lc from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
5 3
6import * as commands from './commands'; 4import * as commands from './commands';
7import { autoCargoWatchTask, createTask } from './commands/runnables'; 5import { interactivelyStartCargoWatch } from './commands/runnables';
8import { SyntaxTreeContentProvider } from './commands/syntaxTree'; 6import { SyntaxTreeContentProvider } from './commands/syntaxTree';
9import * as events from './events'; 7import * as events from './events';
10import * as notifications from './notifications'; 8import * as notifications from './notifications';
@@ -122,8 +120,8 @@ export function activate(context: vscode.ExtensionContext) {
122 context.subscriptions 120 context.subscriptions
123 ); 121 );
124 122
125 // Attempts to run `cargo watch`, which provides inline diagnostics on save 123 // Executing `cargo watch` provides us with inline diagnostics on save
126 askToCargoWatch(); 124 interactivelyStartCargoWatch();
127 125
128 // Start the language server, finally! 126 // Start the language server, finally!
129 Server.start(allNotifications); 127 Server.start(allNotifications);
@@ -135,61 +133,3 @@ export function deactivate(): Thenable<void> {
135 } 133 }
136 return Server.client.stop(); 134 return Server.client.stop();
137} 135}
138
139async function askToCargoWatch() {
140 const watch = await vscode.window.showInformationMessage(
141 'Start watching changes with cargo? (Executes `cargo watch`, provides inline diagnostics)',
142 'yes',
143 'no'
144 );
145 if (watch === 'no') {
146 return;
147 }
148
149 const { stderr } = await util
150 .promisify(exec)('cargo watch --version')
151 .catch(e => e);
152 if (stderr.includes('no such subcommand: `watch`')) {
153 const msg =
154 'The `cargo-watch` subcommand is not installed. Install? (takes ~1-2 minutes)';
155 const install = await vscode.window.showInformationMessage(
156 msg,
157 'yes',
158 'no'
159 );
160 if (install === 'no') {
161 return;
162 }
163
164 const label = 'install-cargo-watch';
165 const taskFinished = new Promise((resolve, reject) => {
166 let disposable = vscode.tasks.onDidEndTask(({ execution }) => {
167 if (execution.task.name === label) {
168 disposable.dispose();
169 resolve();
170 }
171 });
172 });
173
174 vscode.tasks.executeTask(
175 createTask({
176 label,
177 bin: 'cargo',
178 args: ['install', 'cargo-watch'],
179 env: {}
180 })
181 );
182 await taskFinished;
183 const { stderr } = await util
184 .promisify(exec)('cargo watch --version')
185 .catch(e => e);
186 if (stderr !== '') {
187 vscode.window.showErrorMessage(
188 `Couldn't install \`cargo-\`watch: ${stderr}`
189 );
190 return;
191 }
192 }
193
194 vscode.tasks.executeTask(autoCargoWatchTask);
195}