Skip to content

Commit

Permalink
Merge pull request #70 from randomVariable2000/feat/support-drop-into…
Browse files Browse the repository at this point in the history
…-editor

Support drop into editor
  • Loading branch information
tahabasri committed May 6, 2023
2 parents 21f337c + d742d56 commit dcb0852
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 dcb0852

Please sign in to comment.