about summary refs log tree commit diff
path: root/editors/code
diff options
context:
space:
mode:
authorVeetaha <gerzoh1@gmail.com>2020-03-09 19:54:54 +0200
committerVeetaha <gerzoh1@gmail.com>2020-03-14 02:01:46 +0200
commit98b2a942d1c67f80a67a5779ecaa482f84c3a30d (patch)
treee5cfafdaa7dcc537851bc7c4db3ae9d4967553f7 /editors/code
parent8203828bb081faae4cd9d39c8abe6bc073138176 (diff)
downloadrust-98b2a942d1c67f80a67a5779ecaa482f84c3a30d.tar.gz
rust-98b2a942d1c67f80a67a5779ecaa482f84c3a30d.zip
vscode-prerefactor: add some utility functions
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/src/util.ts69
1 files changed, 63 insertions, 6 deletions
diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts
index 95a5f1227ca..2bfc145e6fe 100644
--- a/editors/code/src/util.ts
+++ b/editors/code/src/util.ts
@@ -1,5 +1,6 @@
 import * as lc from "vscode-languageclient";
 import * as vscode from "vscode";
+import { promises as dns } from "dns";
 import { strict as nativeAssert } from "assert";
 
 export function assert(condition: boolean, explanation: string): asserts condition {
@@ -11,21 +12,40 @@ export function assert(condition: boolean, explanation: string): asserts conditi
     }
 }
 
-export const log = {
-    enabled: true,
+export const log = new class {
+    private enabled = true;
+
+    setEnabled(yes: boolean): void {
+        log.enabled = yes;
+    }
+
     debug(message?: any, ...optionalParams: any[]): void {
         if (!log.enabled) return;
         // eslint-disable-next-line no-console
         console.log(message, ...optionalParams);
-    },
+    }
+
     error(message?: any, ...optionalParams: any[]): void {
         if (!log.enabled) return;
         debugger;
         // eslint-disable-next-line no-console
         console.error(message, ...optionalParams);
-    },
-    setEnabled(yes: boolean): void {
-        log.enabled = yes;
+    }
+
+    downloadError(err: Error, artifactName: string, repoName: string) {
+        vscode.window.showErrorMessage(
+            `Failed to download the rust-analyzer ${artifactName} from ${repoName} ` +
+            `GitHub repository: ${err.message}`
+        );
+        log.error(err);
+        dns.resolve('example.com').then(
+            addrs => log.debug("DNS resolution for example.com was successful", addrs),
+            err => log.error(
+                "DNS resolution for example.com failed, " +
+                "there might be an issue with Internet availability",
+                err
+            )
+        );
     }
 };
 
@@ -66,6 +86,17 @@ function sleep(ms: number) {
     return new Promise(resolve => setTimeout(resolve, ms));
 }
 
+export function notReentrant<TThis, TParams extends any[], TRet>(
+    fn: (this: TThis, ...params: TParams) => Promise<TRet>
+): typeof fn {
+    let entered = false;
+    return function(...params) {
+        assert(!entered, `Reentrancy invariant for ${fn.name} is violated`);
+        entered = true;
+        return fn.apply(this, params).finally(() => entered = false);
+    };
+}
+
 export type RustDocument = vscode.TextDocument & { languageId: "rust" };
 export type RustEditor = vscode.TextEditor & { document: RustDocument; id: string };
 
@@ -79,3 +110,29 @@ export function isRustDocument(document: vscode.TextDocument): document is RustD
 export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor {
     return isRustDocument(editor.document);
 }
+
+/**
+ * @param extensionId The canonical extension identifier in the form of: `publisher.name`
+ */
+export async function vscodeReinstallExtension(extensionId: string) {
+    // Unfortunately there is no straightforward way as of now, these commands
+    // were found in vscode source code.
+
+    log.debug("Uninstalling extension", extensionId);
+    await vscode.commands.executeCommand("workbench.extensions.uninstallExtension", extensionId);
+    log.debug("Installing extension", extensionId);
+    await vscode.commands.executeCommand("workbench.extensions.installExtension", extensionId);
+}
+
+export async function vscodeReloadWindow(): Promise<never> {
+    await vscode.commands.executeCommand("workbench.action.reloadWindow");
+
+    assert(false, "unreachable");
+}
+
+export async function vscodeInstallExtensionFromVsix(vsixPath: string) {
+    await vscode.commands.executeCommand(
+        "workbench.extensions.installExtension",
+        vscode.Uri.file(vsixPath)
+    );
+}