Skip to content

Commit 3b36328

Browse files
committed
feat: allow users to write file extension in any format
1 parent 7bf9e3e commit 3b36328

File tree

1 file changed

+74
-2
lines changed
  • src/extensionsIntegrated/CustomSnippets

1 file changed

+74
-2
lines changed

src/extensionsIntegrated/CustomSnippets/helper.js

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,72 @@ define(function (require, exports, module) {
9595
return languageMap[languageId] || languageId;
9696
}
9797

98+
/**
99+
* This function is to make sure file extensions are properly formatted with leading dots
100+
* because user may provide values in not very consistent manner, we need to handle all those cases
101+
* For ex: what we expect: `.js, .html, .css`
102+
* what user may provide: `js, html, css` or: `js html css` or `.js.html.css` etc
103+
*
104+
* This function processes file extensions in various formats and ensures they:
105+
* - Have a leading dot (if not empty or "all")
106+
* - Are properly separated with commas and spaces
107+
* - Don't contain empty or standalone dots
108+
*
109+
* @param {string} extension - The file extension(s) to process
110+
* @returns {string} - The properly formatted file extension(s)
111+
*/
112+
function processFileExtensionInput(extension) {
113+
if (!extension || extension === "all") {
114+
return extension;
115+
}
116+
117+
// Step 1: normalize the input by converting spaces to commas if no commas exist
118+
if (extension.includes(" ")) {
119+
extension = extension.replace(/\s+/g, ",");
120+
}
121+
122+
// Step 2: handle multiple extensions joined by dots (e.g., ".less.css.js")
123+
// Only process if multiple dots exist and not already comma-separated
124+
const dotCount = (extension.match(/\./g) || []).length;
125+
if (dotCount > 1) {
126+
// remove the leading dot if present for consistent processing
127+
const extensionWithoutLeadingDot = extension.startsWith(".") ? extension.substring(1) : extension;
128+
129+
// split by dot, filter empty parts, add leading dot to each part
130+
const parts = extensionWithoutLeadingDot
131+
.split(".")
132+
.filter((part) => part !== "")
133+
.map((part) => "." + part);
134+
135+
return parts.join(", ");
136+
}
137+
138+
// Step 3: process comma-separated extensions
139+
if (extension.includes(",")) {
140+
return extension
141+
.split(",")
142+
.map((ext) => {
143+
ext = ext.trim();
144+
// skip all the standalone dots or empty entries
145+
if (ext === "." || ext === "") {
146+
return "";
147+
}
148+
// Add leading dot if missing
149+
return ext.startsWith(".") ? ext : "." + ext;
150+
})
151+
.filter((ext) => ext !== "") // Remove empty entries
152+
.join(", ");
153+
}
154+
155+
// Step 4: Handle single extension
156+
if (extension === ".") {
157+
return ""; // remove standalone dot
158+
}
159+
160+
// Add leading dot if missing
161+
return extension.startsWith(".") ? extension : "." + extension;
162+
}
163+
98164
/**
99165
* This function is responsible to get the snippet data from all the required input fields
100166
* it is called when the save button is clicked
@@ -108,11 +174,14 @@ define(function (require, exports, module) {
108174
const templateText = $("#template-text-box").val().trim();
109175
const fileExtension = $("#file-extn-box").val().trim();
110176

177+
// process the file extension so that we can get the value in the required format
178+
const processedFileExtension = processFileExtensionInput(fileExtension);
179+
111180
return {
112181
abbreviation: abbreviation,
113182
description: description || "", // allow empty description
114183
templateText: templateText,
115-
fileExtension: fileExtension || "all" // default to "all" if empty
184+
fileExtension: processedFileExtension || "all" // default to "all" if empty
116185
};
117186
}
118187

@@ -374,11 +443,14 @@ define(function (require, exports, module) {
374443
const templateText = $("#edit-template-text-box").val().trim();
375444
const fileExtension = $("#edit-file-extn-box").val().trim();
376445

446+
// process the file extension so that we can get the value in the required format
447+
const processedFileExtension = processFileExtensionInput(fileExtension);
448+
377449
return {
378450
abbreviation: abbreviation,
379451
description: description || "", // allow empty description
380452
templateText: templateText,
381-
fileExtension: fileExtension || "all" // default to "all" if empty
453+
fileExtension: processedFileExtension || "all" // default to "all" if empty
382454
};
383455
}
384456

0 commit comments

Comments
 (0)