@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 5.29 kB
JavaScript
import*as Common from"../../core/common/common.js";import*as Platform from"../../core/platform/platform.js";import*as Bindings from"../../models/bindings/bindings.js";import*as Persistence from"../../models/persistence/persistence.js";import*as TextUtils from"../../models/text_utils/text_utils.js";import*as Workspace from"../../models/workspace/workspace.js";export class SourcesSearchScope{searchId;searchResultCandidates;searchResultCallback;searchFinishedCallback;searchConfig;constructor(){this.searchId=0,this.searchResultCandidates=[],this.searchResultCallback=null,this.searchFinishedCallback=null,this.searchConfig=null}static filesComparator(e,t){if(e.isDirty()&&!t.isDirty())return-1;if(!e.isDirty()&&t.isDirty())return 1;const s=e.project().type()===Workspace.Workspace.projectTypes.FileSystem&&!Persistence.Persistence.PersistenceImpl.instance().binding(e);if(s!==(t.project().type()===Workspace.Workspace.projectTypes.FileSystem&&!Persistence.Persistence.PersistenceImpl.instance().binding(t)))return s?1:-1;const r=e.url(),i=t.url();return r&&!i?-1:!r&&i?1:Platform.StringUtilities.naturalOrderComparator(e.fullDisplayName(),t.fullDisplayName())}static urlComparator(e,t){return Platform.StringUtilities.naturalOrderComparator(e.url(),t.url())}performIndexing(e){this.stopSearch();const t=this.projects(),s=new Common.Progress.CompositeProgress(e);for(let e=0;e<t.length;++e){const r=t[e],i=s.createSubProgress([...r.uiSourceCodes()].length);r.indexContent(i)}}projects(){const e=Common.Settings.Settings.instance().moduleSetting("searchInAnonymousAndContentScripts").get();return Workspace.Workspace.WorkspaceImpl.instance().projects().filter((t=>t.type()!==Workspace.Workspace.projectTypes.Service&&(!(!e&&t.isServiceProject()&&t.type()!==Workspace.Workspace.projectTypes.Formatter)&&!(!e&&t.type()===Workspace.Workspace.projectTypes.ContentScripts))))}performSearch(e,t,s,r){this.stopSearch(),this.searchResultCandidates=[],this.searchResultCallback=s,this.searchFinishedCallback=r,this.searchConfig=e;const i=[],o=new Common.Progress.CompositeProgress(t),c=o.createSubProgress(),a=new Common.Progress.CompositeProgress(o.createSubProgress());for(const t of this.projects()){const s=[...t.uiSourceCodes()].length,r=a.createSubProgress(s),o=this.projectFilesMatchingFileQuery(t,e),c=t.findFilesMatchingSearchRequest(e,o,r).then(this.processMatchingFilesForProject.bind(this,this.searchId,t,e,o));i.push(c)}Promise.all(i).then(this.processMatchingFiles.bind(this,this.searchId,c,this.searchFinishedCallback.bind(this,!0)))}projectFilesMatchingFileQuery(e,t,s){const r=[];for(const i of e.uiSourceCodes()){if(!i.contentType().isTextType())continue;if(Bindings.IgnoreListManager.IgnoreListManager.instance().isUserOrSourceMapIgnoreListedUISourceCode(i))continue;const e=Persistence.Persistence.PersistenceImpl.instance().binding(i);e&&e.network===i||(s&&!i.isDirty()||t.filePathMatchesFileQuery(i.fullDisplayName())&&r.push(i))}return r.sort(SourcesSearchScope.urlComparator),r}processMatchingFilesForProject(e,t,s,r,i){if(e!==this.searchId&&this.searchFinishedCallback)return void this.searchFinishedCallback(!1);i.sort(SourcesSearchScope.urlComparator),i=Platform.ArrayUtilities.intersectOrdered(i,r,SourcesSearchScope.urlComparator);const o=this.projectFilesMatchingFileQuery(t,s,!0);i=Platform.ArrayUtilities.mergeOrdered(i,o,SourcesSearchScope.urlComparator);const c=[];for(const e of i){const t=Bindings.DefaultScriptMapping.DefaultScriptMapping.scriptForUISourceCode(e);t&&!t.isAnonymousScript()||c.push(e)}c.sort(SourcesSearchScope.filesComparator),this.searchResultCandidates=Platform.ArrayUtilities.mergeOrdered(this.searchResultCandidates,c,SourcesSearchScope.filesComparator)}processMatchingFiles(e,t,s){if(e!==this.searchId&&this.searchFinishedCallback)return void this.searchFinishedCallback(!1);const r=this.searchResultCandidates;if(!r.length)return t.done(),void s();t.setTotalWork(r.length);let i=0;let o=0;for(let e=0;e<20&&e<r.length;++e)a.call(this);function c(e){e.isDirty()?n.call(this,e,e.workingCopy()):e.requestContent().then((t=>{n.call(this,e,t.content||"")}))}function a(){if(i>=r.length)return o?void 0:(t.done(),void s());++o;const e=r[i++];window.setTimeout(c.bind(this,e),0)}function n(e,s){t.incrementWorked(1);let r=[];const i=this.searchConfig,c=i.queries();if(null!==s){for(let e=0;e<c.length;++e){const t=TextUtils.TextUtils.performSearchInContent(s,c[e],!i.ignoreCase(),i.isRegex());r=Platform.ArrayUtilities.mergeOrdered(r,t,TextUtils.ContentProvider.SearchMatch.comparator)}i.queries().length||(r=[new TextUtils.ContentProvider.SearchMatch(0,new TextUtils.Text.Text(s).lineAt(0))])}if(r&&this.searchResultCallback){const t=new FileBasedSearchResult(e,r);this.searchResultCallback(t)}--o,a.call(this)}}stopSearch(){++this.searchId}}export class FileBasedSearchResult{uiSourceCode;searchMatches;constructor(e,t){this.uiSourceCode=e,this.searchMatches=t}label(){return this.uiSourceCode.displayName()}description(){return this.uiSourceCode.fullDisplayName()}matchesCount(){return this.searchMatches.length}matchLineContent(e){return this.searchMatches[e].lineContent}matchRevealable(e){const t=this.searchMatches[e];return this.uiSourceCode.uiLocation(t.lineNumber,t.columnNumber)}matchLabel(e){return this.searchMatches[e].lineNumber+1}}