Path: blob/main/extensions/markdown-language-features/src/languageFeatures/fileReferences.ts
3292 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as vscode from 'vscode';6import type * as lsp from 'vscode-languageserver-types';7import { MdLanguageClient } from '../client/client';8import { Command, CommandManager } from '../commandManager';91011export class FindFileReferencesCommand implements Command {1213public readonly id = 'markdown.findAllFileReferences';1415constructor(16private readonly _client: MdLanguageClient,17) { }1819public async execute(resource?: vscode.Uri) {20resource ??= vscode.window.activeTextEditor?.document.uri;21if (!resource) {22vscode.window.showErrorMessage(vscode.l10n.t("Find file references failed. No resource provided."));23return;24}2526await vscode.window.withProgress({27location: vscode.ProgressLocation.Window,28title: vscode.l10n.t("Finding file references")29}, async (_progress, token) => {30const locations = (await this._client.getReferencesToFileInWorkspace(resource, token)).map(loc => {31return new vscode.Location(vscode.Uri.parse(loc.uri), convertRange(loc.range));32});3334const config = vscode.workspace.getConfiguration('references');35const existingSetting = config.inspect<string>('preferredLocation');3637await config.update('preferredLocation', 'view');38try {39await vscode.commands.executeCommand('editor.action.showReferences', resource, new vscode.Position(0, 0), locations);40} finally {41await config.update('preferredLocation', existingSetting?.workspaceFolderValue ?? existingSetting?.workspaceValue);42}43});44}45}4647export function convertRange(range: lsp.Range): vscode.Range {48return new vscode.Range(range.start.line, range.start.character, range.end.line, range.end.character);49}5051export function registerFindFileReferenceSupport(52commandManager: CommandManager,53client: MdLanguageClient,54): vscode.Disposable {55return commandManager.register(new FindFileReferencesCommand(client));56}575859