Skip to content

Commit

Permalink
Finalize markdown update links on paste setting
Browse files Browse the repository at this point in the history
Fixes microsoft#209318

Enables this new feature by default (but as an option, not the default way to paste)
  • Loading branch information
mjbvz committed Jul 15, 2024
1 parent 286795f commit 0a1504f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
9 changes: 3 additions & 6 deletions extensions/markdown-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -708,14 +708,11 @@
"%configuration.markdown.preferredMdPathExtensionStyle.removeExtension%"
]
},
"markdown.experimental.updateLinksOnPaste": {
"markdown.editor.updateLinksOnPaste.enabled": {
"type": "boolean",
"default": false,
"markdownDescription": "%configuration.markdown.experimental.updateLinksOnPaste%",
"markdownDescription": "%configuration.markdown.editor.updateLinksOnPaste.enabled%",
"scope": "resource",
"tags": [
"experimental"
]
"default": true
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion extensions/markdown-language-features/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@
"configuration.markdown.preferredMdPathExtensionStyle.removeExtension": "Prefer removing the file extension. For example, path completions to a file named `file.md` will insert `file` without the `.md`.",
"configuration.markdown.editor.filePaste.videoSnippet": "Snippet used when adding videos to Markdown. This snippet can use the following variables:\n- `${src}` — The resolved path of the video file.\n- `${title}` — The title used for the video. A snippet placeholder will automatically be created for this variable.",
"configuration.markdown.editor.filePaste.audioSnippet": "Snippet used when adding audio to Markdown. This snippet can use the following variables:\n- `${src}` — The resolved path of the audio file.\n- `${title}` — The title used for the audio. A snippet placeholder will automatically be created for this variable.",
"configuration.markdown.experimental.updateLinksOnPaste": "Enable/disable automatic updating of links in text that is copied and pasted from one Markdown editor to another.",
"configuration.markdown.editor.updateLinksOnPaste.enabled": "Enable/disable updating of links in text that is copied and pasted from one Markdown editor to another.",
"workspaceTrust": "Required for loading styles configured in the workspace."
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Mime } from '../util/mimes';

class UpdatePastedLinksEditProvider implements vscode.DocumentPasteEditProvider {

public static readonly kind = vscode.DocumentDropOrPasteEditKind.Empty.append('text', 'markdown', 'updateLinks');
public static readonly kind = vscode.DocumentDropOrPasteEditKind.Empty.append('markdown', 'updateLinks');

public static readonly metadataMime = 'vnd.vscode.markdown.updateLinksMetadata';

Expand All @@ -26,14 +26,15 @@ class UpdatePastedLinksEditProvider implements vscode.DocumentPasteEditProvider
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,
context: vscode.DocumentPasteEditContext,
token: vscode.CancellationToken,
): Promise<vscode.DocumentPasteEdit[] | undefined> {
if (!this._isEnabled(document)) {
Expand All @@ -56,19 +57,24 @@ class UpdatePastedLinksEditProvider implements vscode.DocumentPasteEditProvider
// - 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) {
if (!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;

if (!context.only || !UpdatePastedLinksEditProvider.kind.contains(context.only)) {
pasteEdit.yieldTo = [vscode.DocumentDropOrPasteEditKind.Empty.append('text')];
}

return [pasteEdit];
}

private _isEnabled(document: vscode.TextDocument): boolean {
return vscode.workspace.getConfiguration('markdown', document.uri).get<boolean>('experimental.updateLinksOnPaste', false);
return vscode.workspace.getConfiguration('markdown', document.uri).get<boolean>('editor.updateLinksOnPaste.enabled', true);
}
}

Expand Down

0 comments on commit 0a1504f

Please sign in to comment.