Skip to content

Commit 772791e

Browse files
authored
Fix markdown link pasting when selection is inline code (microsoft#203657)
If the user selects a complete inline code block, we should paste as a markdown link with the code as the link text
1 parent 0c62489 commit 772791e

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

extensions/markdown-language-features/src/languageFeatures/copyFiles/pasteUrlProvider.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const smartPasteLineRegexes = [
8989
{ regex: /\$\$[\s\S]*?\$\$/gm }, // In a fenced math block
9090
{ regex: /`[^`]*`/g }, // In inline code
9191
{ regex: /\$[^$]*\$/g }, // In inline math
92-
{ regex: /^[ ]{0,3}\[\w+\]:\s.*$/g }, // Block link definition (needed as tokens are not generated for these)
92+
{ regex: /^[ ]{0,3}\[\w+\]:\s.*$/g, isWholeLine: true }, // Block link definition (needed as tokens are not generated for these)
9393
];
9494

9595
export async function shouldInsertMarkdownLinkByDefault(
@@ -184,7 +184,15 @@ async function shouldSmartPasteForSelection(
184184
const line = document.getText(new vscode.Range(selectedRange.start.line, 0, selectedRange.start.line, Number.MAX_SAFE_INTEGER));
185185
for (const regex of smartPasteLineRegexes) {
186186
for (const match of line.matchAll(regex.regex)) {
187-
if (match.index !== undefined && selectedRange.start.character >= match.index && selectedRange.start.character <= match.index + match[0].length) {
187+
if (match.index === undefined) {
188+
continue;
189+
}
190+
191+
if (regex.isWholeLine) {
192+
return false;
193+
}
194+
195+
if (selectedRange.start.character > match.index && selectedRange.start.character < match.index + match[0].length) {
188196
return false;
189197
}
190198
}

extensions/markdown-language-features/src/test/pasteUrl.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ suite('createEditAddingLinksForUriList', () => {
195195
assert.strictEqual(
196196
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('[ref]: '), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 7, 0, 7)], noopToken),
197197
false);
198+
199+
assert.strictEqual(
200+
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('[ref]: '), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 0, 0, 0)], noopToken),
201+
false);
198202
});
199203

200204
test('Smart should be disabled in html blocks', async () => {
@@ -235,11 +239,24 @@ suite('createEditAddingLinksForUriList', () => {
235239
test('Smart should be disabled in inline code', async () => {
236240
assert.strictEqual(
237241
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('``'), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 1, 0, 1)], noopToken),
238-
false);
242+
false,
243+
'Should be disabled inside of inline code');
239244

240245
assert.strictEqual(
241246
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('``'), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 0, 0, 0)], noopToken),
242-
false);
247+
true,
248+
'Should be enabled when cursor is outside but next to inline code');
249+
250+
assert.strictEqual(
251+
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('`a`'), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 3, 0, 3)], noopToken),
252+
true,
253+
'Should be enabled when cursor is outside but next to inline code');
254+
});
255+
256+
test('Smart should be enabled when pasting over inline code ', async () => {
257+
assert.strictEqual(
258+
await shouldInsertMarkdownLinkByDefault(createNewMarkdownEngine(), makeTestDoc('`xyz`'), PasteUrlAsMarkdownLink.Smart, [new vscode.Range(0, 0, 0, 5)], noopToken),
259+
true);
243260
});
244261

245262
test('Smart should be disabled in inline math', async () => {

0 commit comments

Comments
 (0)