forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
154 lines (147 loc) · 6.85 KB
/
api.ts
File metadata and controls
154 lines (147 loc) · 6.85 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { Event, Uri } from 'vscode';
import { isTestExecution } from './common/constants';
import { traceError } from './common/logger';
import { IConfigurationService, Resource } from './common/types';
import { IDataViewerDataProvider, IDataViewerFactory } from './datascience/data-viewing/types';
import { IJupyterUriProvider, IJupyterUriProviderRegistration } from './datascience/types';
import { getDebugpyLauncherArgs, getDebugpyPackagePath } from './debugger/extension/adapter/remoteLaunchers';
import { IInterpreterService } from './interpreter/contracts';
import { IServiceContainer, IServiceManager } from './ioc/types';
/*
* Do not introduce any breaking changes to this API.
* This is the public API for other extensions to interact with this extension.
*/
export interface IExtensionApi {
/**
* Promise indicating whether all parts of the extension have completed loading or not.
* @type {Promise<void>}
* @memberof IExtensionApi
*/
ready: Promise<void>;
debug: {
/**
* Generate an array of strings for commands to pass to the Python executable to launch the debugger for remote debugging.
* Users can append another array of strings of what they want to execute along with relevant arguments to Python.
* E.g `['/Users/..../pythonVSCode/pythonFiles/lib/python/debugpy', '--listen', 'localhost:57039', '--wait-for-client']`
* @param {string} host
* @param {number} port
* @param {boolean} [waitUntilDebuggerAttaches=true]
* @returns {Promise<string[]>}
*/
getRemoteLauncherCommand(host: string, port: number, waitUntilDebuggerAttaches: boolean): Promise<string[]>;
/**
* Gets the path to the debugger package used by the extension.
* @returns {Promise<string>}
*/
getDebuggerPackagePath(): Promise<string | undefined>;
};
/**
* Return internal settings within the extension which are stored in VSCode storage
*/
settings: {
/**
* An event that is emitted when execution details (for a resource) change. For instance, when interpreter configuration changes.
*/
readonly onDidChangeExecutionDetails: Event<Uri | undefined>;
/**
* Returns all the details the consumer needs to execute code within the selected environment,
* corresponding to the specified resource taking into account any workspace-specific settings
* for the workspace to which this resource belongs.
* @param {Resource} [resource] A resource for which the setting is asked for.
* * When no resource is provided, the setting scoped to the first workspace folder is returned.
* * If no folder is present, it returns the global setting.
* @returns {({ execCommand: string[] | undefined })}
*/
getExecutionDetails(
resource?: Resource
): {
/**
* E.g of execution commands returned could be,
* * `['<path to the interpreter set in settings>']`
* * `['<path to the interpreter selected by the extension when setting is not set>']`
* * `['conda', 'run', 'python']` which is used to run from within Conda environments.
* or something similar for some other Python environments.
*
* @type {(string[] | undefined)} When return value is `undefined`, it means no interpreter is set.
* Otherwise, join the items returned using space to construct the full execution command.
*/
execCommand: string[] | undefined;
};
};
datascience: {
/**
* Launches Data Viewer component.
* @param {IDataViewerDataProvider} dataProvider Instance that will be used by the Data Viewer component to fetch data.
* @param {string} title Data Viewer title
*/
showDataViewer(dataProvider: IDataViewerDataProvider, title: string): Promise<void>;
/**
* Registers a remote server provider component that's used to pick remote jupyter server URIs
* @param serverProvider object called back when picking jupyter server URI
*/
registerRemoteServerProvider(serverProvider: IJupyterUriProvider): void;
};
}
export function buildApi(
// tslint:disable-next-line:no-any
ready: Promise<any>,
serviceManager: IServiceManager,
serviceContainer: IServiceContainer
): IExtensionApi {
const configurationService = serviceContainer.get<IConfigurationService>(IConfigurationService);
const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
const api: IExtensionApi = {
// 'ready' will propagate the exception, but we must log it here first.
ready: ready.catch((ex) => {
traceError('Failure during activation.', ex);
return Promise.reject(ex);
}),
debug: {
async getRemoteLauncherCommand(
host: string,
port: number,
waitUntilDebuggerAttaches: boolean = true
): Promise<string[]> {
return getDebugpyLauncherArgs({
host,
port,
waitUntilDebuggerAttaches
});
},
async getDebuggerPackagePath(): Promise<string | undefined> {
return getDebugpyPackagePath();
}
},
settings: {
onDidChangeExecutionDetails: interpreterService.onDidChangeInterpreterConfiguration,
getExecutionDetails(resource?: Resource) {
const pythonPath = configurationService.getSettings(resource).pythonPath;
// If pythonPath equals an empty string, no interpreter is set.
return { execCommand: pythonPath === '' ? undefined : [pythonPath] };
}
},
datascience: {
async showDataViewer(dataProvider: IDataViewerDataProvider, title: string): Promise<void> {
const dataViewerProviderService = serviceContainer.get<IDataViewerFactory>(IDataViewerFactory);
await dataViewerProviderService.create(dataProvider, title);
},
registerRemoteServerProvider(picker: IJupyterUriProvider): void {
const container = serviceContainer.get<IJupyterUriProviderRegistration>(
IJupyterUriProviderRegistration
);
container.registerProvider(picker);
}
}
};
// In test environment return the DI Container.
if (isTestExecution()) {
// tslint:disable:no-any
(api as any).serviceContainer = serviceContainer;
(api as any).serviceManager = serviceManager;
// tslint:enable:no-any
}
return api;
}