Skip to content

Commit 54a6fdb

Browse files
committed
Prevent duplicate token counting notifications for workspace roots
- the 'crunching tokens' notification should be shown only on startup or when user adds new root folder to the workspace. └── apps └── editor └── src └── context ├── context-initialization.ts ├── helpers │ ├── get-all-workspace-files.ts │ └── get-selected-files.ts ├── providers │ ├── open-editors │ │ └── open-editors-provider.ts │ ├── selected-files │ │ └── selected-files-provider.ts │ └── workspace │ ├── modules │ │ ├── index.ts │ │ └── token-calculator.ts │ └── workspace-provider.ts ├── shared-context-state.ts └── utils └── shrink-file ├── index.ts ├── languages │ ├── c-style.ts │ ├── css.ts │ ├── html.ts │ ├── jsx.ts │ ├── python.ts │ ├── ruby.ts │ └── sql.ts ├── shrink-file.spec.ts ├── shrink-file.ts └── test-cases ├── c-style │ ├── input.txt │ └── output.txt ├── css │ ├── input.txt │ └── output.txt ├── html │ ├── input.txt │ └── output.txt ├── jsx │ ├── input.txt │ └── output.txt ├── python │ ├── input.txt │ └── output.txt ├── ruby │ ├── input.txt │ └── output.txt ├── scss │ ├── input.txt │ └── output.txt └── sql ├── input.txt └── output.txt
1 parent 004833b commit 54a6fdb

1 file changed

Lines changed: 35 additions & 69 deletions

File tree

‎apps/editor/src/context/providers/workspace/modules/token-calculator.ts‎

Lines changed: 35 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export class TokenCalculator implements vscode.Disposable {
3737
private _session_cache: TokenCountsCache = {}
3838
private _token_cache_update_timeout: NodeJS.Timeout | null = null
3939
private _has_token_counts_cache_updated_once = false
40+
private _notified_roots: Set<string> = new Set()
4041

4142
private _progress_stack: {
4243
progress: vscode.Progress<{ message?: string; increment?: number }>
@@ -248,16 +249,28 @@ export class TokenCalculator implements vscode.Disposable {
248249
task: () => Promise<T>
249250
}): Promise<T> {
250251
let total_uncached = 0
252+
let should_notify = false
251253

252254
if (params.dir_path) {
253255
total_uncached = await this._count_uncached_files_in_dir(params.dir_path)
256+
const workspace_roots = this._provider.get_workspace_roots()
257+
if (workspace_roots.includes(params.dir_path)) {
258+
if (!this._notified_roots.has(params.dir_path)) {
259+
should_notify = true
260+
this._notified_roots.add(params.dir_path)
261+
}
262+
}
254263
} else if (params.roots) {
255264
for (const root of params.roots) {
256265
total_uncached += await this._count_uncached_files_in_dir(root)
266+
if (!this._notified_roots.has(root)) {
267+
should_notify = true
268+
this._notified_roots.add(root)
269+
}
257270
}
258271
}
259272

260-
if (total_uncached === 0) {
273+
if (total_uncached === 0 || !should_notify) {
261274
return params.task()
262275
}
263276

@@ -678,85 +691,38 @@ export class TokenCalculator implements vscode.Disposable {
678691
}): Promise<{ total: number; shrink: number }> {
679692
const checked_files = this._provider.get_checked_files()
680693

681-
let total_uncached = 0
694+
const result = { total: 0, shrink: 0 }
682695
for (const file_path of checked_files) {
683-
if (options?.exclude_file_path && file_path == options.exclude_file_path)
684-
continue
685696
try {
686-
const stat = fs.lstatSync(file_path)
687-
if (stat.isSymbolicLink() || stat.isDirectory()) continue
688697
if (
689-
!this._file_token_counts.has(file_path) ||
690-
!this._file_shrink_token_counts.has(file_path)
698+
options?.exclude_file_path &&
699+
file_path == options.exclude_file_path
691700
) {
692-
total_uncached++
701+
continue
693702
}
694-
} catch {}
695-
}
696703

697-
const task = async () => {
698-
const result = { total: 0, shrink: 0 }
699-
for (const file_path of checked_files) {
700-
try {
701-
if (
702-
options?.exclude_file_path &&
703-
file_path == options.exclude_file_path
704-
) {
705-
continue
706-
}
704+
const stat = fs.lstatSync(file_path)
705+
if (stat.isSymbolicLink()) continue
707706

708-
const stat = fs.lstatSync(file_path)
709-
if (stat.isSymbolicLink()) continue
710-
711-
if (stat.isFile()) {
712-
if (this._file_token_counts.has(file_path)) {
713-
result.total += this._file_token_counts.get(file_path)!
714-
result.shrink += this._file_shrink_token_counts.get(file_path)!
715-
} else {
716-
const count = await this.calculate_file_tokens(file_path)
717-
result.total += count.total
718-
result.shrink += count.shrink
719-
}
707+
if (stat.isFile()) {
708+
if (this._file_token_counts.has(file_path)) {
709+
result.total += this._file_token_counts.get(file_path)!
710+
result.shrink += this._file_shrink_token_counts.get(file_path)!
711+
} else {
712+
const count = await this.calculate_file_tokens(file_path)
713+
result.total += count.total
714+
result.shrink += count.shrink
720715
}
721-
} catch (error) {
722-
Logger.error({
723-
function_name: 'get_checked_files_token_count',
724-
message: `Error accessing file ${file_path} for token count`,
725-
data: error
726-
})
727716
}
717+
} catch (error) {
718+
Logger.error({
719+
function_name: 'get_checked_files_token_count',
720+
message: `Error accessing file ${file_path} for token count`,
721+
data: error
722+
})
728723
}
729-
return result
730-
}
731-
732-
if (total_uncached === 0) {
733-
return task()
734724
}
735-
736-
return vscode.window.withProgress(
737-
{
738-
location: vscode.ProgressLocation.Notification,
739-
title: t('context.crunching-token-counts'),
740-
cancellable: false
741-
},
742-
async (progress) => {
743-
const progress_state = {
744-
progress,
745-
total_files: total_uncached,
746-
processed_files: 0
747-
}
748-
this._progress_stack.push(progress_state)
749-
750-
try {
751-
return await task()
752-
} finally {
753-
const index = this._progress_stack.indexOf(progress_state)
754-
if (index !== -1) {
755-
this._progress_stack.splice(index, 1)
756-
}
757-
}
758-
}
759-
)
725+
return result
760726
}
761727

762728
public dispose() {

0 commit comments

Comments
 (0)