SearchConfig.js 3.1 KB

123456789101112131415161718192021222324252627282930313233
  1. export class SearchConfig{constructor(query,ignoreCase,isRegex){this._query=query;this._ignoreCase=ignoreCase;this._isRegex=isRegex;this._parse();}
  2. static fromPlainObject(object){return new SearchConfig(object.query,object.ignoreCase,object.isRegex);}
  3. query(){return this._query;}
  4. ignoreCase(){return this._ignoreCase;}
  5. isRegex(){return this._isRegex;}
  6. toPlainObject(){return{query:this.query(),ignoreCase:this.ignoreCase(),isRegex:this.isRegex()};}
  7. _parse(){const quotedPattern=/"([^\\"]|\\.)+"/;const unquotedWordPattern=/(\s*(?!-?f(ile)?:)[^\\ ]|\\.)+/;const unquotedPattern=unquotedWordPattern.source+'(\\s+'+unquotedWordPattern.source+')*';const pattern=['(\\s*'+FilePatternRegex.source+'\\s*)','('+quotedPattern.source+')','('+unquotedPattern+')',].join('|');const regexp=new RegExp(pattern,'g');const queryParts=this._query.match(regexp)||[];this._fileQueries=[];this._queries=[];for(let i=0;i<queryParts.length;++i){const queryPart=queryParts[i];if(!queryPart){continue;}
  8. const fileQuery=this._parseFileQuery(queryPart);if(fileQuery){this._fileQueries.push(fileQuery);this._fileRegexQueries=this._fileRegexQueries||[];this._fileRegexQueries.push({regex:new RegExp(fileQuery.text,this.ignoreCase?'i':''),isNegative:fileQuery.isNegative});continue;}
  9. if(this._isRegex){this._queries.push(queryPart);continue;}
  10. if(queryPart.startsWith('"')){if(!queryPart.endsWith('"')){continue;}
  11. this._queries.push(this._parseQuotedQuery(queryPart));continue;}
  12. this._queries.push(this._parseUnquotedQuery(queryPart));}}
  13. filePathMatchesFileQuery(filePath){if(!this._fileRegexQueries){return true;}
  14. for(let i=0;i<this._fileRegexQueries.length;++i){if(!!filePath.match(this._fileRegexQueries[i].regex)===this._fileRegexQueries[i].isNegative){return false;}}
  15. return true;}
  16. queries(){return this._queries;}
  17. _parseUnquotedQuery(query){return query.replace(/\\(.)/g,'$1');}
  18. _parseQuotedQuery(query){return query.substring(1,query.length-1).replace(/\\(.)/g,'$1');}
  19. _parseFileQuery(query){const match=query.match(FilePatternRegex);if(!match){return null;}
  20. const isNegative=!!match[1];query=match[3];let result='';for(let i=0;i<query.length;++i){const char=query[i];if(char==='*'){result+='.*';}else if(char==='\\'){++i;const nextChar=query[i];if(nextChar===' '){result+=' ';}}else{if(String.regexSpecialCharacters().indexOf(query.charAt(i))!==-1){result+='\\';}
  21. result+=query.charAt(i);}}
  22. return new QueryTerm(result,isNegative);}}
  23. export const FilePatternRegex=/(-)?f(ile)?:((?:[^\\ ]|\\.)+)/;export class QueryTerm{constructor(text,isNegative){this.text=text;this.isNegative=isNegative;}}
  24. export class SearchResult{label(){}
  25. description(){}
  26. matchesCount(){}
  27. matchLabel(index){}
  28. matchLineContent(index){}
  29. matchRevealable(index){}}
  30. export class SearchScope{performSearch(searchConfig,progress,searchResultCallback,searchFinishedCallback){}
  31. performIndexing(progress){}
  32. stopSearch(){}}
  33. self.Search=self.Search||{};Search=Search||{};Search.SearchConfig=SearchConfig;Search.SearchConfig.RegexQuery;Search.SearchConfig.FilePatternRegex=FilePatternRegex;Search.SearchConfig.QueryTerm=QueryTerm;Search.SearchResult=SearchResult;Search.SearchScope=SearchScope;