Skip to content

Commit c453041

Browse files
committed
chore: add emmet metrics and minor refactor
1 parent da99c55 commit c453041

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

src/extensions/default/CSSCodeHints/main.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ define(function (require, exports, module) {
4343
* Emmet API:
4444
* This provides a function to expand abbreviations into full CSS properties.
4545
*/
46-
const EXPAND_ABBR = Phoenix.libs.Emmet.expand;
46+
const expandAbbr = Phoenix.libs.Emmet.expand;
4747
let enabled = true; // whether Emmet is enabled or not in preferences
4848

4949
require("./css-lint");
@@ -408,15 +408,15 @@ define(function (require, exports, module) {
408408
// wrapped in try catch block because EXPAND_ABBR might throw error when it gets unexpected
409409
// characters such as `, =, etc
410410
try {
411-
let expandedAbbr = EXPAND_ABBR(needle, { syntax: "css", type: "stylesheet" });
412-
if(expandedAbbr && isEmmetExpandable(needle, expandedAbbr)) {
411+
let expandedAbbr = expandAbbr(needle, { syntax: "css", type: "stylesheet" });
412+
if(expandedAbbr && _isEmmetExpandable(needle, expandedAbbr)) {
413413

414414
// if the expandedAbbr doesn't have any numbers, we should split the expandedAbbr to,
415415
// get its first word before `:`.
416416
// For instance, `m` expands to `margin: ;`. Here the `: ;` is unnecessary.
417417
// Also, `bgc` expands to `background-color: #fff;`. Here we don't need the `: #fff;`
418418
// as we have cssIntelligence to display hints based on the property
419-
if(!isEmmetAbbrNumeric(expandedAbbr)) {
419+
if(!_isEmmetAbbrNumeric(expandedAbbr)) {
420420
expandedAbbr = expandedAbbr.split(':')[0];
421421
}
422422

@@ -436,7 +436,8 @@ define(function (require, exports, module) {
436436

437437
const $emmetHintObj = $("<span>")
438438
.addClass("brackets-css-hints brackets-hints")
439-
.attr("data-val", expandedAbbr);
439+
.attr("data-val", expandedAbbr)
440+
.attr("data-isEmmet", true);
440441

441442
// for highlighting the already-typed characters
442443
if (token.stringRanges) {
@@ -498,7 +499,7 @@ define(function (require, exports, module) {
498499
* @param {String} expandedAbbr the expanded abbr returned by EXPAND_ABBR emmet api
499500
* @returns {boolean} true if emmet should be expanded, otherwise false
500501
*/
501-
function isEmmetExpandable(needle, expandedAbbr) {
502+
function _isEmmetExpandable(needle, expandedAbbr) {
502503
return needle + ': ;' !== expandedAbbr;
503504
}
504505

@@ -511,7 +512,7 @@ define(function (require, exports, module) {
511512
* @param {String} expandedAbbr the expanded abbr returned by EXPAND_ABBR emmet api
512513
* @returns {boolean} true if expandedAbbr has numbers (and doesn't include '#') otherwise false.
513514
*/
514-
function isEmmetAbbrNumeric(expandedAbbr) {
515+
function _isEmmetAbbrNumeric(expandedAbbr) {
515516
return expandedAbbr.match(/\d/) !== null && !expandedAbbr.includes('#') && !expandedAbbr.includes(',');
516517
}
517518

@@ -597,6 +598,7 @@ define(function (require, exports, module) {
597598
ctx;
598599

599600
if (hint.jquery) {
601+
hint.data("isEmmet") && Metrics.countEvent(Metrics.EVENT_TYPE.CODE_HINTS, "emmet", "cssInsert");
600602
hint = hint.data("val") + ""; // font-weight: 400, 400 is returned as number so,
601603
}
602604

src/extensions/default/DefaultExtensions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"note": "This is only to for legacy extensions that havent been updated for a long time, remove flag unconditionally if instructed by the extension author, but inform of the issues in extension.",
3434
"extensionIDs": [
3535
"brackets-beautify", "beautify.io", "hirse.beautify", "jsbeautifier", "sizuhiko.brackets.prettier", "pretty_json",
36-
"bib-locked-live-preview", "brackets-display-shortcuts", "changing-tags", "brackets-indent-guides"
36+
"bib-locked-live-preview", "brackets-display-shortcuts", "changing-tags", "brackets-indent-guides", "brackets-emmet",
37+
"github-Pluto-custom-line-height"
3738
]
3839
}
3940
}

src/extensions/default/HTMLCodeHints/main.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ define(function (require, exports, module) {
5353
/**
5454
* The Emmet api's
5555
*/
56-
const EXPAND_ABBR = Phoenix.libs.Emmet.expand;
56+
const expandAbbr = Phoenix.libs.Emmet.expand;
5757

5858
/**
5959
* A list of all the markup snippets that can be expanded.
@@ -98,7 +98,7 @@ define(function (require, exports, module) {
9898
// make sure we donot have empty spaces
9999
if (wordObj.word.trim()) {
100100

101-
const expandedAbbr = isExpandable(editor, wordObj.word);
101+
const expandedAbbr = _isExpandable(editor, wordObj.word);
102102
if (expandedAbbr) {
103103
return true;
104104
}
@@ -119,7 +119,7 @@ define(function (require, exports, module) {
119119
const wordObj = getWordBeforeCursor(this.editor);
120120

121121
// Check if the abbreviation is expandable
122-
const expandedAbbr = isExpandable(this.editor, wordObj.word);
122+
const expandedAbbr = _isExpandable(this.editor, wordObj.word);
123123
if (!expandedAbbr) {
124124
return null;
125125
}
@@ -166,8 +166,9 @@ define(function (require, exports, module) {
166166
*/
167167
EmmetMarkupHints.prototype.insertHint = function () {
168168
const wordObj = getWordBeforeCursor(this.editor);
169-
const expandedAbbr = isExpandable(this.editor, wordObj.word);
170-
updateAbbrInEditor(this.editor, wordObj, expandedAbbr);
169+
const expandedAbbr = _isExpandable(this.editor, wordObj.word);
170+
_updateAbbrInEditor(this.editor, wordObj, expandedAbbr);
171+
Metrics.countEvent(Metrics.EVENT_TYPE.CODE_HINTS, "emmet", "htmlInsert");
171172
return false;
172173
};
173174

@@ -179,7 +180,7 @@ define(function (require, exports, module) {
179180
* @param {Boolean} insideBraces - Flag indicating if we are inside braces (e.g. {} or [])
180181
* @returns True if the character is valid for an abbreviation
181182
*/
182-
function isEmmetChar(char, insideBraces) {
183+
function _isEmmetChar(char, insideBraces) {
183184
// Valid abbreviation characters: letters, digits, and some punctuation
184185
// Adjust this regex or the list as needed for your implementation
185186
const validPattern = /[a-zA-Z0-9:+*<>()/!$\-@#}{]/;
@@ -195,7 +196,7 @@ define(function (require, exports, module) {
195196
* @param {Number} cursorCh - The cursor's character (column) position on that line
196197
* @returns The index (column) where the abbreviation starts
197198
*/
198-
function findAbbreviationStart(line, cursorCh) {
199+
function _findAbbreviationStart(line, cursorCh) {
199200
let start = cursorCh;
200201
let insideBraces = false;
201202

@@ -217,7 +218,7 @@ define(function (require, exports, module) {
217218
}
218219

219220
// If the character is valid as part of an Emmet abbreviation, continue scanning backwards
220-
if (isEmmetChar(char, insideBraces)) {
221+
if (_isEmmetChar(char, insideBraces)) {
221222
start--;
222223
} else {
223224
break;
@@ -245,7 +246,7 @@ define(function (require, exports, module) {
245246
const lineText = editor.document.getLine(pos.line);
246247

247248
// to determine where the abbreviation starts on the line
248-
const abbreviationStart = findAbbreviationStart(lineText, pos.ch);
249+
const abbreviationStart = _findAbbreviationStart(lineText, pos.ch);
249250

250251
// Optionally, adjust the end position if the cursor is immediately before a closing brace.
251252
let abbreviationEnd = pos.ch;
@@ -401,7 +402,7 @@ define(function (require, exports, module) {
401402
* }
402403
* @param {String} expandedAbbr - the expanded version of abbr that will replace the abbr
403404
*/
404-
function updateAbbrInEditor(editor, wordObj, expandedAbbr) {
405+
function _updateAbbrInEditor(editor, wordObj, expandedAbbr) {
405406
// Get the current line's indentation
406407
const baseIndent = getLineIndentation(editor, wordObj.start);
407408

@@ -456,30 +457,30 @@ define(function (require, exports, module) {
456457
*
457458
* @param {Editor} editor - the editor instance
458459
* @param {String} word - the abbr
459-
* @returns {String | false} - returns the expanded abbr, and if cannot be expanded, returns false
460+
* @returns {String | null} - returns the expanded abbr, and if cannot be expanded, returns null
460461
*/
461-
function isExpandable(editor, word) {
462+
function _isExpandable(editor, word) {
462463
const pos = editor.getCursorPos();
463464
const line = editor.document.getLine(pos.line);
464465

465466
// to prevent hints from appearing in <!DOCTYPE html> line. Also to prevent hints from appearing in comments
466467
if(line.includes('<!')) {
467-
return false;
468+
return null;
468469
}
469470

470471
// to show emmet hint when either a single or three exclamation mark(s) is present
471472
if (line.includes('!!') && !line.includes('!!!')) {
472-
return false;
473+
return null;
473474
}
474475

475476
// if more than three, then don't show emmet hint
476477
if(line.includes('!!!!')) {
477-
return false;
478+
return null;
478479
}
479480

480481
// make sure that word doesn't contain any negativeSymbols
481482
if (negativeSymbols.some(symbol => word.includes(symbol))) {
482-
return false;
483+
return null;
483484
}
484485

485486
// the word must be either in markupSnippetsList, htmlList or it must have a positive symbol
@@ -491,8 +492,7 @@ define(function (require, exports, module) {
491492
positiveSymbols.some(symbol => word.includes(symbol))) {
492493

493494
try {
494-
const expanded = EXPAND_ABBR(word, { syntax: "html", type: "markup" });
495-
return expanded;
495+
return expandAbbr(word, { syntax: "html", type: "markup" }); // expanded
496496
} catch (error) {
497497

498498
// emmet api throws an error when abbr contains unclosed quotes, handling that case
@@ -504,21 +504,20 @@ define(function (require, exports, module) {
504504
const modifiedWord = word + nextChar;
505505

506506
try {
507-
const expandedModified = EXPAND_ABBR(modifiedWord, { syntax: "html", type: "markup" });
508-
return expandedModified;
507+
return expandAbbr(modifiedWord, { syntax: "html", type: "markup" }); //expandedModified
509508
} catch (innerError) {
510509
// If it still fails, return false
511-
return false;
510+
return null;
512511
}
513512
}
514513
}
515514

516515
// If no quote is found or expansion fails, return false
517-
return false;
516+
return null;
518517
}
519518
}
520519

521-
return false;
520+
return null;
522521
}
523522

524523

0 commit comments

Comments
 (0)