diff --git a/src/multiConfigs.ts b/src/multiConfigs.ts index 761cc09..347b5a8 100644 --- a/src/multiConfigs.ts +++ b/src/multiConfigs.ts @@ -1,9 +1,16 @@ import * as vscode from "vscode"; import * as path from "path"; import * as fs from "fs"; -import { userInfo } from "os"; import { SERIALIZED_FILE_EXTENSION } from "./codeMarker"; -import { ConfigurationEntry, WorkspaceRootEntry, ConfigTreeEntry, isConfigurationEntry, isWorkspaceRootEntry, configEntryEquals } from "./types"; +import { + ConfigurationEntry, + WorkspaceRootEntry, + ConfigTreeEntry, + isConfigurationEntry, + isWorkspaceRootEntry, + configEntryEquals, + RootPathAndLabel, +} from "./types"; let lightEyePath: vscode.Uri; let darkEyePath: vscode.Uri; @@ -11,9 +18,8 @@ let darkEyePath: vscode.Uri; export class MultipleSavedFindingsTree implements vscode.TreeDataProvider { private configurationEntries: ConfigurationEntry[]; private rootEntries: WorkspaceRootEntry[]; - private rootPathsAndLabels: [string, string][]; + private rootPathsAndLabels: RootPathAndLabel[]; private activeConfigs: ConfigurationEntry[]; - private username: string; private _onDidChangeTreeDataEmitter = new vscode.EventEmitter(); readonly onDidChangeTreeData = this._onDidChangeTreeDataEmitter.event; @@ -29,10 +35,8 @@ export class MultipleSavedFindingsTree implements vscode.TreeDataProvider { + vscode.commands.registerCommand("weAudit.setMultiConfigRoots", (rootPathsAndLabels: RootPathAndLabel[]) => { this.rootPathsAndLabels = rootPathsAndLabels; }); @@ -43,7 +47,8 @@ export class MultipleSavedFindingsTree implements vscode.TreeDataProvider { + vscode.commands.registerCommand("weAudit.findAndLoadConfigurationFiles", async () => { + await vscode.commands.executeCommand("weAudit.getMultiConfigRoots"); this.findAndLoadConfigurationFiles(); this.refresh(); }); @@ -52,13 +57,13 @@ export class MultipleSavedFindingsTree implements vscode.TreeDataProvider { diff --git a/src/panels/gitConfigPanel.ts b/src/panels/gitConfigPanel.ts index b259e1d..0928bc4 100644 --- a/src/panels/gitConfigPanel.ts +++ b/src/panels/gitConfigPanel.ts @@ -3,6 +3,7 @@ import * as crypto from "crypto"; import { getUri } from "../utilities/getUri"; import { WebviewMessage, UpdateRepositoryMessage, SetWorkspaceRootsMessage } from "../webview/webviewMessageTypes"; +import { RootPathAndLabel } from "../types"; export function activateGitConfigWebview(context: vscode.ExtensionContext) { const provider = new GitConfigProvider(context.extensionUri); @@ -13,22 +14,22 @@ export function activateGitConfigWebview(context: vscode.ExtensionContext) { class GitConfigProvider implements vscode.WebviewViewProvider { public static readonly viewType = "gitConfig"; private _disposables: vscode.Disposable[] = []; - private currentRootPathAndLabel: [string, string]; + private currentRootPathAndLabel: RootPathAndLabel; private dirToPathMap: Map; private _view?: vscode.WebviewView; constructor(private readonly _extensionUri: vscode.Uri) { - this.currentRootPathAndLabel = ["", ""]; + this.currentRootPathAndLabel = { rootPath: "", rootLabel: "" } as RootPathAndLabel; this.dirToPathMap = new Map(); vscode.commands.registerCommand( "weAudit.setGitConfigView", - (rootPathAndLabel: [string, string], clientRepo: string, auditRepo: string, commitHash: string) => { + (rootPathAndLabel: RootPathAndLabel, clientRepo: string, auditRepo: string, commitHash: string) => { this.currentRootPathAndLabel = rootPathAndLabel; const msg: UpdateRepositoryMessage = { command: "update-repository-config", - rootLabel: rootPathAndLabel[1], + rootLabel: rootPathAndLabel.rootLabel, clientURL: clientRepo, auditURL: auditRepo, commitHash, @@ -37,36 +38,42 @@ class GitConfigProvider implements vscode.WebviewViewProvider { }, ); - vscode.commands.registerCommand("weAudit.setGitConfigRoots", (rootPathsAndLabels: [string, string][]) => { - if (!rootPathsAndLabels.includes(this.currentRootPathAndLabel)) { + vscode.commands.registerCommand("weAudit.setGitConfigRoots", (rootPathsAndLabels: RootPathAndLabel[]) => { + const idx = rootPathsAndLabels.findIndex((rootPathAndLabel) => { + return ( + rootPathAndLabel.rootPath === this.currentRootPathAndLabel.rootPath && rootPathAndLabel.rootLabel === this.currentRootPathAndLabel.rootLabel + ); + }); + + if (idx === -1) { if (rootPathsAndLabels.length > 0) { this.currentRootPathAndLabel = rootPathsAndLabels[0]; } } this.dirToPathMap.clear(); - for (const [rootPath, rootLabel] of rootPathsAndLabels) { - this.dirToPathMap.set(rootLabel, rootPath); + for (const rootPathAndLabel of rootPathsAndLabels) { + this.dirToPathMap.set(rootPathAndLabel.rootLabel, rootPathAndLabel.rootPath); } const msg: SetWorkspaceRootsMessage = { command: "set-workspace-roots", - rootLabels: rootPathsAndLabels.map((rootPathAndLabel) => rootPathAndLabel[1]), + rootLabels: rootPathsAndLabels.map((rootPathAndLabel) => rootPathAndLabel.rootLabel), }; this._view?.webview.postMessage(msg); }); vscode.commands.registerCommand("weAudit.nextGitConfig", async () => { - const nextRootPath = await vscode.commands.executeCommand("weAudit.nextRoot", this.currentRootPathAndLabel[0], true); + const nextRootPath = await vscode.commands.executeCommand("weAudit.nextRoot", this.currentRootPathAndLabel.rootPath, true); vscode.commands.executeCommand("weAudit.pushGitConfigView", nextRootPath); }); vscode.commands.registerCommand("weAudit.prevGitConfig", async () => { - const prevRootPath = await vscode.commands.executeCommand("weAudit.nextRoot", this.currentRootPathAndLabel[0], false); + const prevRootPath = await vscode.commands.executeCommand("weAudit.nextRoot", this.currentRootPathAndLabel.rootPath, false); vscode.commands.executeCommand("weAudit.pushGitConfigView", prevRootPath); }); vscode.commands.registerCommand("weAudit.setupRepositoriesCurrent", () => { - vscode.commands.executeCommand("weAudit.setupRepositoriesOne", this.currentRootPathAndLabel[0]); + vscode.commands.executeCommand("weAudit.setupRepositoriesOne", this.currentRootPathAndLabel.rootPath); }); } @@ -87,7 +94,7 @@ class GitConfigProvider implements vscode.WebviewViewProvider { webviewView.onDidChangeVisibility(() => { if (webviewView.visible) { vscode.commands.executeCommand("weAudit.getGitConfigRoots"); - vscode.commands.executeCommand("weAudit.pushGitConfigView", this.currentRootPathAndLabel[0]); + vscode.commands.executeCommand("weAudit.pushGitConfigView", this.currentRootPathAndLabel.rootPath); } }); } @@ -140,16 +147,19 @@ class GitConfigProvider implements vscode.WebviewViewProvider { vscode.commands.executeCommand("weAudit.updateGitConfig", rootPath, message.clientURL, message.auditURL, message.commitHash); return; case "choose-workspace-root": - rootPath = this.dirToPathMap.get(message.rootDir); + rootPath = this.dirToPathMap.get(message.rootLabel); if (rootPath === undefined) { - vscode.window.showErrorMessage(`weAudit: Error choosing workspace root. Directory: ${message.rootDir} is not a workspace root.`); + vscode.window.showErrorMessage(`weAudit: Error choosing workspace root. Directory: ${message.rootLabel} is not a workspace root.`); return; } vscode.commands.executeCommand("weAudit.pushGitConfigView", rootPath); return; case "webview-ready": vscode.commands.executeCommand("weAudit.getGitConfigRoots"); - vscode.commands.executeCommand("weAudit.pushGitConfigView", this.currentRootPathAndLabel[0] ? this.currentRootPathAndLabel[0] : null); + vscode.commands.executeCommand( + "weAudit.pushGitConfigView", + this.currentRootPathAndLabel.rootPath ? this.currentRootPathAndLabel.rootPath : null, + ); return; } }, diff --git a/src/types.ts b/src/types.ts index 7521de3..2a369ae 100644 --- a/src/types.ts +++ b/src/types.ts @@ -313,6 +313,13 @@ export interface FullPath { path: string; } +/** + * A tuple containing a root path and a unique label for that root path + */ +export interface RootPathAndLabel { + rootPath: string; + rootLabel: string; +} /** * Creates a PathOrganizer entry. * @param path the path of the file diff --git a/src/webview/gitConfigMain.ts b/src/webview/gitConfigMain.ts index 2028152..66f69ce 100644 --- a/src/webview/gitConfigMain.ts +++ b/src/webview/gitConfigMain.ts @@ -83,7 +83,7 @@ function handleDropdownChange(_e: Event): void { const message: ChooseWorkspaceRootMessage = { command: "choose-workspace-root", - rootDir: rootDropdown.value, + rootLabel: rootDropdown.value, }; vscode.postMessage(message); } diff --git a/src/webview/webviewMessageTypes.ts b/src/webview/webviewMessageTypes.ts index 2ac8a74..5db78e4 100644 --- a/src/webview/webviewMessageTypes.ts +++ b/src/webview/webviewMessageTypes.ts @@ -17,7 +17,7 @@ export interface UpdateRepositoryMessage { export interface ChooseWorkspaceRootMessage { command: "choose-workspace-root"; - rootDir: string; + rootLabel: string; } export interface SetWorkspaceRootsMessage {