Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move CodeUsageNode class into its own file #50

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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[]
})
Expand All @@ -28,92 +21,6 @@ const collapsedMap = {
header: vscode.TreeItemCollapsibleState.Expanded,
}

export class UsagesTreeProvider
implements vscode.TreeDataProvider<CodeUsageNode>
{
private _onDidChangeTreeData: vscode.EventEmitter<
CodeUsageNode | undefined | void
> = new vscode.EventEmitter<CodeUsageNode | undefined | void>()
readonly onDidChangeTreeData: vscode.Event<CodeUsageNode | undefined | void> =
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<string, VariableCodeReference>
await Promise.all(
Object.entries(variables).map(async ([key, variable]) => {
const data = await getCombinedVariableDetails(variable)
result[key] = data
}),
)
hideBusyMessage()
return result
}

async refresh(): Promise<void> {
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<CodeUsageNode[]> {
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,
Expand Down
98 changes: 98 additions & 0 deletions src/components/UsagesTree/UsagesTreeProvider.ts
Original file line number Diff line number Diff line change
@@ -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<CodeUsageNode>
{
private _onDidChangeTreeData: vscode.EventEmitter<
CodeUsageNode | undefined | void
> = new vscode.EventEmitter<CodeUsageNode | undefined | void>()
readonly onDidChangeTreeData: vscode.Event<CodeUsageNode | undefined | void> =
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<string, VariableCodeReference>
await Promise.all(
Object.entries(variables).map(async ([key, variable]) => {
const data = await getCombinedVariableDetails(variable)
result[key] = data
}),
)
hideBusyMessage()
return result
}

async refresh(): Promise<void> {
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<CodeUsageNode[]> {
if (!this.workspaceRoot) {
vscode.window.showInformationMessage('No dependency in empty workspace')
return []
}

if (element) {
return element.children
}

return this.flagsSeen
}
}
1 change: 1 addition & 0 deletions src/components/UsagesTree/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './UsagesTreeProvider'
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
Loading