-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathextension.ts
227 lines (207 loc) · 6.96 KB
/
extension.ts
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import * as vscode from "vscode";
import { LanguageClient } from "vscode-languageclient/node";
import { LazyOutputChannel, logger } from "./common/logger";
import {
checkVersion,
initializePython,
onDidChangePythonInterpreter,
resolveInterpreter,
} from "./common/python";
import { startServer, stopServer } from "./common/server";
import {
checkIfConfigurationChanged,
getInterpreterFromSetting,
getWorkspaceSettings,
ISettings,
checkNotebookCodeActionsOnSave,
} from "./common/settings";
import { loadServerDefaults } from "./common/setup";
import { registerLanguageStatusItem, updateStatus } from "./common/status";
import {
getConfiguration,
onDidChangeConfiguration,
onDidGrantWorkspaceTrust,
registerCommand,
} from "./common/vscodeapi";
import { getProjectRoot } from "./common/utilities";
import {
executeAutofix,
executeFormat,
executeOrganizeImports,
createDebugInformationProvider,
} from "./common/commands";
let lsClient: LanguageClient | undefined;
let restartInProgress = false;
let restartQueued = false;
function getClient(): LanguageClient | undefined {
return lsClient;
}
export async function activate(context: vscode.ExtensionContext): Promise<void> {
// This is required to get server name and module. This should be
// the first thing that we do in this extension.
const serverInfo = loadServerDefaults();
const serverName = serverInfo.name;
const serverId = serverInfo.module;
// Log Server information
logger.info(`Name: ${serverInfo.name}`);
logger.info(`Module: ${serverInfo.module}`);
logger.debug(`Full Server Info: ${JSON.stringify(serverInfo)}`);
// Create output channels for the server and trace logs
const outputChannel = vscode.window.createOutputChannel(`${serverName} Language Server`);
const traceOutputChannel = new LazyOutputChannel(`${serverName} Language Server Trace`);
// Make sure that these channels are disposed when the extension is deactivated.
context.subscriptions.push(outputChannel);
context.subscriptions.push(traceOutputChannel);
context.subscriptions.push(logger.channel);
context.subscriptions.push(
onDidChangeConfiguration((event) => {
if (event.affectsConfiguration("ruff.enable")) {
vscode.window.showWarningMessage(
"To enable or disable Ruff after changing the `enable` setting, you must restart VS Code.",
);
}
}),
);
const { enable } = getConfiguration(serverId) as unknown as ISettings;
if (!enable) {
logger.info(
"Extension is disabled. To enable, change `ruff.enable` to `true` and restart VS Code.",
);
return;
}
if (restartInProgress) {
if (!restartQueued) {
// Schedule a new restart after the current restart.
logger.info(
`Triggered ${serverName} restart while restart is in progress; queuing a restart.`,
);
restartQueued = true;
}
return;
}
const runServer = async () => {
if (restartInProgress) {
if (!restartQueued) {
// Schedule a new restart after the current restart.
logger.info(
`Triggered ${serverName} restart while restart is in progress; queuing a restart.`,
);
restartQueued = true;
}
return;
}
restartInProgress = true;
try {
if (lsClient) {
await stopServer(lsClient);
}
const projectRoot = await getProjectRoot();
const workspaceSettings = await getWorkspaceSettings(serverId, projectRoot);
if (vscode.workspace.isTrusted) {
if (workspaceSettings.interpreter.length === 0) {
updateStatus(
vscode.l10n.t("Please select a Python interpreter."),
vscode.LanguageStatusSeverity.Error,
);
logger.error(
"Python interpreter missing:\r\n" +
"[Option 1] Select Python interpreter using the ms-python.python.\r\n" +
`[Option 2] Set an interpreter using "${serverId}.interpreter" setting.\r\n` +
"Please use Python 3.7 or greater.",
);
return;
}
logger.info(`Using interpreter: ${workspaceSettings.interpreter.join(" ")}`);
const resolvedEnvironment = await resolveInterpreter(workspaceSettings.interpreter);
if (resolvedEnvironment === undefined) {
updateStatus(
vscode.l10n.t("Python interpreter not found."),
vscode.LanguageStatusSeverity.Error,
);
logger.error(
"Unable to find any Python environment for the interpreter path:",
workspaceSettings.interpreter.join(" "),
);
return;
} else if (!checkVersion(resolvedEnvironment)) {
return;
}
}
lsClient = await startServer(
projectRoot,
workspaceSettings,
serverId,
serverName,
outputChannel,
traceOutputChannel,
);
} finally {
// Ensure that we reset the flag in case of an error, early return, or success.
restartInProgress = false;
if (restartQueued) {
restartQueued = false;
await runServer();
}
}
};
context.subscriptions.push(
onDidChangePythonInterpreter(async () => {
await runServer();
}),
onDidChangeConfiguration(async (e: vscode.ConfigurationChangeEvent) => {
if (checkIfConfigurationChanged(e, serverId)) {
await runServer();
}
}),
onDidGrantWorkspaceTrust(async () => {
await runServer();
}),
registerCommand(`${serverId}.showLogs`, () => {
logger.channel.show();
}),
registerCommand(`${serverId}.showServerLogs`, () => {
outputChannel.show();
}),
registerCommand(`${serverId}.restart`, async () => {
await runServer();
}),
registerCommand(`${serverId}.executeAutofix`, async () => {
if (lsClient) {
await executeAutofix(lsClient, serverId);
}
}),
registerCommand(`${serverId}.executeFormat`, async () => {
if (lsClient) {
await executeFormat(lsClient, serverId);
}
}),
registerCommand(`${serverId}.executeOrganizeImports`, async () => {
if (lsClient) {
await executeOrganizeImports(lsClient, serverId);
}
}),
registerCommand(
`${serverId}.debugInformation`,
createDebugInformationProvider(getClient, serverId, context),
),
registerLanguageStatusItem(serverId, serverName, `${serverId}.showLogs`),
);
checkNotebookCodeActionsOnSave(serverId);
setImmediate(async () => {
if (vscode.workspace.isTrusted) {
const interpreter = getInterpreterFromSetting(serverId);
if (interpreter === undefined || interpreter.length === 0) {
logger.info(`Python extension loading`);
await initializePython(context.subscriptions);
logger.info(`Python extension loaded`);
return; // The `onDidChangePythonInterpreter` event will trigger the server start.
}
}
await runServer();
});
}
export async function deactivate(): Promise<void> {
if (lsClient) {
await stopServer(lsClient);
}
}