Skip to content

Commit b63cbbf

Browse files
committed
chore: use picomatch for glob matching
1 parent bf3cebe commit b63cbbf

File tree

7 files changed

+181
-187
lines changed

7 files changed

+181
-187
lines changed

package-lock.json

Lines changed: 61 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"@floating-ui/dom": "^0.5.4",
9090
"@fortawesome/fontawesome-free": "^6.1.2",
9191
"@highlightjs/cdn-assets": "^11.5.1",
92-
"@phcode/fs": "^2.1.0",
92+
"@phcode/fs": "^3.0.0",
9393
"@pixelbrackets/gfm-stylesheet": "^1.1.0",
9494
"@prettier/plugin-php": "0.18.9",
9595
"@uiw/file-icons": "^1.3.2",
@@ -113,4 +113,4 @@
113113
"tinycolor2": "^1.4.2",
114114
"underscore": "^1.13.4"
115115
}
116-
}
116+
}

src-node/package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"IMPORTANT!!": "Adding things here will bloat up the package size",
2121
"dependencies": {
22-
"@phcode/fs": "^2.1.0",
22+
"@phcode/fs": "^3.0.0",
2323
"npm": "10.1.0",
2424
"ws": "^8.13.0",
2525
"lmdb": "^2.9.2",

src/nls/root/strings.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ define({
249249
"INCLUDE_FILE_FILTER_DROPDOWN": "Include",
250250
"EDIT_FILE_FILTER": "Edit\u2026",
251251
"FILE_FILTER_DIALOG": "Edit Exclusion Filter",
252-
"FILE_FILTER_INSTRUCTIONS": "Exclude files and folders matching any of the following <a href='{0}' title='{0}'>ignore wildcards</a>. Enter each string on a new line.",
253-
"FILE_FILTER_INSTRUCTIONS_INCLUDE": "Include only files and folders matching any of the following <a href='{0}' title='{0}'>ignore wildcards</a>. Enter each string on a new line.",
252+
"FILE_FILTER_INSTRUCTIONS": "Exclude files and folders matching any of the following <a href='{0}' title='{0}'>ignore wildcards</a>. Enter each string on a new line. E.g. <ul><li><code>*.js</code> will match <code>a/b/x.js</code>, <code>xyx.js</code>, etc..</li> <li><code>./*.css</code> will only match <code>x.css</code> in project root, but not <code>y/x.css</code> in sub folder.</li></ul>",
253+
"FILE_FILTER_INSTRUCTIONS_INCLUDE": "Include only files and folders matching any of the following <a href='{0}' title='{0}'>ignore wildcards</a>. Enter each string on a new line. E.g. <ul><li><code>*.js</code> will match <code>a/b/x.js</code>, <code>xyx.js</code>, etc..</li> <li><code>./*.css</code> will only match <code>x.css</code> in project root, but not <code>y/x.css</code> in sub folder.</li></ul>",
254254
"FILTER_NAME_PLACEHOLDER": "Name this exclusion set (optional)",
255255
"FILTER_NAME_REMAINING": "{0} characters remaining",
256256
"FILE_FILTER_CLIPPED_SUFFIX": "and {0} more",

src/search/FileFilters.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,48 @@ define(function (require, exports, module) {
210210
* that can be used with filterPath()/filterFileList().
211211
* @param {!Array.<string>} userFilter
212212
* @param {string} filterType - one of FILTER_TYPE_EXCLUDE or FILTER_TYPE_INCLUDE
213-
* @return {{add: function, filter: function}} a gitIgnoreFilter filter that can be passed to filterPath()/filterFileList().
213+
* @return {{filterType: string, ignores: function}} a globeFilter filter that can be passed to filterPath()/filterFileList().
214214
*/
215215
function compile(userFilter, filterType) {
216-
const gitIgnoreFilter = window.fs.utils.ignore();
217-
gitIgnoreFilter.add(userFilter);
218-
gitIgnoreFilter.filterType = filterType || FILTER_TYPE_EXCLUDE;
219-
return gitIgnoreFilter;
216+
// Automatically apply transforms make writing simple filters more intuitive
217+
const subStringFilter = [];
218+
const wrappedGlobs = [];
219+
for(let glob of userFilter){
220+
// *.js -> **/*.js; *.config.js -> **/*.config.js; ?.js -> **/?.js;
221+
if (glob.startsWith("*.") || glob.startsWith("?.")) {
222+
wrappedGlobs.push(`**/${glob}`); // **/*.txt
223+
continue;
224+
}
225+
// ./ will only match in present project root, this is as an escape for the above transform we apply
226+
if(glob.startsWith("./")) {
227+
wrappedGlobs.push(glob.slice(2)); // ./*.txt to *.txt
228+
continue;
229+
}
230+
// if not a glob string, we should do a string.includes search to match any substring.
231+
if(!(glob.includes("?") || glob.includes("*") ||
232+
glob.includes("[") || glob.includes("]") ||
233+
glob.includes("\\") || glob.includes("!"))) {
234+
subStringFilter.push(glob);
235+
continue;
236+
}
237+
wrappedGlobs.push(glob);
238+
}
239+
240+
const isMatch = window.fs.utils.picomatch(wrappedGlobs, {
241+
dot: true
242+
});
243+
function ignores(relativeOrFullPath) {
244+
for(let subStr of subStringFilter){
245+
if(relativeOrFullPath.includes(subStr)){
246+
return true;
247+
}
248+
}
249+
return isMatch(relativeOrFullPath);
250+
}
251+
return {
252+
ignores: ignores,
253+
filterType: filterType || FILTER_TYPE_EXCLUDE
254+
};
220255
}
221256

222257

@@ -307,7 +342,7 @@ define(function (require, exports, module) {
307342
function _getInstructionText() {
308343
return StringUtils.format(
309344
isExclusionFilter ? Strings.FILE_FILTER_INSTRUCTIONS : Strings.FILE_FILTER_INSTRUCTIONS_INCLUDE,
310-
"https://git-scm.com/docs/gitignore#_pattern_format");
345+
"https://docs.phcode.dev/docs/find-in-files/#creating-an-exclusioninclusion-filter");
311346
}
312347

313348
let templateVars = {

0 commit comments

Comments
 (0)