@@ -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 } ) ;
0 commit comments