|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as path from 'path'; |
| 7 | +import * as picomatch from 'picomatch'; |
| 8 | +import * as vscode from 'vscode'; |
| 9 | +import { BaseLanguageClient } from 'vscode-languageclient'; |
| 10 | +import * as nls from 'vscode-nls'; |
| 11 | +import { getEditForFileRenames } from '../protocol'; |
| 12 | +import { Delayer } from '../util/async'; |
| 13 | +import { noopToken } from '../util/cancellation'; |
| 14 | +import { Disposable } from '../util/dispose'; |
| 15 | +import { looksLikeMarkdownPath } from '../util/file'; |
| 16 | +import { convertRange } from './fileReferences'; |
| 17 | + |
| 18 | +const localize = nls.loadMessageBundle(); |
| 19 | + |
| 20 | +const settingNames = Object.freeze({ |
| 21 | + enabled: 'experimental.updateLinksOnFileMove.enabled', |
| 22 | + externalFileGlobs: 'experimental.updateLinksOnFileMove.externalFileGlobs' |
| 23 | +}); |
| 24 | + |
| 25 | +const enum UpdateLinksOnFileMoveSetting { |
| 26 | + Prompt = 'prompt', |
| 27 | + Always = 'always', |
| 28 | + Never = 'never', |
| 29 | +} |
| 30 | + |
| 31 | +interface RenameAction { |
| 32 | + readonly oldUri: vscode.Uri; |
| 33 | + readonly newUri: vscode.Uri; |
| 34 | +} |
| 35 | + |
| 36 | +class UpdateImportsOnFileRenameHandler extends Disposable { |
| 37 | + |
| 38 | + private readonly _delayer = new Delayer(50); |
| 39 | + private readonly _pendingRenames = new Set<RenameAction>(); |
| 40 | + |
| 41 | + public constructor( |
| 42 | + private readonly client: BaseLanguageClient, |
| 43 | + ) { |
| 44 | + super(); |
| 45 | + |
| 46 | + this._register(vscode.workspace.onDidRenameFiles(async (e) => { |
| 47 | + const [{ newUri, oldUri }] = e.files; // TODO: only handles first file |
| 48 | + |
| 49 | + const config = this.getConfiguration(newUri); |
| 50 | + |
| 51 | + const setting = config.get<UpdateLinksOnFileMoveSetting>(settingNames.enabled); |
| 52 | + if (setting === UpdateLinksOnFileMoveSetting.Never) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (!this.shouldParticipateInLinkUpdate(config, newUri)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + this._pendingRenames.add({ oldUri, newUri }); |
| 61 | + |
| 62 | + this._delayer.trigger(() => { |
| 63 | + vscode.window.withProgress({ |
| 64 | + location: vscode.ProgressLocation.Window, |
| 65 | + title: localize('renameProgress.title', "Checking for Markdown links to update") |
| 66 | + }, () => this.flushRenames()); |
| 67 | + }); |
| 68 | + })); |
| 69 | + } |
| 70 | + |
| 71 | + private async flushRenames(): Promise<void> { |
| 72 | + const renames = Array.from(this._pendingRenames); |
| 73 | + this._pendingRenames.clear(); |
| 74 | + |
| 75 | + const edit = new vscode.WorkspaceEdit(); |
| 76 | + const resourcesBeingRenamed: vscode.Uri[] = []; |
| 77 | + |
| 78 | + for (const { oldUri, newUri } of renames) { |
| 79 | + if (await this.withEditsForFileRename(edit, oldUri, newUri, noopToken)) { |
| 80 | + resourcesBeingRenamed.push(newUri); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (edit.size) { |
| 85 | + if (await this.confirmActionWithUser(resourcesBeingRenamed)) { |
| 86 | + await vscode.workspace.applyEdit(edit); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private async confirmActionWithUser(newResources: readonly vscode.Uri[]): Promise<boolean> { |
| 92 | + if (!newResources.length) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + const config = this.getConfiguration(newResources[0]); |
| 97 | + const setting = config.get<UpdateLinksOnFileMoveSetting>(settingNames.enabled); |
| 98 | + switch (setting) { |
| 99 | + case UpdateLinksOnFileMoveSetting.Prompt: |
| 100 | + return this.promptUser(newResources); |
| 101 | + case UpdateLinksOnFileMoveSetting.Always: |
| 102 | + return true; |
| 103 | + case UpdateLinksOnFileMoveSetting.Never: |
| 104 | + default: |
| 105 | + return false; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private getConfiguration(resource: vscode.Uri) { |
| 110 | + return vscode.workspace.getConfiguration('markdown', resource); |
| 111 | + } |
| 112 | + |
| 113 | + private shouldParticipateInLinkUpdate(config: vscode.WorkspaceConfiguration, newUri: vscode.Uri) { |
| 114 | + if (looksLikeMarkdownPath(newUri)) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + const externalGlob = config.get<string>(settingNames.externalFileGlobs); |
| 119 | + return !!externalGlob && picomatch.isMatch(newUri.fsPath, externalGlob); |
| 120 | + } |
| 121 | + |
| 122 | + private async promptUser(newResources: readonly vscode.Uri[]): Promise<boolean> { |
| 123 | + if (!newResources.length) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + const enum Choice { |
| 128 | + None = 0, |
| 129 | + Accept = 1, |
| 130 | + Reject = 2, |
| 131 | + Always = 3, |
| 132 | + Never = 4, |
| 133 | + } |
| 134 | + |
| 135 | + interface Item extends vscode.MessageItem { |
| 136 | + readonly choice: Choice; |
| 137 | + } |
| 138 | + |
| 139 | + const response = await vscode.window.showInformationMessage<Item>( |
| 140 | + newResources.length === 1 |
| 141 | + ? localize('prompt', "Update Markdown links for '{0}'?", path.basename(newResources[0].fsPath)) |
| 142 | + : this.getConfirmMessage(localize('promptMoreThanOne', "Update Markdown link for the following {0} files?", newResources.length), newResources), { |
| 143 | + modal: true, |
| 144 | + }, { |
| 145 | + title: localize('reject.title', "No"), |
| 146 | + choice: Choice.Reject, |
| 147 | + isCloseAffordance: true, |
| 148 | + }, { |
| 149 | + title: localize('accept.title', "Yes"), |
| 150 | + choice: Choice.Accept, |
| 151 | + }, { |
| 152 | + title: localize('always.title', "Always automatically update Markdown Links"), |
| 153 | + choice: Choice.Always, |
| 154 | + }, { |
| 155 | + title: localize('never.title', "Never automatically update Markdown Links"), |
| 156 | + choice: Choice.Never, |
| 157 | + }); |
| 158 | + |
| 159 | + if (!response) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
| 163 | + switch (response.choice) { |
| 164 | + case Choice.Accept: { |
| 165 | + return true; |
| 166 | + } |
| 167 | + case Choice.Reject: { |
| 168 | + return false; |
| 169 | + } |
| 170 | + case Choice.Always: { |
| 171 | + const config = this.getConfiguration(newResources[0]); |
| 172 | + config.update( |
| 173 | + settingNames.enabled, |
| 174 | + UpdateLinksOnFileMoveSetting.Always, |
| 175 | + vscode.ConfigurationTarget.Global); |
| 176 | + return true; |
| 177 | + } |
| 178 | + case Choice.Never: { |
| 179 | + const config = this.getConfiguration(newResources[0]); |
| 180 | + config.update( |
| 181 | + settingNames.enabled, |
| 182 | + UpdateLinksOnFileMoveSetting.Never, |
| 183 | + vscode.ConfigurationTarget.Global); |
| 184 | + return false; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + private async withEditsForFileRename( |
| 192 | + workspaceEdit: vscode.WorkspaceEdit, |
| 193 | + oldUri: vscode.Uri, |
| 194 | + newUri: vscode.Uri, |
| 195 | + token: vscode.CancellationToken, |
| 196 | + ): Promise<boolean> { |
| 197 | + const edit = await this.client.sendRequest(getEditForFileRenames, [{ oldUri: oldUri.toString(), newUri: newUri.toString() }], token); |
| 198 | + if (!edit.changes) { |
| 199 | + return false; |
| 200 | + } |
| 201 | + |
| 202 | + for (const [path, edits] of Object.entries(edit.changes)) { |
| 203 | + const uri = vscode.Uri.parse(path); |
| 204 | + for (const edit of edits) { |
| 205 | + workspaceEdit.replace(uri, convertRange(edit.range), edit.newText); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + return true; |
| 210 | + } |
| 211 | + |
| 212 | + private getConfirmMessage(start: string, resourcesToConfirm: readonly vscode.Uri[]): string { |
| 213 | + const MAX_CONFIRM_FILES = 10; |
| 214 | + |
| 215 | + const paths = [start]; |
| 216 | + paths.push(''); |
| 217 | + paths.push(...resourcesToConfirm.slice(0, MAX_CONFIRM_FILES).map(r => path.basename(r.fsPath))); |
| 218 | + |
| 219 | + if (resourcesToConfirm.length > MAX_CONFIRM_FILES) { |
| 220 | + if (resourcesToConfirm.length - MAX_CONFIRM_FILES === 1) { |
| 221 | + paths.push(localize('moreFile', "...1 additional file not shown")); |
| 222 | + } else { |
| 223 | + paths.push(localize('moreFiles', "...{0} additional files not shown", resourcesToConfirm.length - MAX_CONFIRM_FILES)); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + paths.push(''); |
| 228 | + return paths.join('\n'); |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +export function registerUpdatePathsOnRename(client: BaseLanguageClient) { |
| 233 | + return new UpdateImportsOnFileRenameHandler(client); |
| 234 | +} |
0 commit comments