mirror of
https://github.com/Microsoft/vscode
synced 2024-09-13 21:55:38 +00:00
Deprioritize pasteAsMarkdown (#189433)
* deprioritize * fix tests * cleaned up code
This commit is contained in:
parent
a371c36d7c
commit
1e882b5626
|
@ -40,10 +40,19 @@ class PasteLinkEditProvider implements vscode.DocumentPasteEditProvider {
|
|||
return;
|
||||
}
|
||||
|
||||
uriEdit.label = pasteUrlSetting === PasteUrlAsFormattedLink.Smart ? vscode.l10n.t('Smartly Insert Link') : pasteEdit.label;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
export function registerLinkPasteSupport(selector: vscode.DocumentSelector,) {
|
||||
|
|
|
@ -63,12 +63,12 @@ export const mediaMimes = new Set([
|
|||
]);
|
||||
|
||||
const smartPasteRegexes = [
|
||||
{ regex: /(\[[^\[\]]*](?:\([^\(\)]*\)|\[[^\[\]]*]))/g, isMarkdownLink: false, isInline: true }, // In a Markdown link
|
||||
{ regex: /^```[\s\S]*?```$/gm, isMarkdownLink: false, isInline: false }, // In a backtick fenced code block
|
||||
{ regex: /^~~~[\s\S]*?~~~$/gm, isMarkdownLink: false, isInline: false }, // In a tildefenced code block
|
||||
{ regex: /^\$\$[\s\S]*?\$\$$/gm, isMarkdownLink: false, isInline: false }, // In a fenced math block
|
||||
{ regex: /`[^`]*`/g, isMarkdownLink: false, isInline: true }, // In inline code
|
||||
{ regex: /\$[^$]*\$/g, isMarkdownLink: false, isInline: true }, // In inline math
|
||||
{ regex: /(\[[^\[\]]*](?:\([^\(\)]*\)|\[[^\[\]]*]))/g }, // In a Markdown link
|
||||
{ regex: /^```[\s\S]*?```$/gm }, // In a backtick fenced code block
|
||||
{ regex: /^~~~[\s\S]*?~~~$/gm }, // In a tildefenced code block
|
||||
{ regex: /^\$\$[\s\S]*?\$\$$/gm }, // In a fenced math block
|
||||
{ regex: /`[^`]*`/g }, // In inline code
|
||||
{ regex: /\$[^$]*\$/g }, // In inline math
|
||||
];
|
||||
|
||||
export interface SkinnyTextDocument {
|
||||
|
@ -94,7 +94,7 @@ export async function createEditAddingLinksForUriList(
|
|||
isExternalLink: boolean,
|
||||
useSmartPaste: boolean,
|
||||
token: vscode.CancellationToken,
|
||||
): Promise<{ additionalEdits: vscode.WorkspaceEdit; label: string } | undefined> {
|
||||
): Promise<{ additionalEdits: vscode.WorkspaceEdit; label: string; markdownLink: boolean } | undefined> {
|
||||
|
||||
if (ranges.length === 0) {
|
||||
return;
|
||||
|
@ -103,6 +103,7 @@ export async function createEditAddingLinksForUriList(
|
|||
let placeHolderValue: number = ranges.length;
|
||||
let label: string = '';
|
||||
let pasteAsMarkdownLink: boolean = true;
|
||||
let markdownLink: boolean = true;
|
||||
|
||||
for (const range of ranges) {
|
||||
const selectedRange: vscode.Range = new vscode.Range(
|
||||
|
@ -112,6 +113,7 @@ export async function createEditAddingLinksForUriList(
|
|||
|
||||
if (useSmartPaste) {
|
||||
pasteAsMarkdownLink = checkSmartPaste(document, selectedRange, range);
|
||||
markdownLink = pasteAsMarkdownLink; // FIX: this will only match the last range
|
||||
}
|
||||
|
||||
const snippet = await tryGetUriListSnippet(document, urlList, token, document.getText(range), placeHolderValue, pasteAsMarkdownLink, isExternalLink);
|
||||
|
@ -128,7 +130,7 @@ export async function createEditAddingLinksForUriList(
|
|||
const additionalEdits = new vscode.WorkspaceEdit();
|
||||
additionalEdits.set(document.uri, edits);
|
||||
|
||||
return { additionalEdits, label };
|
||||
return { additionalEdits, label, markdownLink };
|
||||
}
|
||||
|
||||
export function checkSmartPaste(document: SkinnyTextDocument, selectedRange: vscode.Range, range: vscode.Range): boolean {
|
||||
|
@ -142,9 +144,8 @@ export function checkSmartPaste(document: SkinnyTextDocument, selectedRange: vsc
|
|||
const matches = [...document.getText().matchAll(regex.regex)];
|
||||
for (const match of matches) {
|
||||
if (match.index !== undefined) {
|
||||
const inLink = selectedRange.start.character > match.index && selectedRange.end.character < match.index + match[0].length;
|
||||
const overLink = regex.isMarkdownLink && selectedRange.start.character === match.index && selectedRange.end.character === match.index + match[0].length;
|
||||
if (inLink || overLink) {
|
||||
const useDefaultPaste = selectedRange.start.character > match.index && selectedRange.end.character < match.index + match[0].length;
|
||||
if (useDefaultPaste) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -203,20 +204,14 @@ interface UriListSnippetOptions {
|
|||
|
||||
export function appendToLinkSnippet(
|
||||
snippet: vscode.SnippetString,
|
||||
pasteAsMarkdownLink: boolean,
|
||||
mdPath: string,
|
||||
title: string,
|
||||
uriString: string,
|
||||
placeholderValue: number,
|
||||
isExternalLink: boolean,
|
||||
): vscode.SnippetString {
|
||||
if (pasteAsMarkdownLink) {
|
||||
snippet.appendText('[');
|
||||
snippet.appendPlaceholder(escapeBrackets(title) || 'Title', placeholderValue);
|
||||
snippet.appendText(`](${escapeMarkdownLinkPath(isExternalLink ? uriString : mdPath, isExternalLink)})`);
|
||||
} else {
|
||||
snippet.appendText((escapeMarkdownLinkPath(isExternalLink ? uriString : mdPath, isExternalLink)));
|
||||
}
|
||||
snippet.appendText('[');
|
||||
snippet.appendPlaceholder(escapeBrackets(title) || 'Title', placeholderValue);
|
||||
snippet.appendText(`](${escapeMarkdownLinkPath(uriString, isExternalLink)})`);
|
||||
return snippet;
|
||||
}
|
||||
|
||||
|
@ -274,8 +269,10 @@ export function createUriListSnippet(
|
|||
}
|
||||
} else {
|
||||
insertedLinkCount++;
|
||||
if (uriStrings) {
|
||||
snippet = appendToLinkSnippet(snippet, pasteAsMarkdownLink, mdPath, title, uriStrings[i], placeholderValue, isExternalLink);
|
||||
if (uriStrings && isExternalLink) {
|
||||
snippet = appendToLinkSnippet(snippet, title, uriStrings[i], placeholderValue, isExternalLink);
|
||||
} else {
|
||||
snippet.appendText(escapeMarkdownLinkPath(mdPath, isExternalLink));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import * as vscode from 'vscode';
|
|||
import * as assert from 'assert';
|
||||
import 'mocha';
|
||||
import { SkinnyTextDocument, checkSmartPaste, createEditAddingLinksForUriList, appendToLinkSnippet, validateLink } from '../languageFeatures/copyFiles/shared';
|
||||
|
||||
suite('createEditAddingLinksForUriList', () => {
|
||||
|
||||
test('Markdown Link Pasting should occur for a valid link (end to end)', async () => {
|
||||
|
@ -15,16 +16,6 @@ suite('createEditAddingLinksForUriList', () => {
|
|||
uri: vscode.Uri.parse('file:///path/to/your/file'),
|
||||
offsetAt: function () { return 0; },
|
||||
getText: function () { return 'hello world!'; },
|
||||
// lineAt: function (position: vscode.Position) {
|
||||
// return {
|
||||
// lineNumber: 0,
|
||||
// text: 'hello world!',
|
||||
// range: new vscode.Range(position, position),
|
||||
// rangeIncludingLineBreak: new vscode.Range(position, position),
|
||||
// firstNonWhitespaceCharacterIndex: 0,
|
||||
// isEmptyOrWhitespace: false
|
||||
// } as vscode.TextLine;
|
||||
// }
|
||||
};
|
||||
|
||||
const result = await createEditAddingLinksForUriList(skinnyDocument, [new vscode.Range(0, 0, 0, 12)], 'https://www.microsoft.com/', true, true, new vscode.CancellationTokenSource().token);
|
||||
|
@ -101,45 +92,34 @@ suite('createEditAddingLinksForUriList', () => {
|
|||
});
|
||||
|
||||
suite('appendToLinkSnippet', () => {
|
||||
test('Should create auto link when pasted link has an mismatched parentheses', () => {
|
||||
const uriString = 'https://www.mic(rosoft.com';
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), false, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, '<https://www.mic(rosoft.com>');
|
||||
});
|
||||
|
||||
test('Should create snippet with < > when pasted link has an mismatched parentheses', () => {
|
||||
const uriString = 'https://www.mic(rosoft.com';
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', 'abc', uriString, 0, true);
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), 'abc', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:abc}](<https://www.mic(rosoft.com>)');
|
||||
});
|
||||
|
||||
test('Should not create Markdown link snippet when pasteAsMarkdownLink is false', () => {
|
||||
const uriString = 'https://www.microsoft.com';
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), false, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, 'https://www.microsoft.com');
|
||||
});
|
||||
|
||||
test('Should create Markdown link snippet when pasteAsMarkdownLink is true', () => {
|
||||
const uriString = 'https://www.microsoft.com';
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), '', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com)');
|
||||
});
|
||||
|
||||
test('Should use an unencoded URI string in Markdown link when passing in an external browser link', () => {
|
||||
const uriString = 'https://www.microsoft.com';
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), '', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com)');
|
||||
});
|
||||
|
||||
test('Should not decode an encoded URI string when passing in an external browser link', () => {
|
||||
const uriString = 'https://www.microsoft.com/%20';
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), '', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com/%20)');
|
||||
});
|
||||
|
||||
test('Should not encode an unencoded URI string when passing in an external browser link', () => {
|
||||
const uriString = 'https://www.example.com/path?query=value&another=value#fragment';
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), '', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.example.com/path?query=value&another=value#fragment)');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue