-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathupdatePastedLinks.ts
230 lines (190 loc) · 9.62 KB
/
updatePastedLinks.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as lsp from 'vscode-languageserver-protocol';
import { URI } from 'vscode-uri';
import { LsConfiguration } from '../config';
import { HrefKind, LinkDefinitionSet, MdLinkDefinition } from '../types/documentLink';
import { InMemoryDocument, tempDocVersion } from '../types/inMemoryDocument';
import { isBefore, isBeforeOrEqual } from '../types/position';
import { rangeContains } from '../types/range';
import { getDocUri, ITextDocument } from '../types/textDocument';
import { removeNewUriExtIfNeeded } from '../util/mdLinks';
import { computeRelativePath, isSameResource } from '../util/path';
import { createAddDefinitionEdit } from './codeActions/extractLinkDef';
import { MdLinkProvider } from './documentLinks';
class PasteLinksCopyMetadata {
static fromJSON(json: string): PasteLinksCopyMetadata {
const obj = JSON.parse(json);
return new PasteLinksCopyMetadata(URI.parse(obj.source), new LinkDefinitionSet(obj.links));
}
constructor(
readonly source: URI,
readonly links: LinkDefinitionSet | undefined,
) { }
toJSON(): string {
return JSON.stringify({
source: this.source.toString(),
links: this.links ? Array.from(this.links) : undefined,
});
}
}
export class MdUpdatePastedLinksProvider {
readonly #config: LsConfiguration;
readonly #linkProvider: MdLinkProvider;
constructor(
config: LsConfiguration,
linkProvider: MdLinkProvider,
) {
this.#config = config;
this.#linkProvider = linkProvider;
}
async prepareDocumentPaste(document: ITextDocument, _ranges: readonly lsp.Range[], token: lsp.CancellationToken): Promise<string> {
const linkInfo = await this.#linkProvider.getLinks(document);
if (token.isCancellationRequested) {
return '';
}
const metadata = new PasteLinksCopyMetadata(getDocUri(document), linkInfo.definitions);
return metadata.toJSON();
}
async provideDocumentPasteEdits(
targetDocument: ITextDocument,
pastes: readonly lsp.TextEdit[],
rawCopyMetadata: string,
token: lsp.CancellationToken,
): Promise<lsp.TextEdit[] | undefined> {
const metadata = this.#parseMetadata(rawCopyMetadata);
if (!metadata) {
return;
}
// If pasting into same doc copied from, there's no need to rewrite anything
if (isSameResource(getDocUri(targetDocument), metadata.source)) {
return;
}
// Bail early if there's nothing that looks like it could be a link in the pasted text
if (!pastes.some(p => p.newText.includes(']') || p.newText.includes('<'))) {
return undefined;
}
const sortedPastes = Array.from(pastes).sort((a, b) => targetDocument.offsetAt(a.range.start) - targetDocument.offsetAt(b.range.start));
// Find the links in the pasted text by applying the paste edits to an in-memory document.
// Use `copySource` as the doc uri to make sure links are resolved in its context
const editedDoc = new InMemoryDocument(metadata.source, targetDocument.getText(), tempDocVersion);
editedDoc.replaceContents(editedDoc.previewEdits(sortedPastes));
const allLinks = await this.#linkProvider.getLinksWithoutCaching(editedDoc, token);
if (token.isCancellationRequested) {
return;
}
const pastedRanges = this.#computedPastedRanges(sortedPastes, targetDocument, editedDoc);
const linksToRewrite = allLinks.links
// We only rewrite relative links and references
.filter(link => {
if (link.href.kind === HrefKind.Reference) {
return true;
}
return link.href.kind === HrefKind.Internal
&& !link.source.hrefText.startsWith('/') // No need to rewrite absolute paths
&& link.href.path.scheme === metadata.source.scheme && link.href.path.authority === metadata.source.authority; // Only rewrite links that are in the same workspace
})
// And the link be newly added (i.e. in one of the pasted ranges)
.filter(link => pastedRanges.some(range => rangeContains(range, link.source.range)));
// Generate edits
const newDefinitionsToAdd: MdLinkDefinition[] = [];
const rewriteLinksEdits: lsp.TextEdit[] = [];
for (const link of linksToRewrite) {
if (link.href.kind === HrefKind.Reference) {
// See if we've already added the def
if (new LinkDefinitionSet(newDefinitionsToAdd).lookup(link.href.ref)) {
continue;
}
const originalRef = metadata.links?.lookup(link.href.ref);
if (!originalRef) {
continue;
}
// If there's an existing definition with the same exact ref, we don't need to add it again
if (allLinks.definitions.lookup(link.href.ref)?.source.hrefText === originalRef.source.hrefText) {
continue;
}
newDefinitionsToAdd.push(originalRef);
} else if (link.href.kind === HrefKind.Internal) {
const targetDocUri = getDocUri(targetDocument);
const newPathText = isSameResource(targetDocUri, link.href.path)
? ''
: computeRelativePath(targetDocUri, removeNewUriExtIfNeeded(this.#config, link.href, link.href.path));
if (typeof newPathText === 'undefined') {
continue;
}
let newHrefText = newPathText;
if (link.source.fragmentRange) {
newHrefText += '#' + link.href.fragment;
}
if (link.source.hrefText !== newHrefText) {
rewriteLinksEdits.push(lsp.TextEdit.replace(link.source.hrefRange, newHrefText));
}
}
}
// If nothing was rewritten we can just use normal text paste
if (!rewriteLinksEdits.length && !newDefinitionsToAdd.length) {
return;
}
// Generate a minimal set of edits for the pastes
const outEdits: lsp.TextEdit[] = [];
const finalDoc = new InMemoryDocument(editedDoc.$uri, editedDoc.previewEdits(rewriteLinksEdits));
let offsetAdjustment = 0;
for (let i = 0; i < pastedRanges.length; ++i) {
const pasteRange = pastedRanges[i];
const originalPaste = sortedPastes[i];
// Adjust the range to account for the `rewriteLinksEdits`
for (
let edit: lsp.TextEdit | undefined;
(edit = rewriteLinksEdits[0]) && isBefore(edit.range.start, pasteRange.start);
rewriteLinksEdits.shift()
) {
offsetAdjustment += computeEditLengthChange(edit, editedDoc);
}
const startOffset = editedDoc.offsetAt(pasteRange.start) + offsetAdjustment;
for (
let edit: lsp.TextEdit | undefined;
(edit = rewriteLinksEdits[0]) && isBeforeOrEqual(edit.range.end, pasteRange.end);
rewriteLinksEdits.shift()
) {
offsetAdjustment += computeEditLengthChange(edit, editedDoc);
}
const endOffset = editedDoc.offsetAt(pasteRange.end) + offsetAdjustment;
const range = lsp.Range.create(finalDoc.positionAt(startOffset), finalDoc.positionAt(endOffset));
outEdits.push(lsp.TextEdit.replace(originalPaste.range, finalDoc.getText(range)));
}
// Add an edit that inserts new definitions
if (newDefinitionsToAdd.length) {
const targetLinks = await this.#linkProvider.getLinks(targetDocument);
if (token.isCancellationRequested) {
return;
}
outEdits.push(createAddDefinitionEdit(targetDocument, Array.from(targetLinks.definitions), newDefinitionsToAdd.map(def => ({ placeholder: def.ref.text, definitionText: def.source.hrefText }))));
}
return outEdits;
}
#parseMetadata(rawCopyMetadata: string): PasteLinksCopyMetadata | undefined {
try {
return PasteLinksCopyMetadata.fromJSON(rawCopyMetadata);
} catch {
return undefined;
}
}
#computedPastedRanges(sortedPastes: readonly lsp.TextEdit[], targetDocument: ITextDocument, editedDoc: InMemoryDocument): lsp.Range[] {
const pastedRanges: lsp.Range[] = [];
let offsetAdjustment = 0;
for (const paste of sortedPastes) {
const originalStartOffset = targetDocument.offsetAt(paste.range.start);
const originalEndOffset = targetDocument.offsetAt(paste.range.end);
pastedRanges.push(lsp.Range.create(
editedDoc.positionAt(originalStartOffset + offsetAdjustment),
editedDoc.positionAt(originalStartOffset + offsetAdjustment + paste.newText.length)));
offsetAdjustment += paste.newText.length - (originalEndOffset - originalStartOffset);
}
return pastedRanges;
}
}
function computeEditLengthChange(edit: lsp.TextEdit, editedDoc: InMemoryDocument) {
return edit.newText.length - (editedDoc.offsetAt(edit.range.end) - editedDoc.offsetAt(edit.range.start));
}