|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) IBM Corp. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +import { ConfigurationTarget, extensions, workspace } from 'vscode'; |
| 6 | +import type { ExtensionContext, WorkspaceConfiguration } from 'vscode'; |
| 7 | + |
| 8 | +interface AutoDisableSchemaDetectionEntry { |
| 9 | + extensionId: string; |
| 10 | + configurationKey?: string; |
| 11 | + fileMatches: string[]; |
| 12 | +} |
| 13 | + |
| 14 | +const autoDisableSchemaDetectionEntries: AutoDisableSchemaDetectionEntry[] = [ |
| 15 | + { |
| 16 | + extensionId: 'docker.docker', |
| 17 | + configurationKey: 'docker.extension.enableComposeLanguageServer', |
| 18 | + fileMatches: [ |
| 19 | + '**/docker-compose.yml', |
| 20 | + '**/docker-compose.yaml', |
| 21 | + '**/docker-compose.*.yml', |
| 22 | + '**/docker-compose.*.yaml', |
| 23 | + '**/compose.yml', |
| 24 | + '**/compose.yaml', |
| 25 | + '**/compose.*.yml', |
| 26 | + '**/compose.*.yaml', |
| 27 | + ], |
| 28 | + }, |
| 29 | + { |
| 30 | + extensionId: 'github.vscode-github-actions', |
| 31 | + fileMatches: [ |
| 32 | + '**/.github/workflows/*.yml', |
| 33 | + '**/.github/workflows/*.yaml', |
| 34 | + '**/.gitea/workflows/*.yml', |
| 35 | + '**/.gitea/workflows/*.yaml', |
| 36 | + '**/.forgejo/workflows/*.yml', |
| 37 | + '**/.forgejo/workflows/*.yaml', |
| 38 | + ], |
| 39 | + }, |
| 40 | + { |
| 41 | + extensionId: 'ms-azure-devops.azure-pipelines', |
| 42 | + fileMatches: ['azure-pipelines.yml', 'azure-pipelines.yaml'], |
| 43 | + }, |
| 44 | +]; |
| 45 | + |
| 46 | +export async function initializeAutoDisableSchemaDetection(context: ExtensionContext): Promise<void> { |
| 47 | + await updateAutoDisableSchemaDetectionSetting(); |
| 48 | + context.subscriptions.push( |
| 49 | + extensions.onDidChange(() => { |
| 50 | + updateAutoDisableSchemaDetectionSetting(); |
| 51 | + }), |
| 52 | + workspace.onDidChangeConfiguration((event) => { |
| 53 | + if ( |
| 54 | + event.affectsConfiguration('docker.extension.enableComposeLanguageServer') || |
| 55 | + event.affectsConfiguration('yaml.disableSchemaDetection') |
| 56 | + ) { |
| 57 | + updateAutoDisableSchemaDetectionSetting(); |
| 58 | + } |
| 59 | + }) |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +async function updateAutoDisableSchemaDetectionSetting(): Promise<void> { |
| 64 | + const configuration = workspace.getConfiguration('yaml'); |
| 65 | + const currentDisableSchemaDetection = getDisableSchemaDetectionSetting(configuration); |
| 66 | + const update = computeAutoDisableSchemaDetectionUpdate( |
| 67 | + currentDisableSchemaDetection.value, |
| 68 | + getAutoDisabledSchemaDetectionExtensions() |
| 69 | + ); |
| 70 | + if ( |
| 71 | + !( |
| 72 | + currentDisableSchemaDetection.value.length === update.length && |
| 73 | + currentDisableSchemaDetection.value.every((value, index) => value === update[index]) |
| 74 | + ) |
| 75 | + ) { |
| 76 | + await configuration.update('disableSchemaDetection', update, currentDisableSchemaDetection.target); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function getDisableSchemaDetectionSetting( |
| 81 | + configuration: WorkspaceConfiguration |
| 82 | +): { |
| 83 | + value: string[]; |
| 84 | + target: ConfigurationTarget; |
| 85 | +} { |
| 86 | + const inspected = configuration.inspect<string | string[]>('disableSchemaDetection'); |
| 87 | + if (inspected?.workspaceValue !== undefined) { |
| 88 | + return { |
| 89 | + value: toStringArray(inspected.workspaceValue), |
| 90 | + target: ConfigurationTarget.Workspace, |
| 91 | + }; |
| 92 | + } |
| 93 | + return { |
| 94 | + value: toStringArray(inspected?.globalValue ?? []), |
| 95 | + target: ConfigurationTarget.Global, |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +export function getAutoDisabledSchemaDetectionExtensions(): string[] { |
| 100 | + const configuration = workspace.getConfiguration(); |
| 101 | + const enabledExtensions: string[] = []; |
| 102 | + for (const entry of autoDisableSchemaDetectionEntries) { |
| 103 | + if ( |
| 104 | + !extensions.getExtension(entry.extensionId) || |
| 105 | + (entry.configurationKey && configuration.get<boolean>(entry.configurationKey, true) === false) |
| 106 | + ) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + enabledExtensions.push(entry.extensionId); |
| 110 | + } |
| 111 | + return enabledExtensions; |
| 112 | +} |
| 113 | + |
| 114 | +export function computeAutoDisableSchemaDetectionUpdate( |
| 115 | + currentDisableSchemaDetection: string[], |
| 116 | + enabledExtensions: string[] |
| 117 | +): string[] { |
| 118 | + const desiredFileMatches = getFileMatchesForExtensions(enabledExtensions); |
| 119 | + const disabledExtensions = autoDisableSchemaDetectionEntries |
| 120 | + .map((entry) => entry.extensionId) |
| 121 | + .filter((extensionId) => !enabledExtensions.includes(extensionId)); |
| 122 | + const disabledFileMatches = getFileMatchesForExtensions(disabledExtensions); |
| 123 | + const withoutInactiveManagedFileMatches = currentDisableSchemaDetection.filter( |
| 124 | + (fileMatch) => !disabledFileMatches.includes(fileMatch) || desiredFileMatches.includes(fileMatch) |
| 125 | + ); |
| 126 | + const additions = desiredFileMatches.filter((fileMatch) => !withoutInactiveManagedFileMatches.includes(fileMatch)); |
| 127 | + return withoutInactiveManagedFileMatches.concat(additions); |
| 128 | +} |
| 129 | + |
| 130 | +function getFileMatchesForExtensions(extensionIds: string[]): string[] { |
| 131 | + const fileMatches: string[] = []; |
| 132 | + for (const entry of autoDisableSchemaDetectionEntries) { |
| 133 | + if (extensionIds.includes(entry.extensionId)) { |
| 134 | + fileMatches.push(...entry.fileMatches); |
| 135 | + } |
| 136 | + } |
| 137 | + return fileMatches; |
| 138 | +} |
| 139 | + |
| 140 | +function toStringArray(value: unknown): string[] { |
| 141 | + if (typeof value === 'string') { |
| 142 | + return [value]; |
| 143 | + } |
| 144 | + if (Array.isArray(value)) { |
| 145 | + return value.filter((item): item is string => typeof item === 'string'); |
| 146 | + } |
| 147 | + return []; |
| 148 | +} |
0 commit comments