mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-20 16:49:06 +01:00
priority -> yieldTo for drop/paste API proposals (#189881)
Move await from `priority` for drop/paste API proposals For #179430, #30066 Switching to use `yieldTo` instead of `priority` to let an extension de-rank itself in the list of edits. `priority` was an arbitrary number while `yieldTo` gives more control over how the ranking takes place
This commit is contained in:
@@ -11,6 +11,11 @@ class PasteEditProvider implements vscode.DocumentPasteEditProvider {
|
||||
|
||||
private readonly _id = 'insertLink';
|
||||
|
||||
private readonly _yieldTo = [
|
||||
{ mimeType: 'text/plain' },
|
||||
{ extensionId: 'vscode.ipynb', editId: 'insertAttachment' },
|
||||
];
|
||||
|
||||
async provideDocumentPasteEdits(
|
||||
document: vscode.TextDocument,
|
||||
ranges: readonly vscode.Range[],
|
||||
@@ -32,7 +37,8 @@ class PasteEditProvider implements vscode.DocumentPasteEditProvider {
|
||||
if (!urlList) {
|
||||
return;
|
||||
}
|
||||
const pasteUrlSetting = await getPasteUrlAsFormattedLinkSetting(document);
|
||||
|
||||
const pasteUrlSetting = getPasteUrlAsFormattedLinkSetting(document);
|
||||
const pasteEdit = await createEditAddingLinksForUriList(document, ranges, urlList, false, pasteUrlSetting === PasteUrlAsFormattedLink.Smart, token);
|
||||
if (!pasteEdit) {
|
||||
return;
|
||||
@@ -40,7 +46,7 @@ class PasteEditProvider implements vscode.DocumentPasteEditProvider {
|
||||
|
||||
uriEdit.label = pasteEdit.label;
|
||||
uriEdit.additionalEdit = pasteEdit.additionalEdits;
|
||||
uriEdit.priority = this._getPriority(dataTransfer);
|
||||
uriEdit.yieldTo = this._yieldTo;
|
||||
return uriEdit;
|
||||
}
|
||||
|
||||
@@ -61,17 +67,9 @@ class PasteEditProvider implements vscode.DocumentPasteEditProvider {
|
||||
|
||||
const pasteEdit = new vscode.DocumentPasteEdit(edit.snippet, this._id, edit.label);
|
||||
pasteEdit.additionalEdit = edit.additionalEdits;
|
||||
pasteEdit.priority = this._getPriority(dataTransfer);
|
||||
pasteEdit.yieldTo = this._yieldTo;
|
||||
return pasteEdit;
|
||||
}
|
||||
|
||||
private _getPriority(dataTransfer: vscode.DataTransfer): number {
|
||||
if (dataTransfer.get('text/plain')) {
|
||||
// Deprioritize in favor of normal text content
|
||||
return -10;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
export function registerPasteSupport(selector: vscode.DocumentSelector,) {
|
||||
|
||||
@@ -5,60 +5,44 @@
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { createEditAddingLinksForUriList, getPasteUrlAsFormattedLinkSetting, PasteUrlAsFormattedLink, validateLink } from './shared';
|
||||
|
||||
const textPlainMime = 'text/plain';
|
||||
|
||||
class PasteLinkEditProvider implements vscode.DocumentPasteEditProvider {
|
||||
|
||||
readonly id = 'insertMarkdownLink';
|
||||
|
||||
async provideDocumentPasteEdits(
|
||||
document: vscode.TextDocument,
|
||||
ranges: readonly vscode.Range[],
|
||||
dataTransfer: vscode.DataTransfer,
|
||||
token: vscode.CancellationToken,
|
||||
): Promise<vscode.DocumentPasteEdit | undefined> {
|
||||
const pasteUrlSetting = await getPasteUrlAsFormattedLinkSetting(document);
|
||||
const pasteUrlSetting = getPasteUrlAsFormattedLinkSetting(document);
|
||||
if (pasteUrlSetting === PasteUrlAsFormattedLink.Never) {
|
||||
return;
|
||||
}
|
||||
|
||||
const item = dataTransfer.get('text/plain');
|
||||
const item = dataTransfer.get(textPlainMime);
|
||||
const urlList = await item?.asString();
|
||||
|
||||
if (urlList === undefined) {
|
||||
if (token.isCancellationRequested || !urlList || !validateLink(urlList).isValid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!validateLink(urlList).isValid) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uriEdit = new vscode.DocumentPasteEdit('', this.id, '');
|
||||
if (!urlList) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const pasteEdit = await createEditAddingLinksForUriList(document, ranges, validateLink(urlList).cleanedUrlList, true, pasteUrlSetting === PasteUrlAsFormattedLink.Smart, token);
|
||||
if (!pasteEdit) {
|
||||
return;
|
||||
}
|
||||
|
||||
uriEdit.label = pasteEdit.label;
|
||||
uriEdit.additionalEdit = pasteEdit.additionalEdits;
|
||||
uriEdit.priority = this._getPriority(pasteEdit.markdownLink);
|
||||
return uriEdit;
|
||||
}
|
||||
|
||||
private _getPriority(pasteAsMarkdownLink: boolean): number {
|
||||
if (!pasteAsMarkdownLink) {
|
||||
// Deprioritize in favor of default paste
|
||||
return -10;
|
||||
}
|
||||
return 0;
|
||||
const edit = new vscode.DocumentPasteEdit('', this.id, pasteEdit.label);
|
||||
edit.additionalEdit = pasteEdit.additionalEdits;
|
||||
edit.yieldTo = pasteEdit.markdownLink ? undefined : [{ mimeType: textPlainMime }];
|
||||
return edit;
|
||||
}
|
||||
}
|
||||
|
||||
export function registerLinkPasteSupport(selector: vscode.DocumentSelector,) {
|
||||
return vscode.languages.registerDocumentPasteEditProvider(selector, new PasteLinkEditProvider(), {
|
||||
pasteMimeTypes: [
|
||||
'text/plain',
|
||||
]
|
||||
pasteMimeTypes: [textPlainMime]
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9,8 +9,14 @@ import { Schemes } from '../../util/schemes';
|
||||
|
||||
|
||||
class MarkdownImageDropProvider implements vscode.DocumentDropEditProvider {
|
||||
|
||||
private readonly _id = 'insertLink';
|
||||
|
||||
private readonly _yieldTo = [
|
||||
{ mimeType: 'text/plain' },
|
||||
{ extensionId: 'vscode.ipynb', editId: 'insertAttachment' },
|
||||
];
|
||||
|
||||
async provideDocumentDropEdits(document: vscode.TextDocument, _position: vscode.Position, dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken): Promise<vscode.DocumentDropEdit | undefined> {
|
||||
const enabled = vscode.workspace.getConfiguration('markdown', document).get('editor.drop.enabled', true);
|
||||
if (!enabled) {
|
||||
@@ -42,6 +48,7 @@ class MarkdownImageDropProvider implements vscode.DocumentDropEditProvider {
|
||||
const edit = new vscode.DocumentDropEdit(snippet.snippet);
|
||||
edit.id = this._id;
|
||||
edit.label = snippet.label;
|
||||
edit.yieldTo = this._yieldTo;
|
||||
return edit;
|
||||
}
|
||||
|
||||
@@ -64,6 +71,7 @@ class MarkdownImageDropProvider implements vscode.DocumentDropEditProvider {
|
||||
edit.id = this._id;
|
||||
edit.label = filesEdit.label;
|
||||
edit.additionalEdit = filesEdit.additionalEdits;
|
||||
edit.yieldTo = this._yieldTo;
|
||||
return edit;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ export enum PasteUrlAsFormattedLink {
|
||||
Never = 'never'
|
||||
}
|
||||
|
||||
export async function getPasteUrlAsFormattedLinkSetting(document: vscode.TextDocument): Promise<PasteUrlAsFormattedLink> {
|
||||
export function getPasteUrlAsFormattedLinkSetting(document: vscode.TextDocument): PasteUrlAsFormattedLink {
|
||||
return vscode.workspace.getConfiguration('markdown', document).get<PasteUrlAsFormattedLink>('editor.pasteUrlAsFormattedLink.enabled', PasteUrlAsFormattedLink.Smart);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user