forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpyProjectTomlContext.ts
More file actions
44 lines (40 loc) · 1.57 KB
/
Copy pathpyProjectTomlContext.ts
File metadata and controls
44 lines (40 loc) · 1.57 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { TextDocument } from 'vscode';
import { IDisposableRegistry } from '../../common/types';
import { executeCommand } from '../../common/vscodeApis/commandApis';
import {
onDidOpenTextDocument,
onDidSaveTextDocument,
getOpenTextDocuments,
} from '../../common/vscodeApis/workspaceApis';
import { isPipInstallableToml } from './provider/venvUtils';
async function setPyProjectTomlContextKey(doc: TextDocument): Promise<void> {
if (isPipInstallableToml(doc.getText())) {
await executeCommand('setContext', 'pipInstallableToml', true);
} else {
await executeCommand('setContext', 'pipInstallableToml', false);
}
}
export function registerPyProjectTomlFeatures(disposables: IDisposableRegistry): void {
disposables.push(
onDidOpenTextDocument(async (doc: TextDocument) => {
if (doc.fileName.endsWith('pyproject.toml')) {
await setPyProjectTomlContextKey(doc);
}
}),
onDidSaveTextDocument(async (doc: TextDocument) => {
if (doc.fileName.endsWith('pyproject.toml')) {
await setPyProjectTomlContextKey(doc);
}
}),
);
const docs = getOpenTextDocuments().filter(
(doc) => doc.fileName.endsWith('pyproject.toml') && isPipInstallableToml(doc.getText()),
);
if (docs.length > 0) {
executeCommand('setContext', 'pipInstallableToml', true);
} else {
executeCommand('setContext', 'pipInstallableToml', false);
}
}