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
183 lines (163 loc) · 8.75 KB
/
extension.ts
File metadata and controls
183 lines (163 loc) · 8.75 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
181
182
183
'use strict';
import * as vscode from 'vscode';
import { PythonCompletionItemProvider } from './providers/completionProvider';
import { PythonHoverProvider } from './providers/hoverProvider';
import { PythonDefinitionProvider } from './providers/definitionProvider';
import { PythonReferenceProvider } from './providers/referenceProvider';
import { PythonRenameProvider } from './providers/renameProvider';
import { PythonFormattingEditProvider } from './providers/formatProvider';
import * as sortImports from './sortImports';
import { LintProvider } from './providers/lintProvider';
import { PythonSymbolProvider } from './providers/symbolProvider';
import { PythonSignatureProvider } from './providers/signatureProvider';
import * as settings from './common/configSettings';
import * as telemetryHelper from './common/telemetry';
import * as telemetryContracts from './common/telemetryContracts';
import { activateSimplePythonRefactorProvider } from './providers/simpleRefactorProvider';
import { activateSetInterpreterProvider } from './providers/setInterpreterProvider';
import { activateExecInTerminalProvider } from './providers/execInTerminalProvider';
import { Commands } from './common/constants';
import * as tests from './unittests/main';
import * as jup from './jupyter/main';
import { HelpProvider } from './helpProvider';
import { activateUpdateSparkLibraryProvider } from './providers/updateSparkLibraryProvider';
import { activateFormatOnSaveProvider } from './providers/formatOnSaveProvider';
import { WorkspaceSymbols } from './workspaceSymbols/main';
import { BlockFormatProviders } from './typeFormatters/blockFormatProvider';
import * as os from 'os';
import * as fs from 'fs';
import { activateSingleFileDebug } from './singleFileDebug';
import { getPathFromPythonCommand } from './common/utils';
import { JupyterProvider } from './jupyter/provider';
const PYTHON: vscode.DocumentFilter = { language: 'python', scheme: 'file' };
let unitTestOutChannel: vscode.OutputChannel;
let formatOutChannel: vscode.OutputChannel;
let lintingOutChannel: vscode.OutputChannel;
let jupMain: jup.Jupyter;
export function activate(context: vscode.ExtensionContext) {
let pythonSettings = settings.PythonSettings.getInstance();
let pythonExt = new PythonExt();
const hasPySparkInCompletionPath = pythonSettings.autoComplete.extraPaths.some(p => p.toLowerCase().indexOf('spark') >= 0);
telemetryHelper.sendTelemetryEvent(telemetryContracts.EVENT_LOAD, {
CodeComplete_Has_ExtraPaths: pythonSettings.autoComplete.extraPaths.length > 0 ? 'true' : 'false',
Format_Has_Custom_Python_Path: pythonSettings.pythonPath.length !== 'python'.length ? 'true' : 'false',
Has_PySpark_Path: hasPySparkInCompletionPath ? 'true' : 'false'
});
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);
context.subscriptions.push(activateSetInterpreterProvider());
context.subscriptions.push(...activateExecInTerminalProvider());
context.subscriptions.push(activateUpdateSparkLibraryProvider());
activateSimplePythonRefactorProvider(context, formatOutChannel);
context.subscriptions.push(activateFormatOnSaveProvider(PYTHON, settings.PythonSettings.getInstance(), formatOutChannel));
context.subscriptions.push(vscode.commands.registerCommand(Commands.Start_REPL, () => {
getPathFromPythonCommand(["-c", "import sys;print(sys.executable)"]).catch(() => {
return pythonSettings.pythonPath;
}).then(pythonExecutablePath => {
let term = vscode.window.createTerminal('Python', pythonExecutablePath);
term.show();
context.subscriptions.push(term);
});
}));
// Enable indentAction
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: '# ' }
}
]
});
context.subscriptions.push(vscode.languages.registerRenameProvider(PYTHON, new PythonRenameProvider(formatOutChannel)));
const definitionProvider = new PythonDefinitionProvider(context);
const jediProx = definitionProvider.JediProxy;
context.subscriptions.push(vscode.languages.registerDefinitionProvider(PYTHON, definitionProvider));
context.subscriptions.push(vscode.languages.registerHoverProvider(PYTHON, new PythonHoverProvider(context, jediProx)));
context.subscriptions.push(vscode.languages.registerReferenceProvider(PYTHON, new PythonReferenceProvider(context, jediProx)));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(PYTHON, new PythonCompletionItemProvider(context, jediProx), '.'));
context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(PYTHON, new PythonSymbolProvider(context, jediProx)));
if (pythonSettings.devOptions.indexOf('DISABLE_SIGNATURE') === -1) {
context.subscriptions.push(vscode.languages.registerSignatureHelpProvider(PYTHON, new PythonSignatureProvider(context, jediProx), '(', ','));
}
if (pythonSettings.formatting.provider !== 'none') {
const formatProvider = new PythonFormattingEditProvider(context, formatOutChannel, pythonSettings);
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(PYTHON, formatProvider));
context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider(PYTHON, formatProvider));
}
const jupyterExtInstalled = vscode.extensions.getExtension('donjayamanne.jupyter');
let linterProvider = new LintProvider(context, lintingOutChannel, (a, b) => Promise.resolve(false));
context.subscriptions.push();
if (jupyterExtInstalled) {
if (jupyterExtInstalled.isActive) {
jupyterExtInstalled.exports.registerLanguageProvider(PYTHON.language, new JupyterProvider());
linterProvider.documentHasJupyterCodeCells = jupyterExtInstalled.exports.hasCodeCells;
}
jupyterExtInstalled.activate().then(() => {
jupyterExtInstalled.exports.registerLanguageProvider(PYTHON.language, new JupyterProvider());
linterProvider.documentHasJupyterCodeCells = jupyterExtInstalled.exports.hasCodeCells;
});
}
else {
jupMain = new jup.Jupyter(lintingOutChannel);
const documentHasJupyterCodeCells = jupMain.hasCodeCells.bind(jupMain);
jupMain.activate();
context.subscriptions.push(jupMain);
linterProvider.documentHasJupyterCodeCells = documentHasJupyterCodeCells;
}
tests.activate(context, unitTestOutChannel);
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();
const hepProvider = new HelpProvider();
context.subscriptions.push(hepProvider);
context.subscriptions.push(activateSingleFileDebug());
}
// this method is called when your extension is deactivated
export function deactivate() {
}
class PythonExt {
private _isDjangoProject: ContextKey;
constructor() {
this._isDjangoProject = new ContextKey('python.isDjangoProject');
this._ensureState();
}
private _ensureState(): void {
// context: python.isDjangoProject
if (typeof vscode.workspace.rootPath === 'string') {
this._isDjangoProject.set(fs.existsSync(vscode.workspace.rootPath.concat("/manage.py")));
}
else {
this._isDjangoProject.set(false);
}
}
}
class ContextKey {
private _name: string;
private _lastValue: boolean;
constructor(name: string) {
this._name = name;
}
public set(value: boolean): void {
if (this._lastValue === value) {
return;
}
this._lastValue = value;
vscode.commands.executeCommand('setContext', this._name, this._lastValue);
}
}