Skip to content

Commit 9f43733

Browse files
committed
refactor: improve code readability
1 parent 6098c69 commit 9f43733

File tree

2 files changed

+62
-39
lines changed

2 files changed

+62
-39
lines changed

src/extensionsIntegrated/CustomSnippets/src/helper.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ define(function (require, exports, module) {
183183
$("#file-extn-box").val("");
184184
}
185185

186+
/**
187+
* Updates the snippets count which is displayed in the toolbar at the left side
188+
* @private
189+
*/
190+
function updateSnippetsCount() {
191+
const count = Global.SnippetHintsList.length;
192+
const $countSpan = $("#snippets-count");
193+
if (count > 0) {
194+
$countSpan.text(`(${count})`);
195+
} else {
196+
$countSpan.text("");
197+
}
198+
}
199+
186200
exports.toggleSaveButtonDisability = toggleSaveButtonDisability;
187201
exports.createHintItem = createHintItem;
188202
exports.clearAllInputFields = clearAllInputFields;
@@ -191,4 +205,5 @@ define(function (require, exports, module) {
191205
exports.isSnippetSupportedInFile = isSnippetSupportedInFile;
192206
exports.hasExactMatchingSnippet = hasExactMatchingSnippet;
193207
exports.getMatchingSnippets = getMatchingSnippets;
208+
exports.updateSnippetsCount = updateSnippetsCount;
194209
});

src/extensionsIntegrated/CustomSnippets/src/snippetsList.js

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ define(function (require, exports, module) {
1010
const SnippetsState = require("./snippetsState");
1111
const UIHelper = require("./UIHelper");
1212
const FilterSnippets = require("./filterSnippets");
13+
const Helper = require("./helper");
1314

1415
/**
1516
* This function is responsible to create a snippet item
@@ -58,61 +59,68 @@ define(function (require, exports, module) {
5859
}
5960

6061
/**
61-
* Updates the snippets count which is displayed in the toolbar at the left side
62+
* Shows the appropriate empty state message based on context
63+
* the context might be either one of the two cases:
64+
* when no snippets are added
65+
* or
66+
* when no snippets match the filtered text
6267
* @private
6368
*/
64-
function _updateSnippetsCount() {
65-
const count = Global.SnippetHintsList.length;
66-
const $countSpan = $("#snippets-count");
67-
if (count > 0) {
68-
$countSpan.text(`(${count})`);
69+
function _showEmptyState() {
70+
UIHelper.showEmptySnippetMessage();
71+
const $emptyMessage = $("#no-snippets-message");
72+
const $filterInput = $("#filter-snippets-input");
73+
const filterText = $filterInput.val().trim();
74+
75+
if (filterText) {
76+
$emptyMessage.text(`No snippets match "${filterText}"`);
6977
} else {
70-
$countSpan.text("");
78+
$emptyMessage.text("No custom snippets added yet!");
7179
}
7280
}
7381

82+
/**
83+
* this function is responsible to render the filtered snippets list
84+
* @private
85+
* @param {Array} filteredSnippets - array of filtered snippet objects
86+
*/
87+
function _renderSnippetsList(filteredSnippets) {
88+
UIHelper.showSnippetsList(); // show the snippets list wrapper
89+
90+
// add each filtered snippet to the list
91+
filteredSnippets.forEach(function(snippetItem) {
92+
_createSnippetItem(snippetItem);
93+
});
94+
}
95+
7496
/**
7597
* This function is called when the user clicks on the custom snippets button from the file menu
7698
* this also gets called when user clicks on the 'back' button to move back to the snippets list menu
7799
* refer to '_registerHandlers' function inside the main.js file
78100
*/
79101
function showSnippetsList() {
80-
UIHelper.clearSnippetsList(); // to clear existing snippets list, as we'll rebuild it
81-
const snippetList = Global.SnippetHintsList; // gets the list of the snippets, this is an array of objects
102+
UIHelper.clearSnippetsList(); // clear existing snippets list, as we'll rebuild it
103+
const snippetList = Global.SnippetHintsList; // get the list of snippets
82104

83-
_updateSnippetsCount();
105+
Helper.updateSnippetsCount();
84106

85-
// if there are no snippets available, we show the message that no snippets are present
86-
// refer to html file
107+
// handle empty snippets case
87108
if (snippetList.length === 0) {
88-
UIHelper.showEmptySnippetMessage();
89-
} else {
90-
// Apply filter to the snippets list
91-
const filteredSnippets = FilterSnippets.filterSnippets(snippetList);
92-
93-
// Check if we have any snippets after filtering
94-
if (filteredSnippets.length === 0) {
95-
// Show a message indicating no matches found
96-
UIHelper.showEmptySnippetMessage();
97-
const $emptyMessage = $("#no-snippets-message");
98-
const $filterInput = $("#filter-snippets-input");
99-
const filterText = $filterInput.val().trim();
100-
101-
if (filterText) {
102-
$emptyMessage.text(`No snippets match "${filterText}"`);
103-
} else {
104-
$emptyMessage.text("No custom snippets added yet!");
105-
}
106-
} else {
107-
UIHelper.showSnippetsList(); // to remove the hidden class from the snippets list wrapper
108-
109-
// rebuild the snippets menu with filtered results
110-
for (let i = 0; i < filteredSnippets.length; i++) {
111-
const snippetItem = filteredSnippets[i];
112-
_createSnippetItem(snippetItem);
113-
}
114-
}
109+
_showEmptyState();
110+
return;
111+
}
112+
113+
// apply the filtering and get results
114+
const filteredSnippets = FilterSnippets.filterSnippets(snippetList);
115+
116+
// if there are no matches after filtering
117+
if (filteredSnippets.length === 0) {
118+
_showEmptyState();
119+
return;
115120
}
121+
122+
// render the filtered snippets
123+
_renderSnippetsList(filteredSnippets);
116124
}
117125

118126
/**

0 commit comments

Comments
 (0)