diff options
author | Zac Pullar-Strecker <[email protected]> | 2020-07-31 03:12:44 +0100 |
---|---|---|
committer | Zac Pullar-Strecker <[email protected]> | 2020-07-31 03:12:44 +0100 |
commit | f05d7b41a719d848844b054a16477b29d0f063c6 (patch) | |
tree | 0a8a0946e8aef2ce64d4c13d0035ba41cce2daf3 /editors/code/src/util.ts | |
parent | 73ff610e41959e3e7c78a2b4b25b086883132956 (diff) | |
parent | 6b7cb8b5ab539fc4333ce34bc29bf77c976f232a (diff) |
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Hasn't fixed tests yet.
Diffstat (limited to 'editors/code/src/util.ts')
-rw-r--r-- | editors/code/src/util.ts | 45 |
1 files changed, 36 insertions, 9 deletions
diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index fec4c3295..970fedb37 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts | |||
@@ -2,6 +2,7 @@ import * as lc from "vscode-languageclient"; | |||
2 | import * as vscode from "vscode"; | 2 | import * as vscode from "vscode"; |
3 | import { strict as nativeAssert } from "assert"; | 3 | import { strict as nativeAssert } from "assert"; |
4 | import { spawnSync } from "child_process"; | 4 | import { spawnSync } from "child_process"; |
5 | import { inspect } from "util"; | ||
5 | 6 | ||
6 | export function assert(condition: boolean, explanation: string): asserts condition { | 7 | export function assert(condition: boolean, explanation: string): asserts condition { |
7 | try { | 8 | try { |
@@ -14,21 +15,46 @@ export function assert(condition: boolean, explanation: string): asserts conditi | |||
14 | 15 | ||
15 | export const log = new class { | 16 | export const log = new class { |
16 | private enabled = true; | 17 | private enabled = true; |
18 | private readonly output = vscode.window.createOutputChannel("Rust Analyzer Client"); | ||
17 | 19 | ||
18 | setEnabled(yes: boolean): void { | 20 | setEnabled(yes: boolean): void { |
19 | log.enabled = yes; | 21 | log.enabled = yes; |
20 | } | 22 | } |
21 | 23 | ||
22 | debug(message?: any, ...optionalParams: any[]): void { | 24 | // Hint: the type [T, ...T[]] means a non-empty array |
25 | debug(...msg: [unknown, ...unknown[]]): void { | ||
23 | if (!log.enabled) return; | 26 | if (!log.enabled) return; |
24 | // eslint-disable-next-line no-console | 27 | log.write("DEBUG", ...msg); |
25 | console.log(message, ...optionalParams); | 28 | log.output.toString(); |
26 | } | 29 | } |
27 | 30 | ||
28 | error(message?: any, ...optionalParams: any[]): void { | 31 | info(...msg: [unknown, ...unknown[]]): void { |
32 | log.write("INFO", ...msg); | ||
33 | } | ||
34 | |||
35 | warn(...msg: [unknown, ...unknown[]]): void { | ||
36 | debugger; | ||
37 | log.write("WARN", ...msg); | ||
38 | } | ||
39 | |||
40 | error(...msg: [unknown, ...unknown[]]): void { | ||
29 | debugger; | 41 | debugger; |
30 | // eslint-disable-next-line no-console | 42 | log.write("ERROR", ...msg); |
31 | console.error(message, ...optionalParams); | 43 | log.output.show(true); |
44 | } | ||
45 | |||
46 | private write(label: string, ...messageParts: unknown[]): void { | ||
47 | const message = messageParts.map(log.stringify).join(" "); | ||
48 | const dateTime = new Date().toLocaleString(); | ||
49 | log.output.appendLine(`${label} [${dateTime}]: ${message}`); | ||
50 | } | ||
51 | |||
52 | private stringify(val: unknown): string { | ||
53 | if (typeof val === "string") return val; | ||
54 | return inspect(val, { | ||
55 | colors: false, | ||
56 | depth: 6, // heuristic | ||
57 | }); | ||
32 | } | 58 | } |
33 | }; | 59 | }; |
34 | 60 | ||
@@ -46,7 +72,7 @@ export async function sendRequestWithRetry<TParam, TRet>( | |||
46 | ); | 72 | ); |
47 | } catch (error) { | 73 | } catch (error) { |
48 | if (delay === null) { | 74 | if (delay === null) { |
49 | log.error("LSP request timed out", { method: reqType.method, param, error }); | 75 | log.warn("LSP request timed out", { method: reqType.method, param, error }); |
50 | throw error; | 76 | throw error; |
51 | } | 77 | } |
52 | 78 | ||
@@ -55,7 +81,7 @@ export async function sendRequestWithRetry<TParam, TRet>( | |||
55 | } | 81 | } |
56 | 82 | ||
57 | if (error.code !== lc.ErrorCodes.ContentModified) { | 83 | if (error.code !== lc.ErrorCodes.ContentModified) { |
58 | log.error("LSP request failed", { method: reqType.method, param, error }); | 84 | log.warn("LSP request failed", { method: reqType.method, param, error }); |
59 | throw error; | 85 | throw error; |
60 | } | 86 | } |
61 | 87 | ||
@@ -89,7 +115,8 @@ export function isValidExecutable(path: string): boolean { | |||
89 | 115 | ||
90 | const res = spawnSync(path, ["--version"], { encoding: 'utf8' }); | 116 | const res = spawnSync(path, ["--version"], { encoding: 'utf8' }); |
91 | 117 | ||
92 | log.debug(res, "--version output:", res.output); | 118 | const printOutput = res.error && (res.error as any).code !== 'ENOENT' ? log.warn : log.debug; |
119 | printOutput(path, "--version:", res); | ||
93 | 120 | ||
94 | return res.status === 0; | 121 | return res.status === 0; |
95 | } | 122 | } |