Skip to content

Commit 921c6d2

Browse files
committed
Refactor provider imports and equality checks to use consistent path aliases and loose equality operators
1 parent f2d106b commit 921c6d2

7 files changed

Lines changed: 196 additions & 13 deletions

File tree

‎packages/shared/src/hooks/use-compacting.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const use_compacting = (max_compact_step = 4) => {
77

88
const report_width = useCallback((width: number, step: number) => {
99
set_thresholds((prev) => {
10-
if (prev[step] === width) return prev
10+
if (prev[step] == width) return prev
1111
return { ...prev, [step]: width }
1212
})
1313
}, [])
@@ -26,7 +26,7 @@ export const use_compacting = (max_compact_step = 4) => {
2626

2727
// Try to shrink (increase compaction)
2828
if (thresholds[0] !== undefined && width < thresholds[0]) {
29-
if (compact_step === 0) {
29+
if (compact_step == 0) {
3030
set_compact_step(1)
3131
} else if (compact_step < max_compact_step) {
3232
if (

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {
55
FileItem
66
} from './providers/workspace/workspace-provider'
77
import { FilesCollector } from '../utils/files-collector'
8-
import { OpenEditorsProvider } from './providers/open-editors-provider'
9-
import { WebsitesProvider, WebsiteItem } from './providers/websites-provider'
8+
import { OpenEditorsProvider } from './providers/open-editors/open-editors-provider'
109
import { SharedFileState } from './shared-file-state'
1110
import { marked } from 'marked'
1211
import { EventEmitter } from 'events'
@@ -20,7 +19,8 @@ import {
2019
RANGES_STATE_KEY,
2120
type DuplicateWorkspaceContext
2221
} from '../constants/state-keys'
23-
import { ContextProvider } from './providers/context-provider'
22+
import { ContextProvider } from './providers/context/context-provider'
23+
import { WebsiteItem, WebsitesProvider } from './providers/websites-provider'
2424

2525
export const token_count_emitter = new EventEmitter()
2626

packages/vscode/src/context/providers/context-provider.ts renamed to packages/vscode/src/context/providers/context/context-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode'
2-
import { WorkspaceProvider, FileItem } from './workspace/workspace-provider'
2+
import { WorkspaceProvider, FileItem } from '../workspace/workspace-provider'
33

44
export class ContextProvider implements vscode.TreeDataProvider<FileItem> {
55
private _onDidChangeTreeData: vscode.EventEmitter<

packages/vscode/src/context/providers/open-editors-provider.ts renamed to packages/vscode/src/context/providers/open-editors/open-editors-provider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as vscode from 'vscode'
22
import * as fs from 'fs'
33
import * as path from 'path'
4-
import { FileItem } from './workspace/workspace-provider'
5-
import { SharedFileState } from '../shared-file-state'
6-
import { WorkspaceProvider } from './workspace/workspace-provider'
7-
import { display_token_count } from '../../utils/display-token-count'
4+
import { FileItem } from '../workspace/workspace-provider'
5+
import { WorkspaceProvider } from '../workspace/workspace-provider'
6+
import { display_token_count } from '../../../utils/display-token-count'
7+
import { SharedFileState } from '@/context/shared-file-state'
88

99
export class OpenEditorsProvider
1010
implements vscode.TreeDataProvider<FileItem>, vscode.Disposable

‎packages/vscode/src/context/providers/websites-provider.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { CONTEXT_CHECKED_URLS_STATE_KEY } from '@/constants/state-keys'
2+
import { display_token_count } from '@/utils/display-token-count'
13
import { Website } from '@shared/types/websocket-message'
24
import * as vscode from 'vscode'
3-
import { CONTEXT_CHECKED_URLS_STATE_KEY } from '../../constants/state-keys'
4-
import { display_token_count } from '../../utils/display-token-count'
55

66
export class WebsiteItem extends vscode.TreeItem {
77
public readonly token_count: number
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import { Website } from '@shared/types/websocket-message'
2+
import * as vscode from 'vscode'
3+
import { CONTEXT_CHECKED_URLS_STATE_KEY } from '../../../constants/state-keys'
4+
import { display_token_count } from '../../../utils/display-token-count'
5+
6+
export class WebsiteItem extends vscode.TreeItem {
7+
public readonly token_count: number
8+
9+
constructor(
10+
public readonly title: string,
11+
public readonly url: string,
12+
public readonly content: string,
13+
public readonly favicon: string,
14+
public checkboxState: vscode.TreeItemCheckboxState = vscode
15+
.TreeItemCheckboxState.Unchecked
16+
) {
17+
super(title, vscode.TreeItemCollapsibleState.None)
18+
19+
const content_xml = `<document title="${title}">\n<![CDATA[\n${content}\n]]>\n</document>\n`
20+
this.token_count = Math.floor(content_xml.length / 4)
21+
const formatted_token_count = display_token_count(this.token_count)
22+
this.tooltip = `${title} - About ${formatted_token_count} tokens`
23+
this.description = formatted_token_count
24+
25+
if (favicon) {
26+
this.iconPath = vscode.Uri.parse(favicon)
27+
} else {
28+
this.iconPath = new vscode.ThemeIcon('globe')
29+
}
30+
31+
this.command = {
32+
command: 'codeWebChat.previewWebsite',
33+
title: 'Preview Website Content',
34+
arguments: [this]
35+
}
36+
37+
this.contextValue = 'website'
38+
}
39+
}
40+
41+
export class WebsitesProvider
42+
implements vscode.TreeDataProvider<WebsiteItem>, vscode.Disposable
43+
{
44+
private _websites: Website[] = []
45+
private _checked_websites: Map<string, vscode.TreeItemCheckboxState> =
46+
new Map()
47+
private _onDidChangeTreeData = new vscode.EventEmitter<
48+
WebsiteItem | undefined | null | void
49+
>()
50+
readonly onDidChangeTreeData = this._onDidChangeTreeData.event
51+
52+
private _onDidChangeCheckedWebsites = new vscode.EventEmitter<void>()
53+
readonly onDidChangeCheckedWebsites = this._onDidChangeCheckedWebsites.event
54+
55+
constructor(private context: vscode.ExtensionContext) {
56+
this.onDidChangeCheckedWebsites(() => this.save_checked_websites_state())
57+
}
58+
59+
update_websites(websites: Website[]): void {
60+
const new_website_urls = new Set(websites.map((website) => website.url))
61+
62+
for (const url of this._checked_websites.keys()) {
63+
if (!new_website_urls.has(url)) {
64+
this._checked_websites.delete(url)
65+
}
66+
}
67+
68+
this._websites = websites
69+
this._onDidChangeTreeData.fire()
70+
this._onDidChangeCheckedWebsites.fire()
71+
}
72+
73+
getTreeItem(element: WebsiteItem): vscode.TreeItem {
74+
const checkbox_state =
75+
this._checked_websites.get(element.url) ??
76+
vscode.TreeItemCheckboxState.Unchecked
77+
element.checkboxState = checkbox_state
78+
return element
79+
}
80+
81+
getChildren(): Thenable<WebsiteItem[]> {
82+
if (this._websites.length == 0) {
83+
return Promise.resolve([])
84+
}
85+
86+
return Promise.resolve(
87+
this._websites.map(
88+
(website) =>
89+
new WebsiteItem(
90+
website.title || website.url,
91+
website.url,
92+
website.content,
93+
website.favicon || '',
94+
this._checked_websites.get(website.url) ??
95+
vscode.TreeItemCheckboxState.Unchecked
96+
)
97+
)
98+
)
99+
}
100+
101+
get_websites_count(): number {
102+
return this._websites.length
103+
}
104+
105+
get_checked_websites(): Website[] {
106+
return this._websites.filter(
107+
(website) =>
108+
this._checked_websites.get(website.url) ===
109+
vscode.TreeItemCheckboxState.Checked
110+
)
111+
}
112+
113+
get_checked_websites_token_count(): number {
114+
return this.get_checked_websites()
115+
.map((website) => {
116+
const content_xml = `<document title="${website.title}">\n<![CDATA[\n${website.content}\n]]>\n</document>\n`
117+
return Math.floor(content_xml.length / 4)
118+
})
119+
.reduce((sum, count) => sum + count, 0)
120+
}
121+
122+
async update_check_state(
123+
item: WebsiteItem,
124+
state: vscode.TreeItemCheckboxState
125+
): Promise<void> {
126+
this._checked_websites.set(item.url, state)
127+
this._onDidChangeCheckedWebsites.fire()
128+
this._onDidChangeTreeData.fire()
129+
}
130+
131+
check_all(): void {
132+
let changed = false
133+
for (const website of this._websites) {
134+
if (
135+
this._checked_websites.get(website.url) !==
136+
vscode.TreeItemCheckboxState.Checked
137+
) {
138+
this._checked_websites.set(
139+
website.url,
140+
vscode.TreeItemCheckboxState.Checked
141+
)
142+
changed = true
143+
}
144+
}
145+
if (changed) {
146+
this._onDidChangeCheckedWebsites.fire()
147+
this._onDidChangeTreeData.fire()
148+
}
149+
}
150+
151+
clear_checks(): void {
152+
if (this._checked_websites.size == 0) {
153+
return
154+
}
155+
this._checked_websites.clear()
156+
this._onDidChangeCheckedWebsites.fire()
157+
this._onDidChangeTreeData.fire()
158+
}
159+
private async save_checked_websites_state(): Promise<void> {
160+
const checked_urls = this.get_checked_websites().map((w) => w.url)
161+
await this.context.workspaceState.update(
162+
CONTEXT_CHECKED_URLS_STATE_KEY,
163+
checked_urls
164+
)
165+
}
166+
167+
public load_checked_websites_state(): void {
168+
const checked_urls = this.context.workspaceState.get<string[]>(
169+
CONTEXT_CHECKED_URLS_STATE_KEY
170+
)
171+
if (checked_urls) {
172+
for (const url of checked_urls) {
173+
this._checked_websites.set(url, vscode.TreeItemCheckboxState.Checked)
174+
}
175+
this._onDidChangeCheckedWebsites.fire()
176+
}
177+
}
178+
179+
dispose(): void {
180+
this._onDidChangeTreeData.dispose()
181+
this._onDidChangeCheckedWebsites.dispose()
182+
}
183+
}

‎packages/vscode/src/context/shared-file-state.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
WorkspaceProvider,
66
FileItem
77
} from './providers/workspace/workspace-provider'
8-
import { OpenEditorsProvider } from './providers/open-editors-provider'
8+
import { OpenEditorsProvider } from './providers/open-editors/open-editors-provider'
99

1010
export class SharedFileState {
1111
private static instance: SharedFileState

0 commit comments

Comments
 (0)