Skip to content

Commit 06c04f7

Browse files
committed
chore: color gutter optimize
1 parent ec7064d commit 06c04f7

File tree

3 files changed

+49
-78
lines changed

3 files changed

+49
-78
lines changed

src/editor/Editor.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,12 +2520,8 @@ define(function (require, exports, module) {
25202520
}
25212521
};
25222522

2523-
/**
2524-
* Renders all registered gutters
2525-
* @private
2526-
*/
2527-
Editor.prototype._renderGutters = function () {
2528-
var languageId = this.document.getLanguage().getId();
2523+
Editor.prototype._getRegisteredGutters = function () {
2524+
const languageId = this.document.getLanguage().getId();
25292525

25302526
function _filterByLanguages(gutter) {
25312527
return !gutter.languages || gutter.languages.indexOf(languageId) > -1;
@@ -2539,19 +2535,26 @@ define(function (require, exports, module) {
25392535
return gutter.name;
25402536
}
25412537

2542-
var gutters = registeredGutters.map(_getName),
2543-
rootElement = this.getRootElement();
2544-
25452538
// If the line numbers gutter has not been explicitly registered and the CodeMirror lineNumbes option is
25462539
// set to true, we explicitly add the line numbers gutter. This case occurs the first time the editor loads
25472540
// and showLineNumbers is set to true in preferences
2541+
const gutters = registeredGutters.map(_getName);
25482542
if (gutters.indexOf(LINE_NUMBER_GUTTER) < 0 && this._codeMirror.getOption(cmOptions[SHOW_LINE_NUMBERS])) {
25492543
registeredGutters.push({ name: LINE_NUMBER_GUTTER, priority: LINE_NUMBER_GUTTER_PRIORITY });
25502544
}
25512545

2552-
gutters = registeredGutters.sort(_sortByPriority)
2546+
return registeredGutters.sort(_sortByPriority)
25532547
.filter(_filterByLanguages)
25542548
.map(_getName);
2549+
};
2550+
2551+
/**
2552+
* Renders all registered gutters
2553+
* @private
2554+
*/
2555+
Editor.prototype._renderGutters = function () {
2556+
const rootElement = this.getRootElement();
2557+
const gutters = this._getRegisteredGutters();
25552558

25562559
this._codeMirror.setOption("gutters", gutters);
25572560
this._codeMirror.refresh();
@@ -2603,6 +2606,16 @@ define(function (require, exports, module) {
26032606
this.setGutterMarker(lineNumber, gutterName, null);
26042607
};
26052608

2609+
/**
2610+
* Returns true if this editor has the named gutter activated. gutters are considered active if the gutter is
2611+
* registered for the language of the file currently shown in the editor.
2612+
* @param {string} gutterName The name of the gutter to check
2613+
*/
2614+
Editor.prototype.isGutterActive = function (gutterName) {
2615+
const gutters = this._getRegisteredGutters();
2616+
return gutters.includes(gutterName);
2617+
};
2618+
26062619
/**
26072620
* Clears all marks from the gutter with the specified name.
26082621
* @param {string} gutterName The name of the gutter to clear.

src/extensionsIntegrated/CSSColorPreview/main.js

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ define(function (require, exports, module) {
100100
*/
101101
function showColorMarks() {
102102
if (!enabled) {
103-
removeColorMarks();
104103
return;
105104
}
106105

@@ -128,22 +127,6 @@ define(function (require, exports, module) {
128127
});
129128
}
130129

131-
/**
132-
* To remove the color marks from the gutter of all the active editors
133-
*/
134-
function removeColorMarks() {
135-
136-
const allActiveEditors = MainViewManager.getAllViewedEditors();
137-
138-
allActiveEditors.forEach((activeEditor) => {
139-
const currEditor = activeEditor.editor;
140-
if(currEditor) {
141-
currEditor.clearGutter(GUTTER_NAME);
142-
}
143-
});
144-
}
145-
146-
147130
/**
148131
* To move the cursor to the color text and display the color quick edit
149132
* @param {Editor} editor the codemirror instance
@@ -173,8 +156,8 @@ define(function (require, exports, module) {
173156
* all the line numbers and the colors to be displayed on that line.
174157
*/
175158
function showGutters(editor, _results) {
159+
// TODO we should show the gutter in those languages only if a color is present in that file.
176160
if (editor && enabled) {
177-
initGutter(editor);
178161
const cm = editor._codeMirror;
179162
editor.clearGutter(GUTTER_NAME); // clear color markers
180163
_addDummyGutterMarkerIfNotExist(editor, editor.getCursorPos().line);
@@ -234,19 +217,6 @@ define(function (require, exports, module) {
234217
}
235218
}
236219

237-
238-
/**
239-
* Initialize the gutter
240-
* @param {activeEditor} editor
241-
*/
242-
function initGutter(editor) {
243-
if (!Editor.isGutterRegistered(GUTTER_NAME)) {
244-
// we should restrict the languages here to Editor.registerGutter(..., ["css", "less", "scss", etc..]);
245-
// TODO we should show the gutter in those languages only if a color is present in that file.
246-
Editor.registerGutter(GUTTER_NAME, COLOR_PREVIEW_GUTTER_PRIORITY, COLOR_LANGUAGES);
247-
}
248-
}
249-
250220
function _addDummyGutterMarkerIfNotExist(editor, line) {
251221
let marker = editor.getGutterMarker(line, GUTTER_NAME);
252222
if(!marker){
@@ -270,8 +240,7 @@ define(function (require, exports, module) {
270240

271241
// Add listener for all editor changes
272242
EditorManager.on("activeEditorChange", function (event, newEditor, oldEditor) {
273-
if (newEditor) {
274-
// todo: only attach if the color gutter is present as we disable it in certain languages
243+
if (newEditor && newEditor.isGutterActive(GUTTER_NAME)) {
275244
newEditor.off("cursorActivity.colorPreview");
276245
newEditor.on("cursorActivity.colorPreview", _cursorActivity);
277246
// Unbind the previous editor's change event if it exists
@@ -289,6 +258,7 @@ define(function (require, exports, module) {
289258
}
290259

291260
showColorMarks();
261+
_cursorActivity(null, newEditor);
292262
}
293263
});
294264

@@ -311,9 +281,9 @@ define(function (require, exports, module) {
311281
const value = PreferencesManager.get(PREFERENCES_CSS_COLOR_PREVIEW);
312282
enabled = value;
313283
if (!value) {
314-
// to dynamically remove color to all active editors
315-
removeColorMarks();
284+
Editor.unregisterGutter(GUTTER_NAME);
316285
} else {
286+
Editor.registerGutter(GUTTER_NAME, COLOR_PREVIEW_GUTTER_PRIORITY, COLOR_LANGUAGES);
317287
// to dynamically add color to all active editors
318288
addColorMarksToAllEditors();
319289
}
@@ -326,19 +296,11 @@ define(function (require, exports, module) {
326296
showColorMarks();
327297
}
328298

329-
/**
330-
* Driver function, runs at the start of the program
331-
*/
332-
function init() {
333-
// preferenceChanged calls 'showColorMarks' or 'removeColorMarks'
334-
preferenceChanged();
335-
registerHandlers();
336-
}
337-
338299
// init after appReady
339300
AppInit.appReady(function () {
340301
PreferencesManager.on("change", PREFERENCES_CSS_COLOR_PREVIEW, preferenceChanged);
341-
init();
302+
preferenceChanged();
303+
registerHandlers();
342304
});
343305
});
344306

test/spec/Editor-test.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,32 +2128,28 @@ define(function (require, exports, module) {
21282128
});
21292129

21302130
it("should return gutters registered with the same priority in insertion order", function () {
2131-
var secondRightGutter = "second-right";
2131+
const secondRightGutter = "second-right";
21322132
Editor.registerGutter(secondRightGutter, 101);
2133-
var expectedGutters = [leftGutter, lineNumberGutter, rightGutter, secondRightGutter];
2134-
var guttersInEditor = myEditor._codeMirror.getOption("gutters");
2135-
var registeredGutters = Editor.getRegisteredGutters().map(function (gutter) {
2133+
const expectedGutters = [leftGutter, lineNumberGutter, rightGutter, secondRightGutter];
2134+
const gutters = myEditor._codeMirror.getOption("gutters");
2135+
const registeredGutters = Editor.getRegisteredGutters().map(function (gutter) {
21362136
return gutter.name;
21372137
});
2138-
// registered color gutter is disabled in javascript files
2139-
expect(guttersInEditor.includes(colorGutter)).toBeFalse();
2140-
expect(registeredGutters.includes(colorGutter)).toBeTrue();
2141-
const allNonColorRegisteredGutters = registeredGutters.filter(function (gutter) {
2142-
return gutter !== colorGutter;
2143-
});
2144-
expect(guttersInEditor).toEqual(expectedGutters);
2145-
expect(allNonColorRegisteredGutters).toEqual(expectedGutters);
2138+
expect(gutters).toEqual(expectedGutters);
2139+
expect(registeredGutters).toEqual(expectedGutters);
21462140
});
21472141

2148-
it("should have only gutters registered with the intended languageIds ", function () {
2149-
var lessOnlyGutter = "less-only-gutter";
2142+
it("should have only gutters registered with the intended languageIds, test isGutterActive", function () {
2143+
const lessOnlyGutter = "less-only-gutter";
21502144
Editor.registerGutter(lessOnlyGutter, 101, ["less"]);
2151-
var expectedGutters = [leftGutter, lineNumberGutter, rightGutter];
2152-
var expectedRegisteredGutters = [leftGutter, lineNumberGutter, rightGutter, lessOnlyGutter, colorGutter];
2153-
var gutters = myEditor._codeMirror.getOption("gutters");
2154-
var registeredGutters = Editor.getRegisteredGutters().map(function (gutter) {
2145+
const expectedGutters = [leftGutter, lineNumberGutter, rightGutter];
2146+
const expectedRegisteredGutters = [leftGutter, lineNumberGutter, rightGutter, lessOnlyGutter];
2147+
const gutters = myEditor._codeMirror.getOption("gutters");
2148+
const registeredGutters = Editor.getRegisteredGutters().map(function (gutter) {
21552149
return gutter.name;
21562150
});
2151+
expect(myEditor.isGutterActive(lessOnlyGutter)).toBeFalse();
2152+
expect(myEditor.isGutterActive(leftGutter)).toBeTrue();
21572153
expect(gutters).toEqual(expectedGutters);
21582154
expect(registeredGutters).toEqual(expectedRegisteredGutters);
21592155
});
@@ -2162,13 +2158,13 @@ define(function (require, exports, module) {
21622158
Editor.unregisterGutter(leftGutter);
21632159
Editor.unregisterGutter(rightGutter);
21642160
Editor.registerGutter(leftGutter, 1);
2165-
var expectedGutters = [leftGutter, lineNumberGutter];
2166-
var guttersInEditor = myEditor._codeMirror.getOption("gutters");
2167-
var registeredGutters = Editor.getRegisteredGutters().map(function (gutter) {
2161+
const expectedGutters = [leftGutter, lineNumberGutter];
2162+
const gutters = myEditor._codeMirror.getOption("gutters");
2163+
const registeredGutters = Editor.getRegisteredGutters().map(function (gutter) {
21682164
return gutter.name;
21692165
});
2170-
expect(guttersInEditor).toEqual(expectedGutters); // js files dont have color gutter
2171-
expect(registeredGutters).toEqual([...expectedGutters, colorGutter]);
2166+
expect(gutters).toEqual(expectedGutters);
2167+
expect(registeredGutters).toEqual(expectedGutters);
21722168
});
21732169

21742170
it("should set gutter marker correctly", function () {

0 commit comments

Comments
 (0)