aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src
diff options
context:
space:
mode:
authorBenjamin Coenen <[email protected]>2020-05-02 19:27:02 +0100
committerBenjamin Coenen <[email protected]>2020-05-02 19:27:02 +0100
commit4613497a7714c6cd87166e6525d764d75f8acefd (patch)
tree2527ae2c0ef2ef100efee3fcb8899f8e34d0d573 /editors/code/src
parent19e28888aa41b2845b47adb7314aed99d3c48679 (diff)
parent89e1f97515c36ab97bd378d972cabec0feb6d77e (diff)
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into fix_4202
Diffstat (limited to 'editors/code/src')
-rw-r--r--editors/code/src/cargo.ts106
-rw-r--r--editors/code/src/commands/runnables.ts70
-rw-r--r--editors/code/src/config.ts9
3 files changed, 172 insertions, 13 deletions
diff --git a/editors/code/src/cargo.ts b/editors/code/src/cargo.ts
new file mode 100644
index 000000000..a328ba9bd
--- /dev/null
+++ b/editors/code/src/cargo.ts
@@ -0,0 +1,106 @@
1import * as cp from 'child_process';
2import * as readline from 'readline';
3import { OutputChannel } from 'vscode';
4
5interface CompilationArtifact {
6 fileName: string;
7 name: string;
8 kind: string;
9 isTest: boolean;
10}
11
12export class Cargo {
13 rootFolder: string;
14 env?: Record<string, string>;
15 output: OutputChannel;
16
17 public constructor(cargoTomlFolder: string, output: OutputChannel, env: Record<string, string> | undefined = undefined) {
18 this.rootFolder = cargoTomlFolder;
19 this.output = output;
20 this.env = env;
21 }
22
23 public async artifactsFromArgs(cargoArgs: string[]): Promise<CompilationArtifact[]> {
24 const artifacts: CompilationArtifact[] = [];
25
26 try {
27 await this.runCargo(cargoArgs,
28 message => {
29 if (message.reason === 'compiler-artifact' && message.executable) {
30 const isBinary = message.target.crate_types.includes('bin');
31 const isBuildScript = message.target.kind.includes('custom-build');
32 if ((isBinary && !isBuildScript) || message.profile.test) {
33 artifacts.push({
34 fileName: message.executable,
35 name: message.target.name,
36 kind: message.target.kind[0],
37 isTest: message.profile.test
38 });
39 }
40 }
41 else if (message.reason === 'compiler-message') {
42 this.output.append(message.message.rendered);
43 }
44 },
45 stderr => {
46 this.output.append(stderr);
47 }
48 );
49 }
50 catch (err) {
51 this.output.show(true);
52 throw new Error(`Cargo invocation has failed: ${err}`);
53 }
54
55 return artifacts;
56 }
57
58 public async executableFromArgs(args: string[]): Promise<string> {
59 const cargoArgs = [...args]; // to remain args unchanged
60 cargoArgs.push("--message-format=json");
61
62 const artifacts = await this.artifactsFromArgs(cargoArgs);
63
64 if (artifacts.length === 0) {
65 throw new Error('No compilation artifacts');
66 } else if (artifacts.length > 1) {
67 throw new Error('Multiple compilation artifacts are not supported.');
68 }
69
70 return artifacts[0].fileName;
71 }
72
73 runCargo(
74 cargoArgs: string[],
75 onStdoutJson: (obj: any) => void,
76 onStderrString: (data: string) => void
77 ): Promise<number> {
78 return new Promise<number>((resolve, reject) => {
79 const cargo = cp.spawn('cargo', cargoArgs, {
80 stdio: ['ignore', 'pipe', 'pipe'],
81 cwd: this.rootFolder,
82 env: this.env,
83 });
84
85 cargo.on('error', err => {
86 reject(new Error(`could not launch cargo: ${err}`));
87 });
88 cargo.stderr.on('data', chunk => {
89 onStderrString(chunk.toString());
90 });
91
92 const rl = readline.createInterface({ input: cargo.stdout });
93 rl.on('line', line => {
94 const message = JSON.parse(line);
95 onStdoutJson(message);
96 });
97
98 cargo.on('exit', (exitCode, _) => {
99 if (exitCode === 0)
100 resolve(exitCode);
101 else
102 reject(new Error(`exit code: ${exitCode}.`));
103 });
104 });
105 }
106} \ No newline at end of file
diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts
index 2635a1440..d77e8188c 100644
--- a/editors/code/src/commands/runnables.ts
+++ b/editors/code/src/commands/runnables.ts
@@ -1,8 +1,10 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
3import * as ra from '../rust-analyzer-api'; 3import * as ra from '../rust-analyzer-api';
4import * as os from "os";
4 5
5import { Ctx, Cmd } from '../ctx'; 6import { Ctx, Cmd } from '../ctx';
7import { Cargo } from '../cargo';
6 8
7export function run(ctx: Ctx): Cmd { 9export function run(ctx: Ctx): Cmd {
8 let prevRunnable: RunnableQuickPick | undefined; 10 let prevRunnable: RunnableQuickPick | undefined;
@@ -62,25 +64,69 @@ export function runSingle(ctx: Ctx): Cmd {
62 }; 64 };
63} 65}
64 66
67function getLldbDebugConfig(config: ra.Runnable, sourceFileMap: Record<string, string>): vscode.DebugConfiguration {
68 return {
69 type: "lldb",
70 request: "launch",
71 name: config.label,
72 cargo: {
73 args: config.args,
74 },
75 args: config.extraArgs,
76 cwd: config.cwd,
77 sourceMap: sourceFileMap
78 };
79}
80
81const debugOutput = vscode.window.createOutputChannel("Debug");
82
83async function getCppvsDebugConfig(config: ra.Runnable, sourceFileMap: Record<string, string>): Promise<vscode.DebugConfiguration> {
84 debugOutput.clear();
85
86 const cargo = new Cargo(config.cwd || '.', debugOutput);
87 const executable = await cargo.executableFromArgs(config.args);
88
89 // if we are here, there were no compilation errors.
90 return {
91 type: (os.platform() === "win32") ? "cppvsdbg" : 'cppdbg',
92 request: "launch",
93 name: config.label,
94 program: executable,
95 args: config.extraArgs,
96 cwd: config.cwd,
97 sourceFileMap: sourceFileMap,
98 };
99}
100
65export function debugSingle(ctx: Ctx): Cmd { 101export function debugSingle(ctx: Ctx): Cmd {
66 return async (config: ra.Runnable) => { 102 return async (config: ra.Runnable) => {
67 const editor = ctx.activeRustEditor; 103 const editor = ctx.activeRustEditor;
68 if (!editor) return; 104 if (!editor) return;
69 if (!vscode.extensions.getExtension("vadimcn.vscode-lldb")) { 105
70 vscode.window.showErrorMessage("Install `vadimcn.vscode-lldb` extension for debugging"); 106 const lldbId = "vadimcn.vscode-lldb";
107 const cpptoolsId = "ms-vscode.cpptools";
108
109 const debugEngineId = ctx.config.debug.engine;
110 let debugEngine = null;
111 if (debugEngineId === "auto") {
112 debugEngine = vscode.extensions.getExtension(lldbId);
113 if (!debugEngine) {
114 debugEngine = vscode.extensions.getExtension(cpptoolsId);
115 }
116 }
117 else {
118 debugEngine = vscode.extensions.getExtension(debugEngineId);
119 }
120
121 if (!debugEngine) {
122 vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=${lldbId})`
123 + ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=${cpptoolsId}) extension for debugging.`);
71 return; 124 return;
72 } 125 }
73 126
74 const debugConfig = { 127 const debugConfig = lldbId === debugEngine.id
75 type: "lldb", 128 ? getLldbDebugConfig(config, ctx.config.debug.sourceFileMap)
76 request: "launch", 129 : await getCppvsDebugConfig(config, ctx.config.debug.sourceFileMap);
77 name: config.label,
78 cargo: {
79 args: config.args,
80 },
81 args: config.extraArgs,
82 cwd: config.cwd
83 };
84 130
85 return vscode.debug.startDebugging(undefined, debugConfig); 131 return vscode.debug.startDebugging(undefined, debugConfig);
86 }; 132 };
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index 3b2eec8ba..110e54180 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -92,7 +92,6 @@ export class Config {
92 get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); } 92 get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
93 get traceExtension() { return this.get<boolean>("trace.extension"); } 93 get traceExtension() { return this.get<boolean>("trace.extension"); }
94 94
95
96 get inlayHints() { 95 get inlayHints() {
97 return { 96 return {
98 typeHints: this.get<boolean>("inlayHints.typeHints"), 97 typeHints: this.get<boolean>("inlayHints.typeHints"),
@@ -107,4 +106,12 @@ export class Config {
107 command: this.get<string>("checkOnSave.command"), 106 command: this.get<string>("checkOnSave.command"),
108 }; 107 };
109 } 108 }
109
110 get debug() {
111 return {
112 engine: this.get<string>("debug.engine"),
113 sourceFileMap: this.get<Record<string, string>>("debug.sourceFileMap"),
114 };
115 }
116
110} 117}