diff --git a/src/components/UsagesTreeProvider.ts b/src/components/UsagesTree/CodeUsageNode.ts similarity index 62% rename from src/components/UsagesTreeProvider.ts rename to src/components/UsagesTree/CodeUsageNode.ts index 59bf269..33dffe1 100644 --- a/src/components/UsagesTreeProvider.ts +++ b/src/components/UsagesTree/CodeUsageNode.ts @@ -1,21 +1,14 @@ import * as vscode from 'vscode' import { - usages, JSONMatch, VariableReference, - getAllVariables, - getAllEnvironments, - getCombinedVariableDetails, CombinedVariableData, getOrganizationId, - getAllFeatures, -} from '../cli' +} from '../../cli' -import { showBusyMessage, hideBusyMessage } from './statusBarItem' -import { KEYS, StateManager } from '../StateManager' +import { KEYS, StateManager } from '../../StateManager' - -type VariableCodeReference = +export type VariableCodeReference = | (CombinedVariableData & { references?: VariableReference[] }) @@ -28,92 +21,6 @@ const collapsedMap = { header: vscode.TreeItemCollapsibleState.Expanded, } -export class UsagesTreeProvider - implements vscode.TreeDataProvider -{ - private _onDidChangeTreeData: vscode.EventEmitter< - CodeUsageNode | undefined | void - > = new vscode.EventEmitter() - readonly onDidChangeTreeData: vscode.Event = - this._onDidChangeTreeData.event - private flagsSeen: CodeUsageNode[] = [] - private isRefreshing = false - - constructor( - private workspaceRoot: string | undefined, - private context: vscode.ExtensionContext, - ) {} - - private async getCombinedAPIData() { - showBusyMessage('Fetching DevCycle data') - const [variables] = await Promise.all([getAllVariables(), getAllFeatures(), getAllEnvironments()]) - const result = {} as Record - await Promise.all( - Object.entries(variables).map(async ([key, variable]) => { - const data = await getCombinedVariableDetails(variable) - result[key] = data - }), - ) - hideBusyMessage() - return result - } - - async refresh(): Promise { - if (this.isRefreshing) { - return - } - this.isRefreshing = true - this.flagsSeen = [] - this._onDidChangeTreeData.fire(undefined) - const root = this.workspaceRoot - if (!root) { - throw new Error('Must have a workspace to check for code usages') - } - - // Use withProgress to show a progress indicator - await vscode.window.withProgress( - { - location: { viewId: 'devcycleCodeUsages' }, - }, - async () => { - const variables = await this.getCombinedAPIData() - const matches = await usages() - matches.forEach((usage) => { - if (variables[usage.key]) { - variables[usage.key].references = usage.references - } else { - variables[usage.key] = usage - } - }) - await getOrganizationId() // load organization id into state first, otherwise each of the parallel requests will fetch it - await Promise.all(Object.values(variables).map(async (match) => { - this.flagsSeen.push(await CodeUsageNode.flagFrom(match, root, this.context)) - return - })) - this.flagsSeen.sort((a, b) => (a.key > b.key ? 1 : -1)) - this._onDidChangeTreeData.fire() - }) - this.isRefreshing = false - } - - getTreeItem(element: CodeUsageNode): vscode.TreeItem { - return element - } - - async getChildren(element?: CodeUsageNode): Promise { - if (!this.workspaceRoot) { - vscode.window.showInformationMessage('No dependency in empty workspace') - return [] - } - - if (element) { - return element.children - } - - return this.flagsSeen - } -} - export class CodeUsageNode extends vscode.TreeItem { static async flagFrom( match: VariableCodeReference, diff --git a/src/components/UsagesTree/UsagesTreeProvider.ts b/src/components/UsagesTree/UsagesTreeProvider.ts new file mode 100644 index 0000000..9e3ede2 --- /dev/null +++ b/src/components/UsagesTree/UsagesTreeProvider.ts @@ -0,0 +1,98 @@ +import * as vscode from 'vscode' +import { + usages, + getAllVariables, + getAllEnvironments, + getCombinedVariableDetails, + getOrganizationId, + getAllFeatures, +} from '../../cli' + +import { showBusyMessage, hideBusyMessage } from '../statusBarItem' +import { CodeUsageNode, VariableCodeReference } from './CodeUsageNode' + +export class UsagesTreeProvider + implements vscode.TreeDataProvider +{ + private _onDidChangeTreeData: vscode.EventEmitter< + CodeUsageNode | undefined | void + > = new vscode.EventEmitter() + readonly onDidChangeTreeData: vscode.Event = + this._onDidChangeTreeData.event + private flagsSeen: CodeUsageNode[] = [] + private isRefreshing = false + + constructor( + private workspaceRoot: string | undefined, + private context: vscode.ExtensionContext, + ) {} + + private async getCombinedAPIData() { + showBusyMessage('Fetching DevCycle data') + const [variables] = await Promise.all([getAllVariables(), getAllFeatures(), getAllEnvironments()]) + const result = {} as Record + await Promise.all( + Object.entries(variables).map(async ([key, variable]) => { + const data = await getCombinedVariableDetails(variable) + result[key] = data + }), + ) + hideBusyMessage() + return result + } + + async refresh(): Promise { + if (this.isRefreshing) { + return + } + this.isRefreshing = true + this.flagsSeen = [] + this._onDidChangeTreeData.fire(undefined) + const root = this.workspaceRoot + if (!root) { + throw new Error('Must have a workspace to check for code usages') + } + + // Use withProgress to show a progress indicator + await vscode.window.withProgress( + { + location: { viewId: 'devcycleCodeUsages' }, + }, + async () => { + const variables = await this.getCombinedAPIData() + const matches = await usages() + matches.forEach((usage) => { + if (variables[usage.key]) { + variables[usage.key].references = usage.references + } else { + variables[usage.key] = usage + } + }) + await getOrganizationId() // load organization id into state first, otherwise each of the parallel requests will fetch it + await Promise.all(Object.values(variables).map(async (match) => { + this.flagsSeen.push(await CodeUsageNode.flagFrom(match, root, this.context)) + return + })) + this.flagsSeen.sort((a, b) => (a.key > b.key ? 1 : -1)) + this._onDidChangeTreeData.fire() + }) + this.isRefreshing = false + } + + getTreeItem(element: CodeUsageNode): vscode.TreeItem { + return element + } + + async getChildren(element?: CodeUsageNode): Promise { + if (!this.workspaceRoot) { + vscode.window.showInformationMessage('No dependency in empty workspace') + return [] + } + + if (element) { + return element.children + } + + return this.flagsSeen + } +} diff --git a/src/components/UsagesTree/index.ts b/src/components/UsagesTree/index.ts new file mode 100644 index 0000000..fd70d94 --- /dev/null +++ b/src/components/UsagesTree/index.ts @@ -0,0 +1 @@ +export * from './UsagesTreeProvider' diff --git a/src/extension.ts b/src/extension.ts index e9c3a53..0a68a04 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,7 +6,7 @@ import { SecretStateManager } from './SecretStateManager' import { autoLoginIfHaveCredentials } from './utils/credentials' import { SidebarProvider } from './components/SidebarProvider' -import { UsagesTreeProvider } from './components/UsagesTreeProvider' +import { UsagesTreeProvider } from './components/UsagesTree' import { getHoverString } from './components/hoverCard' Object.defineProperty(exports, '__esModule', { value: true })