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
193 lines (174 loc) · 9.41 KB
/
Copy pathextension.ts
File metadata and controls
193 lines (174 loc) · 9.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
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
184
185
186
187
188
189
190
191
192
193
'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 { ShebangCodeLensProvider } from './providers/shebangCodeLensProvider'
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 { SetInterpreterProvider } 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';
import { activateGoToObjectDefinitionProvider } from './providers/objectDefinitionProvider';
import { InterpreterManager } from './interpreter';
const PYTHON: vscode.DocumentFilter = { language: 'python' };
let unitTestOutChannel: vscode.OutputChannel;
let formatOutChannel: vscode.OutputChannel;
let lintingOutChannel: vscode.OutputChannel;
let jupMain: jup.Jupyter;
export async function activate(context: vscode.ExtensionContext) {
const pythonSettings = settings.PythonSettings.getInstance();
const pythonExt = new PythonExt();
context.subscriptions.push(pythonExt);
// 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);
const interpreterManager = new InterpreterManager();
await interpreterManager.autoSetInterpreter();
context.subscriptions.push(interpreterManager);
context.subscriptions.push(new SetInterpreterProvider(interpreterManager));
context.subscriptions.push(...activateExecInTerminalProvider());
context.subscriptions.push(activateUpdateSparkLibraryProvider());
activateSimplePythonRefactorProvider(context, formatOutChannel);
context.subscriptions.push(activateFormatOnSaveProvider(PYTHON, settings.PythonSettings.getInstance(), formatOutChannel));
context.subscriptions.push(activateGoToObjectDefinitionProvider(context));
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: '# ' },
},
{
beforeText: /^\s+(continue|break|return)\b.*$/,
action: { indentAction: vscode.IndentAction.Outdent },
}
]
});
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.registerCodeLensProvider(PYTHON, new ShebangCodeLensProvider()))
const symbolProvider = new PythonSymbolProvider(context, jediProx);
context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(PYTHON, symbolProvider));
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, 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();
const hepProvider = new HelpProvider();
context.subscriptions.push(hepProvider);
context.subscriptions.push(activateSingleFileDebug());
}
class PythonExt implements vscode.Disposable {
private isDjangoProject: ContextKey;
constructor() {
this.isDjangoProject = new ContextKey('python.isDjangoProject');
this.ensureState();
}
public dispose() {
this.isDjangoProject = null;
}
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 lastValue: boolean;
constructor(private name: string) {
}
public set(value: boolean): void {
if (this.lastValue === value) {
return;
}
this.lastValue = value;
vscode.commands.executeCommand('setContext', this.name, this.lastValue);
}
}