Skip to content

Commit

Permalink
Support drop into editor
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiang24 committed May 6, 2023
1 parent 21f337c commit d742d56
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<vscode.DocumentDropEdit | undefined> {
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() { }

0 comments on commit d742d56

Please sign in to comment.