@@ -36,7 +36,8 @@ define(function (require, exports, module) {
3636
3737 const COMMAND_NAME = Strings . CMD_TOGGLE_INDENT_GUIDES ,
3838 COMMAND_ID = Commands . TOGGLE_INDENT_GUIDES ,
39- GUIDE_CLASS = "phcode-indent-guides" ;
39+ GUIDE_CLASS = "phcode-indent-guides" ,
40+ GUIDE_CLASS_NONE = "phcode-indent-guides-none" ;
4041
4142 const PREFERENCES_EDITOR_INDENT_GUIDES = "editor.indentGuides" ,
4243 PREFERENCES_EDITOR_INDENT_HIDE_FIRST = "editor.indentHideFirst" ;
@@ -54,69 +55,89 @@ define(function (require, exports, module) {
5455 } ) ;
5556
5657 // CodeMirror overlay code
57- const indentGuidesOverlay = {
58- token : function ( stream , _state ) {
59- let char = "" ,
60- colNum = 0 ,
61- spaceUnits = 0 ,
62- isTabStart = false ;
63-
64- char = stream . next ( ) ;
65- colNum = stream . column ( ) ;
66-
67- // Check for "hide first guide" preference
68- if ( ( hideFirst ) && ( colNum === 0 ) ) {
69- return null ;
70- }
58+ function _createOverlayObject ( spaceUnits ) {
59+ return {
60+ _spaceUnits : spaceUnits ,
61+ _cmRefreshPending : false ,
62+ token : function ( stream , _state ) {
63+ let char = "" ,
64+ colNum = 0 ,
65+ isTabStart = false ;
66+
67+ char = stream . next ( ) ;
68+ colNum = stream . column ( ) ;
69+
70+ // Check for "hide first guide" preference
71+ if ( ( hideFirst ) && ( colNum === 0 ) ) {
72+ return null ;
73+ }
7174
72- if ( char === "\t" ) {
73- return GUIDE_CLASS ;
74- }
75+ if ( char === "\t" ) {
76+ return enabled ? GUIDE_CLASS : GUIDE_CLASS_NONE ;
77+ }
7578
76- if ( char !== " " ) {
77- stream . skipToEnd ( ) ;
78- return null ;
79- }
79+ if ( char !== " " ) {
80+ stream . skipToEnd ( ) ;
81+ return null ;
82+ }
8083
81- spaceUnits = Editor . getSpaceUnits ( ) ;
82- isTabStart = ( colNum % spaceUnits ) ? false : true ;
84+ isTabStart = ( colNum % this . _spaceUnits ) === 0 ? true : false ;
8385
84- if ( ( char === " " ) && ( isTabStart ) ) {
85- return GUIDE_CLASS ;
86- }
87- return null ;
88- } ,
89- flattenSpans : false
90- } ;
86+ if ( ( char === " " ) && ( isTabStart ) ) {
87+ return enabled ? GUIDE_CLASS : GUIDE_CLASS_NONE ;
88+ }
89+ return null ;
90+ } ,
91+ flattenSpans : false
92+ } ;
93+ }
9194
9295 function applyPreferences ( ) {
9396 enabled = PreferencesManager . get ( PREFERENCES_EDITOR_INDENT_GUIDES ) ;
9497 hideFirst = PreferencesManager . get ( PREFERENCES_EDITOR_INDENT_HIDE_FIRST ) ;
9598 }
9699
97100 function updateUI ( ) {
98- const editor = EditorManager . getActiveEditor ( ) ,
99- cm = editor ? editor . _codeMirror : null ;
100-
101- // Update CodeMirror overlay if editor is available
102- if ( cm ) {
103- if ( editor . _overlayPresent ) {
104- if ( ! enabled ) {
105- cm . removeOverlay ( indentGuidesOverlay ) ;
106- editor . _overlayPresent = false ;
107- cm . refresh ( ) ;
101+ const editor = EditorManager . getActiveEditor ( ) ;
102+ if ( ! editor || ! editor . _codeMirror ) {
103+ return ;
104+ }
105+ const cm = editor . _codeMirror ;
106+ if ( ! editor . __indentGuidesOverlay ) {
107+ editor . __indentGuidesOverlay = _createOverlayObject ( Editor . getSpaceUnits ( editor . document . file . fullPath ) ) ;
108+ }
109+ function _reRenderOverlay ( ) {
110+ cm . removeOverlay ( editor . __indentGuidesOverlay ) ;
111+ cm . addOverlay ( editor . __indentGuidesOverlay ) ;
112+ }
113+ editor . off ( Editor . EVENT_OPTION_CHANGE + ".indentGuide" ) ;
114+ editor . on ( Editor . EVENT_OPTION_CHANGE + ".indentGuide" , ( ) => {
115+ const newSpaceUnits = Editor . getSpaceUnits ( editor . document . file . fullPath ) ;
116+ if ( newSpaceUnits !== editor . __indentGuidesOverlay . _spaceUnits ) {
117+ editor . __indentGuidesOverlay . _spaceUnits = newSpaceUnits ;
118+ if ( EditorManager . getActiveEditor ( ) === editor ) {
119+ console . log ( "space units changed, refreshing indent guides for" ,
120+ newSpaceUnits , editor . document . file . fullPath ) ;
121+ _reRenderOverlay ( ) ;
108122 }
109- } else if ( enabled ) {
110- cm . removeOverlay ( indentGuidesOverlay ) ;
111- cm . addOverlay ( indentGuidesOverlay ) ;
112- editor . _overlayPresent = true ;
113- cm . refresh ( ) ;
114123 }
124+ } ) ;
125+
126+ let shouldRerender = false ;
127+ if ( ! cm . __indentGuidesOverlayAttached ) {
128+ cm . addOverlay ( editor . __indentGuidesOverlay ) ;
129+ cm . __indentGuidesOverlayAttached = editor . __indentGuidesOverlay ;
130+ cm . __overlayEnabled = enabled ;
131+ } else if ( cm . __indentGuidesOverlayAttached &&
132+ cm . __indentGuidesOverlayAttached !== editor . __indentGuidesOverlay ) {
133+ cm . removeOverlay ( cm . __indentGuidesOverlayAttached ) ;
134+ cm . addOverlay ( editor . __indentGuidesOverlay ) ;
135+ cm . __overlayEnabled = enabled ;
136+ } else if ( shouldRerender || cm . __overlayEnabled !== enabled ) {
137+ cm . __overlayEnabled = enabled ;
138+ _reRenderOverlay ( ) ;
139+ console . log ( "Refreshing indent guides" ) ;
115140 }
116-
117- // Update menu
118- CommandManager . get ( COMMAND_ID )
119- . setChecked ( enabled ) ;
120141 }
121142
122143 function handleToggleGuides ( ) {
@@ -127,6 +148,8 @@ define(function (require, exports, module) {
127148 function preferenceChanged ( ) {
128149 applyPreferences ( ) ;
129150 updateUI ( ) ;
151+ CommandManager . get ( COMMAND_ID )
152+ . setChecked ( enabled ) ;
130153 }
131154
132155 // Initialize extension
0 commit comments