Skip to content

Commit 84bcace

Browse files
committed
feat: add allPreferences module and enable Emmet preference
1 parent 0894786 commit 84bcace

File tree

4 files changed

+135
-64
lines changed

4 files changed

+135
-64
lines changed

src/brackets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ define(function (require, exports, module) {
121121
// load modules for later use
122122
require("utils/Global");
123123
require("editor/CSSInlineEditor");
124+
require("preferences/AllPreferences");
124125
require("project/WorkingSetSort");
125126
require("search/QuickOpen");
126127
require("search/QuickOpenHelper");

src/extensions/default/CSSCodeHints/main.js

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ define(function (require, exports, module) {
3535
KeyEvent = brackets.getModule("utils/KeyEvent"),
3636
LiveDevelopment = brackets.getModule("LiveDevelopment/main"),
3737
Metrics = brackets.getModule("utils/Metrics"),
38+
AllPreferences = brackets.getModule("preferences/AllPreferences"),
3839
CSSProperties = require("text!CSSProperties.json"),
3940
properties = JSON.parse(CSSProperties);
4041

@@ -43,6 +44,7 @@ define(function (require, exports, module) {
4344
* This provides a function to expand abbreviations into full CSS properties.
4445
*/
4546
const EXPAND_ABBR = Phoenix.libs.Emmet.expand;
47+
let enabled = true; // whether Emmet is enabled or not in preferences
4648

4749
require("./css-lint");
4850

@@ -394,81 +396,85 @@ define(function (require, exports, module) {
394396
// pushedHints stores all the hints that will be displayed to the user
395397
let pushedHints = formatHints(result);
396398

397-
// needle gives the current word before cursor, make sure that it exists
398-
// also needle shouldn't contain `-`, because for example if user typed:
399-
// `box-siz` then in that case it is very obvious that user wants to type `box-sizing`
400-
// but emmet expands it `box: siz;`. So we prevent calling emmet when needle has `-`.
401-
if(needle && !needle.includes('-')) {
402-
403-
// wrapped in try catch block because EXPAND_ABBR might throw error when it gets unexpected
404-
// characters such as `, =, etc
405-
try {
406-
let expandedAbbr = EXPAND_ABBR(needle, { syntax: "css", type: "stylesheet" });
407-
if(expandedAbbr && isEmmetExpandable(needle, expandedAbbr)) {
408-
409-
// if the expandedAbbr doesn't have any numbers, we should split the expandedAbbr to,
410-
// get its first word before `:`.
411-
// For instance, `m` expands to `margin: ;`. Here the `: ;` is unnecessary.
412-
// Also, `bgc` expands to `background-color: #fff;`. Here we don't need the `: #fff;`
413-
// as we have cssIntelligence to display hints based on the property
414-
if(!isEmmetAbbrNumeric(expandedAbbr)) {
415-
expandedAbbr = expandedAbbr.split(':')[0];
416-
}
399+
// make sure that emmet feature is on in preferences
400+
if(enabled) {
401+
402+
// needle gives the current word before cursor, make sure that it exists
403+
// also needle shouldn't contain `-`, because for example if user typed:
404+
// `box-siz` then in that case it is very obvious that user wants to type `box-sizing`
405+
// but emmet expands it `box: siz;`. So we prevent calling emmet when needle has `-`.
406+
if(needle && !needle.includes('-')) {
407+
408+
// wrapped in try catch block because EXPAND_ABBR might throw error when it gets unexpected
409+
// characters such as `, =, etc
410+
try {
411+
let expandedAbbr = EXPAND_ABBR(needle, { syntax: "css", type: "stylesheet" });
412+
if(expandedAbbr && isEmmetExpandable(needle, expandedAbbr)) {
413+
414+
// if the expandedAbbr doesn't have any numbers, we should split the expandedAbbr to,
415+
// get its first word before `:`.
416+
// For instance, `m` expands to `margin: ;`. Here the `: ;` is unnecessary.
417+
// Also, `bgc` expands to `background-color: #fff;`. Here we don't need the `: #fff;`
418+
// as we have cssIntelligence to display hints based on the property
419+
if(!isEmmetAbbrNumeric(expandedAbbr)) {
420+
expandedAbbr = expandedAbbr.split(':')[0];
421+
}
417422

418-
// token is required for highlighting the matched part. It gives access to
419-
// stringRanges property. Refer to `formatHints()` function in this file for more detail
420-
const [token] = StringMatch.codeHintsSort(needle, [expandedAbbr]);
423+
// token is required for highlighting the matched part. It gives access to
424+
// stringRanges property. Refer to `formatHints()` function in this file for more detail
425+
const [token] = StringMatch.codeHintsSort(needle, [expandedAbbr]);
421426

422-
// this displays an emmet icon at the side of the hint
423-
// this gives an idea to the user that the hint is coming from Emmet
424-
let $icon = $(`<a class="emmet-css-code-hint" style="text-decoration: none">Emmet</a>`);
427+
// this displays an emmet icon at the side of the hint
428+
// this gives an idea to the user that the hint is coming from Emmet
429+
let $icon = $(`<a class="emmet-css-code-hint" style="text-decoration: none">Emmet</a>`);
425430

426-
// if MDN_URL is available for the property, add the href attribute to redirect to mdn
427-
if(MDN_PROPERTIES_URLS[expandedAbbr]) {
428-
$icon.attr("href", MDN_PROPERTIES_URLS[expandedAbbr]);
429-
$icon.attr("title", Strings.DOCS_MORE_LINK_MDN_TITLE);
430-
}
431+
// if MDN_URL is available for the property, add the href attribute to redirect to mdn
432+
if(MDN_PROPERTIES_URLS[expandedAbbr]) {
433+
$icon.attr("href", MDN_PROPERTIES_URLS[expandedAbbr]);
434+
$icon.attr("title", Strings.DOCS_MORE_LINK_MDN_TITLE);
435+
}
431436

432-
const $emmetHintObj = $("<span>")
433-
.addClass("brackets-css-hints brackets-hints")
434-
.attr("data-val", expandedAbbr);
435-
436-
// for highlighting the already-typed characters
437-
if (token.stringRanges) {
438-
token.stringRanges.forEach(function (range) {
439-
if (range.matched) {
440-
$emmetHintObj.append($("<span>")
441-
.text(range.text)
442-
.addClass("matched-hint"));
443-
} else {
444-
$emmetHintObj.append(range.text);
445-
}
446-
});
447-
} else {
448-
// fallback
449-
$emmetHintObj.text(expandedAbbr);
450-
}
437+
const $emmetHintObj = $("<span>")
438+
.addClass("brackets-css-hints brackets-hints")
439+
.attr("data-val", expandedAbbr);
440+
441+
// for highlighting the already-typed characters
442+
if (token.stringRanges) {
443+
token.stringRanges.forEach(function (range) {
444+
if (range.matched) {
445+
$emmetHintObj.append($("<span>")
446+
.text(range.text)
447+
.addClass("matched-hint"));
448+
} else {
449+
$emmetHintObj.append(range.text);
450+
}
451+
});
452+
} else {
453+
// fallback
454+
$emmetHintObj.text(expandedAbbr);
455+
}
451456

452-
// add the emmet icon to the final hint object
453-
$emmetHintObj.append($icon);
457+
// add the emmet icon to the final hint object
458+
$emmetHintObj.append($icon);
454459

455-
if(pushedHints) {
460+
if(pushedHints) {
456461

457-
// to remove duplicate hints. one comes from emmet and other from default css hints.
458-
// we remove the default css hints and push emmet hint at the beginning.
459-
for(let i = 0; i < pushedHints.length; i++) {
460-
if(pushedHints[i][0].getAttribute('data-val') === expandedAbbr) {
461-
pushedHints.splice(i, 1);
462-
break;
462+
// to remove duplicate hints. one comes from emmet and other from default css hints.
463+
// we remove the default css hints and push emmet hint at the beginning.
464+
for(let i = 0; i < pushedHints.length; i++) {
465+
if(pushedHints[i][0].getAttribute('data-val') === expandedAbbr) {
466+
pushedHints.splice(i, 1);
467+
break;
468+
}
463469
}
470+
pushedHints.unshift($emmetHintObj);
471+
} else {
472+
pushedHints = $emmetHintObj;
464473
}
465-
pushedHints.unshift($emmetHintObj);
466-
} else {
467-
pushedHints = $emmetHintObj;
468474
}
475+
} catch (e) {
476+
// pass
469477
}
470-
} catch (e) {
471-
// pass
472478
}
473479
}
474480

@@ -712,10 +718,21 @@ define(function (require, exports, module) {
712718
return keepHints;
713719
};
714720

721+
/**
722+
* Checks for preference changes, to enable/disable Emmet
723+
*/
724+
function preferenceChanged() {
725+
enabled = PreferencesManager.get(AllPreferences.EMMET);
726+
}
727+
728+
715729
AppInit.appReady(function () {
716730
var cssPropHints = new CssPropHints();
717731
CodeHintManager.registerHintProvider(cssPropHints, ["css", "scss", "less"], 1);
718732

733+
PreferencesManager.on("change", AllPreferences.EMMET, preferenceChanged);
734+
preferenceChanged();
735+
719736
// For unit testing
720737
exports.cssPropHintProvider = cssPropHints;
721738
});

src/nls/root/strings.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,9 @@ define({
12641264
"DESCRIPTION_HIDE_FIRST": "true to show the first Indent Guide line else false.",
12651265
"DESCRIPTION_CSS_COLOR_PREVIEW": "true to display color previews in the gutter, else false.",
12661266

1267+
// Emmet
1268+
"DESCRIPTION_EMMET": "true to enable Emmet, else false.",
1269+
12671270
// Git extension
12681271
"ENABLE_GIT": "Enable Git",
12691272
"ACTION": "Action",

src/preferences/AllPreferences.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* GNU AGPL-3.0 License
3+
*
4+
* Copyright (c) 2021 - present core.ai . All rights reserved.
5+
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
6+
*
7+
* This program is free software: you can redistribute it and/or modify it
8+
* under the terms of the GNU Affero General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
15+
* for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
19+
*
20+
*/
21+
22+
/*
23+
* This file houses all the preferences used across Phoenix.
24+
*
25+
* To use:
26+
* ```
27+
* const AllPreferences = brackets.getModule("preferences/AllPreferences");
28+
* function preferenceChanged() {
29+
enabled = PreferencesManager.get(AllPreferences.EMMET);
30+
}
31+
* PreferencesManager.on("change", AllPreferences.EMMET, preferenceChanged);
32+
preferenceChanged();
33+
* ```
34+
*/
35+
36+
define(function (require, exports, module) {
37+
const PreferencesManager = require("preferences/PreferencesManager");
38+
const Strings = require("strings");
39+
40+
// list of all the preferences
41+
const PREFERENCES_LIST = {
42+
EMMET: "emmet"
43+
};
44+
45+
PreferencesManager.definePreference(PREFERENCES_LIST.EMMET, "boolean", true, {
46+
description: Strings.DESCRIPTION_EMMET
47+
});
48+
49+
module.exports = PREFERENCES_LIST;
50+
});

0 commit comments

Comments
 (0)