diff options
author | Igor Matuszewski <[email protected]> | 2019-03-18 19:50:52 +0000 |
---|---|---|
committer | Igor Matuszewski <[email protected]> | 2019-03-18 19:50:52 +0000 |
commit | afe9cea6402ec7d2deb129e823c3a6d19aa2c906 (patch) | |
tree | 22ac2d96c8f34f2991aebfb07f27a755b4dd359d /editors | |
parent | 9f1ae658dbb0d091bd384efbab93d622e5fff49f (diff) |
Ask the user to install and start cargo watch
Diffstat (limited to 'editors')
-rw-r--r-- | editors/code/src/extension.ts | 66 |
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 @@ | |||
1 | import { exec, spawn } from 'child_process'; | ||
2 | import * as util from 'util'; | ||
1 | import * as vscode from 'vscode'; | 3 | import * as vscode from 'vscode'; |
2 | import * as lc from 'vscode-languageclient'; | 4 | import * as lc from 'vscode-languageclient'; |
3 | 5 | ||
4 | import * as commands from './commands'; | 6 | import * as commands from './commands'; |
7 | import { autoCargoWatchTask, createTask } from './commands/runnables'; | ||
5 | import { SyntaxTreeContentProvider } from './commands/syntaxTree'; | 8 | import { SyntaxTreeContentProvider } from './commands/syntaxTree'; |
6 | import * as events from './events'; | 9 | import * as events from './events'; |
7 | import * as notifications from './notifications'; | 10 | import * as notifications from './notifications'; |
8 | import { Server } from './server'; | 11 | import { Server } from './server'; |
12 | import { TextDecoder } from 'util'; | ||
9 | 13 | ||
10 | export function activate(context: vscode.ExtensionContext) { | 14 | export 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 | |||
140 | async 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 | } | ||