Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 60ce7af

Browse files
committed
Merge pull request #8156 from MarcelGerber/quick-view-literal
Limit QuickView to literal color names in some languages
2 parents 82bb3f7 + 8c7fcda commit 60ce7af

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

src/extensions/default/QuickView/main.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ define(function (require, exports, module) {
3737
Menus = brackets.getModule("command/Menus"),
3838
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
3939
Strings = brackets.getModule("strings"),
40-
ViewUtils = brackets.getModule("utils/ViewUtils");
40+
ViewUtils = brackets.getModule("utils/ViewUtils"),
41+
TokenUtils = brackets.getModule("utils/TokenUtils");
4142

4243
var previewContainerHTML = require("text!QuickViewTemplate.html");
4344

@@ -54,6 +55,8 @@ define(function (require, exports, module) {
5455
POINTER_HEIGHT = 15, // Pointer height, used to shift popover above pointer (plus a little bit of space)
5556
POPOVER_HORZ_MARGIN = 5; // Horizontal margin
5657

58+
var styleLanguages = ["css", "text/x-less", "sass", "text/x-scss"];
59+
5760
prefs = PreferencesManager.getExtensionPrefs("quickview");
5861
prefs.definePreference("enabled", "boolean", true);
5962

@@ -233,8 +236,9 @@ define(function (require, exports, module) {
233236
};
234237
}
235238

236-
function execColorMatch(line) {
237-
var colorMatch;
239+
function execColorMatch(editor, line, pos) {
240+
var colorMatch,
241+
ignoreNamedColors;
238242

239243
function hyphenOnMatchBoundary(match, line) {
240244
var beforeIndex, afterIndex;
@@ -252,11 +256,24 @@ define(function (require, exports, module) {
252256

253257
return false;
254258
}
259+
function isNamedColor(match) {
260+
if (match && match[0] && /^[a-z]+$/i.test(match[0])) { // only for color names, not for hex-/rgb-values
261+
return true;
262+
}
263+
}
255264

256265
// Hyphens do not count as a regex word boundary (\b), so check for those here
257266
do {
258267
colorMatch = colorRegEx.exec(line);
259-
} while (colorMatch && hyphenOnMatchBoundary(colorMatch, line));
268+
if (!colorMatch) {
269+
break;
270+
}
271+
if (ignoreNamedColors === undefined) {
272+
var mode = TokenUtils.getModeAt(editor._codeMirror, pos).name;
273+
ignoreNamedColors = styleLanguages.indexOf(mode) === -1;
274+
}
275+
} while (hyphenOnMatchBoundary(colorMatch, line) ||
276+
(ignoreNamedColors && isNamedColor(colorMatch)));
260277

261278
return colorMatch;
262279
}
@@ -351,7 +368,7 @@ define(function (require, exports, module) {
351368
}
352369

353370
var gradientMatch = execGradientMatch(line),
354-
match = gradientMatch.match || execColorMatch(line),
371+
match = gradientMatch.match || execColorMatch(editor, line, pos),
355372
cm = editor._codeMirror;
356373

357374
while (match) {
@@ -397,7 +414,7 @@ define(function (require, exports, module) {
397414
if (gradientMatch.match) {
398415
gradientMatch = execGradientMatch(line);
399416
}
400-
match = gradientMatch.match || execColorMatch(line);
417+
match = gradientMatch.match || execColorMatch(editor, line, pos);
401418
}
402419

403420
return null;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Sample JS for testing the QuickView extension. */
2+
3+
function green() { // generate green colors
4+
var color = "green",
5+
tan = Math.tan;
6+
return tan(array["red"], array[red]);
7+
}
8+
darkgray
9+
// #123456
10+
// :rgb(65, 43, 21)

src/extensions/default/QuickView/unittests.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ define(function (require, exports, module) {
4141
Commands,
4242
EditorManager,
4343
QuickView,
44-
editor;
44+
editor,
45+
testFile = "test.css",
46+
oldFile;
4547

4648
beforeEach(function () {
4749
// Create a new window that will be shared by ALL tests in this spec.
@@ -62,13 +64,16 @@ define(function (require, exports, module) {
6264
runs(function () {
6365
SpecRunnerUtils.loadProjectInTestWindow(testFolder);
6466
});
67+
}
6568

69+
if (testFile !== oldFile) {
6670
runs(function () {
67-
waitsForDone(SpecRunnerUtils.openProjectFiles(["test.css"]), "open test file");
71+
waitsForDone(SpecRunnerUtils.openProjectFiles([testFile]), "open test file: " + testFile);
6872
});
6973

7074
runs(function () {
71-
editor = EditorManager.getCurrentFullEditor();
75+
editor = EditorManager.getCurrentFullEditor();
76+
oldFile = testFile;
7277
});
7378
}
7479
});
@@ -191,9 +196,43 @@ define(function (require, exports, module) {
191196
expectNoPreviewAtPos(75, 18); // cursor on white in hyphenated word @bc-bg-highlight
192197
});
193198
});
199+
200+
describe("JavaScript file", function () {
201+
runs(function () {
202+
testFile = "test.js";
203+
});
204+
205+
it("should NOT show preview of color-named functions and object/array keys", function () {
206+
runs(function () {
207+
expectNoPreviewAtPos(2, 12); // cursor on green()
208+
expectNoPreviewAtPos(4, 22); // cursor on Math.tan
209+
expectNoPreviewAtPos(5, 14); // cursor on tan()
210+
expectNoPreviewAtPos(5, 38); // cursor on array[red]
211+
});
212+
});
213+
it("should not show preview of literal color names", function () {
214+
runs(function () {
215+
expectNoPreviewAtPos(2, 36); // green
216+
expectNoPreviewAtPos(3, 21); // green
217+
expectNoPreviewAtPos(4, 11); // tan
218+
expectNoPreviewAtPos(5, 25); // red
219+
expectNoPreviewAtPos(7, 1); // darkgray
220+
});
221+
});
222+
it("should show preview of non-literal color codes", function () {
223+
runs(function () {
224+
checkColorAtPos("#123456", 8, 7);
225+
checkColorAtPos("rgb(65, 43, 21)", 9, 8);
226+
});
227+
});
228+
});
194229
});
195230

196231
describe("Quick view gradients", function () {
232+
runs(function () {
233+
testFile = "test.css";
234+
});
235+
197236
it("Should show linear gradient preview for those with vendor prefix", function () {
198237
runs(function () {
199238
var expectedGradient1 = "-webkit-linear-gradient(top, #d2dfed 0%, #c8d7eb 26%, #bed0ea 51%, #a6c0e3 51%, #afc7e8 62%, #bad0ef 75%, #99b5db 88%, #799bc8 100%)",

0 commit comments

Comments
 (0)