Skip to content

Commit a787195

Browse files
committed
feat: only update the modified lines, instead of re-factoring the whole file
1 parent 14f59e9 commit a787195

File tree

1 file changed

+81
-4
lines changed
  • src/extensionsIntegrated/CSSColorPreview

1 file changed

+81
-4
lines changed

src/extensionsIntegrated/CSSColorPreview/main.js

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,22 @@ define(function (require, exports, module) {
172172

173173
/**
174174
* To display the color marks on the gutter
175+
*
175176
* @param {activeEditor} editor
176177
* @param {Array.<object>} _results An array of objects which stores
177178
* all the line numbers and the colors to be displayed on that line.
179+
* @param {Boolean} update marks whether this function is called when some lines
180+
* are updated or when the whole file is re-updated. Defaults to false.
178181
*/
179-
function showGutters(editor, _results) {
182+
function showGutters(editor, _results, update = false) {
180183
if (editor && enabled) {
181184
initGutter(editor);
182185
const cm = editor._codeMirror;
183-
editor.clearGutter(GUTTER_NAME); // clear color markers
186+
// if the file is updated we don't need to clear the gutter
187+
// as it will clear all the existing markers.
188+
if(!update) {
189+
editor.clearGutter(GUTTER_NAME); // clear color markers
190+
}
184191
_addDummyGutterMarkerIfNotExist(editor, editor.getCursorPos().line);
185192

186193
// Only add markers if enabled
@@ -323,11 +330,81 @@ define(function (require, exports, module) {
323330
}
324331
}
325332

333+
334+
/**
335+
* Responsible to update the color marks only on the modified lines
336+
*
337+
* @param {Editor} editor the editor instance
338+
* @param {Number} fromLineNumber modification start from line number
339+
* @param {Number} toLineNumber modification upto line number
340+
* @return {Array.<Object>} an array of objects with all the line nos and,
341+
* the colors to be added on those lines
342+
*/
343+
function updateColorMarks(editor, fromLineNumber, toLineNumber) {
344+
// const nLen = editor.lineCount();
345+
const aColors = [];
346+
347+
// match colors and push into an array
348+
for (let i = fromLineNumber; i <= toLineNumber; i++) {
349+
let lineText = editor.getLine(i);
350+
351+
// to skip commented lines
352+
// The second parameter in the getToken() method, `true`, enables precise tokenization.
353+
// Without setting `precise` to `true`, multi-line comments are not handled correctly.
354+
const token = editor.getToken({line: i}, true);
355+
if(token.type === "comment") {
356+
editor.setGutterMarker(i, GUTTER_NAME, "");
357+
continue;
358+
}
359+
360+
let regx = /:[^;]*;/g;
361+
362+
lineText = lineText.match(regx);
363+
if (lineText) {
364+
let tempColors = lineText[0].match(COLOR_REGEX);
365+
366+
// to remove color if the line is modified and now the colors are not valid
367+
if(!tempColors) {
368+
editor.setGutterMarker(i, GUTTER_NAME, "");
369+
}
370+
// Support up to 4 colors
371+
if (tempColors && tempColors.length > 0) {
372+
let colors = tempColors.slice(0, 4);
373+
aColors.push({
374+
lineNumber: i,
375+
colorValues: colors
376+
});
377+
}
378+
}
379+
}
380+
381+
return aColors;
382+
}
383+
326384
/**
327385
* Function that gets triggered when any change occurs on the editor
386+
*
387+
* @param {Editor} instance the codemirror instance
388+
* @param {Object} changeObj an object that has properties regarding the line changed and type of change
328389
*/
329-
function onChanged() {
330-
showColorMarks();
390+
function onChanged(instance, changeObj) {
391+
392+
const editor = EditorManager.getActiveEditor();
393+
394+
// for insertion and deletion, update the changed lines
395+
if(changeObj.origin === '+input' || changeObj.origin === '+delete') {
396+
// make sure that the required properties exist and in the form they are expected to be
397+
if(changeObj.from.line && changeObj.to.line && changeObj.from.line <= changeObj.to.line) {
398+
const aColors = updateColorMarks(editor, changeObj.from.line, changeObj.to.line);
399+
showGutters(editor, aColors, true);
400+
} else {
401+
showColorMarks();
402+
}
403+
404+
} else { // for any complex operation like, cut, paste etc, we re-update the whole file
405+
showColorMarks();
406+
}
407+
331408
}
332409

333410
/**

0 commit comments

Comments
 (0)