Skip to content

Commit 5f308ad

Browse files
1 parent b4315ba commit 5f308ad

File tree

11 files changed

+888
-695
lines changed

11 files changed

+888
-695
lines changed

Source/common/constants.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import * as path from 'path';
4+
import * as path from "path";
55

66
const folderName = path.basename(__dirname);
77
export const EXTENSION_ROOT_DIR =
8-
folderName === 'common' ? path.dirname(path.dirname(__dirname)) : path.dirname(__dirname);
9-
export const BUNDLED_PYTHON_SCRIPTS_DIR = path.join(EXTENSION_ROOT_DIR, 'bundled');
10-
export const SERVER_SCRIPT_PATH = path.join(BUNDLED_PYTHON_SCRIPTS_DIR, 'tool', `lsp_server.py`);
11-
export const DEBUG_SERVER_SCRIPT_PATH = path.join(BUNDLED_PYTHON_SCRIPTS_DIR, 'tool', `_debug_server.py`);
8+
folderName === "common"
9+
? path.dirname(path.dirname(__dirname))
10+
: path.dirname(__dirname);
11+
export const BUNDLED_PYTHON_SCRIPTS_DIR = path.join(
12+
EXTENSION_ROOT_DIR,
13+
"bundled",
14+
);
15+
export const SERVER_SCRIPT_PATH = path.join(
16+
BUNDLED_PYTHON_SCRIPTS_DIR,
17+
"tool",
18+
`lsp_server.py`,
19+
);
20+
export const DEBUG_SERVER_SCRIPT_PATH = path.join(
21+
BUNDLED_PYTHON_SCRIPTS_DIR,
22+
"tool",
23+
`_debug_server.py`,
24+
);
1225
export const PYTHON_MAJOR = 3;
1326
export const PYTHON_MINOR = 8;
1427
export const PYTHON_VERSION = `${PYTHON_MAJOR}.${PYTHON_MINOR}`;

Source/common/logging.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import * as util from 'util';
5-
import { Disposable, LogOutputChannel } from 'vscode';
4+
import * as util from "util";
5+
import { Disposable, LogOutputChannel } from "vscode";
66

77
type Arguments = unknown[];
88
class OutputChannelLogger {
9-
constructor(private readonly channel: LogOutputChannel) {}
9+
constructor(private readonly channel: LogOutputChannel) {}
1010

11-
public traceLog(...data: Arguments): void {
12-
this.channel.appendLine(util.format(...data));
13-
}
11+
public traceLog(...data: Arguments): void {
12+
this.channel.appendLine(util.format(...data));
13+
}
1414

15-
public traceError(...data: Arguments): void {
16-
this.channel.error(util.format(...data));
17-
}
15+
public traceError(...data: Arguments): void {
16+
this.channel.error(util.format(...data));
17+
}
1818

19-
public traceWarn(...data: Arguments): void {
20-
this.channel.warn(util.format(...data));
21-
}
19+
public traceWarn(...data: Arguments): void {
20+
this.channel.warn(util.format(...data));
21+
}
2222

23-
public traceInfo(...data: Arguments): void {
24-
this.channel.info(util.format(...data));
25-
}
23+
public traceInfo(...data: Arguments): void {
24+
this.channel.info(util.format(...data));
25+
}
2626

27-
public traceVerbose(...data: Arguments): void {
28-
this.channel.debug(util.format(...data));
29-
}
27+
public traceVerbose(...data: Arguments): void {
28+
this.channel.debug(util.format(...data));
29+
}
3030
}
3131

3232
let channel: OutputChannelLogger | undefined;
3333
export function registerLogger(logChannel: LogOutputChannel): Disposable {
34-
channel = new OutputChannelLogger(logChannel);
35-
return {
36-
dispose: () => {
37-
channel = undefined;
38-
},
39-
};
34+
channel = new OutputChannelLogger(logChannel);
35+
return {
36+
dispose: () => {
37+
channel = undefined;
38+
},
39+
};
4040
}
4141

4242
export function traceLog(...args: Arguments): void {
43-
channel?.traceLog(...args);
43+
channel?.traceLog(...args);
4444
}
4545

4646
export function traceError(...args: Arguments): void {
47-
channel?.traceError(...args);
47+
channel?.traceError(...args);
4848
}
4949

5050
export function traceWarn(...args: Arguments): void {
51-
channel?.traceWarn(...args);
51+
channel?.traceWarn(...args);
5252
}
5353

5454
export function traceInfo(...args: Arguments): void {
55-
channel?.traceInfo(...args);
55+
channel?.traceInfo(...args);
5656
}
5757

5858
export function traceVerbose(...args: Arguments): void {
59-
channel?.traceVerbose(...args);
59+
channel?.traceVerbose(...args);
6060
}

Source/common/python.ts

Lines changed: 98 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,118 +2,133 @@
22
// Licensed under the MIT License.
33

44
/* eslint-disable @typescript-eslint/naming-convention */
5-
import { commands, Disposable, Event, EventEmitter, Uri } from 'vscode';
6-
import { traceError, traceLog } from './logging';
7-
import { PythonExtension, ResolvedEnvironment } from '@vscode/python-extension';
8-
import { PYTHON_MAJOR, PYTHON_MINOR, PYTHON_VERSION } from './constants';
9-
import { getProjectRoot } from './utilities';
5+
import { PythonExtension, ResolvedEnvironment } from "@vscode/python-extension";
6+
import { commands, Disposable, Event, EventEmitter, Uri } from "vscode";
7+
8+
import { PYTHON_MAJOR, PYTHON_MINOR, PYTHON_VERSION } from "./constants";
9+
import { traceError, traceLog } from "./logging";
10+
import { getProjectRoot } from "./utilities";
1011

1112
export interface IInterpreterDetails {
12-
path?: string[];
13-
resource?: Uri;
13+
path?: string[];
14+
resource?: Uri;
1415
}
1516

1617
const onDidChangePythonInterpreterEvent = new EventEmitter<void>();
17-
export const onDidChangePythonInterpreter: Event<void> = onDidChangePythonInterpreterEvent.event;
18+
export const onDidChangePythonInterpreter: Event<void> =
19+
onDidChangePythonInterpreterEvent.event;
1820

1921
let _api: PythonExtension | undefined;
2022
async function getPythonExtensionAPI(): Promise<PythonExtension | undefined> {
21-
if (_api) {
22-
return _api;
23-
}
24-
_api = await PythonExtension.api();
25-
return _api;
23+
if (_api) {
24+
return _api;
25+
}
26+
_api = await PythonExtension.api();
27+
return _api;
2628
}
2729

2830
function sameInterpreter(a: string[], b: string[]): boolean {
29-
if (a.length !== b.length) {
30-
return false;
31-
}
32-
for (let i = 0; i < a.length; i++) {
33-
if (a[i] !== b[i]) {
34-
return false;
35-
}
36-
}
37-
return true;
31+
if (a.length !== b.length) {
32+
return false;
33+
}
34+
for (let i = 0; i < a.length; i++) {
35+
if (a[i] !== b[i]) {
36+
return false;
37+
}
38+
}
39+
return true;
3840
}
3941

4042
let serverPython: string[] | undefined;
4143
function checkAndFireEvent(interpreter: string[] | undefined): void {
42-
if (interpreter === undefined) {
43-
if (serverPython) {
44-
// Python was reset for this uri
45-
serverPython = undefined;
46-
onDidChangePythonInterpreterEvent.fire();
47-
return;
48-
} else {
49-
return; // No change in interpreter
50-
}
51-
}
52-
53-
if (!serverPython || !sameInterpreter(serverPython, interpreter)) {
54-
serverPython = interpreter;
55-
onDidChangePythonInterpreterEvent.fire();
56-
}
44+
if (interpreter === undefined) {
45+
if (serverPython) {
46+
// Python was reset for this uri
47+
serverPython = undefined;
48+
onDidChangePythonInterpreterEvent.fire();
49+
return;
50+
} else {
51+
return; // No change in interpreter
52+
}
53+
}
54+
55+
if (!serverPython || !sameInterpreter(serverPython, interpreter)) {
56+
serverPython = interpreter;
57+
onDidChangePythonInterpreterEvent.fire();
58+
}
5759
}
5860

5961
async function refreshServerPython(): Promise<void> {
60-
const projectRoot = await getProjectRoot();
61-
const interpreter = await getInterpreterDetails(projectRoot?.uri);
62-
checkAndFireEvent(interpreter.path);
62+
const projectRoot = await getProjectRoot();
63+
const interpreter = await getInterpreterDetails(projectRoot?.uri);
64+
checkAndFireEvent(interpreter.path);
6365
}
6466

65-
export async function initializePython(disposables: Disposable[]): Promise<void> {
66-
try {
67-
const api = await getPythonExtensionAPI();
68-
69-
if (api) {
70-
disposables.push(
71-
api.environments.onDidChangeActiveEnvironmentPath(async () => {
72-
await refreshServerPython();
73-
}),
74-
);
75-
76-
traceLog('Waiting for interpreter from Python extension.');
77-
await refreshServerPython();
78-
}
79-
} catch (error) {
80-
traceError('Error initializing Python: ', error);
81-
}
67+
export async function initializePython(
68+
disposables: Disposable[],
69+
): Promise<void> {
70+
try {
71+
const api = await getPythonExtensionAPI();
72+
73+
if (api) {
74+
disposables.push(
75+
api.environments.onDidChangeActiveEnvironmentPath(async () => {
76+
await refreshServerPython();
77+
}),
78+
);
79+
80+
traceLog("Waiting for interpreter from Python extension.");
81+
await refreshServerPython();
82+
}
83+
} catch (error) {
84+
traceError("Error initializing Python: ", error);
85+
}
8286
}
8387

84-
export async function resolveInterpreter(interpreter: string[]): Promise<ResolvedEnvironment | undefined> {
85-
const api = await getPythonExtensionAPI();
86-
return api?.environments.resolveEnvironment(interpreter[0]);
88+
export async function resolveInterpreter(
89+
interpreter: string[],
90+
): Promise<ResolvedEnvironment | undefined> {
91+
const api = await getPythonExtensionAPI();
92+
return api?.environments.resolveEnvironment(interpreter[0]);
8793
}
8894

89-
export async function getInterpreterDetails(resource?: Uri): Promise<IInterpreterDetails> {
90-
const api = await getPythonExtensionAPI();
91-
const environment = await api?.environments.resolveEnvironment(
92-
api?.environments.getActiveEnvironmentPath(resource),
93-
);
94-
if (environment?.executable.uri && checkVersion(environment)) {
95-
return { path: [environment?.executable.uri.fsPath], resource };
96-
}
97-
return { path: undefined, resource };
95+
export async function getInterpreterDetails(
96+
resource?: Uri,
97+
): Promise<IInterpreterDetails> {
98+
const api = await getPythonExtensionAPI();
99+
const environment = await api?.environments.resolveEnvironment(
100+
api?.environments.getActiveEnvironmentPath(resource),
101+
);
102+
if (environment?.executable.uri && checkVersion(environment)) {
103+
return { path: [environment?.executable.uri.fsPath], resource };
104+
}
105+
return { path: undefined, resource };
98106
}
99107

100108
export async function getDebuggerPath(): Promise<string | undefined> {
101-
const api = await getPythonExtensionAPI();
102-
return api?.debug.getDebuggerPackagePath();
109+
const api = await getPythonExtensionAPI();
110+
return api?.debug.getDebuggerPackagePath();
103111
}
104112

105-
export async function runPythonExtensionCommand(command: string, ...rest: any[]) {
106-
await getPythonExtensionAPI();
107-
return await commands.executeCommand(command, ...rest);
113+
export async function runPythonExtensionCommand(
114+
command: string,
115+
...rest: any[]
116+
) {
117+
await getPythonExtensionAPI();
118+
return await commands.executeCommand(command, ...rest);
108119
}
109120

110-
export function checkVersion(resolved: ResolvedEnvironment | undefined): boolean {
111-
const version = resolved?.version;
112-
if (version?.major === PYTHON_MAJOR && version?.minor >= PYTHON_MINOR) {
113-
return true;
114-
}
115-
traceError(`Python version ${version?.major}.${version?.minor} is not supported.`);
116-
traceError(`Selected python path: ${resolved?.executable.uri?.fsPath}`);
117-
traceError(`Supported versions are ${PYTHON_VERSION} and above.`);
118-
return false;
121+
export function checkVersion(
122+
resolved: ResolvedEnvironment | undefined,
123+
): boolean {
124+
const version = resolved?.version;
125+
if (version?.major === PYTHON_MAJOR && version?.minor >= PYTHON_MINOR) {
126+
return true;
127+
}
128+
traceError(
129+
`Python version ${version?.major}.${version?.minor} is not supported.`,
130+
);
131+
traceError(`Selected python path: ${resolved?.executable.uri?.fsPath}`);
132+
traceError(`Supported versions are ${PYTHON_VERSION} and above.`);
133+
return false;
119134
}

0 commit comments

Comments
 (0)