Path: blob/main/src/vs/editor/contrib/dropOrPasteInto/browser/copyPasteContribution.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 { HierarchicalKind } from '../../../../base/common/hierarchicalKind.js';6import { IJSONSchema, SchemaToType } from '../../../../base/common/jsonSchema.js';7import { KeyCode, KeyMod } from '../../../../base/common/keyCodes.js';8import * as nls from '../../../../nls.js';9import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';10import { ICodeEditor } from '../../../browser/editorBrowser.js';11import { EditorAction, EditorCommand, EditorContributionInstantiation, ServicesAccessor, registerEditorAction, registerEditorCommand, registerEditorContribution } from '../../../browser/editorExtensions.js';12import { EditorContextKeys } from '../../../common/editorContextKeys.js';13import { registerEditorFeature } from '../../../common/editorFeatures.js';14import { CopyPasteController, PastePreference, changePasteTypeCommandId, pasteWidgetVisibleCtx } from './copyPasteController.js';15import { DefaultPasteProvidersFeature, DefaultTextPasteOrDropEditProvider } from './defaultProviders.js';1617export const pasteAsCommandId = 'editor.action.pasteAs';1819registerEditorContribution(CopyPasteController.ID, CopyPasteController, EditorContributionInstantiation.Eager); // eager because it listens to events on the container dom node of the editor20registerEditorFeature(DefaultPasteProvidersFeature);2122registerEditorCommand(new class extends EditorCommand {23constructor() {24super({25id: changePasteTypeCommandId,26precondition: pasteWidgetVisibleCtx,27kbOpts: {28weight: KeybindingWeight.EditorContrib,29primary: KeyMod.CtrlCmd | KeyCode.Period,30}31});32}3334public override runEditorCommand(_accessor: ServicesAccessor, editor: ICodeEditor) {35return CopyPasteController.get(editor)?.changePasteType();36}37});3839registerEditorCommand(new class extends EditorCommand {40constructor() {41super({42id: 'editor.hidePasteWidget',43precondition: pasteWidgetVisibleCtx,44kbOpts: {45weight: KeybindingWeight.EditorContrib,46primary: KeyCode.Escape,47}48});49}5051public override runEditorCommand(_accessor: ServicesAccessor, editor: ICodeEditor) {52CopyPasteController.get(editor)?.clearWidgets();53}54});5556registerEditorAction(class PasteAsAction extends EditorAction {57private static readonly argsSchema = {58oneOf: [59{60type: 'object',61required: ['kind'],62properties: {63kind: {64type: 'string',65description: nls.localize('pasteAs.kind', "The kind of the paste edit to try pasting with.\nIf there are multiple edits for this kind, the editor will show a picker. If there are no edits of this kind, the editor will show an error message."),66}67},68},69{70type: 'object',71required: ['preferences'],72properties: {73preferences: {74type: 'array',75description: nls.localize('pasteAs.preferences', "List of preferred paste edit kind to try applying.\nThe first edit matching the preferences will be applied."),76items: { type: 'string' }77}78},79}80]81} as const satisfies IJSONSchema;8283constructor() {84super({85id: pasteAsCommandId,86label: nls.localize2('pasteAs', "Paste As..."),87precondition: EditorContextKeys.writable,88metadata: {89description: 'Paste as',90args: [{91name: 'args',92schema: PasteAsAction.argsSchema93}]94}95});96}9798public override run(_accessor: ServicesAccessor, editor: ICodeEditor, args?: SchemaToType<typeof PasteAsAction.argsSchema>) {99let preference: PastePreference | undefined;100if (args) {101if ('kind' in args) {102preference = { only: new HierarchicalKind(args.kind) };103} else if ('preferences' in args) {104preference = { preferences: args.preferences.map(kind => new HierarchicalKind(kind)) };105}106}107return CopyPasteController.get(editor)?.pasteAs(preference);108}109});110111registerEditorAction(class extends EditorAction {112constructor() {113super({114id: 'editor.action.pasteAsText',115label: nls.localize2('pasteAsText', "Paste as Text"),116precondition: EditorContextKeys.writable,117});118}119120public override run(_accessor: ServicesAccessor, editor: ICodeEditor) {121return CopyPasteController.get(editor)?.pasteAs({ providerId: DefaultTextPasteOrDropEditProvider.id });122}123});124125export type PreferredPasteConfiguration = string;126127128