|
| 1 | +/* eslint-disable max-classes-per-file */ |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +// Licensed under the MIT License. |
| 4 | + |
| 5 | +import { inject, named } from 'inversify'; |
| 6 | +import { ConfigurationTarget, DiagnosticSeverity } from 'vscode'; |
| 7 | +import { LanguageServerType } from '../../../activation/types'; |
| 8 | +import { IWorkspaceService } from '../../../common/application/types'; |
| 9 | +import { IConfigurationService, IDisposableRegistry, Resource } from '../../../common/types'; |
| 10 | +import { Common, Python27Support } from '../../../common/utils/localize'; |
| 11 | +import { IInterpreterService } from '../../../interpreter/contracts'; |
| 12 | +import { IServiceContainer } from '../../../ioc/types'; |
| 13 | +import { BaseDiagnostic, BaseDiagnosticsService } from '../base'; |
| 14 | +import { IDiagnosticsCommandFactory } from '../commands/types'; |
| 15 | +import { DiagnosticCodes } from '../constants'; |
| 16 | +import { DiagnosticCommandPromptHandlerServiceId, MessageCommandPrompt } from '../promptHandler'; |
| 17 | +import { DiagnosticScope, IDiagnostic, IDiagnosticHandlerService } from '../types'; |
| 18 | + |
| 19 | +export class JediPython27NotSupportedDiagnostic extends BaseDiagnostic { |
| 20 | + constructor(message: string, resource: Resource) { |
| 21 | + super( |
| 22 | + DiagnosticCodes.JediPython27NotSupportedDiagnostic, |
| 23 | + message, |
| 24 | + DiagnosticSeverity.Warning, |
| 25 | + DiagnosticScope.Global, |
| 26 | + resource, |
| 27 | + ); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export const JediPython27NotSupportedDiagnosticServiceId = 'JediPython27NotSupportedDiagnosticServiceId'; |
| 32 | + |
| 33 | +export class JediPython27NotSupportedDiagnosticService extends BaseDiagnosticsService { |
| 34 | + constructor( |
| 35 | + @inject(IServiceContainer) serviceContainer: IServiceContainer, |
| 36 | + @inject(IInterpreterService) private readonly interpreterService: IInterpreterService, |
| 37 | + @inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService, |
| 38 | + @inject(IConfigurationService) private readonly configurationService: IConfigurationService, |
| 39 | + @inject(IDiagnosticHandlerService) |
| 40 | + @named(DiagnosticCommandPromptHandlerServiceId) |
| 41 | + protected readonly messageService: IDiagnosticHandlerService<MessageCommandPrompt>, |
| 42 | + @inject(IDisposableRegistry) disposableRegistry: IDisposableRegistry, |
| 43 | + ) { |
| 44 | + super([DiagnosticCodes.JediPython27NotSupportedDiagnostic], serviceContainer, disposableRegistry, true); |
| 45 | + } |
| 46 | + |
| 47 | + public async diagnose(resource: Resource): Promise<IDiagnostic[]> { |
| 48 | + const interpreter = await this.interpreterService.getActiveInterpreter(resource); |
| 49 | + |
| 50 | + if (interpreter && (interpreter.version?.major ?? 0) < 3) { |
| 51 | + return [new JediPython27NotSupportedDiagnostic(Python27Support.jediMessage(), resource)]; |
| 52 | + } |
| 53 | + |
| 54 | + return []; |
| 55 | + } |
| 56 | + |
| 57 | + protected async onHandle(diagnostics: IDiagnostic[]): Promise<void> { |
| 58 | + if (diagnostics.length === 0 || !this.canHandle(diagnostics[0])) { |
| 59 | + return; |
| 60 | + } |
| 61 | + const diagnostic = diagnostics[0]; |
| 62 | + if (await this.filterService.shouldIgnoreDiagnostic(diagnostic.code)) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + this.updateLanguageServerSetting(diagnostic.resource); |
| 67 | + |
| 68 | + const commandFactory = this.serviceContainer.get<IDiagnosticsCommandFactory>(IDiagnosticsCommandFactory); |
| 69 | + const options = [ |
| 70 | + { |
| 71 | + prompt: Common.gotIt(), |
| 72 | + }, |
| 73 | + { |
| 74 | + prompt: Common.doNotShowAgain(), |
| 75 | + command: commandFactory.createCommand(diagnostic, { type: 'ignore', options: DiagnosticScope.Global }), |
| 76 | + }, |
| 77 | + ]; |
| 78 | + |
| 79 | + await this.messageService.handle(diagnostic, { commandPrompts: options }); |
| 80 | + } |
| 81 | + |
| 82 | + private updateLanguageServerSetting(resource: Resource): void { |
| 83 | + // Update settings.json value to Jedi if it's JediLSP. |
| 84 | + const settings = this.workspaceService |
| 85 | + .getConfiguration('python', resource) |
| 86 | + .inspect<LanguageServerType>('languageServer'); |
| 87 | + |
| 88 | + let configTarget: ConfigurationTarget; |
| 89 | + |
| 90 | + if (settings?.workspaceValue === LanguageServerType.JediLSP) { |
| 91 | + configTarget = ConfigurationTarget.Workspace; |
| 92 | + } else if (settings?.globalValue === LanguageServerType.JediLSP) { |
| 93 | + configTarget = ConfigurationTarget.Global; |
| 94 | + } else { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + this.configurationService.updateSetting('languageServer', LanguageServerType.Jedi, resource, configTarget); |
| 99 | + } |
| 100 | +} |
0 commit comments