@@ -26,15 +26,14 @@ import { ILanguageConfigurationService } from 'vs/editor/common/languages/langua
26
26
import { CollapseMemento , FoldingModel , getNextFoldLine , getParentFoldLine as getParentFoldLine , getPreviousFoldLine , setCollapseStateAtLevel , setCollapseStateForMatchingLines , setCollapseStateForRest , setCollapseStateForType , setCollapseStateLevelsDown , setCollapseStateLevelsUp , setCollapseStateUp , toggleCollapseState } from 'vs/editor/contrib/folding/browser/foldingModel' ;
27
27
import { HiddenRangeModel } from 'vs/editor/contrib/folding/browser/hiddenRangeModel' ;
28
28
import { IndentRangeProvider } from 'vs/editor/contrib/folding/browser/indentRangeProvider' ;
29
- import { ID_INIT_PROVIDER , InitializingRangeProvider } from 'vs/editor/contrib/folding/browser/intializingRangeProvider' ;
30
29
import * as nls from 'vs/nls' ;
31
30
import { IContextKey , IContextKeyService , RawContextKey } from 'vs/platform/contextkey/common/contextkey' ;
32
31
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry' ;
33
32
import { editorSelectionBackground , iconForeground , registerColor , transparent } from 'vs/platform/theme/common/colorRegistry' ;
34
33
import { registerThemingParticipant , ThemeIcon } from 'vs/platform/theme/common/themeService' ;
35
- import { foldingCollapsedIcon , FoldingDecorationProvider , foldingExpandedIcon } from './foldingDecorations' ;
36
- import { FoldingRegion , FoldingRegions } from './foldingRanges' ;
37
- import { ID_SYNTAX_PROVIDER , SyntaxRangeProvider } from './syntaxRangeProvider' ;
34
+ import { foldingCollapsedIcon , FoldingDecorationProvider , foldingExpandedIcon , foldingManualIcon } from './foldingDecorations' ;
35
+ import { FoldingRegion , FoldingRegions , FoldRange } from './foldingRanges' ;
36
+ import { SyntaxRangeProvider } from './syntaxRangeProvider' ;
38
37
import { INotificationService } from 'vs/platform/notification/common/notification' ;
39
38
import Severity from 'vs/base/common/severity' ;
40
39
import { IFeatureDebounceInformation , ILanguageFeatureDebounceService } from 'vs/editor/common/services/languageFeatureDebounce' ;
@@ -84,8 +83,6 @@ export class FoldingController extends Disposable implements IEditorContribution
84
83
private rangeProvider : RangeProvider | null ;
85
84
private foldingRegionPromise : CancelablePromise < FoldingRegions | null > | null ;
86
85
87
- private foldingStateMemento : FoldingStateMemento | null ;
88
-
89
86
private foldingModelPromise : Promise < FoldingModel | null > | null ;
90
87
private updateScheduler : Delayer < FoldingModel | null > | null ;
91
88
private readonly updateDebounceInfo : IFeatureDebounceInformation ;
@@ -120,7 +117,6 @@ export class FoldingController extends Disposable implements IEditorContribution
120
117
this . hiddenRangeModel = null ;
121
118
this . rangeProvider = null ;
122
119
this . foldingRegionPromise = null ;
123
- this . foldingStateMemento = null ;
124
120
this . foldingModelPromise = null ;
125
121
this . updateScheduler = null ;
126
122
this . cursorChangedScheduler = null ;
@@ -186,7 +182,7 @@ export class FoldingController extends Disposable implements IEditorContribution
186
182
return { } ;
187
183
}
188
184
if ( this . foldingModel ) { // disposed ?
189
- const collapsedRegions = this . foldingModel . isInitialized ? this . foldingModel . getMemento ( ) : this . hiddenRangeModel ! . getMemento ( ) ;
185
+ const collapsedRegions = this . foldingModel . getMemento ( ) ;
190
186
const provider = this . rangeProvider ? this . rangeProvider . id : undefined ;
191
187
return { collapsedRegions, lineCount : model . getLineCount ( ) , provider, foldedImports : this . _currentModelHasFoldedImports } ;
192
188
}
@@ -206,29 +202,12 @@ export class FoldingController extends Disposable implements IEditorContribution
206
202
}
207
203
208
204
this . _currentModelHasFoldedImports = ! ! state . foldedImports ;
209
- if ( ! state . collapsedRegions ) {
210
- return ;
211
- }
212
-
213
- if ( state . provider === ID_SYNTAX_PROVIDER || state . provider === ID_INIT_PROVIDER ) {
214
- this . foldingStateMemento = state ;
215
- }
216
-
217
- const collapsedRegions = state . collapsedRegions ;
218
- // set the hidden ranges right away, before waiting for the folding model.
219
- if ( this . hiddenRangeModel . applyMemento ( collapsedRegions ) ) {
220
- const foldingModel = this . getFoldingModel ( ) ;
221
- if ( foldingModel ) {
222
- foldingModel . then ( foldingModel => {
223
- if ( foldingModel ) {
224
- this . _restoringViewState = true ;
225
- try {
226
- foldingModel . applyMemento ( collapsedRegions ) ;
227
- } finally {
228
- this . _restoringViewState = false ;
229
- }
230
- }
231
- } ) . then ( undefined , onUnexpectedError ) ;
205
+ if ( state . collapsedRegions && state . collapsedRegions . length > 0 && this . foldingModel ) {
206
+ this . _restoringViewState = true ;
207
+ try {
208
+ this . foldingModel . applyMemento ( state . collapsedRegions ! ) ;
209
+ } finally {
210
+ this . _restoringViewState = false ;
232
211
}
233
212
}
234
213
}
@@ -243,7 +222,7 @@ export class FoldingController extends Disposable implements IEditorContribution
243
222
}
244
223
245
224
this . _currentModelHasFoldedImports = false ;
246
- this . foldingModel = new FoldingModel ( model , this . foldingDecorationProvider ) ;
225
+ this . foldingModel = new FoldingModel ( model , this . foldingDecorationProvider , this . triggerFoldingModelChanged . bind ( this ) ) ;
247
226
this . localToDispose . add ( this . foldingModel ) ;
248
227
249
228
this . hiddenRangeModel = new HiddenRangeModel ( this . foldingModel ) ;
@@ -274,7 +253,6 @@ export class FoldingController extends Disposable implements IEditorContribution
274
253
this . foldingModelPromise = null ;
275
254
this . hiddenRangeModel = null ;
276
255
this . cursorChangedScheduler = null ;
277
- this . foldingStateMemento = null ;
278
256
if ( this . rangeProvider ) {
279
257
this . rangeProvider . dispose ( ) ;
280
258
}
@@ -297,21 +275,12 @@ export class FoldingController extends Disposable implements IEditorContribution
297
275
return this . rangeProvider ;
298
276
}
299
277
this . rangeProvider = new IndentRangeProvider ( editorModel , this . languageConfigurationService , this . _maxFoldingRegions ) ; // fallback
300
-
301
278
if ( this . _useFoldingProviders && this . foldingModel ) {
302
279
const foldingProviders = this . languageFeaturesService . foldingRangeProvider . ordered ( this . foldingModel . textModel ) ;
303
- if ( foldingProviders . length === 0 && this . foldingStateMemento && this . foldingStateMemento . collapsedRegions ) {
304
- const rangeProvider = this . rangeProvider = new InitializingRangeProvider ( editorModel , this . foldingStateMemento . collapsedRegions , ( ) => {
305
- // if after 30 the InitializingRangeProvider is still not replaced, force a refresh
306
- this . foldingStateMemento = null ;
307
- this . onFoldingStrategyChanged ( ) ;
308
- } , 30000 ) ;
309
- return rangeProvider ; // keep memento in case there are still no foldingProviders on the next request.
310
- } else if ( foldingProviders . length > 0 ) {
280
+ if ( foldingProviders . length > 0 ) {
311
281
this . rangeProvider = new SyntaxRangeProvider ( editorModel , foldingProviders , ( ) => this . triggerFoldingModelChanged ( ) , this . _maxFoldingRegions ) ;
312
282
}
313
283
}
314
- this . foldingStateMemento = null ;
315
284
return this . rangeProvider ;
316
285
}
317
286
@@ -1097,6 +1066,59 @@ class GotoNextFoldAction extends FoldingAction<void> {
1097
1066
}
1098
1067
}
1099
1068
1069
+ class FoldSelectedAction extends FoldingAction < void > {
1070
+
1071
+ constructor ( ) {
1072
+ super ( {
1073
+ id : 'editor.foldSelected' ,
1074
+ label : nls . localize ( 'foldSelectedAction.label' , "Fold Selected Lines" ) ,
1075
+ alias : 'Fold Selected Lines' ,
1076
+ precondition : CONTEXT_FOLDING_ENABLED ,
1077
+ kbOpts : {
1078
+ kbExpr : EditorContextKeys . editorTextFocus ,
1079
+ primary : KeyChord ( KeyMod . CtrlCmd | KeyCode . KeyK , KeyMod . CtrlCmd | KeyCode . Period ) ,
1080
+ weight : KeybindingWeight . EditorContrib
1081
+ }
1082
+ } ) ;
1083
+ }
1084
+
1085
+ invoke ( _foldingController : FoldingController , foldingModel : FoldingModel , editor : ICodeEditor ) : void {
1086
+ const collapseRanges : FoldRange [ ] = [ ] ;
1087
+ const selections = editor . getSelections ( ) ;
1088
+ if ( selections ) {
1089
+ for ( const selection of selections ) {
1090
+ let endLineNumber = selection . endLineNumber ;
1091
+ if ( selection . endColumn === 1 ) {
1092
+ -- endLineNumber ;
1093
+ }
1094
+ if ( endLineNumber > selection . startLineNumber ) {
1095
+ collapseRanges . push ( < FoldRange > {
1096
+ startLineNumber : selection . startLineNumber ,
1097
+ endLineNumber : endLineNumber ,
1098
+ type : undefined ,
1099
+ isCollapsed : true ,
1100
+ isManualSelection : true
1101
+ } ) ;
1102
+ editor . setSelection ( {
1103
+ startLineNumber : selection . startLineNumber ,
1104
+ startColumn : 1 ,
1105
+ endLineNumber : selection . startLineNumber ,
1106
+ endColumn : 1
1107
+ } ) ;
1108
+ }
1109
+ }
1110
+ if ( collapseRanges . length > 0 ) {
1111
+ collapseRanges . sort ( ( a , b ) => {
1112
+ return a . startLineNumber - b . startLineNumber ;
1113
+ } ) ;
1114
+ const newRanges = FoldingRegions . sanitizeAndMerge ( foldingModel . regions , collapseRanges , editor . getModel ( ) ?. getLineCount ( ) ) ;
1115
+ foldingModel . updatePost ( FoldingRegions . fromFoldRanges ( newRanges ) ) ;
1116
+ }
1117
+ }
1118
+ }
1119
+ }
1120
+
1121
+
1100
1122
registerEditorContribution ( FoldingController . ID , FoldingController ) ;
1101
1123
registerEditorAction ( UnfoldAction ) ;
1102
1124
registerEditorAction ( UnFoldRecursivelyAction ) ;
@@ -1113,6 +1135,7 @@ registerEditorAction(ToggleFoldAction);
1113
1135
registerEditorAction ( GotoParentFoldAction ) ;
1114
1136
registerEditorAction ( GotoPreviousFoldAction ) ;
1115
1137
registerEditorAction ( GotoNextFoldAction ) ;
1138
+ registerEditorAction ( FoldSelectedAction ) ;
1116
1139
1117
1140
for ( let i = 1 ; i <= 7 ; i ++ ) {
1118
1141
registerInstantiatedEditorAction (
@@ -1143,7 +1166,8 @@ registerThemingParticipant((theme, collector) => {
1143
1166
if ( editorFoldColor ) {
1144
1167
collector . addRule ( `
1145
1168
.monaco-editor .cldr${ ThemeIcon . asCSSSelector ( foldingExpandedIcon ) } ,
1146
- .monaco-editor .cldr${ ThemeIcon . asCSSSelector ( foldingCollapsedIcon ) } {
1169
+ .monaco-editor .cldr${ ThemeIcon . asCSSSelector ( foldingCollapsedIcon ) } ,
1170
+ .monaco-editor .cldr${ ThemeIcon . asCSSSelector ( foldingManualIcon ) } {
1147
1171
color: ${ editorFoldColor } !important;
1148
1172
}
1149
1173
` ) ;
0 commit comments