Fix markdown link pasting when selection is inline code (#203657)

If the user selects a complete inline code block, we should paste as a markdown link with the code as the link text
This commit is contained in:
Matt Bierner
2024-01-29 08:10:52 +00:00
committed by GitHub
parent 0c6248932c
commit 772791e9e5
2 changed files with 29 additions and 4 deletions

View File

@@ -89,7 +89,7 @@ const smartPasteLineRegexes = [
{ regex: /\$\$[\s\S]*?\$\$/gm }, // In a fenced math block
{ regex: /`[^`]*`/g }, // In inline code
{ regex: /\$[^$]*\$/g }, // In inline math
{ regex: /^[ ]{0,3}\[\w+\]:\s.*$/g }, // Block link definition (needed as tokens are not generated for these)
{ regex: /^[ ]{0,3}\[\w+\]:\s.*$/g, isWholeLine: true }, // Block link definition (needed as tokens are not generated for these)
];
export async function shouldInsertMarkdownLinkByDefault(
@@ -184,7 +184,15 @@ async function shouldSmartPasteForSelection(
const line = document.getText(new vscode.Range(selectedRange.start.line, 0, selectedRange.start.line, Number.MAX_SAFE_INTEGER));
for (const regex of smartPasteLineRegexes) {
for (const match of line.matchAll(regex.regex)) {
if (match.index !== undefined && selectedRange.start.character >= match.index && selectedRange.start.character <= match.index + match[0].length) {
if (match.index === undefined) {
continue;
}
if (regex.isWholeLine) {
return false;
}
if (selectedRange.start.character > match.index && selectedRange.start.character < match.index + match[0].length) {
return false;
}
}

View File

@@ -195,6 +195,10 @@ suite('createEditAddingLinksForUriList', () => {
assert.strictEqual(
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('[ref]: '), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 7, 0, 7)], noopToken),
false);
assert.strictEqual(
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('[ref]: '), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 0, 0, 0)], noopToken),
false);
});
test('Smart should be disabled in html blocks', async () => {
@@ -235,11 +239,24 @@ suite('createEditAddingLinksForUriList', () => {
test('Smart should be disabled in inline code', async () => {
assert.strictEqual(
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('``'), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 1, 0, 1)], noopToken),
false);
false,
'Should be disabled inside of inline code');
assert.strictEqual(
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('``'), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 0, 0, 0)], noopToken),
false);
true,
'Should be enabled when cursor is outside but next to inline code');
assert.strictEqual(
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('`a`'), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 3, 0, 3)], noopToken),
true,
'Should be enabled when cursor is outside but next to inline code');
});
test('Smart should be enabled when pasting over inline code ', async () => {
assert.strictEqual(
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('`xyz`'), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 0, 0, 5)], noopToken),
true);
});
test('Smart should be disabled in inline math', async () => {