mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-29 04:53:33 +01:00
Rework markdown paste resource (#201838)
Fixes #184980 This refactors much of the logic around markdown paste/drop. PR got a little large but the main highlights are: - Allow using a custom snippet for inserted audio/video - Merge the drop/paste resource provider classes since these are so similar - Enable smart pasting of url text by default - Refactor url paste logic - For now, disable the behavior where url paste could paste a combination of markdown and plain uris. In practice this is confusing, especially because our labels for this were wrong. We can always reintroduce this later if multicursor users find it useful
This commit is contained in:
@@ -6,15 +6,20 @@ import * as assert from 'assert';
|
||||
import 'mocha';
|
||||
import * as vscode from 'vscode';
|
||||
import { InMemoryDocument } from '../client/inMemoryDocument';
|
||||
import { appendToLinkSnippet, createEditAddingLinksForUriList, findValidUriInText, shouldSmartPaste } from '../languageFeatures/copyFiles/shared';
|
||||
import { findValidUriInText, shouldSmartPaste } from '../languageFeatures/copyFiles/pasteUrlProvider';
|
||||
import { createInsertUriListEdit } from '../languageFeatures/copyFiles/shared';
|
||||
|
||||
function makeTestDoc(contents: string) {
|
||||
return new InMemoryDocument(vscode.Uri.file('test.md'), contents);
|
||||
}
|
||||
|
||||
suite('createEditAddingLinksForUriList', () => {
|
||||
|
||||
test('Markdown Link Pasting should occur for a valid link (end to end)', async () => {
|
||||
// createEditAddingLinksForUriList -> checkSmartPaste -> tryGetUriListSnippet -> createUriListSnippet -> createLinkSnippet
|
||||
|
||||
const result = createEditAddingLinksForUriList(
|
||||
new InMemoryDocument(vscode.Uri.file('test.md'), 'hello world!'), [new vscode.Range(0, 0, 0, 12)], 'https://www.microsoft.com/', true, true);
|
||||
const result = createInsertUriListEdit(
|
||||
new InMemoryDocument(vscode.Uri.file('test.md'), 'hello world!'), [new vscode.Range(0, 0, 0, 12)], 'https://www.microsoft.com/');
|
||||
// need to check the actual result -> snippet value
|
||||
assert.strictEqual(result?.label, 'Insert Markdown Link');
|
||||
});
|
||||
@@ -100,41 +105,31 @@ suite('createEditAddingLinksForUriList', () => {
|
||||
});
|
||||
});
|
||||
|
||||
suite('appendToLinkSnippet', () => {
|
||||
suite('createInsertUriListEdit', () => {
|
||||
|
||||
test('Should create snippet with < > when pasted link has an mismatched parentheses', () => {
|
||||
const uriString = 'https://www.mic(rosoft.com';
|
||||
const snippet = new vscode.SnippetString('');
|
||||
appendToLinkSnippet(snippet, 'abc', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:abc}](<https://www.mic(rosoft.com>)');
|
||||
const edit = createInsertUriListEdit(makeTestDoc(''), [new vscode.Range(0, 0, 0, 0)], 'https://www.mic(rosoft.com');
|
||||
assert.strictEqual(edit?.edits?.[0].snippet.value, '[${1:text}](<https://www.mic(rosoft.com>)');
|
||||
});
|
||||
|
||||
test('Should create Markdown link snippet when pasteAsMarkdownLink is true', () => {
|
||||
const uriString = 'https://www.microsoft.com';
|
||||
const snippet = new vscode.SnippetString('');
|
||||
appendToLinkSnippet(snippet, '', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com)');
|
||||
const edit = createInsertUriListEdit(makeTestDoc(''), [new vscode.Range(0, 0, 0, 0)], 'https://www.microsoft.com');
|
||||
assert.strictEqual(edit?.edits?.[0].snippet.value, '[${1:text}](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 = new vscode.SnippetString('');
|
||||
appendToLinkSnippet(snippet, '', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com)');
|
||||
const edit = createInsertUriListEdit(makeTestDoc(''), [new vscode.Range(0, 0, 0, 0)], 'https://www.microsoft.com');
|
||||
assert.strictEqual(edit?.edits?.[0].snippet.value, '[${1:text}](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 = new vscode.SnippetString('');
|
||||
appendToLinkSnippet(snippet, '', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com/%20)');
|
||||
const edit = createInsertUriListEdit(makeTestDoc(''), [new vscode.Range(0, 0, 0, 0)], 'https://www.microsoft.com/%20');
|
||||
assert.strictEqual(edit?.edits?.[0].snippet.value, '[${1:text}](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 = new vscode.SnippetString('');
|
||||
appendToLinkSnippet(snippet, '', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.example.com/path?query=value&another=value#fragment)');
|
||||
const edit = createInsertUriListEdit(makeTestDoc(''), [new vscode.Range(0, 0, 0, 0)], 'https://www.example.com/path?query=value&another=value#fragment');
|
||||
assert.strictEqual(edit?.edits?.[0].snippet.value, '[${1:text}](https://www.example.com/path?query=value&another=value#fragment)');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -220,7 +215,3 @@ suite('createEditAddingLinksForUriList', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function makeTestDoc(contents: string) {
|
||||
return new InMemoryDocument(vscode.Uri.file('test.md'), contents);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user