forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
180 lines (166 loc) · 9.64 KB
/
extension.ts
File metadata and controls
180 lines (166 loc) · 9.64 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
import * as os from 'os';
import * as vscode from 'vscode';
import { BannerService } from './banner';
import * as settings from './common/configSettings';
import { createDeferred } from './common/helpers';
import { PersistentStateFactory } from './common/persistentState';
import { SimpleConfigurationProvider } from './debugger';
import { FeedbackService } from './feedback';
import { InterpreterManager } from './interpreter';
import { SetInterpreterProvider } from './interpreter/configuration/setInterpreterProvider';
import { ShebangCodeLensProvider } from './interpreter/display/shebangCodeLensProvider';
import { getCondaVersion } from './interpreter/helpers';
import { InterpreterVersionService } from './interpreter/interpreterVersion';
import * as jup from './jupyter/main';
import { JupyterProvider } from './jupyter/provider';
import { JediFactory } from './languageServices/jediProxyFactory';
import { PythonCompletionItemProvider } from './providers/completionProvider';
import { PythonDefinitionProvider } from './providers/definitionProvider';
import { activateExecInTerminalProvider } from './providers/execInTerminalProvider';
import { activateFormatOnSaveProvider } from './providers/formatOnSaveProvider';
import { PythonFormattingEditProvider } from './providers/formatProvider';
import { PythonHoverProvider } from './providers/hoverProvider';
import { LintProvider } from './providers/lintProvider';
import { activateGoToObjectDefinitionProvider } from './providers/objectDefinitionProvider';
import { PythonReferenceProvider } from './providers/referenceProvider';
import { PythonRenameProvider } from './providers/renameProvider';
import { ReplProvider } from './providers/replProvider';
import { PythonSignatureProvider } from './providers/signatureProvider';
import { activateSimplePythonRefactorProvider } from './providers/simpleRefactorProvider';
import { PythonSymbolProvider } from './providers/symbolProvider';
import { activateUpdateSparkLibraryProvider } from './providers/updateSparkLibraryProvider';
import * as sortImports from './sortImports';
import { sendTelemetryEvent } from './telemetry';
import { EDITOR_LOAD } from './telemetry/constants';
import { StopWatch } from './telemetry/stopWatch';
import { BlockFormatProviders } from './typeFormatters/blockFormatProvider';
import * as tests from './unittests/main';
import { WorkspaceSymbols } from './workspaceSymbols/main';
const PYTHON: vscode.DocumentFilter = { language: 'python' };
let unitTestOutChannel: vscode.OutputChannel;
let formatOutChannel: vscode.OutputChannel;
let lintingOutChannel: vscode.OutputChannel;
let jupMain: jup.Jupyter;
const activationDeferred = createDeferred<void>();
export const activated = activationDeferred.promise;
// tslint:disable-next-line:max-func-body-length
export async function activate(context: vscode.ExtensionContext) {
const pythonSettings = settings.PythonSettings.getInstance();
// tslint:disable-next-line:no-floating-promises
sendStartupTelemetry(activated);
lintingOutChannel = vscode.window.createOutputChannel(pythonSettings.linting.outputWindow);
formatOutChannel = lintingOutChannel;
if (pythonSettings.linting.outputWindow !== pythonSettings.formatting.outputWindow) {
formatOutChannel = vscode.window.createOutputChannel(pythonSettings.formatting.outputWindow);
formatOutChannel.clear();
}
if (pythonSettings.linting.outputWindow !== pythonSettings.unitTest.outputWindow) {
unitTestOutChannel = vscode.window.createOutputChannel(pythonSettings.unitTest.outputWindow);
unitTestOutChannel.clear();
}
sortImports.activate(context, formatOutChannel);
const interpreterManager = new InterpreterManager();
await interpreterManager.autoSetInterpreter();
// tslint:disable-next-line:no-floating-promises
interpreterManager.refresh();
context.subscriptions.push(interpreterManager);
const interpreterVersionService = new InterpreterVersionService();
context.subscriptions.push(new SetInterpreterProvider(interpreterManager, interpreterVersionService));
context.subscriptions.push(...activateExecInTerminalProvider());
context.subscriptions.push(activateUpdateSparkLibraryProvider());
activateSimplePythonRefactorProvider(context, formatOutChannel);
context.subscriptions.push(activateFormatOnSaveProvider(PYTHON, formatOutChannel));
const jediFactory = new JediFactory(context.asAbsolutePath('.'));
context.subscriptions.push(...activateGoToObjectDefinitionProvider(jediFactory));
context.subscriptions.push(new ReplProvider());
// Enable indentAction
// tslint:disable-next-line:no-non-null-assertion
vscode.languages.setLanguageConfiguration(PYTHON.language!, {
onEnterRules: [
{
beforeText: /^\s*(?:def|class|for|if|elif|else|while|try|with|finally|except|async).*?:\s*$/,
action: { indentAction: vscode.IndentAction.Indent }
},
{
beforeText: /^ *#.*$/,
afterText: /.+$/,
action: { indentAction: vscode.IndentAction.None, appendText: '# ' }
},
{
beforeText: /^\s+(continue|break|return)\b.*$/,
action: { indentAction: vscode.IndentAction.Outdent }
}
]
});
context.subscriptions.push(jediFactory);
context.subscriptions.push(vscode.languages.registerRenameProvider(PYTHON, new PythonRenameProvider(formatOutChannel)));
const definitionProvider = new PythonDefinitionProvider(jediFactory);
context.subscriptions.push(vscode.languages.registerDefinitionProvider(PYTHON, definitionProvider));
context.subscriptions.push(vscode.languages.registerHoverProvider(PYTHON, new PythonHoverProvider(jediFactory)));
context.subscriptions.push(vscode.languages.registerReferenceProvider(PYTHON, new PythonReferenceProvider(jediFactory)));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(PYTHON, new PythonCompletionItemProvider(jediFactory), '.'));
context.subscriptions.push(vscode.languages.registerCodeLensProvider(PYTHON, new ShebangCodeLensProvider()));
const symbolProvider = new PythonSymbolProvider(jediFactory);
context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(PYTHON, symbolProvider));
if (pythonSettings.devOptions.indexOf('DISABLE_SIGNATURE') === -1) {
context.subscriptions.push(vscode.languages.registerSignatureHelpProvider(PYTHON, new PythonSignatureProvider(jediFactory), '(', ','));
}
if (pythonSettings.formatting.provider !== 'none') {
const formatProvider = new PythonFormattingEditProvider(context, formatOutChannel);
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(PYTHON, formatProvider));
context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider(PYTHON, formatProvider));
}
const jupyterExtInstalled = vscode.extensions.getExtension('donjayamanne.jupyter');
// tslint:disable-next-line:promise-function-async
const linterProvider = new LintProvider(context, lintingOutChannel, (a, b) => Promise.resolve(false));
context.subscriptions.push();
if (jupyterExtInstalled) {
if (jupyterExtInstalled.isActive) {
// tslint:disable-next-line:no-unsafe-any
jupyterExtInstalled.exports.registerLanguageProvider(PYTHON.language, new JupyterProvider());
// tslint:disable-next-line:no-unsafe-any
linterProvider.documentHasJupyterCodeCells = jupyterExtInstalled.exports.hasCodeCells;
}
jupyterExtInstalled.activate().then(() => {
// tslint:disable-next-line:no-unsafe-any
jupyterExtInstalled.exports.registerLanguageProvider(PYTHON.language, new JupyterProvider());
// tslint:disable-next-line:no-unsafe-any
linterProvider.documentHasJupyterCodeCells = jupyterExtInstalled.exports.hasCodeCells;
});
} else {
jupMain = new jup.Jupyter(lintingOutChannel);
const documentHasJupyterCodeCells = jupMain.hasCodeCells.bind(jupMain);
jupMain.activate();
context.subscriptions.push(jupMain);
// tslint:disable-next-line:no-unsafe-any
linterProvider.documentHasJupyterCodeCells = documentHasJupyterCodeCells;
}
tests.activate(context, unitTestOutChannel, symbolProvider);
context.subscriptions.push(new WorkspaceSymbols(lintingOutChannel));
context.subscriptions.push(vscode.languages.registerOnTypeFormattingEditProvider(PYTHON, new BlockFormatProviders(), ':'));
// In case we have CR LF
const triggerCharacters: string[] = os.EOL.split('');
triggerCharacters.shift();
context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('python', new SimpleConfigurationProvider()));
activationDeferred.resolve();
const persistentStateFactory = new PersistentStateFactory(context.globalState, context.workspaceState);
const feedbackService = new FeedbackService(persistentStateFactory);
context.subscriptions.push(feedbackService);
// tslint:disable-next-line:no-unused-expression
new BannerService(persistentStateFactory);
}
async function sendStartupTelemetry(activatedPromise: Promise<void>) {
const stopWatch = new StopWatch();
// tslint:disable-next-line:no-floating-promises
activatedPromise.then(async () => {
const duration = stopWatch.elapsedTime;
let condaVersion: string | undefined;
try {
condaVersion = await getCondaVersion();
// tslint:disable-next-line:no-empty
} catch { }
const props = condaVersion ? { condaVersion } : undefined;
sendTelemetryEvent(EDITOR_LOAD, duration, props);
});
}