diff options
Diffstat (limited to 'editors/code/src/util.ts')
-rw-r--r-- | editors/code/src/util.ts | 43 |
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 @@ | |||
1 | import * as lc from "vscode-languageclient"; | 1 | 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 | 5 | ||
5 | export function assert(condition: boolean, explanation: string): asserts condition { | 6 | export function assert(condition: boolean, explanation: string): asserts condition { |
6 | try { | 7 | try { |
@@ -73,12 +74,46 @@ export type RustDocument = vscode.TextDocument & { languageId: "rust" }; | |||
73 | export type RustEditor = vscode.TextEditor & { document: RustDocument }; | 74 | export type RustEditor = vscode.TextEditor & { document: RustDocument }; |
74 | 75 | ||
75 | export function isRustDocument(document: vscode.TextDocument): document is RustDocument { | 76 | export 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 | ||
82 | export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor { | 84 | export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor { |
83 | return isRustDocument(editor.document); | 85 | return isRustDocument(editor.document); |
84 | } | 86 | } |
87 | |||
88 | export 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 */ | ||
99 | export 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 | */ | ||
107 | export 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 | } | ||