11import type { ExportsInfoForLabs } from '@volar/vscode' ;
22import type { GetProjectsRequest } from '@volar/language-server' ;
3+ import { LoadedTSFilesMetaRequest } from '@volar/language-server/protocol' ;
34import * as path from 'path' ;
5+ import * as fs from 'fs' ;
6+ import * as os from 'os' ;
47import * as vscode from 'vscode' ;
58import * as lsp from 'vscode-languageclient' ;
69import { useVolarExtensions , getIconPath } from '../common/shared' ;
@@ -11,7 +14,7 @@ interface LanguageClientItem {
1114}
1215
1316interface LanguageClientFieldItem extends LanguageClientItem {
14- field : 'start' | 'stop' | 'restart' | 'enableCodegenStack' | 'disableCodegenStack' | 'initializationOptions' | 'initializeResult' | 'projects' ;
17+ field : 'start' | 'stop' | 'restart' | 'enableCodegenStack' | 'disableCodegenStack' | 'initializationOptions' | 'initializeResult' | 'projects' | 'memory' ;
1518}
1619
1720interface LanguageClientProjectItem extends LanguageClientItem {
@@ -65,6 +68,7 @@ export function activate(context: vscode.ExtensionContext) {
6568 }
6669 stats . push ( { ...element , field : 'initializationOptions' } ) ;
6770 stats . push ( { ...element , field : 'initializeResult' } ) ;
71+ stats . push ( { ...element , field : 'memory' } ) ;
6872 stats . push ( { ...element , field : 'projects' } ) ;
6973 }
7074 else if ( element . client . state === lsp . State . Starting ) {
@@ -140,6 +144,17 @@ export function activate(context: vscode.ExtensionContext) {
140144 } ,
141145 } ;
142146 }
147+ else if ( element . field === 'memory' ) {
148+ return {
149+ label : 'TS Memory Treemap' ,
150+ collapsibleState : vscode . TreeItemCollapsibleState . None ,
151+ command : {
152+ command : '_volar.action.tsMemoryTreemap' ,
153+ title : '' ,
154+ arguments : [ element . client ] ,
155+ } ,
156+ } ;
157+ }
143158 else if ( element . field === 'enableCodegenStack' ) {
144159 return {
145160 iconPath : new vscode . ThemeIcon ( 'primitive-dot' ) ,
@@ -192,7 +207,7 @@ export function activate(context: vscode.ExtensionContext) {
192207 }
193208 else if ( element . field === 'projects' ) {
194209 return {
195- label : ` Projects` ,
210+ label : ' Projects' ,
196211 collapsibleState : vscode . TreeItemCollapsibleState . Expanded ,
197212 } ;
198213 }
@@ -216,6 +231,14 @@ export function activate(context: vscode.ExtensionContext) {
216231 await client . stop ( ) ;
217232 await client . start ( ) ;
218233 } ) ,
234+ vscode . commands . registerCommand ( '_volar.action.tsMemoryTreemap' , async ( client : lsp . BaseLanguageClient ) => {
235+ const meta = await client . sendRequest ( LoadedTSFilesMetaRequest . type ) ;
236+ const { visualizer } = await import ( 'esbuild-visualizer/dist/plugin/index' ) ;
237+ const fileContent = await visualizer ( meta as any ) ;
238+ const tmpPath = path . join ( os . tmpdir ( ) , 'stats.html' ) ;
239+ fs . writeFileSync ( tmpPath , fileContent ) ;
240+ await vscode . env . openExternal ( vscode . Uri . file ( tmpPath ) ) ;
241+ } ) ,
219242 vscode . commands . registerCommand ( '_volar.action.enableCodegenStack' , async ( client : lsp . BaseLanguageClient ) => {
220243 client . clientOptions . initializationOptions . codegenStack = true ;
221244 await client . stop ( ) ;
0 commit comments