forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 6
/
pasteUrlProvider.ts
194 lines (165 loc) · 6.02 KB
/
pasteUrlProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { IMdParser } from '../../markdownEngine';
import { ITextDocument } from '../../types/textDocument';
import { Mime } from '../../util/mimes';
import { createInsertUriListEdit, externalUriSchemes } from './shared';
export enum PasteUrlAsMarkdownLink {
Always = 'always',
SmartWithSelection = 'smartWithSelection',
Smart = 'smart',
Never = 'never'
}
function getPasteUrlAsFormattedLinkSetting(document: vscode.TextDocument): PasteUrlAsMarkdownLink {
return vscode.workspace.getConfiguration('markdown', document)
.get<PasteUrlAsMarkdownLink>('editor.pasteUrlAsFormattedLink.enabled', PasteUrlAsMarkdownLink.SmartWithSelection);
}
/**
* Adds support for pasting text uris to create markdown links.
*
* This only applies to `text/plain`. Other mimes like `text/uri-list` are handled by ResourcePasteOrDropProvider.
*/
class PasteUrlEditProvider implements vscode.DocumentPasteEditProvider {
public static readonly id = 'insertMarkdownLink';
public static readonly pasteMimeTypes = [Mime.textPlain];
constructor(
private readonly _parser: IMdParser,
) { }
async provideDocumentPasteEdits(
document: vscode.TextDocument,
ranges: readonly vscode.Range[],
dataTransfer: vscode.DataTransfer,
token: vscode.CancellationToken,
): Promise<vscode.DocumentPasteEdit | undefined> {
const pasteUrlSetting = getPasteUrlAsFormattedLinkSetting(document);
if (pasteUrlSetting === PasteUrlAsMarkdownLink.Never) {
return;
}
const item = dataTransfer.get(Mime.textPlain);
const text = await item?.asString();
if (token.isCancellationRequested || !text) {
return;
}
const uriText = findValidUriInText(text);
if (!uriText) {
return;
}
const edit = createInsertUriListEdit(document, ranges, uriText);
if (!edit) {
return;
}
const pasteEdit = new vscode.DocumentPasteEdit('', edit.label);
const workspaceEdit = new vscode.WorkspaceEdit();
workspaceEdit.set(document.uri, edit.edits);
pasteEdit.additionalEdit = workspaceEdit;
if (!shouldInsertMarkdownLinkByDefault(this._parser, document, pasteUrlSetting, ranges, token)) {
pasteEdit.yieldTo = [{ mimeType: Mime.textPlain }];
}
return pasteEdit;
}
}
export function registerPasteUrlSupport(selector: vscode.DocumentSelector, parser: IMdParser) {
return vscode.languages.registerDocumentPasteEditProvider(selector, new PasteUrlEditProvider(parser), {
id: PasteUrlEditProvider.id,
pasteMimeTypes: PasteUrlEditProvider.pasteMimeTypes,
});
}
const smartPasteLineRegexes = [
{ regex: /(\[[^\[\]]*](?:\([^\(\)]*\)|\[[^\[\]]*]))/g }, // In a Markdown link
{ 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)
];
export async function shouldInsertMarkdownLinkByDefault(
parser: IMdParser,
document: ITextDocument,
pasteUrlSetting: PasteUrlAsMarkdownLink,
ranges: readonly vscode.Range[],
token: vscode.CancellationToken,
): Promise<boolean> {
switch (pasteUrlSetting) {
case PasteUrlAsMarkdownLink.Always: {
return true;
}
case PasteUrlAsMarkdownLink.Smart: {
return checkSmart();
}
case PasteUrlAsMarkdownLink.SmartWithSelection: {
// At least one range must not be empty
if (!ranges.some(range => document.getText(range).trim().length > 0)) {
return false;
}
// And all ranges must be smart
return checkSmart();
}
default: {
return false;
}
}
async function checkSmart(): Promise<boolean> {
return (await Promise.all(ranges.map(range => shouldSmartPasteForSelection(parser, document, range, token)))).every(x => x);
}
}
async function shouldSmartPasteForSelection(
parser: IMdParser,
document: ITextDocument,
selectedRange: vscode.Range,
token: vscode.CancellationToken,
): Promise<boolean> {
// Disable for multi-line selections
if (selectedRange.start.line !== selectedRange.end.line) {
return false;
}
const rangeText = document.getText(selectedRange);
// Disable when the selection is already a link
if (findValidUriInText(rangeText)) {
return false;
}
if (/\[.*\]\(.*\)/.test(rangeText) || /!\[.*\]\(.*\)/.test(rangeText)) {
return false;
}
// Check if selection is inside a special block level element using markdown engine
const tokens = await parser.tokenize(document);
if (token.isCancellationRequested) {
return false;
}
for (const token of tokens) {
if (token.map && token.map[0] <= selectedRange.start.line && token.map[1] > selectedRange.start.line) {
if (!['paragraph_open', 'inline', 'heading_open', 'ordered_list_open', 'bullet_list_open', 'list_item_open', 'blockquote_open'].includes(token.type)) {
return false;
}
}
}
// Run additional regex checks on the current line to check if we are inside an inline element
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) {
return false;
}
}
}
return true;
}
export function findValidUriInText(text: string): string | undefined {
const trimmedUrlList = text.trim();
// Uri must consist of a single sequence of characters without spaces
if (!/^\S+$/.test(trimmedUrlList)) {
return;
}
let uri: vscode.Uri;
try {
uri = vscode.Uri.parse(trimmedUrlList);
} catch {
// Could not parse
return;
}
if (!externalUriSchemes.includes(uri.scheme.toLowerCase()) || uri.authority.length <= 1) {
return;
}
return trimmedUrlList;
}