Skip to content

Commit f8ba693

Browse files
committed
Refactor the VS Code extension to separate the Workspace and Context views into distinct tree providers, ensuring the Context view only displays checked files and directories
1 parent b295150 commit f8ba693

4 files changed

Lines changed: 118 additions & 107 deletions

File tree

‎packages/vscode/package.json‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@
7272
"icon": "resources/activity-bar-icon.svg"
7373
},
7474
{
75-
"id": "codeWebChatViewWorkspace",
75+
"id": "codeWebChatViewContext",
7676
"name": "Context",
77+
"icon": "resources/activity-bar-icon.svg",
78+
"visibility": "collapsed"
79+
},
80+
{
81+
"id": "codeWebChatViewWorkspace",
82+
"name": "Workspace",
7783
"icon": "resources/activity-bar-icon.svg"
7884
},
7985
{
@@ -363,43 +369,43 @@
363369
"view/title": [
364370
{
365371
"command": "codeWebChat.applyContext",
366-
"when": "view == codeWebChatViewWorkspace",
372+
"when": "view == codeWebChatViewContext",
367373
"group": "navigation@1"
368374
},
369375
{
370376
"command": "codeWebChat.saveContext",
371-
"when": "view == codeWebChatViewWorkspace",
377+
"when": "view == codeWebChatViewContext",
372378
"group": "navigation@2"
373379
},
374380
{
375-
"command": "codeWebChat.copyContext",
381+
"command": "codeWebChat.checkAll",
376382
"when": "view == codeWebChatViewWorkspace",
377-
"group": "navigation@3"
383+
"group": "navigation@1"
378384
},
379385
{
380-
"command": "codeWebChat.checkAll",
386+
"command": "codeWebChat.clearChecks",
381387
"when": "view == codeWebChatViewWorkspace",
382-
"group": "navigation@4"
388+
"group": "navigation@2"
383389
},
384390
{
385-
"command": "codeWebChat.clearChecks",
391+
"command": "codeWebChat.copyContext",
386392
"when": "view == codeWebChatViewWorkspace",
387-
"group": "navigation@5"
393+
"group": "navigation@3"
388394
},
389395
{
390396
"command": "codeWebChat.newFile",
391397
"when": "view == codeWebChatViewWorkspace",
392-
"group": "navigation@6"
398+
"group": "navigation@4"
393399
},
394400
{
395401
"command": "codeWebChat.newFolder",
396402
"when": "view == codeWebChatViewWorkspace",
397-
"group": "navigation@7"
403+
"group": "navigation@5"
398404
},
399405
{
400406
"command": "codeWebChat.collapseFolders",
401407
"when": "view == codeWebChatViewWorkspace",
402-
"group": "navigation@8"
408+
"group": "navigation@6"
403409
},
404410
{
405411
"command": "codeWebChat.copyContextOpenEditors",
@@ -495,7 +501,7 @@
495501
},
496502
{
497503
"command": "codeWebChat.referenceInChat",
498-
"when": "view == codeWebChatViewWorkspace",
504+
"when": "view == codeWebChatViewWorkspace || view == codeWebChatViewContext",
499505
"group": "1_modification@3"
500506
}
501507
]

‎packages/vscode/src/context/context-initialization.ts‎

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import * as vscode from 'vscode'
22
import * as path from 'path'
3-
import {
4-
WorkspaceProvider,
5-
FileItem,
6-
ContextFolderItem
7-
} from './providers/workspace-provider'
3+
import { WorkspaceProvider, FileItem } from './providers/workspace-provider'
84
import { FilesCollector } from '../utils/files-collector'
95
import { OpenEditorsProvider } from './providers/open-editors-provider'
106
import { WebsitesProvider, WebsiteItem } from './providers/websites-provider'
@@ -13,6 +9,7 @@ import { marked } from 'marked'
139
import { EventEmitter } from 'events'
1410
import { apply_context_command } from '../commands/apply-context-command'
1511
import { dictionary } from '@shared/constants/dictionary'
12+
import { ContextProvider } from './providers/context-provider'
1613

1714
export const token_count_emitter = new EventEmitter()
1815

@@ -23,14 +20,15 @@ export function context_initialization(context: vscode.ExtensionContext): {
2320
} {
2421
const workspace_folders = vscode.workspace.workspaceFolders ?? []
2522

26-
let workspace_view: vscode.TreeView<FileItem | ContextFolderItem>
23+
let workspace_view: vscode.TreeView<FileItem>
2724

2825
const websites_provider = new WebsitesProvider(context)
2926
const workspace_provider = new WorkspaceProvider(
3027
workspace_folders as any,
3128
context
3229
)
33-
context.subscriptions.push(websites_provider)
30+
const context_provider = new ContextProvider(workspace_provider)
31+
context.subscriptions.push(websites_provider, context_provider)
3432

3533
const open_editors_provider = new OpenEditorsProvider(
3634
workspace_folders as any,
@@ -43,14 +41,14 @@ export function context_initialization(context: vscode.ExtensionContext): {
4341
websites_provider
4442
)
4543

46-
const update_activity_bar_badge_token_count = async () => {
47-
if (workspace_provider && workspace_view) {
48-
const workspace_token_count =
44+
const update_view_badges = async () => {
45+
if (context_provider && context_view) {
46+
const context_token_count =
4947
await workspace_provider.get_checked_files_token_count()
50-
workspace_view.badge = {
51-
value: workspace_token_count,
52-
tooltip: workspace_token_count
53-
? `About ${workspace_token_count} tokens in context`
48+
context_view.badge = {
49+
value: context_token_count,
50+
tooltip: context_token_count
51+
? `About ${context_token_count} tokens in context`
5452
: ''
5553
}
5654
}
@@ -79,7 +77,7 @@ export function context_initialization(context: vscode.ExtensionContext): {
7977
})
8078

8179
const register_workspace_view_handlers = (
82-
view: vscode.TreeView<FileItem | ContextFolderItem>
80+
view: vscode.TreeView<FileItem>
8381
) => {
8482
view.onDidChangeCheckboxState(async (e) => {
8583
for (const [item, state] of e.items) {
@@ -98,7 +96,13 @@ export function context_initialization(context: vscode.ExtensionContext): {
9896
manageCheckboxStateManually: true
9997
})
10098

99+
const context_view = vscode.window.createTreeView('codeWebChatViewContext', {
100+
treeDataProvider: context_provider,
101+
manageCheckboxStateManually: true
102+
})
103+
101104
register_workspace_view_handlers(workspace_view)
105+
register_workspace_view_handlers(context_view)
102106

103107
const open_editors_view = vscode.window.createTreeView(
104108
'codeWebChatViewOpenEditors',
@@ -137,6 +141,7 @@ export function context_initialization(context: vscode.ExtensionContext): {
137141
workspace_provider,
138142
open_editors_provider,
139143
workspace_view,
144+
context_view,
140145
open_editors_view,
141146
websites_view
142147
)
@@ -314,7 +319,7 @@ export function context_initialization(context: vscode.ExtensionContext): {
314319
apply_context_command(
315320
workspace_provider,
316321
() => {
317-
update_activity_bar_badge_token_count()
322+
update_view_badges()
318323
},
319324
context
320325
)
@@ -328,17 +333,17 @@ export function context_initialization(context: vscode.ExtensionContext): {
328333

329334
context.subscriptions.push(
330335
workspace_provider.onDidChangeCheckedFiles(() => {
331-
update_activity_bar_badge_token_count()
336+
update_view_badges()
332337
}),
333338
open_editors_provider.onDidChangeCheckedFiles(() => {
334-
update_activity_bar_badge_token_count()
339+
update_view_badges()
335340
}),
336341
websites_provider.onDidChangeCheckedWebsites(() => {
337-
update_activity_bar_badge_token_count()
342+
update_view_badges()
338343
}),
339344
// Fixes badge not updating when websites list changes
340345
websites_provider.onDidChangeTreeData(() => {
341-
update_activity_bar_badge_token_count()
346+
update_view_badges()
342347
update_websites_view_message()
343348
})
344349
)
@@ -351,7 +356,7 @@ export function context_initialization(context: vscode.ExtensionContext): {
351356
clearTimeout(tab_change_timeout)
352357
}
353358
tab_change_timeout = setTimeout(() => {
354-
update_activity_bar_badge_token_count()
359+
update_view_badges()
355360
tab_change_timeout = null
356361
}, 100) // 100ms debounce
357362
})
@@ -367,7 +372,7 @@ export function context_initialization(context: vscode.ExtensionContext): {
367372
vscode.workspace.workspaceFolders
368373
)
369374
shared_state.set_providers(workspace_provider, open_editors_provider)
370-
update_activity_bar_badge_token_count()
375+
update_view_badges()
371376
}
372377
})
373378
)
@@ -379,14 +384,14 @@ export function context_initialization(context: vscode.ExtensionContext): {
379384
context.subscriptions.push(
380385
open_editors_provider.onDidChangeTreeData(() => {
381386
if (open_editors_provider!.is_initialized()) {
382-
update_activity_bar_badge_token_count()
387+
update_view_badges()
383388
}
384389
})
385390
)
386391

387392
// Also schedule a delayed update for initial badge display
388393
setTimeout(() => {
389-
update_activity_bar_badge_token_count()
394+
update_view_badges()
390395
}, 1000) // Wait for 1 second to ensure VS Code has fully loaded
391396

392397
return {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as vscode from 'vscode'
2+
import { WorkspaceProvider, FileItem } from './workspace-provider'
3+
4+
export class ContextProvider implements vscode.TreeDataProvider<FileItem> {
5+
private _onDidChangeTreeData: vscode.EventEmitter<
6+
FileItem | undefined | null | void
7+
> = new vscode.EventEmitter<FileItem | undefined | null | void>()
8+
readonly onDidChangeTreeData: vscode.Event<
9+
FileItem | undefined | null | void
10+
> = this._onDidChangeTreeData.event
11+
12+
private disposable: vscode.Disposable
13+
14+
constructor(private workspaceProvider: WorkspaceProvider) {
15+
this.disposable = this.workspaceProvider.onDidChangeTreeData(() =>
16+
this.refresh()
17+
)
18+
}
19+
20+
refresh(): void {
21+
this._onDidChangeTreeData.fire()
22+
}
23+
24+
getTreeItem(element: FileItem): vscode.TreeItem | Thenable<vscode.TreeItem> {
25+
return this.workspaceProvider.getTreeItem(element)
26+
}
27+
28+
getChildren(element?: FileItem): vscode.ProviderResult<FileItem[]> {
29+
return this.workspaceProvider.getContextViewChildren(element)
30+
}
31+
32+
dispose() {
33+
this.disposable.dispose()
34+
this._onDidChangeTreeData.dispose()
35+
}
36+
}

0 commit comments

Comments
 (0)