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
+
34
+ get findWidget ( ) : TerminalFindWidget { return this . _findWidget . value ; }
49
35
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 ( ) ;
36
+ constructor (
37
+ private readonly _instance : ITerminalInstance ,
38
+ processManager : ITerminalProcessManager ,
39
+ widgetManager : TerminalWidgetManager ,
40
+ @IInstantiationService instantiationService : IInstantiationService ,
41
+ @ITerminalService terminalService : ITerminalService
42
+ ) {
43
+ super ( ) ;
44
+
45
+ this . _findWidget = new Lazy ( ( ) => {
46
+ const findWidget = instantiationService . createInstance ( TerminalFindWidget , this . _instance ) ;
47
+
48
+ // Track focus and set state so we can force the scroll bar to be visible
49
+ findWidget . focusTracker . onDidFocus ( ( ) => {
50
+ this . _instance . forceScrollbarVisibility ( ) ;
51
+ terminalService . setActiveInstance ( this . _instance ) ;
52
+ } ) ;
53
+ findWidget . focusTracker . onDidBlur ( ( ) => {
54
+ this . _instance . resetScrollbarVisibility ( ) ;
55
+ } ) ;
56
+
57
+ this . _instance . domElement . appendChild ( findWidget . getDomNode ( ) ) ;
58
+ if ( this . _lastLayoutDimensions ) {
59
+ findWidget . layout ( this . _lastLayoutDimensions . width ) ;
57
60
}
61
+
62
+ return findWidget ;
58
63
} ) ;
64
+ }
65
+
66
+ layout ( xterm : IXtermTerminal & { raw : RawXtermTerminal } , dimension : IDimension ) : void {
67
+ this . _lastLayoutDimensions = dimension ;
68
+ this . _findWidget . rawValue ?. layout ( dimension . width ) ;
69
+ }
70
+
71
+ xtermReady ( xterm : IXtermTerminal & { raw : RawXtermTerminal } ) : void {
72
+ this . _register ( xterm . onDidChangeFindResults ( ( ) => this . _findWidget . rawValue ?. updateResultCount ( ) ) ) ;
73
+ }
59
74
60
- findWidgets . set ( instance , widget ) ;
61
- result = widget ;
75
+ override dispose ( ) {
76
+ super . dispose ( ) ;
77
+ this . _findWidget . rawValue ?. dispose ( ) ;
62
78
}
63
- return result ;
79
+
64
80
}
81
+ registerTerminalContribution ( TerminalFindContribution . ID , TerminalFindContribution ) ;
65
82
66
83
registerActiveInstanceAction ( {
67
84
id : TerminalCommandId . FindFocus ,
@@ -72,8 +89,8 @@ registerActiveInstanceAction({
72
89
weight : KeybindingWeight . WorkbenchContrib
73
90
} ,
74
91
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
75
- run : ( activeInstance , c , accessor ) => {
76
- getFindWidget ( activeInstance , accessor ) ? .reveal ( ) ;
92
+ run : ( activeInstance ) => {
93
+ TerminalFindContribution . get ( activeInstance ) ?. findWidget . reveal ( ) ;
77
94
}
78
95
} ) ;
79
96
@@ -87,8 +104,8 @@ registerActiveInstanceAction({
87
104
weight : KeybindingWeight . WorkbenchContrib
88
105
} ,
89
106
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
90
- run : ( activeInstance , c , accessor ) => {
91
- getFindWidget ( activeInstance , accessor ) ? .hide ( ) ;
107
+ run : ( activeInstance ) => {
108
+ TerminalFindContribution . get ( activeInstance ) ?. findWidget . hide ( ) ;
92
109
}
93
110
} ) ;
94
111
@@ -102,11 +119,9 @@ registerActiveInstanceAction({
102
119
weight : KeybindingWeight . WorkbenchContrib
103
120
} ,
104
121
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
105
- run : ( activeInstance , c , accessor ) => {
106
- const state = getFindWidget ( activeInstance , accessor ) ?. state ;
107
- if ( state ) {
108
- state . change ( { matchCase : ! state . isRegex } , false ) ;
109
- }
122
+ run : ( activeInstance ) => {
123
+ const state = TerminalFindContribution . get ( activeInstance ) ?. findWidget . state ;
124
+ state ?. change ( { matchCase : ! state . isRegex } , false ) ;
110
125
}
111
126
} ) ;
112
127
@@ -120,11 +135,9 @@ registerActiveInstanceAction({
120
135
weight : KeybindingWeight . WorkbenchContrib
121
136
} ,
122
137
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
123
- run : ( activeInstance , c , accessor ) => {
124
- const state = getFindWidget ( activeInstance , accessor ) ?. state ;
125
- if ( state ) {
126
- state . change ( { matchCase : ! state . wholeWord } , false ) ;
127
- }
138
+ run : ( activeInstance ) => {
139
+ const state = TerminalFindContribution . get ( activeInstance ) ?. findWidget . state ;
140
+ state ?. change ( { matchCase : ! state . wholeWord } , false ) ;
128
141
}
129
142
} ) ;
130
143
@@ -138,11 +151,9 @@ registerActiveInstanceAction({
138
151
weight : KeybindingWeight . WorkbenchContrib
139
152
} ,
140
153
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
141
- run : ( activeInstance , c , accessor ) => {
142
- const state = getFindWidget ( activeInstance , accessor ) ?. state ;
143
- if ( state ) {
144
- state . change ( { matchCase : ! state . matchCase } , false ) ;
145
- }
154
+ run : ( activeInstance ) => {
155
+ const state = TerminalFindContribution . get ( activeInstance ) ?. findWidget . state ;
156
+ state ?. change ( { matchCase : ! state . matchCase } , false ) ;
146
157
}
147
158
} ) ;
148
159
@@ -163,8 +174,8 @@ registerActiveInstanceAction({
163
174
}
164
175
] ,
165
176
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
166
- run : ( activeInstance , c , accessor ) => {
167
- const widget = getFindWidget ( activeInstance , accessor ) ;
177
+ run : ( activeInstance ) => {
178
+ const widget = TerminalFindContribution . get ( activeInstance ) ?. findWidget ;
168
179
if ( widget ) {
169
180
widget . show ( ) ;
170
181
widget . find ( false ) ;
@@ -189,8 +200,8 @@ registerActiveInstanceAction({
189
200
}
190
201
] ,
191
202
precondition : ContextKeyExpr . or ( TerminalContextKeys . processSupported , TerminalContextKeys . terminalHasBeenCreated ) ,
192
- run : ( activeInstance , c , accessor ) => {
193
- const widget = getFindWidget ( activeInstance , accessor ) ;
203
+ run : ( activeInstance ) => {
204
+ const widget = TerminalFindContribution . get ( activeInstance ) ?. findWidget ;
194
205
if ( widget ) {
195
206
widget . show ( ) ;
196
207
widget . find ( true ) ;
0 commit comments