prettier run

This commit is contained in:
Andrei Listochkin 2022-05-17 18:15:06 +01:00
parent 8e9f54f238
commit f247090558
24 changed files with 1169 additions and 808 deletions

View file

@ -1,14 +1,19 @@
import * as os from "os";
import * as vscode from 'vscode';
import * as path from 'path';
import * as ra from './lsp_ext';
import * as vscode from "vscode";
import * as path from "path";
import * as ra from "./lsp_ext";
import { Cargo, getRustcId, getSysroot } from './toolchain';
import { Cargo, getRustcId, getSysroot } from "./toolchain";
import { Ctx } from "./ctx";
import { prepareEnv } from "./run";
const debugOutput = vscode.window.createOutputChannel("Debug");
type DebugConfigProvider = (config: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>) => vscode.DebugConfiguration;
type DebugConfigProvider = (
config: ra.Runnable,
executable: string,
env: Record<string, string>,
sourceFileMap?: Record<string, string>
) => vscode.DebugConfiguration;
export async function makeDebugConfig(ctx: Ctx, runnable: ra.Runnable): Promise<void> {
const scope = ctx.activeRustEditor?.document.uri;
@ -20,9 +25,13 @@ export async function makeDebugConfig(ctx: Ctx, runnable: ra.Runnable): Promise<
const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope);
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
const index = configurations.findIndex(c => c.name === debugConfig.name);
const index = configurations.findIndex((c) => c.name === debugConfig.name);
if (index !== -1) {
const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update');
const answer = await vscode.window.showErrorMessage(
`Launch configuration '${debugConfig.name}' already exists!`,
"Cancel",
"Update"
);
if (answer === "Cancel") return;
configurations[index] = debugConfig;
@ -40,7 +49,7 @@ export async function startDebugSession(ctx: Ctx, runnable: ra.Runnable): Promis
const wsLaunchSection = vscode.workspace.getConfiguration("launch");
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
const index = configurations.findIndex(c => c.name === runnable.label);
const index = configurations.findIndex((c) => c.name === runnable.label);
if (-1 !== index) {
debugConfig = configurations[index];
message = " (from launch.json)";
@ -56,13 +65,16 @@ export async function startDebugSession(ctx: Ctx, runnable: ra.Runnable): Promis
return vscode.debug.startDebugging(undefined, debugConfig);
}
async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<vscode.DebugConfiguration | undefined> {
async function getDebugConfiguration(
ctx: Ctx,
runnable: ra.Runnable
): Promise<vscode.DebugConfiguration | undefined> {
const editor = ctx.activeRustEditor;
if (!editor) return;
const knownEngines: Record<string, DebugConfigProvider> = {
"vadimcn.vscode-lldb": getLldbDebugConfig,
"ms-vscode.cpptools": getCppvsDebugConfig
"ms-vscode.cpptools": getCppvsDebugConfig,
};
const debugOptions = ctx.config.debug;
@ -77,8 +89,10 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<v
}
if (!debugEngine) {
await vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)`
+ ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) extension for debugging.`);
await vscode.window.showErrorMessage(
`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)` +
` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) extension for debugging.`
);
return;
}
@ -91,15 +105,17 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<v
const workspaceFolders = vscode.workspace.workspaceFolders!;
const isMultiFolderWorkspace = workspaceFolders.length > 1;
const firstWorkspace = workspaceFolders[0];
const workspace = !isMultiFolderWorkspace || !runnable.args.workspaceRoot ?
firstWorkspace :
workspaceFolders.find(w => runnable.args.workspaceRoot?.includes(w.uri.fsPath)) || firstWorkspace;
const workspace =
!isMultiFolderWorkspace || !runnable.args.workspaceRoot
? firstWorkspace
: workspaceFolders.find((w) => runnable.args.workspaceRoot?.includes(w.uri.fsPath)) ||
firstWorkspace;
const wsFolder = path.normalize(workspace.uri.fsPath);
const workspaceQualifier = isMultiFolderWorkspace ? `:${workspace.name}` : '';
const workspaceQualifier = isMultiFolderWorkspace ? `:${workspace.name}` : "";
function simplifyPath(p: string): string {
// see https://github.com/rust-analyzer/rust-analyzer/pull/5513#issuecomment-663458818 for why this is needed
return path.normalize(p).replace(wsFolder, '${workspaceFolder' + workspaceQualifier + '}');
return path.normalize(p).replace(wsFolder, "${workspaceFolder" + workspaceQualifier + "}");
}
const executable = await getDebugExecutable(runnable);
@ -114,7 +130,12 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<v
sourceFileMap[`/rustc/${commitHash}/`] = rustlib;
}
const debugConfig = knownEngines[debugEngine.id](runnable, simplifyPath(executable), env, sourceFileMap);
const debugConfig = knownEngines[debugEngine.id](
runnable,
simplifyPath(executable),
env,
sourceFileMap
);
if (debugConfig.type in debugOptions.engineSettings) {
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
for (var key in settingsMap) {
@ -136,14 +157,19 @@ async function getDebugConfiguration(ctx: Ctx, runnable: ra.Runnable): Promise<v
}
async function getDebugExecutable(runnable: ra.Runnable): Promise<string> {
const cargo = new Cargo(runnable.args.workspaceRoot || '.', debugOutput);
const cargo = new Cargo(runnable.args.workspaceRoot || ".", debugOutput);
const executable = await cargo.executableFromArgs(runnable.args.cargoArgs);
// if we are here, there were no compilation errors.
return executable;
}
function getLldbDebugConfig(runnable: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
function getLldbDebugConfig(
runnable: ra.Runnable,
executable: string,
env: Record<string, string>,
sourceFileMap?: Record<string, string>
): vscode.DebugConfiguration {
return {
type: "lldb",
request: "launch",
@ -153,13 +179,18 @@ function getLldbDebugConfig(runnable: ra.Runnable, executable: string, env: Reco
cwd: runnable.args.workspaceRoot,
sourceMap: sourceFileMap,
sourceLanguages: ["rust"],
env
env,
};
}
function getCppvsDebugConfig(runnable: ra.Runnable, executable: string, env: Record<string, string>, sourceFileMap?: Record<string, string>): vscode.DebugConfiguration {
function getCppvsDebugConfig(
runnable: ra.Runnable,
executable: string,
env: Record<string, string>,
sourceFileMap?: Record<string, string>
): vscode.DebugConfiguration {
return {
type: (os.platform() === "win32") ? "cppvsdbg" : "cppdbg",
type: os.platform() === "win32" ? "cppvsdbg" : "cppdbg",
request: "launch",
name: runnable.label,
program: executable,