rust/editors/code/src/dependencies_provider.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

167 lines
5.8 KiB
TypeScript
Raw Normal View History

2022-07-17 10:38:56 -05:00
import * as vscode from "vscode";
import * as fspath from "path";
import * as fs from "fs";
import * as os from "os";
import { activeToolchain, Cargo, Crate, getRustcVersion } from "./toolchain";
2022-02-25 18:37:55 -06:00
const debugOutput = vscode.window.createOutputChannel("Debug");
2022-07-17 10:38:56 -05:00
export class RustDependenciesProvider
implements vscode.TreeDataProvider<Dependency | DependencyFile>
{
2022-02-25 19:37:09 -06:00
cargo: Cargo;
dependenciesMap: { [id: string]: Dependency | DependencyFile };
2022-07-17 10:38:56 -05:00
constructor(private readonly workspaceRoot: string) {
this.cargo = new Cargo(this.workspaceRoot || ".", debugOutput);
2022-02-25 19:37:09 -06:00
this.dependenciesMap = {};
}
2022-07-17 10:38:56 -05:00
private _onDidChangeTreeData: vscode.EventEmitter<
Dependency | DependencyFile | undefined | null | void
> = new vscode.EventEmitter<Dependency | undefined | null | void>();
2022-02-25 19:37:09 -06:00
2022-07-17 10:38:56 -05:00
readonly onDidChangeTreeData: vscode.Event<
Dependency | DependencyFile | undefined | null | void
> = this._onDidChangeTreeData.event;
2022-02-25 19:37:09 -06:00
getDependency(filePath: string): Dependency | DependencyFile | undefined {
return this.dependenciesMap[filePath.toLowerCase()];
}
contains(filePath: string): boolean {
return filePath.toLowerCase() in this.dependenciesMap;
}
refresh(): void {
this._onDidChangeTreeData.fire();
}
2022-07-17 10:38:56 -05:00
getParent?(
element: Dependency | DependencyFile
): vscode.ProviderResult<Dependency | DependencyFile> {
2022-02-25 19:37:09 -06:00
if (element instanceof Dependency) return undefined;
return element.parent;
}
getTreeItem(element: Dependency | DependencyFile): vscode.TreeItem | Thenable<vscode.TreeItem> {
if (element.id! in this.dependenciesMap) return this.dependenciesMap[element.id!];
return element;
}
2022-07-17 10:38:56 -05:00
getChildren(
element?: Dependency | DependencyFile
): vscode.ProviderResult<Dependency[] | DependencyFile[]> {
2022-02-25 19:37:09 -06:00
return new Promise((resolve, _reject) => {
if (!this.workspaceRoot) {
2022-07-17 10:38:56 -05:00
void vscode.window.showInformationMessage("No dependency in empty workspace");
2022-02-25 19:37:09 -06:00
return Promise.resolve([]);
}
if (element) {
2022-07-17 10:38:56 -05:00
const files = fs.readdirSync(element.dependencyPath).map((fileName) => {
2022-02-25 19:37:09 -06:00
const filePath = fspath.join(element.dependencyPath, fileName);
2022-07-17 10:38:56 -05:00
const collapsibleState = fs.lstatSync(filePath).isDirectory()
? vscode.TreeItemCollapsibleState.Collapsed
: vscode.TreeItemCollapsibleState.None;
const dep = new DependencyFile(fileName, filePath, element, collapsibleState);
2022-02-25 19:37:09 -06:00
this.dependenciesMap[dep.dependencyPath.toLowerCase()] = dep;
return dep;
});
2022-07-17 10:38:56 -05:00
return resolve(files);
2022-02-25 19:37:09 -06:00
} else {
return resolve(this.getRootDependencies());
}
2022-02-25 18:37:55 -06:00
});
2022-02-25 19:37:09 -06:00
}
private async getRootDependencies(): Promise<Dependency[]> {
2022-07-17 10:38:56 -05:00
const registryDir = fspath.join(os.homedir(), ".cargo", "registry", "src");
2022-02-25 19:37:09 -06:00
const basePath = fspath.join(registryDir, fs.readdirSync(registryDir)[0]);
const deps = await this.getDepsInCartoTree(basePath);
const stdlib = await this.getStdLib();
this.dependenciesMap[stdlib.dependencyPath.toLowerCase()] = stdlib;
return [stdlib].concat(deps);
}
private async getStdLib(): Promise<Dependency> {
const toolchain = await activeToolchain();
const rustVersion = await getRustcVersion(os.homedir());
2022-07-17 10:38:56 -05:00
const stdlibPath = fspath.join(
os.homedir(),
".rustup",
"toolchains",
toolchain,
"lib",
"rustlib",
"src",
"rust",
"library"
);
2022-02-25 19:37:09 -06:00
const stdlib = new Dependency(
"stdlib",
rustVersion,
stdlibPath,
vscode.TreeItemCollapsibleState.Collapsed
2022-02-25 18:37:55 -06:00
);
2022-02-25 19:37:09 -06:00
return stdlib;
}
private async getDepsInCartoTree(basePath: string): Promise<Dependency[]> {
const crates: Crate[] = await this.cargo.crates();
const toDep = (moduleName: string, version: string): Dependency => {
const cratePath = fspath.join(basePath, `${moduleName}-${version}`);
return new Dependency(
moduleName,
version,
cratePath,
vscode.TreeItemCollapsibleState.Collapsed
);
};
2022-07-17 10:38:56 -05:00
const deps = crates.map((crate) => {
2022-02-25 19:37:09 -06:00
const dep = toDep(crate.name, crate.version);
this.dependenciesMap[dep.dependencyPath.toLowerCase()] = dep;
return dep;
});
return deps;
}
2022-02-25 18:37:55 -06:00
}
export class Dependency extends vscode.TreeItem {
2022-02-25 19:37:09 -06:00
constructor(
public readonly label: string,
private version: string,
readonly dependencyPath: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState
) {
super(label, collapsibleState);
this.tooltip = `${this.label}-${this.version}`;
this.description = this.version;
this.resourceUri = vscode.Uri.file(dependencyPath);
}
2022-02-25 18:37:55 -06:00
}
export class DependencyFile extends vscode.TreeItem {
2022-02-25 19:37:09 -06:00
constructor(
readonly label: string,
readonly dependencyPath: string,
readonly parent: Dependency | DependencyFile,
public readonly collapsibleState: vscode.TreeItemCollapsibleState
) {
super(vscode.Uri.file(dependencyPath), collapsibleState);
const isDir = fs.lstatSync(this.dependencyPath).isDirectory();
this.id = this.dependencyPath.toLowerCase();
if (!isDir) {
2022-07-17 10:38:56 -05:00
this.command = {
command: "rust-analyzer.openFile",
title: "Open File",
arguments: [vscode.Uri.file(this.dependencyPath)],
};
2022-02-25 19:37:09 -06:00
}
2022-02-25 18:37:55 -06:00
}
}
2022-07-17 10:38:56 -05:00
export type DependencyId = { id: string };