|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License |
| 3 | + |
| 4 | +import { Diagnostic, DiagnosticCollection, DiagnosticSeverity, l10n, Range, TextDocument, Uri } from 'vscode'; |
| 5 | +import { installedCheckScript } from '../../common/process/internal/scripts'; |
| 6 | +import { plainExec } from '../../common/process/rawProcessApis'; |
| 7 | +import { IDisposableRegistry, IInterpreterPathService } from '../../common/types'; |
| 8 | +import { executeCommand } from '../../common/vscodeApis/commandApis'; |
| 9 | +import { createDiagnosticCollection, onDidChangeDiagnostics } from '../../common/vscodeApis/languageApis'; |
| 10 | +import { getActiveTextEditor, onDidChangeActiveTextEditor } from '../../common/vscodeApis/windowApis'; |
| 11 | +import { |
| 12 | + getOpenTextDocuments, |
| 13 | + onDidCloseTextDocument, |
| 14 | + onDidOpenTextDocument, |
| 15 | + onDidSaveTextDocument, |
| 16 | +} from '../../common/vscodeApis/workspaceApis'; |
| 17 | +import { traceVerbose } from '../../logging'; |
| 18 | + |
| 19 | +interface PackageDiagnostic { |
| 20 | + package: string; |
| 21 | + line: number; |
| 22 | + code: string; |
| 23 | + severity: DiagnosticSeverity; |
| 24 | +} |
| 25 | + |
| 26 | +const SOURCE = 'Python-Ext'; |
| 27 | +const PIP_DEPS_NOT_INSTALLED_KEY = 'pipDepsNotInstalled'; |
| 28 | + |
| 29 | +async function getPipRequirementsDiagnostics( |
| 30 | + interpreterPathService: IInterpreterPathService, |
| 31 | + doc: TextDocument, |
| 32 | +): Promise<Diagnostic[]> { |
| 33 | + const interpreter = interpreterPathService.get(doc.uri); |
| 34 | + const result = await plainExec(interpreter, [installedCheckScript(), doc.uri.fsPath]); |
| 35 | + traceVerbose('Installed packages check result:\n', result.stdout); |
| 36 | + let diagnostics: Diagnostic[] = []; |
| 37 | + try { |
| 38 | + const raw = JSON.parse(result.stdout) as PackageDiagnostic[]; |
| 39 | + diagnostics = raw.map((item) => { |
| 40 | + const d = new Diagnostic( |
| 41 | + new Range(item.line, 0, item.line, item.package.length), |
| 42 | + l10n.t(`Package \`${item.package}\` is not installed in the selected environment.`), |
| 43 | + item.severity, |
| 44 | + ); |
| 45 | + d.code = { value: item.code, target: Uri.parse(`https://pypi.org/p/${item.package}`) }; |
| 46 | + d.source = SOURCE; |
| 47 | + return d; |
| 48 | + }); |
| 49 | + } catch { |
| 50 | + diagnostics = []; |
| 51 | + } |
| 52 | + return diagnostics; |
| 53 | +} |
| 54 | + |
| 55 | +async function setContextForActiveEditor(diagnosticCollection: DiagnosticCollection): Promise<void> { |
| 56 | + const doc = getActiveTextEditor()?.document; |
| 57 | + if (doc && doc.languageId === 'pip-requirements') { |
| 58 | + const diagnostics = diagnosticCollection.get(doc.uri); |
| 59 | + if (diagnostics && diagnostics.length > 0) { |
| 60 | + traceVerbose(`Setting context for pip dependencies not installed: ${doc.uri.fsPath}`); |
| 61 | + await executeCommand('setContext', PIP_DEPS_NOT_INSTALLED_KEY, true); |
| 62 | + return; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // undefined here in the logs means no file was selected |
| 67 | + traceVerbose(`Clearing context for pip dependencies not installed: ${doc?.uri.fsPath}`); |
| 68 | + await executeCommand('setContext', PIP_DEPS_NOT_INSTALLED_KEY, false); |
| 69 | +} |
| 70 | + |
| 71 | +export function registerInstalledPackagesChecking( |
| 72 | + interpreterPathService: IInterpreterPathService, |
| 73 | + disposables: IDisposableRegistry, |
| 74 | +): void { |
| 75 | + const diagnosticCollection = createDiagnosticCollection(SOURCE); |
| 76 | + |
| 77 | + disposables.push(diagnosticCollection); |
| 78 | + disposables.push( |
| 79 | + onDidOpenTextDocument(async (e: TextDocument) => { |
| 80 | + if (e.languageId === 'pip-requirements') { |
| 81 | + const diagnostics = await getPipRequirementsDiagnostics(interpreterPathService, e); |
| 82 | + if (diagnostics.length > 0) { |
| 83 | + diagnosticCollection.set(e.uri, diagnostics); |
| 84 | + } else if (diagnosticCollection.has(e.uri)) { |
| 85 | + diagnosticCollection.delete(e.uri); |
| 86 | + } |
| 87 | + } |
| 88 | + }), |
| 89 | + onDidSaveTextDocument(async (e: TextDocument) => { |
| 90 | + if (e.languageId === 'pip-requirements') { |
| 91 | + const diagnostics = await getPipRequirementsDiagnostics(interpreterPathService, e); |
| 92 | + if (diagnostics.length > 0) { |
| 93 | + diagnosticCollection.set(e.uri, diagnostics); |
| 94 | + } else if (diagnosticCollection.has(e.uri)) { |
| 95 | + diagnosticCollection.delete(e.uri); |
| 96 | + } |
| 97 | + } |
| 98 | + }), |
| 99 | + onDidCloseTextDocument((e: TextDocument) => { |
| 100 | + if (diagnosticCollection.has(e.uri)) { |
| 101 | + diagnosticCollection.delete(e.uri); |
| 102 | + } |
| 103 | + }), |
| 104 | + onDidChangeDiagnostics(async () => { |
| 105 | + await setContextForActiveEditor(diagnosticCollection); |
| 106 | + }), |
| 107 | + onDidChangeActiveTextEditor(async () => { |
| 108 | + await setContextForActiveEditor(diagnosticCollection); |
| 109 | + }), |
| 110 | + ); |
| 111 | + |
| 112 | + getOpenTextDocuments().forEach(async (doc: TextDocument) => { |
| 113 | + if (doc.languageId === 'pip-requirements') { |
| 114 | + const diagnostics = await getPipRequirementsDiagnostics(interpreterPathService, doc); |
| 115 | + if (diagnostics.length > 0) { |
| 116 | + diagnosticCollection.set(doc.uri, diagnostics); |
| 117 | + } else if (diagnosticCollection.has(doc.uri)) { |
| 118 | + diagnosticCollection.delete(doc.uri); |
| 119 | + } |
| 120 | + } |
| 121 | + }); |
| 122 | +} |
0 commit comments