Skip to content

Instantly share code, notes, and snippets.

@forivall
Last active January 5, 2022 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forivall/1c8a512a8dcaba123334ba9dcefb8a06 to your computer and use it in GitHub Desktop.
Save forivall/1c8a512a8dcaba123334ba9dcefb8a06 to your computer and use it in GitHub Desktop.
/**
* For vscode globs that can't just do `!`, but do have negative char classes
*
* turn `test` into `{[^t]*,?[^e]*,??[^s]*,???[^t]*}`
* `{[^t]*,?{,[^r]*,?{,[^a]*,?[^p]*}}}`
*
* two directories need to be excluded, the following would be needed:
* (ex. for negating 'test' or 'glob')
*
* `{[^tg]*,?[^el]*,??[^so]*,???[^tb]*,te{sb,ob,ot},t{ls,lo,eo}t,{tl,ge,gl}st,gl{ot,sb,st},g{es,ls,eo}b,{ge,tl,te}ob`
*
* @see https://github.com/microsoft/vscode/issues/869?ts=2#issuecomment-804615384
*/
function globNegate(s: string): string {
switch (s.length) {
case 0:
return '';
case 1:
return `{[^${s}]*,??*}`;
case 2:
return `{[^${s[0]}]*,?[${s[1]}]*,???*}`;
}
return [...s]
.reduceRight((p, c) => `{,[^${c}]*,?${p}}`, `?*`)
.replace(/^\{,/, '{');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment