@@ -97,19 +97,27 @@ export abstract class Presets {
97
97
return this . presets [ this . selectedPreset ] [ 2 ] ;
98
98
}
99
99
100
- protected setSelected ( preset : number ) : boolean {
101
- if ( this . selectedPreset !== preset ) {
100
+ protected getSelectedPreset ( ) : number {
101
+ return this . selectedPreset ;
102
+ }
103
+
104
+ protected async setSelected ( preset : number , forceSelected : boolean = false ) : Promise < boolean | undefined > {
105
+ if ( forceSelected || this . selectedPreset !== preset ) {
102
106
this . selectedPreset = preset ;
103
- if ( this . storage && this . persistenceKey ) {
104
- this . storage . update ( this . persistenceKey , this . selectedPreset ) ;
105
- }
107
+ await this . savePersistent ( this . persistenceKey , this . selectedPreset ) ;
106
108
this . notifyChanged ( ) ;
107
109
return true ;
108
110
} else {
109
111
return false ;
110
112
}
111
113
}
112
114
115
+ protected async savePersistent ( key : string | undefined , value : any ) : Promise < void > {
116
+ if ( this . storage && key ) {
117
+ await this . storage . update ( key , value ) ;
118
+ }
119
+ }
120
+
113
121
private readonly listeners : OnChanged [ ] = [ ] ;
114
122
115
123
onChanged ( listener : OnChanged ) {
@@ -158,28 +166,88 @@ export class WhenStartedPresets extends Presets {
158
166
export class CpuSamplerFilterPresets extends Presets {
159
167
160
168
private static PERSISTENCE_KEY = 'visualvm.presets.CpuSamplerFilter' ;
169
+ private static PERSISTENCE_KEY_CPU_SAMPLER_FILTER_CUSTOM_INCLUSIVE = 'visualvm.presets.CpuSamplerFilterCustomInclusive' ;
170
+ private static PERSISTENCE_KEY_CPU_SAMPLER_FILTER_CUSTOM_EXCLUSIVE = 'visualvm.presets.CpuSamplerFilterCustomExclusive' ;
161
171
private static NAME = 'CPU Sampling Filter' ;
162
172
private static SELECT_PROMPT = 'Select CPU sampling filter' ;
163
173
private static PRESETS = [
164
174
[ 'Include All Classes' , 'Collects data from all classes' , '' ] ,
165
175
[ 'Exclude JDK Classes' , 'Excludes data from JDK classes (java.*, com.sun.*, org.graalvm.*, etc.)' , parameters . CPU_SAMPLER_FILTER_EXCLUSIVE ] ,
166
- [ 'Include Only Project Classes' , 'Collects data only from project classes' , parameters . CPU_SAMPLER_FILTER_INCLUSIVE ]
176
+ [ 'Include Only Project Classes' , 'Collects data only from project classes' , parameters . CPU_SAMPLER_FILTER_INCLUSIVE ] ,
177
+ [ 'Include Only Defined Classes' , 'Collects data only from user defined classes' , parameters . CPU_SAMPLER_FILTER_INCLUSIVE ] ,
178
+ [ 'Exclude Defined Classes' , 'Excludes data from user defined classes' , parameters . CPU_SAMPLER_FILTER_EXCLUSIVE ]
167
179
] ;
168
180
private static INITIAL_PRESET = 0 ;
169
181
private static SINGLE_ROW_CHOICES = false ;
170
182
171
183
static PERSISTENT = new CpuSamplerFilterPresets ( ) ;
172
184
185
+ private customInclusiveFilter : string = '*' ;
186
+ private customExclusiveFilter : string = '*' ;
187
+
173
188
constructor ( ) {
174
189
super ( CpuSamplerFilterPresets . NAME , CpuSamplerFilterPresets . PRESETS , CpuSamplerFilterPresets . INITIAL_PRESET , CpuSamplerFilterPresets . SELECT_PROMPT , CpuSamplerFilterPresets . SINGLE_ROW_CHOICES ) ;
175
190
}
176
191
177
192
initializePersistent ( context : vscode . ExtensionContext ) {
178
193
this . doInitializePersistent ( context . workspaceState , CpuSamplerFilterPresets . PERSISTENCE_KEY ) ;
194
+ this . customInclusiveFilter = context . workspaceState . get ( CpuSamplerFilterPresets . PERSISTENCE_KEY_CPU_SAMPLER_FILTER_CUSTOM_INCLUSIVE , '*' ) ;
195
+ this . customExclusiveFilter = context . workspaceState . get ( CpuSamplerFilterPresets . PERSISTENCE_KEY_CPU_SAMPLER_FILTER_CUSTOM_EXCLUSIVE , '*' ) ;
196
+ }
197
+
198
+ protected async setSelected ( preset : number ) : Promise < boolean | undefined > {
199
+ if ( preset >= 3 ) {
200
+ function validateFilter ( filter : string ) : string | undefined {
201
+ if ( ! filter . length ) {
202
+ return 'Filter cannot be empty' ;
203
+ }
204
+ // TODO: validate properly
205
+ return undefined ;
206
+ }
207
+ const newValue = await vscode . window . showInputBox ( {
208
+ title : CpuSamplerFilterPresets . PRESETS [ preset ] [ 0 ] ,
209
+ value : preset === 3 ? this . customInclusiveFilter : this . customExclusiveFilter ,
210
+ placeHolder : 'Define CPU sampling filter' ,
211
+ prompt : 'Format: org.pkg.**, org.pkg.*, org.pkg.Class' ,
212
+ validateInput : filter => validateFilter ( filter )
213
+ } ) ;
214
+ if ( newValue ) {
215
+ if ( preset === 3 ) {
216
+ this . customInclusiveFilter = newValue . trim ( ) ;
217
+ await this . savePersistent ( CpuSamplerFilterPresets . PERSISTENCE_KEY_CPU_SAMPLER_FILTER_CUSTOM_INCLUSIVE , this . customInclusiveFilter ) ;
218
+ } else if ( preset === 4 ) {
219
+ this . customExclusiveFilter = newValue . trim ( ) ;
220
+ await this . savePersistent ( CpuSamplerFilterPresets . PERSISTENCE_KEY_CPU_SAMPLER_FILTER_CUSTOM_EXCLUSIVE , this . customExclusiveFilter ) ;
221
+ }
222
+ return super . setSelected ( preset , true ) ;
223
+ } else {
224
+ return undefined ;
225
+ }
226
+ } else {
227
+ return super . setSelected ( preset ) ;
228
+ }
179
229
}
180
230
181
231
getSelectedString ( ) : string {
182
- return super . getSelectedString ( ) . replace ( / j d k / g, ' JDK ' ) ;
232
+ switch ( this . getSelectedPreset ( ) ) {
233
+ case 3 :
234
+ return `include ${ this . customInclusiveFilter } ` ;
235
+ case 4 :
236
+ return `exclude ${ this . customExclusiveFilter } ` ;
237
+ default :
238
+ return super . getSelectedString ( ) . replace ( / j d k / g, ' JDK ' ) ;
239
+ }
240
+ }
241
+
242
+ getSelectedValue ( ) : string {
243
+ switch ( this . getSelectedPreset ( ) ) {
244
+ case 3 :
245
+ return `${ parameters . CPU_SAMPLER_FILTER_INCLUSIVE } :${ this . customInclusiveFilter } ` ;
246
+ case 4 :
247
+ return `${ parameters . CPU_SAMPLER_FILTER_EXCLUSIVE } :${ this . customExclusiveFilter } ` ;
248
+ default :
249
+ return super . getSelectedValue ( ) ;
250
+ }
183
251
}
184
252
185
253
}
0 commit comments