forked from luabud/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
86 lines (78 loc) · 3.41 KB
/
common.ts
File metadata and controls
86 lines (78 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable:no-any no-invalid-this no-default-export no-console
import * as assert from 'assert';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as path from 'path';
import * as vscode from 'vscode';
import { JUPYTER_EXTENSION_ID } from '../../client/common/constants';
import { SMOKE_TEST_EXTENSIONS_DIR } from '../constants';
import { noop, sleep } from '../core';
export async function updateSetting(setting: string, value: any) {
const resource = vscode.workspace.workspaceFolders![0].uri;
await vscode.workspace
.getConfiguration('python', resource)
.update(setting, value, vscode.ConfigurationTarget.WorkspaceFolder);
}
export async function removeLanguageServerFiles() {
const folders = await getLanguageServerFolders();
await Promise.all(folders.map((item) => fs.remove(item).catch(noop)));
}
async function getLanguageServerFolders(): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
glob('languageServer.*', { cwd: SMOKE_TEST_EXTENSIONS_DIR }, (ex, matches) => {
ex ? reject(ex) : resolve(matches.map((item) => path.join(SMOKE_TEST_EXTENSIONS_DIR, item)));
});
});
}
export function isJediEnabled() {
const resource = vscode.workspace.workspaceFolders![0].uri;
const settings = vscode.workspace.getConfiguration('python', resource);
return settings.get<string>('languageServer') === 'Jedi';
}
export async function enableJedi(enable: boolean | undefined) {
if (isJediEnabled() === enable) {
return;
}
await updateSetting('languageServer', 'Jedi');
}
export async function openNotebookAndWaitForLS(file: string): Promise<vscode.NotebookDocument> {
await verifyExtensionIsAvailable(JUPYTER_EXTENSION_ID);
await vscode.commands.executeCommand('vscode.openWith', vscode.Uri.file(file), 'jupyter-notebook');
const notebook = vscode.notebook.activeNotebookEditor;
assert(notebook, 'Notebook did not open');
// Make sure LS completes file loading and analysis.
// In test mode it awaits for the completion before trying
// to fetch data for completion, hover.etc.
await vscode.commands.executeCommand(
'vscode.executeCompletionItemProvider',
notebook.document.cells[0].uri,
new vscode.Position(0, 0)
);
// For for LS to get extracted.
await sleep(10_000);
return notebook.document;
}
export async function openFileAndWaitForLS(file: string): Promise<vscode.TextDocument> {
const textDocument = await vscode.workspace.openTextDocument(file);
await vscode.window.showTextDocument(textDocument);
assert(vscode.window.activeTextEditor, 'No active editor');
// Make sure LS completes file loading and analysis.
// In test mode it awaits for the completion before trying
// to fetch data for completion, hover.etc.
await vscode.commands.executeCommand(
'vscode.executeCompletionItemProvider',
textDocument.uri,
new vscode.Position(0, 0)
);
// For for LS to get extracted.
await sleep(10_000);
return textDocument;
}
export async function verifyExtensionIsAvailable(extensionId: string): Promise<void> {
const extension = vscode.extensions.all.find((e) => e.id === extensionId);
assert.ok(extension, `Extension ${extensionId} not installed.`);
await extension.activate();
}