import*as i18n from"../i18n/i18n.js";import*as Persistence from"../persistence/persistence.js";import*as QuickOpen from"../quick_open/quick_open.js";import*as UI from"../ui/ui.js";import*as Workspace from"../workspace/workspace.js";import{FilePathScoreFunction}from"./FilePathScoreFunction.js";const UIStrings={noFilesFound:"No files found"},str_=i18n.i18n.registerUIStrings("sources/FilteredUISourceCodeListProvider.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class FilteredUISourceCodeListProvider extends QuickOpen.FilteredListWidget.Provider{constructor(){super(),this._queryLineNumberAndColumnNumber="",this._defaultScores=null,this._scorer=new FilePathScoreFunction(""),this._uiSourceCodes=[],this._uiSourceCodeUrls=new Set,this._query}_projectRemoved(e){const t=e.data;this._populate(t),this.refresh()}_populate(e){this._uiSourceCodes=[],this._uiSourceCodeUrls.clear();for(const t of Workspace.Workspace.WorkspaceImpl.instance().projects())if(t!==e&&this.filterProject(t))for(const e of t.uiSourceCodes())this._filterUISourceCode(e)&&(this._uiSourceCodes.push(e),this._uiSourceCodeUrls.add(e.url()))}_filterUISourceCode(e){if(this._uiSourceCodeUrls.has(e.url()))return!1;const t=Persistence.Persistence.PersistenceImpl.instance().binding(e);return!t||t.fileSystem===e}uiSourceCodeSelected(e,t,s){}filterProject(e){return!0}itemCount(){return this._uiSourceCodes.length}itemKeyAt(e){return this._uiSourceCodes[e].url()}setDefaultScores(e){this._defaultScores=e}itemScoreAt(e,t){const s=this._uiSourceCodes[e],r=this._defaultScores&&this._defaultScores.get(s)||0;if(!t||t.length<2)return r;this._query!==t&&(this._query=t,this._scorer=new FilePathScoreFunction(t));let i=10;s.project().type()!==Workspace.Workspace.projectTypes.FileSystem||Persistence.Persistence.PersistenceImpl.instance().binding(s)||(i=5);const o=s.fullDisplayName();return r+i*this._scorer.score(o,null)}renderItem(e,t,s,r){t=this.rewriteQuery(t);const i=this._uiSourceCodes[e],o=i.fullDisplayName(),n=[];new FilePathScoreFunction(t).score(o,n);const c=o.lastIndexOf("/");s.classList.add("monospace"),r.classList.add("monospace"),s.textContent=i.displayName()+(this._queryLineNumberAndColumnNumber||""),this._renderSubtitleElement(r,o),UI.Tooltip.Tooltip.install(r,o);const u=[];for(let l=0;l<n.length;++l)u.push({offset:n[l],length:1});if(n[0]>c){for(let e=0;e<u.length;++e)u[e].offset-=c+1;UI.UIUtils.highlightRangesWithStyleClass(s,u,"highlight")}else UI.UIUtils.highlightRangesWithStyleClass(r,u,"highlight")}_renderSubtitleElement(e,t){e.removeChildren();let s=t.lastIndexOf("/");t.length>55&&(s=t.length-55);e.createChild("div","first-part").textContent=t.substring(0,s);e.createChild("div","second-part").textContent=t.substring(s),UI.Tooltip.Tooltip.install(e,t)}selectItem(e,t){const s=t.trim().match(/^([^:]*)(:\d+)?(:\d+)?$/);if(!s)return;let r,i;s[2]&&(r=parseInt(s[2].substr(1),10)-1),s[3]&&(i=parseInt(s[3].substr(1),10)-1);const o=null!==e?this._uiSourceCodes[e]:null;this.uiSourceCodeSelected(o,r,i)}rewriteQuery(e){if(!(e=e?e.trim():"")||":"===e)return"";const t=e.match(/^([^:]+)((?::[^:]*){0,2})$/);return this._queryLineNumberAndColumnNumber=t?t[2]:"",t?t[1]:e}_uiSourceCodeAdded(e){const t=e.data;this._filterUISourceCode(t)&&this.filterProject(t.project())&&(this._uiSourceCodes.push(t),this._uiSourceCodeUrls.add(t.url()),this.refresh())}notFoundText(){return i18nString(UIStrings.noFilesFound)}attach(){Workspace.Workspace.WorkspaceImpl.instance().addEventListener(Workspace.Workspace.Events.UISourceCodeAdded,this._uiSourceCodeAdded,this),Workspace.Workspace.WorkspaceImpl.instance().addEventListener(Workspace.Workspace.Events.ProjectRemoved,this._projectRemoved,this),this._populate()}detach(){Workspace.Workspace.WorkspaceImpl.instance().removeEventListener(Workspace.Workspace.Events.UISourceCodeAdded,this._uiSourceCodeAdded,this),Workspace.Workspace.WorkspaceImpl.instance().removeEventListener(Workspace.Workspace.Events.ProjectRemoved,this._projectRemoved,this),this._queryLineNumberAndColumnNumber="",this._defaultScores=null}}