Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/markdown-language-features/src/languageFeatures/fileReferences.ts
3292 views
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 vscode from 'vscode';
7
import type * as lsp from 'vscode-languageserver-types';
8
import { MdLanguageClient } from '../client/client';
9
import { Command, CommandManager } from '../commandManager';
10
11
12
export class FindFileReferencesCommand implements Command {
13
14
public readonly id = 'markdown.findAllFileReferences';
15
16
constructor(
17
private readonly _client: MdLanguageClient,
18
) { }
19
20
public async execute(resource?: vscode.Uri) {
21
resource ??= vscode.window.activeTextEditor?.document.uri;
22
if (!resource) {
23
vscode.window.showErrorMessage(vscode.l10n.t("Find file references failed. No resource provided."));
24
return;
25
}
26
27
await vscode.window.withProgress({
28
location: vscode.ProgressLocation.Window,
29
title: vscode.l10n.t("Finding file references")
30
}, async (_progress, token) => {
31
const locations = (await this._client.getReferencesToFileInWorkspace(resource, token)).map(loc => {
32
return new vscode.Location(vscode.Uri.parse(loc.uri), convertRange(loc.range));
33
});
34
35
const config = vscode.workspace.getConfiguration('references');
36
const existingSetting = config.inspect<string>('preferredLocation');
37
38
await config.update('preferredLocation', 'view');
39
try {
40
await vscode.commands.executeCommand('editor.action.showReferences', resource, new vscode.Position(0, 0), locations);
41
} finally {
42
await config.update('preferredLocation', existingSetting?.workspaceFolderValue ?? existingSetting?.workspaceValue);
43
}
44
});
45
}
46
}
47
48
export function convertRange(range: lsp.Range): vscode.Range {
49
return new vscode.Range(range.start.line, range.start.character, range.end.line, range.end.character);
50
}
51
52
export function registerFindFileReferenceSupport(
53
commandManager: CommandManager,
54
client: MdLanguageClient,
55
): vscode.Disposable {
56
return commandManager.register(new FindFileReferencesCommand(client));
57
}
58
59