@@ -30,13 +30,17 @@ define(function (require, exports, module) {
3030 EditorManager = require ( 'editor/EditorManager' ) ,
3131 ColorUtils = require ( 'utils/ColorUtils' ) ,
3232 AppInit = require ( "utils/AppInit" ) ,
33+ Editor = require ( "editor/Editor" ) . Editor ,
3334 PreferencesManager = require ( "preferences/PreferencesManager" ) ,
3435 MainViewManager = require ( "view/MainViewManager" ) ,
3536 Strings = require ( "strings" ) ;
3637
3738 // Extension variables.
3839 const COLOR_REGEX = ColorUtils . COLOR_REGEX , // used to match color
39- gutterName = "CodeMirror-colorGutter" ;
40+ GUTTER_NAME = "CodeMirror-colorGutter" ,
41+ COLOR_PREVIEW_GUTTER_PRIORITY = 200 ,
42+ COLOR_LANGUAGES = [ "css" , "scss" , "less" , "sass" , "stylus" , "html" , "svg" , "jsx" , "tsx" ,
43+ "php" , "ejs" , "erb_html" , "pug" ] ;
4044
4145
4246 // For preferences settings, to toggle this feature on/off
@@ -101,10 +105,7 @@ define(function (require, exports, module) {
101105
102106 const editor = EditorManager . getActiveEditor ( ) ;
103107 if ( editor ) {
104-
105- const aColors = _getAllColorsAndLineNums ( editor ) ;
106- showGutters ( editor , aColors ) ;
107-
108+ showGutters ( editor , _getAllColorsAndLineNums ( editor ) ) ;
108109 }
109110 }
110111
@@ -115,16 +116,13 @@ define(function (require, exports, module) {
115116 * CssColorPreview preference and set it to true
116117 */
117118 function addColorMarksToAllEditors ( ) {
118-
119119 const allActiveEditors = MainViewManager . getAllViewedEditors ( ) ;
120120
121121 allActiveEditors . forEach ( ( activeEditor ) => {
122122 const currEditor = activeEditor . editor ;
123123 if ( currEditor ) {
124-
125124 const aColors = _getAllColorsAndLineNums ( currEditor ) ;
126125 showGutters ( currEditor , aColors ) ;
127-
128126 }
129127 } ) ;
130128 }
@@ -139,8 +137,7 @@ define(function (require, exports, module) {
139137 allActiveEditors . forEach ( ( activeEditor ) => {
140138 const currEditor = activeEditor . editor ;
141139 if ( currEditor ) {
142- const cm = currEditor . _codeMirror ;
143- cm . clearGutter ( gutterName ) ;
140+ currEditor . clearGutter ( GUTTER_NAME ) ;
144141 }
145142 } ) ;
146143 }
@@ -155,7 +152,8 @@ define(function (require, exports, module) {
155152 if ( editor && enabled ) {
156153 initGutter ( editor ) ;
157154 const cm = editor . _codeMirror ;
158- cm . clearGutter ( gutterName ) ; // clear color markers
155+ editor . clearGutter ( GUTTER_NAME ) ; // clear color markers
156+ _addDummyGutterMarkerIfNotExist ( editor , editor . getCursorPos ( ) . line ) ;
159157
160158 // Only add markers if enabled
161159 if ( enabled ) {
@@ -170,7 +168,7 @@ define(function (require, exports, module) {
170168 . addClass ( "ico-cssColorPreview" )
171169 . css ( 'background-color' , obj . colorValues [ 0 ] ) ;
172170
173- cm . setGutterMarker ( obj . lineNumber , gutterName , $marker [ 0 ] ) ;
171+ editor . setGutterMarker ( obj . lineNumber , GUTTER_NAME , $marker [ 0 ] ) ;
174172 } else {
175173 // Multiple colors preview
176174 $marker = $ ( "<div>" ) . addClass ( "ico-multiple-cssColorPreview" ) ;
@@ -195,7 +193,7 @@ define(function (require, exports, module) {
195193 }
196194 } ) ;
197195
198- cm . setGutterMarker ( obj . lineNumber , gutterName , $marker [ 0 ] ) ;
196+ editor . setGutterMarker ( obj . lineNumber , GUTTER_NAME , $marker [ 0 ] ) ;
199197 }
200198 } ) ;
201199 }
@@ -209,16 +207,27 @@ define(function (require, exports, module) {
209207 * @param {activeEditor } editor
210208 */
211209 function initGutter ( editor ) {
210+ if ( ! Editor . isGutterRegistered ( GUTTER_NAME ) ) {
211+ // we should restrict the languages here to Editor.registerGutter(..., ["css", "less", "scss", etc..]);
212+ // TODO we should show the gutter in those languages only if a color is present in that file.
213+ Editor . registerGutter ( GUTTER_NAME , COLOR_PREVIEW_GUTTER_PRIORITY , COLOR_LANGUAGES ) ;
214+ }
215+ }
212216
213- const cm = editor . _codeMirror ;
214- const gutters = cm . getOption ( "gutters" ) . slice ( 0 ) ;
215- let str = gutters . join ( '' ) ;
216- if ( str . indexOf ( gutterName ) === - 1 ) {
217- gutters . unshift ( gutterName ) ;
218- cm . setOption ( "gutters" , gutters ) ;
217+ function _addDummyGutterMarkerIfNotExist ( editor , line ) {
218+ let marker = editor . getGutterMarker ( line , GUTTER_NAME ) ;
219+ if ( ! marker ) {
220+ let $marker = $ ( '<div>' )
221+ . addClass ( GUTTER_NAME ) ;
222+ editor . setGutterMarker ( line , GUTTER_NAME , $marker [ 0 ] ) ;
219223 }
220224 }
221225
226+ function _cursorActivity ( _evt , editor ) {
227+ // this is to prevent a gutter gap in the active line if there is no color on this line.
228+ _addDummyGutterMarkerIfNotExist ( editor , editor . getCursorPos ( ) . line ) ;
229+ }
230+
222231 /**
223232 * Register all the required handlers
224233 */
@@ -229,6 +238,8 @@ define(function (require, exports, module) {
229238 // Add listener for all editor changes
230239 EditorManager . on ( "activeEditorChange" , function ( event , newEditor , oldEditor ) {
231240 if ( newEditor ) {
241+ newEditor . off ( "cursorActivity.colorPreview" ) ;
242+ newEditor . on ( "cursorActivity.colorPreview" , _cursorActivity ) ;
232243 // Unbind the previous editor's change event if it exists
233244 if ( oldEditor ) {
234245 const oldCM = oldEditor . _codeMirror ;
0 commit comments