Path: blob/main/src/vs/workbench/contrib/dropOrPasteInto/browser/configurationSchema.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Emitter, Event } from '../../../../base/common/event.js';6import { HierarchicalKind } from '../../../../base/common/hierarchicalKind.js';7import { IJSONSchema } from '../../../../base/common/jsonSchema.js';8import { Disposable } from '../../../../base/common/lifecycle.js';9import { editorConfigurationBaseNode } from '../../../../editor/common/config/editorConfigurationSchema.js';10import { ILanguageFeaturesService } from '../../../../editor/common/services/languageFeatures.js';11import { pasteAsCommandId } from '../../../../editor/contrib/dropOrPasteInto/browser/copyPasteContribution.js';12import { pasteAsPreferenceConfig } from '../../../../editor/contrib/dropOrPasteInto/browser/copyPasteController.js';13import { dropAsPreferenceConfig } from '../../../../editor/contrib/dropOrPasteInto/browser/dropIntoEditorController.js';14import * as nls from '../../../../nls.js';15import { ConfigurationScope, Extensions, IConfigurationNode, IConfigurationPropertySchema, IConfigurationRegistry } from '../../../../platform/configuration/common/configurationRegistry.js';16import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';17import { Registry } from '../../../../platform/registry/common/platform.js';18import { IWorkbenchContribution } from '../../../common/contributions.js';1920const dropEnumValues: string[] = [];2122const dropAsPreferenceSchema: IConfigurationPropertySchema = {23type: 'array',24scope: ConfigurationScope.LANGUAGE_OVERRIDABLE,25description: nls.localize('dropPreferredDescription', "Configures the preferred type of edit to use when dropping content.\n\nThis is an ordered list of edit kinds. The first available edit of a preferred kind will be used."),26default: [],27items: {28description: nls.localize('dropKind', "The kind identifier of the drop edit."),29anyOf: [30{ type: 'string' },31{ enum: dropEnumValues }32],33}34};3536const pasteEnumValues: string[] = [];3738const pasteAsPreferenceSchema: IConfigurationPropertySchema = {39type: 'array',40scope: ConfigurationScope.LANGUAGE_OVERRIDABLE,41description: nls.localize('pastePreferredDescription', "Configures the preferred type of edit to use when pasting content.\n\nThis is an ordered list of edit kinds. The first available edit of a preferred kind will be used."),42default: [],43items: {44description: nls.localize('pasteKind', "The kind identifier of the paste edit."),45anyOf: [46{ type: 'string' },47{ enum: pasteEnumValues }48]49}50};5152export const editorConfiguration = Object.freeze<IConfigurationNode>({53...editorConfigurationBaseNode,54properties: {55[pasteAsPreferenceConfig]: pasteAsPreferenceSchema,56[dropAsPreferenceConfig]: dropAsPreferenceSchema,57}58});5960export class DropOrPasteSchemaContribution extends Disposable implements IWorkbenchContribution {6162public static ID = 'workbench.contrib.dropOrPasteIntoSchema';6364private readonly _onDidChangeSchemaContributions = this._register(new Emitter<void>());6566private _allProvidedDropKinds: HierarchicalKind[] = [];67private _allProvidedPasteKinds: HierarchicalKind[] = [];6869constructor(70@IKeybindingService keybindingService: IKeybindingService,71@ILanguageFeaturesService private readonly languageFeatures: ILanguageFeaturesService72) {73super();7475this._register(76Event.runAndSubscribe(77Event.debounce(78Event.any(languageFeatures.documentPasteEditProvider.onDidChange, languageFeatures.documentPasteEditProvider.onDidChange),79() => { },801000,81), () => {82this.updateProvidedKinds();83this.updateConfigurationSchema();8485this._onDidChangeSchemaContributions.fire();86}));8788this._register(keybindingService.registerSchemaContribution({89getSchemaAdditions: () => this.getKeybindingSchemaAdditions(),90onDidChange: this._onDidChangeSchemaContributions.event,91}));92}9394private updateProvidedKinds(): void {95// Drop96const dropKinds = new Map<string, HierarchicalKind>();97for (const provider of this.languageFeatures.documentDropEditProvider.allNoModel()) {98for (const kind of provider.providedDropEditKinds ?? []) {99dropKinds.set(kind.value, kind);100}101}102this._allProvidedDropKinds = Array.from(dropKinds.values());103104// Paste105const pasteKinds = new Map<string, HierarchicalKind>();106for (const provider of this.languageFeatures.documentPasteEditProvider.allNoModel()) {107for (const kind of provider.providedPasteEditKinds ?? []) {108pasteKinds.set(kind.value, kind);109}110}111this._allProvidedPasteKinds = Array.from(pasteKinds.values());112}113114private updateConfigurationSchema(): void {115pasteEnumValues.length = 0;116for (const codeActionKind of this._allProvidedPasteKinds) {117pasteEnumValues.push(codeActionKind.value);118}119120dropEnumValues.length = 0;121for (const codeActionKind of this._allProvidedDropKinds) {122dropEnumValues.push(codeActionKind.value);123}124125Registry.as<IConfigurationRegistry>(Extensions.Configuration)126.notifyConfigurationSchemaUpdated(editorConfiguration);127}128129private getKeybindingSchemaAdditions(): IJSONSchema[] {130return [131{132if: {133required: ['command'],134properties: {135'command': { const: pasteAsCommandId }136}137},138then: {139properties: {140'args': {141oneOf: [142{143required: ['kind'],144properties: {145'kind': {146anyOf: [147{ enum: Array.from(this._allProvidedPasteKinds.map(x => x.value)) },148{ type: 'string' },149]150}151}152},153{154required: ['preferences'],155properties: {156'preferences': {157type: 'array',158items: {159anyOf: [160{ enum: Array.from(this._allProvidedPasteKinds.map(x => x.value)) },161{ type: 'string' },162]163}164}165}166}167]168}169}170}171},172];173}174}175176177