From d742d56fe59c10866d9a885e3e83684982160f42 Mon Sep 17 00:00:00 2001 From: randomVariable2000 <1597888126@qq.com> Date: Thu, 4 May 2023 17:22:02 +0800 Subject: [PATCH] Support drop into editor --- src/extension.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/extension.ts b/src/extension.ts index 4d45a59..99ad9a5 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -523,6 +523,47 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(vscode.commands.registerCommand(commands.CommandsConsts.globalImportSnippets, async _ => handleCommand(() => commands.importSnippets(snippetsProvider)) )); + + context.subscriptions.push(vscode.languages.registerDocumentDropEditProvider('*', { + async provideDocumentDropEdits( + _document: vscode.TextDocument, + position: vscode.Position, + dataTransfer: vscode.DataTransfer, + _token: vscode.CancellationToken + ): Promise { + const dataItem = dataTransfer.get('application/vnd.code.tree.snippetsProvider'); + if (!dataItem) { + return; + } + try { + const text = await dataItem.asString(); + const parsedSource = JSON.parse(text) as readonly Snippet[]; + // only accept one snippet (not a folder) + if (parsedSource?.length !== 1 || parsedSource[0].folder) { + return; + } + const draggedSnippet = parsedSource[0]; + // same as open snippet command + if (draggedSnippet.resolveSyntax === undefined) { + draggedSnippet.resolveSyntax = true; + } + if (draggedSnippet.resolveSyntax) { + vscode.commands.executeCommand("editor.action.insertSnippet", { snippet: draggedSnippet.value } + ); + } else { + const editor = vscode.window.activeTextEditor; + if (!editor) { + return; + } + editor.edit(edit => { + edit.insert(position, draggedSnippet.value ?? ''); + }); + } + } catch { + // throws error when parsing `dataItem?.value`, just skip + } + } + })); } export function deactivate() { }