Skip to content

Commit c6d3337

Browse files
committed
Reformat VSCode client code
1 parent 1a36f97 commit c6d3337

File tree

5 files changed

+94
-77
lines changed

5 files changed

+94
-77
lines changed

editors/code/src/commands.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { applySnippetWorkspaceEdit, applySnippetTextEdits } from "./snippets";
88
import { spawnSync } from "child_process";
99
import { RunnableQuickPick, selectRunnable, createTask, createArgs } from "./run";
1010
import { AstInspector } from "./ast_inspector";
11-
import { isRustDocument, isCargoTomlDocument, sleep, isRustEditor, RustEditor } from './util';
11+
import { isRustDocument, isCargoTomlDocument, sleep, isRustEditor, RustEditor } from "./util";
1212
import { startDebugSession, makeDebugConfig } from "./debug";
1313
import { LanguageClient } from "vscode-languageclient/node";
1414
import { LINKED_COMMANDS } from "./client";
15-
import { DependencyId } from './dependencies_provider';
15+
import { DependencyId } from "./dependencies_provider";
1616

1717
export * from "./ast_inspector";
1818
export * from "./run";
@@ -281,8 +281,7 @@ export function revealDependency(ctx: Ctx): Cmd {
281281
do {
282282
documentPath = path.dirname(documentPath);
283283
parentChain.push({ id: documentPath.toLowerCase() });
284-
}
285-
while (!ctx.dependencies.contains(documentPath));
284+
} while (!ctx.dependencies.contains(documentPath));
286285
parentChain.reverse();
287286
for (const idx in parentChain) {
288287
await ctx.treeView.reveal(parentChain[idx], { select: true, expand: true });
@@ -292,7 +291,7 @@ export function revealDependency(ctx: Ctx): Cmd {
292291
}
293292

294293
export async function execRevealDependency(e: RustEditor): Promise<void> {
295-
await vscode.commands.executeCommand('rust-analyzer.revealDependency', e);
294+
await vscode.commands.executeCommand("rust-analyzer.revealDependency", e);
296295
}
297296

298297
export function ssr(ctx: Ctx): Cmd {

editors/code/src/ctx.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ import * as vscode from "vscode";
22
import * as lc from "vscode-languageclient/node";
33
import * as ra from "./lsp_ext";
44

5-
import { Config } from './config';
6-
import { createClient } from './client';
7-
import { isRustEditor, RustEditor } from './util';
8-
import { ServerStatusParams } from './lsp_ext';
9-
import { Dependency, DependencyFile, RustDependenciesProvider, DependencyId } from './dependencies_provider';
10-
import { execRevealDependency } from './commands';
5+
import { Config } from "./config";
6+
import { createClient } from "./client";
7+
import { isRustEditor, RustEditor } from "./util";
8+
import { ServerStatusParams } from "./lsp_ext";
9+
import {
10+
Dependency,
11+
DependencyFile,
12+
RustDependenciesProvider,
13+
DependencyId,
14+
} from "./dependencies_provider";
15+
import { execRevealDependency } from "./commands";
1116

1217
export type Workspace =
1318
| {
@@ -27,7 +32,7 @@ export class Ctx {
2732
readonly statusBar: vscode.StatusBarItem,
2833
readonly dependencies: RustDependenciesProvider,
2934
readonly treeView: vscode.TreeView<Dependency | DependencyFile | DependencyId>
30-
) { }
35+
) {}
3136

3237
static async create(
3338
config: Config,
@@ -47,17 +52,25 @@ export class Ctx {
4752
const rootPath = vscode.workspace.workspaceFolders![0].uri.fsPath;
4853

4954
const dependenciesProvider = new RustDependenciesProvider(rootPath);
50-
const treeView = vscode.window.createTreeView('rustDependencies', {
55+
const treeView = vscode.window.createTreeView("rustDependencies", {
5156
treeDataProvider: dependenciesProvider,
52-
showCollapseAll: true
57+
showCollapseAll: true,
5358
});
5459

55-
const res = new Ctx(config, extCtx, client, serverPath, statusBar, dependenciesProvider, treeView);
60+
const res = new Ctx(
61+
config,
62+
extCtx,
63+
client,
64+
serverPath,
65+
statusBar,
66+
dependenciesProvider,
67+
treeView
68+
);
5669
res.pushCleanup(treeView);
5770

58-
vscode.window.onDidChangeActiveTextEditor(e => {
71+
vscode.window.onDidChangeActiveTextEditor((e) => {
5972
if (e && isRustEditor(e)) {
60-
execRevealDependency(e).catch(reason => {
73+
execRevealDependency(e).catch((reason) => {
6174
void vscode.window.showErrorMessage(`Dependency error: ${reason}`);
6275
});
6376
}

editors/code/src/dependencies_provider.ts

+48-36
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
import * as vscode from 'vscode';
2-
import * as fspath from 'path';
3-
import * as fs from 'fs';
4-
import * as os from 'os';
5-
import { activeToolchain, Cargo, Crate, getRustcVersion } from './toolchain';
1+
import * as vscode from "vscode";
2+
import * as fspath from "path";
3+
import * as fs from "fs";
4+
import * as os from "os";
5+
import { activeToolchain, Cargo, Crate, getRustcVersion } from "./toolchain";
66

77
const debugOutput = vscode.window.createOutputChannel("Debug");
88

9-
export class RustDependenciesProvider implements vscode.TreeDataProvider<Dependency | DependencyFile>{
9+
export class RustDependenciesProvider
10+
implements vscode.TreeDataProvider<Dependency | DependencyFile>
11+
{
1012
cargo: Cargo;
1113
dependenciesMap: { [id: string]: Dependency | DependencyFile };
1214

13-
constructor(
14-
private readonly workspaceRoot: string,
15-
) {
16-
this.cargo = new Cargo(this.workspaceRoot || '.', debugOutput);
15+
constructor(private readonly workspaceRoot: string) {
16+
this.cargo = new Cargo(this.workspaceRoot || ".", debugOutput);
1717
this.dependenciesMap = {};
1818
}
1919

20-
private _onDidChangeTreeData: vscode.EventEmitter<Dependency | DependencyFile | undefined | null | void> = new vscode.EventEmitter<Dependency | undefined | null | void>();
21-
22-
readonly onDidChangeTreeData: vscode.Event<Dependency | DependencyFile | undefined | null | void> = this._onDidChangeTreeData.event;
20+
private _onDidChangeTreeData: vscode.EventEmitter<
21+
Dependency | DependencyFile | undefined | null | void
22+
> = new vscode.EventEmitter<Dependency | undefined | null | void>();
2323

24+
readonly onDidChangeTreeData: vscode.Event<
25+
Dependency | DependencyFile | undefined | null | void
26+
> = this._onDidChangeTreeData.event;
2427

2528
getDependency(filePath: string): Dependency | DependencyFile | undefined {
2629
return this.dependenciesMap[filePath.toLowerCase()];
@@ -34,7 +37,9 @@ export class RustDependenciesProvider implements vscode.TreeDataProvider<Depende
3437
this._onDidChangeTreeData.fire();
3538
}
3639

37-
getParent?(element: Dependency | DependencyFile): vscode.ProviderResult<Dependency | DependencyFile> {
40+
getParent?(
41+
element: Dependency | DependencyFile
42+
): vscode.ProviderResult<Dependency | DependencyFile> {
3843
if (element instanceof Dependency) return undefined;
3944
return element.parent;
4045
}
@@ -44,39 +49,34 @@ export class RustDependenciesProvider implements vscode.TreeDataProvider<Depende
4449
return element;
4550
}
4651

47-
getChildren(element?: Dependency | DependencyFile): vscode.ProviderResult<Dependency[] | DependencyFile[]> {
52+
getChildren(
53+
element?: Dependency | DependencyFile
54+
): vscode.ProviderResult<Dependency[] | DependencyFile[]> {
4855
return new Promise((resolve, _reject) => {
4956
if (!this.workspaceRoot) {
50-
void vscode.window.showInformationMessage('No dependency in empty workspace');
57+
void vscode.window.showInformationMessage("No dependency in empty workspace");
5158
return Promise.resolve([]);
5259
}
5360

5461
if (element) {
55-
const files = fs.readdirSync(element.dependencyPath).map(fileName => {
62+
const files = fs.readdirSync(element.dependencyPath).map((fileName) => {
5663
const filePath = fspath.join(element.dependencyPath, fileName);
57-
const collapsibleState = fs.lstatSync(filePath).isDirectory() ?
58-
vscode.TreeItemCollapsibleState.Collapsed :
59-
vscode.TreeItemCollapsibleState.None;
60-
const dep = new DependencyFile(
61-
fileName,
62-
filePath,
63-
element,
64-
collapsibleState
65-
);
64+
const collapsibleState = fs.lstatSync(filePath).isDirectory()
65+
? vscode.TreeItemCollapsibleState.Collapsed
66+
: vscode.TreeItemCollapsibleState.None;
67+
const dep = new DependencyFile(fileName, filePath, element, collapsibleState);
6668
this.dependenciesMap[dep.dependencyPath.toLowerCase()] = dep;
6769
return dep;
6870
});
69-
return resolve(
70-
files
71-
);
71+
return resolve(files);
7272
} else {
7373
return resolve(this.getRootDependencies());
7474
}
7575
});
7676
}
7777

7878
private async getRootDependencies(): Promise<Dependency[]> {
79-
const registryDir = fspath.join(os.homedir(), '.cargo', 'registry', 'src');
79+
const registryDir = fspath.join(os.homedir(), ".cargo", "registry", "src");
8080
const basePath = fspath.join(registryDir, fs.readdirSync(registryDir)[0]);
8181
const deps = await this.getDepsInCartoTree(basePath);
8282
const stdlib = await this.getStdLib();
@@ -87,7 +87,17 @@ export class RustDependenciesProvider implements vscode.TreeDataProvider<Depende
8787
private async getStdLib(): Promise<Dependency> {
8888
const toolchain = await activeToolchain();
8989
const rustVersion = await getRustcVersion(os.homedir());
90-
const stdlibPath = fspath.join(os.homedir(), '.rustup', 'toolchains', toolchain, 'lib', 'rustlib', 'src', 'rust', 'library');
90+
const stdlibPath = fspath.join(
91+
os.homedir(),
92+
".rustup",
93+
"toolchains",
94+
toolchain,
95+
"lib",
96+
"rustlib",
97+
"src",
98+
"rust",
99+
"library"
100+
);
91101
const stdlib = new Dependency(
92102
"stdlib",
93103
rustVersion,
@@ -110,7 +120,7 @@ export class RustDependenciesProvider implements vscode.TreeDataProvider<Depende
110120
);
111121
};
112122

113-
const deps = crates.map(crate => {
123+
const deps = crates.map((crate) => {
114124
const dep = toDep(crate.name, crate.version);
115125
this.dependenciesMap[dep.dependencyPath.toLowerCase()] = dep;
116126
return dep;
@@ -119,7 +129,6 @@ export class RustDependenciesProvider implements vscode.TreeDataProvider<Depende
119129
}
120130
}
121131

122-
123132
export class Dependency extends vscode.TreeItem {
124133
constructor(
125134
public readonly label: string,
@@ -135,7 +144,6 @@ export class Dependency extends vscode.TreeItem {
135144
}
136145

137146
export class DependencyFile extends vscode.TreeItem {
138-
139147
constructor(
140148
readonly label: string,
141149
readonly dependencyPath: string,
@@ -146,9 +154,13 @@ export class DependencyFile extends vscode.TreeItem {
146154
const isDir = fs.lstatSync(this.dependencyPath).isDirectory();
147155
this.id = this.dependencyPath.toLowerCase();
148156
if (!isDir) {
149-
this.command = { command: 'rust-analyzer.openFile', title: "Open File", arguments: [vscode.Uri.file(this.dependencyPath)], };
157+
this.command = {
158+
command: "rust-analyzer.openFile",
159+
title: "Open File",
160+
arguments: [vscode.Uri.file(this.dependencyPath)],
161+
};
150162
}
151163
}
152164
}
153165

154-
export type DependencyId = { id: string };
166+
export type DependencyId = { id: string };

editors/code/src/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ async function initCommonContext(context: vscode.ExtensionContext, ctx: Ctx) {
161161
ctx.registerCommand("peekTests", commands.peekTests);
162162
ctx.registerCommand("moveItemUp", commands.moveItemUp);
163163
ctx.registerCommand("moveItemDown", commands.moveItemDown);
164-
ctx.registerCommand('openFile', commands.openFile);
165-
ctx.registerCommand('revealDependency', commands.revealDependency);
164+
ctx.registerCommand("openFile", commands.openFile);
165+
ctx.registerCommand("revealDependency", commands.revealDependency);
166166

167167
defaultOnEnter.dispose();
168168
ctx.registerCommand("onEnter", commands.onEnter);

editors/code/src/toolchain.ts

+15-22
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as readline from "readline";
55
import * as vscode from "vscode";
66
import { execute, log, memoizeAsync } from "./util";
77

8-
98
const TREE_LINE_PATTERN = new RegExp(/(.+)\sv(\d+\.\d+\.\d+)(?:\s\((.+)\))?/);
109
const TOOLCHAIN_PATTERN = new RegExp(/(.*)\s\(.*\)/);
1110

@@ -106,12 +105,12 @@ export class Cargo {
106105
return await new Promise((resolve, reject) => {
107106
const crates: Crate[] = [];
108107

109-
const cargo = cp.spawn(pathToCargo, ['tree', '--prefix', 'none'], {
110-
stdio: ['ignore', 'pipe', 'pipe'],
111-
cwd: this.rootFolder
108+
const cargo = cp.spawn(pathToCargo, ["tree", "--prefix", "none"], {
109+
stdio: ["ignore", "pipe", "pipe"],
110+
cwd: this.rootFolder,
112111
});
113112
const rl = readline.createInterface({ input: cargo.stdout });
114-
rl.on('line', line => {
113+
rl.on("line", (line) => {
115114
const match = line.match(TREE_LINE_PATTERN);
116115
if (match) {
117116
const name = match[1];
@@ -124,18 +123,15 @@ export class Cargo {
124123
crates.push({ name, version });
125124
}
126125
});
127-
cargo.on('exit', (exitCode, _) => {
128-
if (exitCode === 0)
129-
resolve(crates);
130-
else
131-
reject(new Error(`exit code: ${exitCode}.`));
126+
cargo.on("exit", (exitCode, _) => {
127+
if (exitCode === 0) resolve(crates);
128+
else reject(new Error(`exit code: ${exitCode}.`));
132129
});
133-
134130
});
135131
}
136132

137133
private shouldIgnore(extraInfo: string): boolean {
138-
return extraInfo !== undefined && (extraInfo === '*' || path.isAbsolute(extraInfo));
134+
return extraInfo !== undefined && (extraInfo === "*" || path.isAbsolute(extraInfo));
139135
}
140136

141137
private async runCargo(
@@ -171,26 +167,23 @@ export class Cargo {
171167
export async function activeToolchain(): Promise<string> {
172168
const pathToRustup = await rustupPath();
173169
return await new Promise((resolve, reject) => {
174-
const execution = cp.spawn(pathToRustup, ['show', 'active-toolchain'], {
175-
stdio: ['ignore', 'pipe', 'pipe'],
176-
cwd: os.homedir()
170+
const execution = cp.spawn(pathToRustup, ["show", "active-toolchain"], {
171+
stdio: ["ignore", "pipe", "pipe"],
172+
cwd: os.homedir(),
177173
});
178174
const rl = readline.createInterface({ input: execution.stdout });
179175

180176
let currToolchain: string | undefined = undefined;
181-
rl.on('line', line => {
177+
rl.on("line", (line) => {
182178
const match = line.match(TOOLCHAIN_PATTERN);
183179
if (match) {
184180
currToolchain = match[1];
185181
}
186182
});
187-
execution.on('exit', (exitCode, _) => {
188-
if (exitCode === 0 && currToolchain)
189-
resolve(currToolchain);
190-
else
191-
reject(new Error(`exit code: ${exitCode}.`));
183+
execution.on("exit", (exitCode, _) => {
184+
if (exitCode === 0 && currToolchain) resolve(currToolchain);
185+
else reject(new Error(`exit code: ${exitCode}.`));
192186
});
193-
194187
});
195188
}
196189

0 commit comments

Comments
 (0)