Skip to content

Commit bde0868

Browse files
refactor: extract codewhisperer tree view setup (#4428)
Problem: We want to use the CodeWhisperer tree view in the explorer but when enabling it the current code will try and enable the other tree views such as the Resource Explorer Solution: Extract the logic for the tree view that we want to work in both node and browser environments in to its own shared activate function. Now we can use that shared activation for both environments. Signed-off-by: nkomonen <[email protected]>
1 parent 5c3b12c commit bde0868

File tree

3 files changed

+61
-31
lines changed

3 files changed

+61
-31
lines changed

packages/toolkit/src/awsexplorer/activation.ts

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,21 @@ import { AwsExplorer } from './awsExplorer'
2121
import { copyTextCommand } from './commands/copyText'
2222
import { loadMoreChildrenCommand } from './commands/loadMoreChildren'
2323
import { checkExplorerForDefaultRegion } from './defaultRegion'
24-
import { createToolView, ToolView } from './toolView'
24+
import { ToolView } from './toolView'
2525
import { telemetry } from '../shared/telemetry/telemetry'
26-
import { cdkNode, CdkRootNode, refreshCdkExplorer } from '../cdk/explorer/rootNode'
27-
import {
28-
CodeWhispererNode,
29-
getCodewhispererNode,
30-
refreshCodeWhisperer,
31-
refreshCodeWhispererRootNode,
32-
} from '../codewhisperer/explorer/codewhispererNode'
33-
import { once } from '../shared/utilities/functionUtils'
26+
import { cdkNode, refreshCdkExplorer } from '../cdk/explorer/rootNode'
3427
import { CodeCatalystRootNode } from '../codecatalyst/explorer'
3528
import { CodeCatalystAuthenticationProvider } from '../codecatalyst/auth'
3629
import { S3FolderNode } from '../s3/explorer/s3FolderNode'
3730
import { amazonQNode, refreshAmazonQ, refreshAmazonQRootNode } from '../amazonq/explorer/amazonQNode'
3831
import { GlobalState } from '../shared/globalState'
32+
import { activateViewsShared, registerToolView } from './activationShared'
3933

4034
/**
4135
* Activates the AWS Explorer UI and related functionality.
36+
*
37+
* IMPORTANT: Views that should work in all vscode environments (node or browser)
38+
* should be setup in {@link activateViewsShared}.
4239
*/
4340
export async function activate(args: {
4441
context: ExtContext
@@ -119,30 +116,9 @@ export async function activate(args: {
119116
...amazonQViewNode,
120117
...codecatalystViewNode,
121118
{ nodes: [cdkNode], view: 'aws.cdk', refreshCommands: [refreshCdkExplorer] },
122-
{
123-
nodes: [getCodewhispererNode()],
124-
view: 'aws.codewhisperer',
125-
refreshCommands: [refreshCodeWhisperer, refreshCodeWhispererRootNode],
126-
},
127119
]
128120
for (const viewNode of viewNodes) {
129-
const toolView = createToolView(viewNode)
130-
args.context.extensionContext.subscriptions.push(toolView)
131-
if (viewNode.view === 'aws.cdk') {
132-
// Legacy CDK behavior. Mostly useful for C9 as they do not have inline buttons.
133-
toolView.onDidChangeVisibility(({ visible }) => visible && cdkNode.refresh())
134-
}
135-
136-
toolView.onDidExpandElement(e => {
137-
if (e.element.resource instanceof CdkRootNode) {
138-
// Legacy CDK metric, remove this when we add something generic
139-
const recordExpandCdkOnce = once(() => telemetry.cdk_appExpanded.emit())
140-
recordExpandCdkOnce()
141-
} else if (e.element.resource instanceof CodeWhispererNode) {
142-
const onDidExpandCodeWhisperer = once(() => telemetry.ui_click.emit({ elementId: 'cw_parentNode' }))
143-
onDidExpandCodeWhisperer()
144-
}
145-
})
121+
registerToolView(viewNode, args.context.extensionContext)
146122
}
147123
}
148124

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import * as vscode from 'vscode'
7+
import { createToolView, ToolView } from './toolView'
8+
import { telemetry } from '../shared/telemetry/telemetry'
9+
import { cdkNode, CdkRootNode } from '../cdk/explorer/rootNode'
10+
import {
11+
CodeWhispererNode,
12+
getCodewhispererNode,
13+
refreshCodeWhisperer,
14+
refreshCodeWhispererRootNode,
15+
} from '../codewhisperer/explorer/codewhispererNode'
16+
import { once } from '../shared/utilities/functionUtils'
17+
18+
/**
19+
* Activates vscode Views (eg tree view) that work in any vscode environment (nodejs or browser).
20+
*/
21+
export async function activateViewsShared(context: vscode.ExtensionContext): Promise<void> {
22+
registerToolView(getCodeWhispererToolView(), context)
23+
}
24+
25+
export function getCodeWhispererToolView(): ToolView {
26+
return {
27+
nodes: [getCodewhispererNode()],
28+
view: 'aws.codewhisperer',
29+
refreshCommands: [refreshCodeWhisperer, refreshCodeWhispererRootNode],
30+
}
31+
}
32+
33+
export function registerToolView(viewNode: ToolView, context: vscode.ExtensionContext) {
34+
const toolView = createToolView(viewNode)
35+
context.subscriptions.push(toolView)
36+
if (viewNode.view === 'aws.cdk') {
37+
// Legacy CDK behavior. Mostly useful for C9 as they do not have inline buttons.
38+
toolView.onDidChangeVisibility(({ visible }) => visible && cdkNode.refresh())
39+
}
40+
41+
toolView.onDidExpandElement(e => {
42+
if (e.element.resource instanceof CdkRootNode) {
43+
// Legacy CDK metric, remove this when we add something generic
44+
const recordExpandCdkOnce = once(() => telemetry.cdk_appExpanded.emit())
45+
recordExpandCdkOnce()
46+
} else if (e.element.resource instanceof CodeWhispererNode) {
47+
const onDidExpandCodeWhisperer = once(() => telemetry.ui_click.emit({ elementId: 'cw_parentNode' }))
48+
onDidExpandCodeWhisperer()
49+
}
50+
})
51+
}

packages/toolkit/src/extensionShared.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { getIdeProperties, aboutToolkit, isCloud9 } from './shared/extensionUtil
1919
import { telemetry } from './shared/telemetry/telemetry'
2020
import { openUrl } from './shared/utilities/vsCodeUtils'
2121
import { activate as activateCodeWhisperer, shutdown as codewhispererShutdown } from './codewhisperer/activation'
22+
import { activateViewsShared } from './awsexplorer/activationShared'
2223

2324
import { activate as activateLogger } from './shared/logger/activation'
2425
import { initializeComputeRegion } from './shared/extensionUtilities'
@@ -135,6 +136,8 @@ export async function activateShared(
135136
credentialsStore: globals.loginManager.store,
136137
}
137138

139+
await activateViewsShared(extContext.extensionContext)
140+
138141
await activateCodeWhisperer(extContext)
139142

140143
return extContext

0 commit comments

Comments
 (0)