@@ -13,6 +13,7 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
13
13
import { ContextKeyExpr , IContextKey , IContextKeyService , IReadableSet } from 'vs/platform/contextkey/common/contextkey' ;
14
14
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation' ;
15
15
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry' ;
16
+ import { IStorageService , StorageScope , StorageTarget } from 'vs/platform/storage/common/storage' ;
16
17
import { TerminalSettingId } from 'vs/platform/terminal/common/terminal' ;
17
18
import { ShellIntegrationOscPs } from 'vs/platform/terminal/common/xterm/shellIntegrationAddon' ;
18
19
import { ITerminalContribution , ITerminalInstance , IXtermTerminal } from 'vs/workbench/contrib/terminal/browser/terminal' ;
@@ -23,9 +24,13 @@ import { ITerminalProcessManager, TERMINAL_CONFIG_SECTION, type ITerminalConfigu
23
24
import { TerminalContextKeys } from 'vs/workbench/contrib/terminal/common/terminalContextKey' ;
24
25
import { parseCompletionsFromShell , SuggestAddon , VSCodeSuggestOscPt , type CompressedPwshCompletion , type PwshCompletion } from 'vs/workbench/contrib/terminalContrib/suggest/browser/terminalSuggestAddon' ;
25
26
import { TerminalSuggestCommandId } from 'vs/workbench/contrib/terminalContrib/suggest/common/terminal.suggest' ;
26
- import { terminalSuggestConfigSection , type ITerminalSuggestConfiguration } from 'vs/workbench/contrib/terminalContrib/suggest/common/terminalSuggestConfiguration' ;
27
+ import { terminalSuggestConfigSection , TerminalSuggestSettingId , type ITerminalSuggestConfiguration } from 'vs/workbench/contrib/terminalContrib/suggest/common/terminalSuggestConfiguration' ;
27
28
import { SimpleCompletionItem } from 'vs/workbench/services/suggest/browser/simpleCompletionItem' ;
28
29
30
+ const enum Constants {
31
+ CachedPwshCommandsStorageKey = 'terminal.suggest.pwshCommands'
32
+ }
33
+
29
34
// #region Terminal Contributions
30
35
31
36
class TerminalSuggestContribution extends DisposableStore implements ITerminalContribution {
@@ -42,17 +47,37 @@ class TerminalSuggestContribution extends DisposableStore implements ITerminalCo
42
47
43
48
get addon ( ) :SuggestAddon | undefined { return this . _addon . value ; }
44
49
50
+ private static readonly _cachedPwshCommands :Set < SimpleCompletionItem > = new Set ( ) ;
51
+
45
52
constructor (
46
53
private readonly _instance :ITerminalInstance ,
47
54
processManager :ITerminalProcessManager ,
48
55
widgetManager :TerminalWidgetManager ,
49
56
@IContextKeyService private readonly _contextKeyService :IContextKeyService ,
50
57
@IConfigurationService private readonly _configurationService :IConfigurationService ,
51
- @IInstantiationService private readonly _instantiationService :IInstantiationService
58
+ @IInstantiationService private readonly _instantiationService :IInstantiationService ,
59
+ @IStorageService private readonly _storageService :IStorageService ,
52
60
) {
53
61
super ( ) ;
54
62
this . add ( toDisposable ( ( ) => this . _addon ?. dispose ( ) ) ) ;
55
63
this . _terminalSuggestWidgetVisibleContextKey = TerminalContextKeys . suggestWidgetVisible . bindTo ( this . _contextKeyService ) ;
64
+
65
+ // Attempt to load cached pwsh commands if not already loaded
66
+ if ( TerminalSuggestContribution . _cachedPwshCommands . size === 0 ) {
67
+ const config = this . _storageService . get ( Constants . CachedPwshCommandsStorageKey , StorageScope . APPLICATION , undefined ) ;
68
+ if ( config !== undefined ) {
69
+ const completions = JSON . parse ( config ) ;
70
+ for ( const c of completions ) {
71
+ TerminalSuggestContribution . _cachedPwshCommands . add ( c ) ;
72
+ }
73
+ }
74
+ }
75
+
76
+ this . add ( this . _configurationService . onDidChangeConfiguration ( e => {
77
+ if ( e . affectsConfiguration ( TerminalSuggestSettingId . Enabled ) ) {
78
+ this . clearSuggestCache ( ) ;
79
+ }
80
+ } ) ) ;
56
81
}
57
82
58
83
xtermReady ( xterm :IXtermTerminal & { raw :RawXtermTerminal } ) :void {
@@ -83,22 +108,27 @@ class TerminalSuggestContribution extends DisposableStore implements ITerminalCo
83
108
return false ;
84
109
}
85
110
86
- // TODO: These aren't persisted across reloads
87
- private _cachedPwshCommands :Set < SimpleCompletionItem > = new Set ( ) ;
88
111
private async _handleCompletionsPwshCommandsSequence ( terminal :RawXtermTerminal , data :string , command :string , args :string [ ] ) :Promise < boolean > {
89
112
const type = args [ 0 ] ;
90
113
const rawCompletions :PwshCompletion | PwshCompletion [ ] | CompressedPwshCompletion [ ] | CompressedPwshCompletion = JSON . parse ( data . slice ( command . length + type . length + 2 /*semi-colons*/ ) ) ;
91
114
const completions = parseCompletionsFromShell ( rawCompletions ) ;
92
115
93
- const set = this . _cachedPwshCommands ;
116
+ const set = TerminalSuggestContribution . _cachedPwshCommands ;
94
117
set . clear ( ) ;
95
118
for ( const c of completions ) {
96
119
set . add ( c ) ;
97
120
}
98
121
122
+ this . _storageService . store ( Constants . CachedPwshCommandsStorageKey , JSON . stringify ( Array . from ( set . values ( ) ) ) , StorageScope . APPLICATION , StorageTarget . MACHINE ) ;
123
+
99
124
return true ;
100
125
}
101
126
127
+ clearSuggestCache ( ) :void {
128
+ TerminalSuggestContribution . _cachedPwshCommands . clear ( ) ;
129
+ this . _storageService . remove ( Constants . CachedPwshCommandsStorageKey , StorageScope . APPLICATION ) ;
130
+ }
131
+
102
132
xtermOpen ( xterm :IXtermTerminal & { raw :RawXtermTerminal } ) :void {
103
133
const config = this . _configurationService . getValue < ITerminalSuggestConfiguration > ( terminalSuggestConfigSection ) ;
104
134
const enabled = config . enabled ;
@@ -127,7 +157,7 @@ class TerminalSuggestContribution extends DisposableStore implements ITerminalCo
127
157
return ;
128
158
}
129
159
if ( this . _terminalSuggestWidgetVisibleContextKey ) {
130
- this . _addon . value = this . _instantiationService . createInstance ( SuggestAddon , this . _cachedPwshCommands , this . _instance . capabilities , this . _terminalSuggestWidgetVisibleContextKey ) ;
160
+ this . _addon . value = this . _instantiationService . createInstance ( SuggestAddon , TerminalSuggestContribution . _cachedPwshCommands , this . _instance . capabilities , this . _terminalSuggestWidgetVisibleContextKey ) ;
131
161
xterm . loadAddon ( this . _addon . value ) ;
132
162
this . _addon . value . setPanel ( dom . findParentWithClass ( xterm . element ! , 'panel' ) ! ) ;
133
163
this . _addon . value . setScreen ( xterm . element ! . querySelector ( '.xterm-screen' ) ! ) ;
@@ -238,4 +268,11 @@ registerActiveInstanceAction({
238
268
run :( activeInstance ) => TerminalSuggestContribution . get ( activeInstance ) ?. addon ?. hideSuggestWidget ( )
239
269
} ) ;
240
270
271
+ registerActiveInstanceAction ( {
272
+ id :TerminalSuggestCommandId . ClearSuggestCache ,
273
+ title :localize2 ( 'workbench.action.terminal.clearSuggestCache' , 'Clear Suggest Cache' ) ,
274
+ f1 :true ,
275
+ run :( activeInstance ) => TerminalSuggestContribution . get ( activeInstance ) ?. clearSuggestCache ( )
276
+ } ) ;
277
+
241
278
// #endregion