forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental support for updating markdown links on copy/paste
For microsoft#209318
- Loading branch information
Showing
13 changed files
with
164 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
extensions/markdown-language-features/src/languageFeatures/updateLinksOnPaste.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* 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 { MdLanguageClient } from '../client/client'; | ||
import { Mime } from '../util/mimes'; | ||
|
||
class UpdatePastedLinksEditProvider implements vscode.DocumentPasteEditProvider { | ||
|
||
public static readonly kind = vscode.DocumentPasteEditKind.Empty.append('text', 'markdown', 'updateLinks'); | ||
|
||
public static readonly metadataMime = 'vnd.vscode.markdown.updateLinksMetadata'; | ||
|
||
constructor( | ||
private readonly _client: MdLanguageClient, | ||
) { } | ||
|
||
async prepareDocumentPaste(document: vscode.TextDocument, ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken): Promise<void> { | ||
if (!this._isEnabled(document)) { | ||
return; | ||
} | ||
|
||
const metadata = await this._client.prepareUpdatePastedLinks(document.uri, ranges, token); | ||
if (token.isCancellationRequested) { | ||
return; | ||
} | ||
dataTransfer.set(UpdatePastedLinksEditProvider.metadataMime, new vscode.DataTransferItem(metadata)); | ||
} | ||
|
||
async provideDocumentPasteEdits( | ||
document: vscode.TextDocument, | ||
ranges: readonly vscode.Range[], | ||
dataTransfer: vscode.DataTransfer, | ||
_context: vscode.DocumentPasteEditContext, | ||
token: vscode.CancellationToken, | ||
): Promise<vscode.DocumentPasteEdit[] | undefined> { | ||
if (!this._isEnabled(document)) { | ||
return; | ||
} | ||
|
||
const metadata = dataTransfer.get(UpdatePastedLinksEditProvider.metadataMime)?.value; | ||
if (!metadata) { | ||
return; | ||
} | ||
|
||
const textItem = dataTransfer.get(Mime.textPlain); | ||
const text = await textItem?.asString(); | ||
if (!text || token.isCancellationRequested) { | ||
return; | ||
} | ||
|
||
// TODO: Handle cases such as: | ||
// - copy empty line | ||
// - Copy with multiple cursors and paste into multiple locations | ||
// - ... | ||
const edits = await this._client.getUpdatePastedLinksEdit(document.uri, ranges.map(x => new vscode.TextEdit(x, text)), metadata, token); | ||
if (!edits || !edits.length || token.isCancellationRequested) { | ||
return; | ||
} | ||
|
||
const pasteEdit = new vscode.DocumentPasteEdit('', vscode.l10n.t("Paste and update pasted links"), UpdatePastedLinksEditProvider.kind); | ||
const workspaceEdit = new vscode.WorkspaceEdit(); | ||
workspaceEdit.set(document.uri, edits.map(x => new vscode.TextEdit(new vscode.Range(x.range.start.line, x.range.start.character, x.range.end.line, x.range.end.character,), x.newText))); | ||
pasteEdit.additionalEdit = workspaceEdit; | ||
return [pasteEdit]; | ||
} | ||
|
||
private _isEnabled(document: vscode.TextDocument): boolean { | ||
return vscode.workspace.getConfiguration('markdown', document.uri).get<boolean>('experimental.updateLinksOnPaste', false); | ||
} | ||
} | ||
|
||
export function registerUpdatePastedLinks(selector: vscode.DocumentSelector, client: MdLanguageClient) { | ||
return vscode.languages.registerDocumentPasteEditProvider(selector, new UpdatePastedLinksEditProvider(client), { | ||
copyMimeTypes: [UpdatePastedLinksEditProvider.metadataMime], | ||
providedPasteEditKinds: [UpdatePastedLinksEditProvider.kind], | ||
pasteMimeTypes: [UpdatePastedLinksEditProvider.metadataMime], | ||
}); | ||
} |