@@ -45,7 +45,11 @@ define(function (require, exports, module) {
45
45
EXPAND = "codefolding.expand" ,
46
46
EXPAND_ALL = "codefolding.expand.all" ,
47
47
gutterName = "CodeMirror-foldgutter" ,
48
- COLLAPSE_CUSTOM_REGIONS = "codefolding.collapse.customregions" ;
48
+ codeFoldingMenuDivider = "codefolding.divider" ,
49
+ collapseKey = "Ctrl-Alt-[" ,
50
+ expandKey = "Ctrl-Alt-]" ,
51
+ collapseAllKey = "Alt-1" ,
52
+ expandAllKey = "Shift-Alt-1" ;
49
53
50
54
ExtensionUtils . loadStyleSheet ( module , "main.less" ) ;
51
55
@@ -210,8 +214,12 @@ define(function (require, exports, module) {
210
214
_lineFolds = _lineFolds || { } ;
211
215
cm . _lineFolds = _lineFolds ;
212
216
var gutters = cm . getOption ( "gutters" ) . slice ( 0 ) ;
217
+
213
218
var lnIndex = gutters . indexOf ( "CodeMirror-linenumbers" ) ;
214
- gutters . splice ( lnIndex + 1 , 0 , gutterName ) ;
219
+ //reuse any existing fold gutter
220
+ if ( gutters . indexOf ( gutterName ) < 0 ) {
221
+ gutters . splice ( lnIndex + 1 , 0 , gutterName ) ;
222
+ }
215
223
cm . setOption ( "gutters" , gutters ) ;
216
224
cm . setOption ( "foldGutter" , { onGutterClick : onGutterClick } ) ;
217
225
@@ -244,6 +252,11 @@ define(function (require, exports, module) {
244
252
if ( previous ) {
245
253
saveLineFolds ( previous ) ;
246
254
}
255
+ } else {
256
+ if ( current && current . _codeMirror ) {
257
+ CodeMirror . commands . unfoldAll ( current . _codeMirror ) ;
258
+ foldGutter . remove ( current . _codeMirror ) ;
259
+ }
247
260
}
248
261
}
249
262
@@ -254,11 +267,119 @@ define(function (require, exports, module) {
254
267
saveLineFolds ( EditorManager . getCurrentFullEditor ( ) ) ;
255
268
}
256
269
270
+ /**
271
+ * Utility function to check if the code folding menu options exist in the menu
272
+ */
273
+ function menuExists ( id ) {
274
+ var viewMenu = Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) ;
275
+ return Menus . getMenuItem ( viewMenu . _getMenuItemId ( id ) ) ;
276
+ }
277
+
278
+ /**
279
+ * Create the codefolding menu items and register key bindings if they dont already exist
280
+ */
281
+ function createMenuItems ( ) {
282
+ //register commands
283
+ if ( ! CommandManager . get ( COLLAPSE_ALL ) ) {
284
+ CommandManager . register ( Strings . COLLAPSE_ALL , COLLAPSE_ALL , collapseAll ) ;
285
+ }
286
+
287
+ if ( ! CommandManager . get ( EXPAND_ALL ) ) {
288
+ CommandManager . register ( Strings . EXPAND_ALL , EXPAND_ALL , expandAll ) ;
289
+ }
290
+
291
+ if ( ! CommandManager . get ( COLLAPSE ) ) {
292
+ CommandManager . register ( Strings . COLLAPSE_CURRENT , COLLAPSE , collapseCurrent ) ;
293
+ }
294
+
295
+ if ( ! CommandManager . get ( EXPAND ) ) {
296
+ CommandManager . register ( Strings . EXPAND_CURRENT , EXPAND , expandCurrent ) ;
297
+ }
298
+
299
+ //create menus
300
+ if ( ! menuExists ( codeFoldingMenuDivider ) ) {
301
+ Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . addMenuDivider ( Menus . LAST , codeFoldingMenuDivider ) ;
302
+ }
303
+
304
+ if ( ! menuExists ( COLLAPSE_ALL ) ) {
305
+ Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . addMenuItem ( COLLAPSE_ALL ) ;
306
+ }
307
+
308
+ if ( ! menuExists ( EXPAND_ALL ) ) {
309
+ Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . addMenuItem ( EXPAND_ALL ) ;
310
+ }
311
+
312
+ if ( ! menuExists ( COLLAPSE ) ) {
313
+ Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . addMenuItem ( COLLAPSE ) ;
314
+ }
315
+
316
+ if ( ! menuExists ( EXPAND ) ) {
317
+ Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . addMenuItem ( EXPAND ) ;
318
+ }
319
+ //register keybindings
320
+ if ( KeyBindingManager . getKeyBindings ( COLLAPSE_ALL ) . length === 0 ) {
321
+ KeyBindingManager . addBinding ( COLLAPSE_ALL , collapseAllKey ) ;
322
+ }
323
+ if ( KeyBindingManager . getKeyBindings ( EXPAND_ALL ) . length === 0 ) {
324
+ KeyBindingManager . addBinding ( EXPAND_ALL , expandAllKey ) ;
325
+ }
326
+ if ( KeyBindingManager . getKeyBindings ( COLLAPSE ) . length === 0 ) {
327
+ KeyBindingManager . addBinding ( COLLAPSE , collapseKey ) ;
328
+ }
329
+ if ( KeyBindingManager . getKeyBindings ( EXPAND ) . length === 0 ) {
330
+ KeyBindingManager . addBinding ( EXPAND , expandKey ) ;
331
+ }
332
+ }
333
+
334
+ /**
335
+ * Remove the codefolding menu items and removeany key bindings attributed to them
336
+ */
337
+ function removeMenuItems ( ) {
338
+ //remove keybindings
339
+ if ( KeyBindingManager . getKeyBindings ( COLLAPSE ) . length > 0 ) {
340
+ KeyBindingManager . removeBinding ( collapseKey ) ;
341
+ }
342
+
343
+ if ( KeyBindingManager . getKeyBindings ( EXPAND ) . length > 0 ) {
344
+ KeyBindingManager . removeBinding ( expandKey ) ;
345
+ }
346
+
347
+ if ( KeyBindingManager . getKeyBindings ( COLLAPSE_ALL ) . length > 0 ) {
348
+ KeyBindingManager . removeBinding ( collapseAllKey ) ;
349
+ }
350
+
351
+ if ( KeyBindingManager . getKeyBindings ( EXPAND_ALL ) . length > 0 ) {
352
+ KeyBindingManager . removeBinding ( expandAllKey ) ;
353
+ }
354
+
355
+ //remove menus
356
+ if ( menuExists ( codeFoldingMenuDivider ) ) {
357
+ Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . removeMenuDivider ( codeFoldingMenuDivider ) ;
358
+ }
359
+
360
+ if ( menuExists ( COLLAPSE ) ) {
361
+ Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . removeMenuItem ( COLLAPSE ) ;
362
+ }
363
+
364
+ if ( menuExists ( EXPAND ) ) {
365
+ Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . removeMenuItem ( EXPAND ) ;
366
+ }
367
+
368
+ if ( menuExists ( COLLAPSE_ALL ) ) {
369
+ Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . removeMenuItem ( COLLAPSE_ALL ) ;
370
+ }
371
+
372
+ if ( menuExists ( EXPAND_ALL ) ) {
373
+ Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . removeMenuItem ( EXPAND_ALL ) ;
374
+ }
375
+ }
376
+
257
377
/**
258
378
Initialise the extension
259
379
*/
260
380
function init ( ) {
261
381
if ( ! prefs . getSetting ( "enabled" ) ) {
382
+ removeMenuItems ( ) ;
262
383
return ;
263
384
}
264
385
foldCode . init ( ) ;
@@ -287,27 +408,12 @@ define(function (require, exports, module) {
287
408
init ( ) ;
288
409
} ) ;
289
410
290
- CommandManager . register ( Strings . COLLAPSE_ALL , COLLAPSE_ALL , collapseAll ) ;
291
- CommandManager . register ( Strings . EXPAND_ALL , EXPAND_ALL , expandAll ) ;
292
-
293
- CommandManager . register ( Strings . COLLAPSE_CURRENT , COLLAPSE , collapseCurrent ) ;
294
- CommandManager . register ( Strings . EXPAND_CURRENT , EXPAND , expandCurrent ) ;
295
-
296
- Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . addMenuDivider ( ) ;
297
- Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . addMenuItem ( COLLAPSE ) ;
298
- Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . addMenuItem ( EXPAND ) ;
299
- Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . addMenuItem ( COLLAPSE_ALL ) ;
300
- Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . addMenuItem ( EXPAND_ALL ) ;
301
- Menus . getMenu ( Menus . AppMenuBar . VIEW_MENU ) . addMenuItem ( COLLAPSE_CUSTOM_REGIONS ) ;
302
-
303
- KeyBindingManager . addBinding ( COLLAPSE , "Ctrl-Alt-[" ) ;
304
- KeyBindingManager . addBinding ( EXPAND , "Ctrl-Alt-]" ) ;
305
- KeyBindingManager . addBinding ( COLLAPSE_ALL , "Alt-1" ) ;
306
- KeyBindingManager . addBinding ( EXPAND_ALL , "Shift-Alt-1" ) ;
411
+ createMenuItems ( ) ;
307
412
308
413
var editor = EditorManager . getCurrentFullEditor ( ) ;
309
414
if ( editor ) {
310
415
var cm = editor . _codeMirror ;
416
+ createGutter ( editor ) ;
311
417
if ( prefs . getSetting ( "fadeFoldButtons" ) ) {
312
418
foldGutter . clearGutter ( cm ) ;
313
419
} else {
@@ -316,5 +422,28 @@ define(function (require, exports, module) {
316
422
}
317
423
}
318
424
319
- AppInit . appReady ( init ) ;
425
+ /**
426
+ * Register change listener for the preferences file.
427
+ */
428
+ function watchPrefsForChanges ( ) {
429
+ prefs . prefBase . on ( "change" , function ( e , data ) {
430
+ if ( data . ids . indexOf ( "enabled" ) > - 1 ) {
431
+ if ( prefs . getSetting ( "enabled" ) ) {
432
+ init ( ) ;
433
+ } else {
434
+ var editor = EditorManager . getCurrentFullEditor ( ) ;
435
+ if ( editor && editor . _codeMirror && CodeMirror . commands . unfoldAll ) {
436
+ CodeMirror . commands . unfoldAll ( editor . _codeMirror ) ;
437
+ foldGutter . remove ( editor . _codeMirror ) ;
438
+ }
439
+ removeMenuItems ( ) ;
440
+ }
441
+ }
442
+ } ) ;
443
+ }
444
+
445
+ AppInit . appReady ( function ( ) {
446
+ init ( ) ;
447
+ watchPrefsForChanges ( ) ;
448
+ } ) ;
320
449
} ) ;
0 commit comments