Skip to content

Commit 4ca17c8

Browse files
committed
refactor: code beautification
1 parent fc8f7f8 commit 4ca17c8

File tree

7 files changed

+138
-126
lines changed

7 files changed

+138
-126
lines changed

src/extensionsIntegrated/CustomSnippets/src/UIHelper.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@ define(function (require, exports, module) {
1616
const $wrapper = $(`#${wrapperId}`);
1717

1818
// Remove any existing error messages in this wrapper
19-
$wrapper.find('.error-message').remove();
19+
$wrapper.find(".error-message").remove();
2020

2121
// Remove error styling from the input field
2222
$inputField.removeClass("error-input");
2323

2424
// Now show the new error message
25-
const $errorMessage = $("<div>")
26-
.attr("id", errorId)
27-
.addClass("error-message")
28-
.text(errorMessage);
25+
const $errorMessage = $("<div>").attr("id", errorId).addClass("error-message").text(errorMessage);
2926

3027
$wrapper.append($errorMessage);
3128

@@ -166,16 +163,11 @@ define(function (require, exports, module) {
166163
* @param {boolean} isEditForm - Whether this is for the edit form (optional, defaults to false)
167164
*/
168165
function showDuplicateAbbreviationError(abbreviation, isEditForm = false) {
169-
const inputId = isEditForm ? 'edit-abbr-box' : 'abbr-box';
170-
const wrapperId = isEditForm ? 'edit-abbr-box-wrapper' : 'abbr-box-wrapper';
171-
const errorId = isEditForm ? 'edit-abbreviation-duplicate-error' : 'abbreviation-duplicate-error';
172-
173-
showError(
174-
inputId,
175-
wrapperId,
176-
`A snippet with abbreviation "${abbreviation}" already exists.`,
177-
errorId
178-
);
166+
const inputId = isEditForm ? "edit-abbr-box" : "abbr-box";
167+
const wrapperId = isEditForm ? "edit-abbr-box-wrapper" : "abbr-box-wrapper";
168+
const errorId = isEditForm ? "edit-abbreviation-duplicate-error" : "abbreviation-duplicate-error";
169+
170+
showError(inputId, wrapperId, `A snippet with abbreviation "${abbreviation}" already exists.`, errorId);
179171
}
180172

181173
/**

src/extensionsIntegrated/CustomSnippets/src/driver.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,11 @@ define(function (require, exports, module) {
169169
// this is needed because sometimes when there are no default hints present then the
170170
// SnippetCodeHints.js shows some hints, so we don't want to duplicate hints
171171
if (Array.isArray(response.hints) && response.hints.length > 0) {
172-
const hasCustomSnippets = response.hints.some(hint => {
173-
return (hint && hint.hasClass && hint.hasClass('emmet-hint')) ||
174-
(hint && hint.attr && hint.attr('data-isCustomSnippet'));
172+
const hasCustomSnippets = response.hints.some((hint) => {
173+
return (
174+
(hint && hint.hasClass && hint.hasClass("emmet-hint")) ||
175+
(hint && hint.attr && hint.attr("data-isCustomSnippet"))
176+
);
175177
});
176178

177179
if (hasCustomSnippets) {

src/extensionsIntegrated/CustomSnippets/src/filterSnippets.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ define(function (require, exports, module) {
3434
const template = (snippet.templateText || "").toLowerCase();
3535
const fileExt = (snippet.fileExtension || "").toLowerCase();
3636

37-
filterTerms.forEach(function(term) {
37+
filterTerms.forEach(function (term) {
3838
// abbreviation matching. this has the highest priority
3939
if (abbr.indexOf(term) === 0) {
4040
score += 1000; // exact start match in abbreviation
@@ -82,17 +82,17 @@ define(function (require, exports, module) {
8282
const filterTerms = filterText.split(/\s+/);
8383

8484
// filter snippets that match all terms
85-
const matchingSnippets = snippetList.filter(function(snippet) {
85+
const matchingSnippets = snippetList.filter(function (snippet) {
8686
const filterString = _createFilterString(snippet);
8787

8888
// all terms must match (AND logic)
89-
return filterTerms.every(function(term) {
89+
return filterTerms.every(function (term) {
9090
return filterString.indexOf(term) > -1;
9191
});
9292
});
9393

9494
// sort by relevance (higher priority scores first)
95-
return matchingSnippets.sort(function(a, b) {
95+
return matchingSnippets.sort(function (a, b) {
9696
const scoreA = _calculateMatchPriority(a, filterTerms);
9797
const scoreB = _calculateMatchPriority(b, filterTerms);
9898
return scoreB - scoreA; // in descending order (highest score will be at first)

src/extensionsIntegrated/CustomSnippets/src/global.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
define(function (require, exports, module) {
22
/**
3-
* This is an array of objects. this will store the list of all the snippets
4-
* it is an array of objects stored in the format
5-
* [{
6-
* abbreviation: 'clg',
7-
* description: 'console log shortcut',
8-
* templateText: 'console.log()',
9-
* fileExtension: '.js, .css'
10-
* }]
11-
*/
3+
* This is an array of objects. this will store the list of all the snippets
4+
* it is an array of objects stored in the format
5+
* [{
6+
* abbreviation: 'clg',
7+
* description: 'console log shortcut',
8+
* templateText: 'console.log()',
9+
* fileExtension: '.js, .css'
10+
* }]
11+
*/
1212
const SnippetHintsList = [];
1313

1414
exports.SnippetHintsList = SnippetHintsList;

src/extensionsIntegrated/CustomSnippets/src/helper.js

Lines changed: 82 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ define(function (require, exports, module) {
33
const Global = require("./global");
44
const UIHelper = require("./UIHelper");
55

6-
76
/**
87
* map the language IDs to their file extensions for snippet matching
98
* this is needed because we expect the user to enter file extensions and not the file type inside the input field
@@ -13,41 +12,40 @@ define(function (require, exports, module) {
1312
*/
1413
function mapLanguageToExtension(languageId) {
1514
const languageMap = {
16-
'javascript': '.js',
17-
'css': '.css',
18-
'html': '.html',
19-
'php': '.php',
20-
'python': '.py',
21-
'java': '.java',
22-
'c': '.c',
23-
'cpp': '.cpp',
24-
'csharp': '.cs',
25-
'typescript': '.ts',
26-
'json': '.json',
27-
'xml': '.xml',
28-
'sql': '.sql',
29-
'sass': '.sass',
30-
'scss': '.scss',
31-
'less': '.less',
32-
'stylus': '.styl',
33-
'coffeescript': '.coffee',
34-
'markdown': '.md',
35-
'yaml': '.yml',
36-
'ruby': '.rb',
37-
'go': '.go',
38-
'rust': '.rs',
39-
'swift': '.swift',
40-
'kotlin': '.kt',
41-
'dart': '.dart',
42-
'vue': '.vue',
43-
'jsx': '.jsx',
44-
'tsx': '.tsx'
15+
javascript: ".js",
16+
css: ".css",
17+
html: ".html",
18+
php: ".php",
19+
python: ".py",
20+
java: ".java",
21+
c: ".c",
22+
cpp: ".cpp",
23+
csharp: ".cs",
24+
typescript: ".ts",
25+
json: ".json",
26+
xml: ".xml",
27+
sql: ".sql",
28+
sass: ".sass",
29+
scss: ".scss",
30+
less: ".less",
31+
stylus: ".styl",
32+
coffeescript: ".coffee",
33+
markdown: ".md",
34+
yaml: ".yml",
35+
ruby: ".rb",
36+
go: ".go",
37+
rust: ".rs",
38+
swift: ".swift",
39+
kotlin: ".kt",
40+
dart: ".dart",
41+
vue: ".vue",
42+
jsx: ".jsx",
43+
tsx: ".tsx"
4544
};
4645

4746
return languageMap[languageId] || languageId;
4847
}
4948

50-
5149
/**
5250
* This function is responsible to get the snippet data from all the required input fields
5351
* it is called when the save button is clicked
@@ -139,7 +137,7 @@ define(function (require, exports, module) {
139137
const effectiveExtension = mapLanguageToExtension(languageContext);
140138

141139
// if we have a proper mapping (starts with .), use language context matching
142-
if (effectiveExtension.startsWith('.')) {
140+
if (effectiveExtension.startsWith(".")) {
143141
const supportedExtensions = snippet.fileExtension
144142
.toLowerCase()
145143
.split(",")
@@ -241,13 +239,11 @@ define(function (require, exports, module) {
241239
// create highlighting for matching characters like other hint providers
242240
if (query && query.length > 0) {
243241
// use the StringMatch to get proper highlighting ranges
244-
const matchResult = StringMatch.stringMatch(abbr, query, {preferPrefixMatches: true});
242+
const matchResult = StringMatch.stringMatch(abbr, query, { preferPrefixMatches: true });
245243
if (matchResult && matchResult.stringRanges) {
246244
matchResult.stringRanges.forEach(function (item) {
247245
if (item.matched) {
248-
$hint.append($("<span>")
249-
.text(item.text)
250-
.addClass("matched-hint"));
246+
$hint.append($("<span>").text(item.text).addClass("matched-hint"));
251247
} else {
252248
$hint.append(item.text);
253249
}
@@ -463,7 +459,7 @@ define(function (require, exports, module) {
463459
// to insert the tab character
464460
textarea.value = value.substring(0, start) + "\t" + value.substring(end);
465461
textarea.selectionStart = textarea.selectionEnd = start + 1;
466-
$(textarea).trigger('input');
462+
$(textarea).trigger("input");
467463
}
468464
}
469465

@@ -475,51 +471,66 @@ define(function (require, exports, module) {
475471

476472
// Allow navigation and function keys
477473
const allowedKeys = [
478-
'Backspace', 'Delete', 'Tab', 'Escape', 'Enter',
479-
'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown',
480-
'Home', 'End', 'PageUp', 'PageDown',
481-
'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12'
474+
"Backspace",
475+
"Delete",
476+
"Tab",
477+
"Escape",
478+
"Enter",
479+
"ArrowLeft",
480+
"ArrowRight",
481+
"ArrowUp",
482+
"ArrowDown",
483+
"Home",
484+
"End",
485+
"PageUp",
486+
"PageDown",
487+
"F1",
488+
"F2",
489+
"F3",
490+
"F4",
491+
"F5",
492+
"F6",
493+
"F7",
494+
"F8",
495+
"F9",
496+
"F10",
497+
"F11",
498+
"F12"
482499
];
483500

484501
if (allowedKeys.includes(e.key)) {
485502
return; // Allow these keys to work normally
486503
}
487504

488505
// Prevent space character
489-
if (e.key === ' ') {
506+
if (e.key === " ") {
490507
e.preventDefault();
491508

492509
// Determine if this is the edit form or new form
493-
const isEditForm = abbrBox.id === 'edit-abbr-box';
494-
const inputId = isEditForm ? 'edit-abbr-box' : 'abbr-box';
495-
const wrapperId = isEditForm ? 'edit-abbr-box-wrapper' : 'abbr-box-wrapper';
496-
const errorId = isEditForm ? 'edit-abbreviation-space-error' : 'abbreviation-space-error';
497-
498-
UIHelper.showError(
499-
inputId,
500-
wrapperId,
501-
"Space is not accepted as a valid abbreviation character.",
502-
errorId
503-
);
510+
const isEditForm = abbrBox.id === "edit-abbr-box";
511+
const inputId = isEditForm ? "edit-abbr-box" : "abbr-box";
512+
const wrapperId = isEditForm ? "edit-abbr-box-wrapper" : "abbr-box-wrapper";
513+
const errorId = isEditForm ? "edit-abbreviation-space-error" : "abbreviation-space-error";
514+
515+
UIHelper.showError(inputId, wrapperId, "Space is not accepted as a valid abbreviation character.", errorId);
504516
return;
505517
}
506518

507519
// Check for character limit (30 characters) - only for printable characters
508-
if (abbrBox.value.length >= 30 && e.key.length === 1 && e.key.match(/[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/)) {
520+
if (
521+
abbrBox.value.length >= 30 &&
522+
e.key.length === 1 &&
523+
e.key.match(/[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/)
524+
) {
509525
e.preventDefault();
510526

511527
// Determine if this is the edit form or new form
512-
const isEditForm = abbrBox.id === 'edit-abbr-box';
513-
const inputId = isEditForm ? 'edit-abbr-box' : 'abbr-box';
514-
const wrapperId = isEditForm ? 'edit-abbr-box-wrapper' : 'abbr-box-wrapper';
515-
const errorId = isEditForm ? 'edit-abbreviation-length-error' : 'abbreviation-length-error';
516-
517-
UIHelper.showError(
518-
inputId,
519-
wrapperId,
520-
"Abbreviation cannot be more than 30 characters.",
521-
errorId
522-
);
528+
const isEditForm = abbrBox.id === "edit-abbr-box";
529+
const inputId = isEditForm ? "edit-abbr-box" : "abbr-box";
530+
const wrapperId = isEditForm ? "edit-abbr-box-wrapper" : "abbr-box-wrapper";
531+
const errorId = isEditForm ? "edit-abbreviation-length-error" : "abbreviation-length-error";
532+
533+
UIHelper.showError(inputId, wrapperId, "Abbreviation cannot be more than 30 characters.", errorId);
523534
}
524535
}
525536

@@ -575,21 +586,16 @@ define(function (require, exports, module) {
575586

576587
// Show appropriate error message
577588
if (wasTruncated || hadSpaces) {
578-
const isEditForm = $input.attr("id") === 'edit-abbr-box';
579-
const inputId = isEditForm ? 'edit-abbr-box' : 'abbr-box';
580-
const wrapperId = isEditForm ? 'edit-abbr-box-wrapper' : 'abbr-box-wrapper';
589+
const isEditForm = $input.attr("id") === "edit-abbr-box";
590+
const inputId = isEditForm ? "edit-abbr-box" : "abbr-box";
591+
const wrapperId = isEditForm ? "edit-abbr-box-wrapper" : "abbr-box-wrapper";
581592

582593
// Prioritize length error over space error if both occurred
583594
if (wasTruncated) {
584-
const errorId = isEditForm ? 'edit-abbreviation-paste-length-error' : 'abbreviation-paste-length-error';
585-
UIHelper.showError(
586-
inputId,
587-
wrapperId,
588-
"Abbreviation cannot be more than 30 characters.",
589-
errorId
590-
);
595+
const errorId = isEditForm ? "edit-abbreviation-paste-length-error" : "abbreviation-paste-length-error";
596+
UIHelper.showError(inputId, wrapperId, "Abbreviation cannot be more than 30 characters.", errorId);
591597
} else if (hadSpaces) {
592-
const errorId = isEditForm ? 'edit-abbreviation-paste-space-error' : 'abbreviation-paste-space-error';
598+
const errorId = isEditForm ? "edit-abbreviation-paste-space-error" : "abbreviation-paste-space-error";
593599
UIHelper.showError(
594600
inputId,
595601
wrapperId,

0 commit comments

Comments
 (0)