aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/util.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/util.ts')
-rw-r--r--editors/code/src/util.ts43
1 files changed, 39 insertions, 4 deletions
diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts
index 6f91f81d6..fe3fb71cd 100644
--- a/editors/code/src/util.ts
+++ b/editors/code/src/util.ts
@@ -1,6 +1,7 @@
1import * as lc from "vscode-languageclient"; 1import * as lc from "vscode-languageclient";
2import * as vscode from "vscode"; 2import * as vscode from "vscode";
3import { strict as nativeAssert } from "assert"; 3import { strict as nativeAssert } from "assert";
4import { spawnSync } from "child_process";
4 5
5export function assert(condition: boolean, explanation: string): asserts condition { 6export function assert(condition: boolean, explanation: string): asserts condition {
6 try { 7 try {
@@ -73,12 +74,46 @@ export type RustDocument = vscode.TextDocument & { languageId: "rust" };
73export type RustEditor = vscode.TextEditor & { document: RustDocument }; 74export type RustEditor = vscode.TextEditor & { document: RustDocument };
74 75
75export function isRustDocument(document: vscode.TextDocument): document is RustDocument { 76export function isRustDocument(document: vscode.TextDocument): document is RustDocument {
76 return document.languageId === 'rust' 77 // Prevent corrupted text (particularly via inlay hints) in diff views
77 // SCM diff views have the same URI as the on-disk document but not the same content 78 // by allowing only `file` schemes
78 && document.uri.scheme !== 'git' 79 // unfortunately extensions that use diff views not always set this
79 && document.uri.scheme !== 'svn'; 80 // to something different than 'file' (see ongoing bug: #4608)
81 return document.languageId === 'rust' && document.uri.scheme === 'file';
80} 82}
81 83
82export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor { 84export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor {
83 return isRustDocument(editor.document); 85 return isRustDocument(editor.document);
84} 86}
87
88export function isValidExecutable(path: string): boolean {
89 log.debug("Checking availability of a binary at", path);
90
91 const res = spawnSync(path, ["--version"], { encoding: 'utf8' });
92
93 log.debug(res, "--version output:", res.output);
94
95 return res.status === 0;
96}
97
98/** Sets ['when'](https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts) clause contexts */
99export function setContextValue(key: string, value: any): Thenable<void> {
100 return vscode.commands.executeCommand('setContext', key, value);
101}
102
103/**
104 * Returns a higher-order function that caches the results of invoking the
105 * underlying function.
106 */
107export function memoize<Ret, TThis, Param extends string>(func: (this: TThis, arg: Param) => Ret) {
108 const cache = new Map<string, Ret>();
109
110 return function(this: TThis, arg: Param) {
111 const cached = cache.get(arg);
112 if (cached) return cached;
113
114 const result = func.call(this, arg);
115 cache.set(arg, result);
116
117 return result;
118 };
119}