Skip to content

Commit 9f00288

Browse files
committed
fix: gutter gaps in color gutter and move to editor from cm gutter apis
1 parent 98b056f commit 9f00288

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

src/extensionsIntegrated/CSSColorPreview/main.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
// These styles are for CssColorPreview feature
22
// Check `extensionsIntegrated/CSSColorPreview for more reference
33
/* Single color preview */
4+
5+
.CodeMirror .CodeMirror-colorGutter {
6+
width: 1em;
7+
background-color: transparent;
8+
text-align: center;
9+
}
10+
411
.ico-cssColorPreview {
512
border: solid 1px rgba(255, 255, 255, 0.2);
613
.dark & {
@@ -10,7 +17,6 @@
1017
top: .1em;
1118
height:.7em;
1219
width: .7em;
13-
margin-left: .15em;
1420
position: relative;
1521
}
1622

@@ -22,16 +28,15 @@
2228
}
2329
position: relative;
2430
display: inline-block;
25-
top: .1em;
26-
width: .7em;
27-
height:.7em;
28-
margin-left: .15em;
31+
top: .2em;
32+
width: .9em;
33+
height:.9em;
2934
}
3035

3136
/* For individual color boxes, they will be added inside the main .ico-multiple-cssColorPreview div */
3237
.ico-multiple-cssColorPreview .color-box {
3338
position: absolute;
34-
width: .3em;
35-
height: .3em;
39+
width: .4em;
40+
height: .4em;
3641
box-sizing: border-box;
3742
}

src/styles/brackets.less

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2962,6 +2962,7 @@ textarea.exclusions-editor {
29622962
.CodeMirror .code-inspection-gutter {
29632963
width: 1em;
29642964
background-color: transparent;
2965+
text-align: center;
29652966
}
29662967

29672968
.brackets-inspection-gutter-marker {
@@ -2970,7 +2971,7 @@ textarea.exclusions-editor {
29702971
line-height: 1em;
29712972
height: 1em;
29722973
width: 1em;
2973-
font-size: .8em;
2974+
font-size: .7em;
29742975
pointer-events: auto;
29752976
}
29762977

0 commit comments

Comments
 (0)