mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-24 12:19:20 +00:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user