3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
+ import { IDimension } from 'vs/base/browser/dom' ;
6
7
import { KeyCode , KeyMod } from 'vs/base/common/keyCodes' ;
8
+ import { Lazy } from 'vs/base/common/lazy' ;
9
+ import { Disposable } from 'vs/base/common/lifecycle' ;
7
10
import { localize } from 'vs/nls' ;
8
11
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey' ;
9
- import { IInstantiationService , ServicesAccessor } from 'vs/platform/instantiation/common/instantiation' ;
12
+ import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation' ;
10
13
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry' ;
11
14
import { findInFilesCommand } from 'vs/workbench/contrib/search/browser/searchActionsFind' ;
12
- import { ITerminalInstance , ITerminalService } from 'vs/workbench/contrib/terminal/browser/terminal' ;
15
+ import { ITerminalContribution , ITerminalInstance , ITerminalService , IXtermTerminal } from 'vs/workbench/contrib/terminal/browser/terminal' ;
13
16
import { registerActiveInstanceAction } from 'vs/workbench/contrib/terminal/browser/terminalActions' ;
14
- import { TerminalCommandId } from 'vs/workbench/contrib/terminal/common/terminal' ;
17
+ import { registerTerminalContribution } from 'vs/workbench/contrib/terminal/browser/terminalExtensions' ;
18
+ import { TerminalWidgetManager } from 'vs/workbench/contrib/terminal/browser/widgets/widgetManager' ;
19
+ import { ITerminalProcessManager , TerminalCommandId } from 'vs/workbench/contrib/terminal/common/terminal' ;
15
20
import { TerminalContextKeys } from 'vs/workbench/contrib/terminal/common/terminalContextKey' ;
16
21
import { TerminalFindWidget } from 'vs/workbench/contrib/terminalContrib/find/browser/terminalFindWidget' ;
22
+ import { Terminal as RawXtermTerminal } from 'xterm' ;
17
23
18
- const findWidgets : Map < ITerminalInstance , TerminalFindWidget > = new Map ( ) ;
24
+ class TerminalFindContribution extends Disposable implements ITerminalContribution {
25
+ static readonly ID = 'terminal.find' ;
19
26
20
- function getFindWidget ( instance : ITerminalInstance | undefined , accessor : ServicesAccessor ) : TerminalFindWidget | undefined {
21
- if ( instance === undefined ) {
22
- return undefined ;
27
+ static get ( instance : ITerminalInstance ) : TerminalFindContribution | null {
28
+ return instance . getContribution < TerminalFindContribution > ( TerminalFindContribution . ID ) ;
23
29
}
24
- let result = findWidgets . get ( instance ) ;
25
- if ( ! result ) {
26
- const terminalService = accessor . get ( ITerminalService ) ;
27
- const widget = accessor . get ( IInstantiationService ) . createInstance ( TerminalFindWidget , instance ) ;
28
-
29
- // Track focus and set state so we can force the scroll bar to be visible
30
- let focusState = false ;
31
- widget . focusTracker . onDidFocus ( ( ) => {
32
- focusState = true ;
33
- instance . forceScrollbarVisibility ( ) ;
34
- terminalService . setActiveInstance ( instance ) ;
35
- } ) ;
36
- widget . focusTracker . onDidBlur ( ( ) => {
37
- focusState = false ;
38
- instance . resetScrollbarVisibility ( ) ;
39
- } ) ;
40
30
41
- // Attach the find widget and listen for layout
42
- instance . registerChildElement ( {
43
- element : widget . getDomNode ( ) ,
44
- layout : dimension => widget . layout ( dimension . width ) ,
45
- xtermReady : xterm => {
46
- xterm . onDidChangeFindResults ( ( ) => widget . updateResultCount ( ) ) ;
47
- }
48
- } ) ;
31
+ private _findWidget : Lazy < TerminalFindWidget > ;
32
+ private _lastLayoutDimensions : IDimension | undefined ;
33
+ private _focusState : boolean = false ;
34
+
35
+ get findWidget ( ) : TerminalFindWidget { return this . _findWidget . value ; }
49
36
50
- // Cache the widget while the instance exists, dispose it when the terminal is disposed
51
- instance . onDisposed ( e => {
52
- const focusTerminal = focusState ;
53
- widget ?. dispose ( ) ;
54
- findWidgets . delete ( e ) ;
55
- if ( focusTerminal ) {
56
- instance . focus ( ) ;
37
+ constructor (
38
+ private readonly _instance : ITerminalInstance ,
39
+ processManager : ITerminalProcessManager ,
40
+ widgetManager : TerminalWidgetManager ,
41
+ @IInstantiationService readonly _instantiationService : IInstantiationService ,
42
+ @ITerminalService readonly _terminalService : ITerminalService
43
+ ) {
44
+ super ( ) ;
45
+
46
+ this . _findWidget = new Lazy ( ( ) => {
47
+ const findWidget = this . _instantiationService . createInstance ( TerminalFindWidget , _instance ) ;
48
+
49
+ // Track focus and set state so we can force the scroll bar to be visible
50
+ findWidget . focusTracker . onDidFocus ( ( ) => {
51
+ this . _focusState = true ;
52
+ _instance . forceScrollbarVisibility ( ) ;
53
+ _terminalService . setActiveInstance ( _instance ) ;
54
+ } ) ;
55
+ findWidget . focusTracker . onDidBlur ( ( ) => {
56
+ this . _focusState = false ;
57
+ _instance . resetScrollbarVisibility ( ) ;
58
+ } ) ;
59
+
60
+ _instance . domElement . appendChild ( findWidget . getDomNode ( ) ) ;
61
+ if ( this . _lastLayoutDimensions ) {
62
+ findWidget . layout ( this . _lastLayoutDimensions . width ) ;
57
63
}
64
+
65
+ return findWidget ;
58
66
} ) ;
67
+ }
68
+
69
+ layout ( xterm : IXtermTerminal & { raw : RawXtermTerminal } , dimension : IDimension ) : void {
70
+ this . _lastLayoutDimensions = dimension ;
71
+ this . _findWidget . rawValue ?. layout ( dimension . width ) ;
72
+ }
73
+
74
+ xtermReady ( xterm : IXtermTerminal & { raw : RawXtermTerminal } ) : void {
75
+ this . _register ( xterm . onDidChangeFindResults ( ( ) => this . _findWidget . rawValue ?. updateResultCount ( ) ) ) ;
76
+ }
77
+
78
+ override dispose ( ) {
79
+ super . dispose ( ) ;
59
80
60
- findWidgets . set ( instance , widget ) ;
61
- result = widget ;
81
+ const focusTerminal = this . _focusState ;
82
+ this . _findWidget . rawValue ?. dispose ( ) ;
83
+ if ( focusTerminal ) {
84
+ this . _instance . focus ( ) ;
85
+ }
62
86
}
63
- return result ;
87
+
64
88
}
89
+ registerTerminalContribution ( TerminalFindContribution . ID , TerminalFindContribution ) ;
65
90
66
91
registerActiveInstanceAction ( {
67
92
id : TerminalCommandId . FindFocus ,
@@ -72,8 +97,8 @@ registerActiveInstanceAction({
72
97
weight : KeybindingWeight . WorkbenchContrib
73
98
} ,
74
99
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
75
- run : ( activeInstance , c , accessor ) => {
76
- getFindWidget ( activeInstance , accessor ) ?. reveal ( ) ;
100
+ run : ( activeInstance ) => {
101
+ TerminalFindContribution . get ( activeInstance ) ?. findWidget ?. reveal ( ) ;
77
102
}
78
103
} ) ;
79
104
@@ -87,8 +112,8 @@ registerActiveInstanceAction({
87
112
weight : KeybindingWeight . WorkbenchContrib
88
113
} ,
89
114
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
90
- run : ( activeInstance , c , accessor ) => {
91
- getFindWidget ( activeInstance , accessor ) ?. hide ( ) ;
115
+ run : ( activeInstance ) => {
116
+ TerminalFindContribution . get ( activeInstance ) ?. findWidget ?. hide ( ) ;
92
117
}
93
118
} ) ;
94
119
@@ -102,8 +127,8 @@ registerActiveInstanceAction({
102
127
weight : KeybindingWeight . WorkbenchContrib
103
128
} ,
104
129
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
105
- run : ( activeInstance , c , accessor ) => {
106
- const state = getFindWidget ( activeInstance , accessor ) ?. state ;
130
+ run : ( activeInstance ) => {
131
+ const state = TerminalFindContribution . get ( activeInstance ) ?. findWidget ?. state ;
107
132
if ( state ) {
108
133
state . change ( { matchCase : ! state . isRegex } , false ) ;
109
134
}
@@ -120,8 +145,8 @@ registerActiveInstanceAction({
120
145
weight : KeybindingWeight . WorkbenchContrib
121
146
} ,
122
147
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
123
- run : ( activeInstance , c , accessor ) => {
124
- const state = getFindWidget ( activeInstance , accessor ) ?. state ;
148
+ run : ( activeInstance ) => {
149
+ const state = TerminalFindContribution . get ( activeInstance ) ?. findWidget ?. state ;
125
150
if ( state ) {
126
151
state . change ( { matchCase : ! state . wholeWord } , false ) ;
127
152
}
@@ -138,8 +163,8 @@ registerActiveInstanceAction({
138
163
weight : KeybindingWeight . WorkbenchContrib
139
164
} ,
140
165
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
141
- run : ( activeInstance , c , accessor ) => {
142
- const state = getFindWidget ( activeInstance , accessor ) ?. state ;
166
+ run : ( activeInstance ) => {
167
+ const state = TerminalFindContribution . get ( activeInstance ) ?. findWidget ?. state ;
143
168
if ( state ) {
144
169
state . change ( { matchCase : ! state . matchCase } , false ) ;
145
170
}
@@ -163,8 +188,8 @@ registerActiveInstanceAction({
163
188
}
164
189
] ,
165
190
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
166
- run : ( activeInstance , c , accessor ) => {
167
- const widget = getFindWidget ( activeInstance , accessor ) ;
191
+ run : ( activeInstance ) => {
192
+ const widget = TerminalFindContribution . get ( activeInstance ) ?. findWidget ;
168
193
if ( widget ) {
169
194
widget . show ( ) ;
170
195
widget . find ( false ) ;
@@ -189,8 +214,8 @@ registerActiveInstanceAction({
189
214
}
190
215
] ,
191
216
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
192
- run : ( activeInstance , c , accessor ) => {
193
- const widget = getFindWidget ( activeInstance , accessor ) ;
217
+ run : ( activeInstance ) => {
218
+ const widget = TerminalFindContribution . get ( activeInstance ) ?. findWidget ;
194
219
if ( widget ) {
195
220
widget . show ( ) ;
196
221
widget . find ( true ) ;
0 commit comments