Skip to content

Commit

Permalink
Parse config.yml file to get variable aliases for hover
Browse files Browse the repository at this point in the history
  • Loading branch information
emmawillis committed Aug 3, 2023
1 parent 5b75b0d commit b28c7dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/StateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const enum KEYS {
SEND_METRICS_PROMPTED = 'send_metrics_prompted',
CODE_USAGE_KEYS = 'code_usage_keys',
EXTENSION_INSTALLED = 'extension_installed',
REPO_CONFIG = 'repo_config',
}

export class StateManager {
Expand Down Expand Up @@ -39,6 +40,7 @@ export class StateManager {
static setState(key: KEYS.ORGANIZATION, value: Organization | undefined): Thenable<void>
static setState(key: KEYS.SEND_METRICS_PROMPTED, value: boolean | undefined): Thenable<void>
static setState(key: KEYS.CODE_USAGE_KEYS, value: string[] | undefined): Thenable<void>
static setState(key: KEYS.REPO_CONFIG, value: Record<string, any>): Thenable<void>
static setState(key: string, value: any) {
return this.workspaceState.update(key, value)
}
Expand All @@ -51,6 +53,7 @@ export class StateManager {
static getState(key: KEYS.ORGANIZATION): Organization | undefined
static getState(key: KEYS.SEND_METRICS_PROMPTED): boolean | undefined
static getState(key: KEYS.CODE_USAGE_KEYS): string[] | undefined
static getState(key: KEYS.REPO_CONFIG): Record<string, any> | undefined
static getState(key: string) {
return this.workspaceState.get(key)
}
Expand Down
29 changes: 26 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UsagesTreeProvider } from './components/UsagesTree'
import { getHoverString } from './components/hoverCard'
import { trackRudderstackEvent } from './RudderStackService'
import { CodeUsageNode } from './components/UsagesTree/CodeUsageNode'
import yaml from 'js-yaml'

Object.defineProperty(exports, '__esModule', { value: true })
exports.deactivate = exports.activate = void 0
Expand All @@ -18,6 +19,23 @@ const SCHEME_FILE = {
scheme: 'file',
}

const loadRepoConfig = async (rootPath?: string, repoConfigPath?: string) => {
if (rootPath && repoConfigPath) {
try {
const configFileByteArray = await vscode.workspace.fs.readFile(
vscode.Uri.parse(`file:${rootPath}/${repoConfigPath}`)
)
const configFileString = new TextDecoder().decode(configFileByteArray)
const configFileJson = yaml.load(configFileString) as Record<string, any> | undefined
if (configFileJson) {
StateManager.setState(KEYS.REPO_CONFIG, configFileJson)
}
} catch { // do nothing if file doesn't exist
return
}
}
}

export const activate = async (context: vscode.ExtensionContext) => {
StateManager.globalState = context.globalState
StateManager.workspaceState = context.workspaceState
Expand Down Expand Up @@ -63,6 +81,10 @@ export const activate = async (context: vscode.ExtensionContext) => {
trackRudderstackEvent('Usages Viewed')
})

const status = await cliStatus()

await loadRepoConfig(rootPath, status.repoConfigPath)

context.subscriptions.push(
vscode.commands.registerCommand(
'devcycle-featureflags.usagesNodeClicked',
Expand Down Expand Up @@ -112,7 +134,6 @@ export const activate = async (context: vscode.ExtensionContext) => {
false,
),
])
// TODO we can probably remove logout() since we aren't logging into the CLI anymore
await logout()
},
),
Expand Down Expand Up @@ -152,7 +173,6 @@ export const activate = async (context: vscode.ExtensionContext) => {
}
}

const status = await cliStatus()
if (status.organization) {
await vscode.commands.executeCommand(
'setContext',
Expand All @@ -177,7 +197,10 @@ export const activate = async (context: vscode.ExtensionContext) => {
return
}

const variableKey = document.getText(range)
const variableAliases = StateManager.getState(KEYS.REPO_CONFIG)?.codeInsights?.variableAliases || {}
let variableKey = document.getText(range)
variableKey = variableAliases[variableKey] || variableKey

const variables = StateManager.getState(KEYS.VARIABLES) || {}
const keyInAPIVariables = !!variables[variableKey]
const keyInCodeUsages = StateManager.getState(KEYS.CODE_USAGE_KEYS)?.includes(variableKey)
Expand Down

0 comments on commit b28c7dc

Please sign in to comment.