var D=Object.defineProperty;var R=(n,e)=>{for(var t in e)D(n,t,{get:e[t],enumerable:!0})};var W={};R(W,{SnippetFileSystem:()=>y,evaluateScriptSnippet:()=>x,findSnippetsProject:()=>U,isSnippetsProject:()=>K,isSnippetsUISourceCode:()=>_});import*as r from"./../../core/common/common.js";import*as C from"./../../core/i18n/i18n.js";import*as I from"./../../core/platform/platform.js";import*as c from"./../../core/sdk/sdk.js";import*as l from"./../../models/persistence/persistence.js";import*as T from"./../../models/text_utils/text_utils.js";import*as u from"./../../models/workspace/workspace.js";import*as v from"./../../ui/legacy/legacy.js";var P={scriptSnippet:"Script snippet #{PH1}",linkedTo:"Linked to {PH1}"},E=C.i18n.registerUIStrings("panels/snippets/ScriptSnippetFileSystem.ts",P),k=C.i18n.getLocalizedString.bind(void 0,E);function h(n){return r.ParsedURL.ParsedURL.rawPathToEncodedPathString(n)}function m(n){return r.ParsedURL.ParsedURL.encodedPathToRawPathString(n)}var y=class extends l.PlatformFileSystem.PlatformFileSystem{lastSnippetIdentifierSetting;snippetsSetting;constructor(){super("snippet://",l.PlatformFileSystem.PlatformFileSystemType.SNIPPETS,!1),this.lastSnippetIdentifierSetting=r.Settings.Settings.instance().createSetting("script-snippets-last-identifier",0),this.snippetsSetting=r.Settings.Settings.instance().createSetting("script-snippets",[])}initialFilePaths(){return this.snippetsSetting.get().map(t=>h(t.name))}async createFile(e,t){let i=this.lastSnippetIdentifierSetting.get()+1;this.lastSnippetIdentifierSetting.set(i);let o=k(P.scriptSnippet,{PH1:i}),s=this.snippetsSetting.get();return s.push({name:o,content:""}),this.snippetsSetting.set(s),h(o)}async deleteFile(e){let t=m(r.ParsedURL.ParsedURL.substring(e,1)),i=this.snippetsSetting.get(),o=i.filter(s=>s.name!==t);return i.length!==o.length?(this.snippetsSetting.set(o),!0):!1}async requestFileContent(e){let t=m(r.ParsedURL.ParsedURL.substring(e,1)),o=this.snippetsSetting.get().find(s=>s.name===t);return o?new T.ContentData.ContentData(o.content,!1,"text/javascript"):{error:`A snippet with name '${t}' was not found`}}async setFileContent(e,t,i){let o=m(r.ParsedURL.ParsedURL.substring(e,1)),s=this.snippetsSetting.get(),p=s.find(a=>a.name===o);return p?(p.content=t,this.snippetsSetting.set(s),!0):!1}renameFile(e,t,i){let o=m(r.ParsedURL.ParsedURL.substring(e,1)),s=this.snippetsSetting.get(),p=s.find(a=>a.name===o);if(t=r.ParsedURL.ParsedURL.trim(t),!p||t.length===0||s.find(a=>a.name===t)){i(!1);return}p.name=t,this.snippetsSetting.set(s),i(!0,t)}async searchInPath(e,t){let i=new RegExp(I.StringUtilities.escapeForRegExp(e),"i");return this.snippetsSetting.get().filter(p=>p.content.match(i)).map(p=>`snippet:///${h(p.name)}`)}mimeFromPath(e){return"text/javascript"}contentType(e){return r.ResourceType.resourceTypes.Script}tooltipForURL(e){return k(P.linkedTo,{PH1:m(r.ParsedURL.ParsedURL.sliceUrlToEncodedPathString(e,this.path().length))})}supportsAutomapping(){return!0}};async function x(n){if(!r.ParsedURL.schemeIs(n.url(),"snippet:"))return;let e=v.Context.Context.instance().flavor(c.RuntimeModel.ExecutionContext);if(!e)return;let t=e.runtimeModel,i=e.target().model(c.ConsoleModel.ConsoleModel);await n.requestContentData(),n.commitWorkingCopy();let o=n.workingCopy();r.Console.Console.instance().show();let s=n.url(),p=await e.evaluate({expression:`${o}
//# sourceURL=${s}`,objectGroup:"console",silent:!1,includeCommandLineAPI:!0,returnByValue:!1,generatePreview:!0,replMode:!0},!0,!0);if("exceptionDetails"in p&&p.exceptionDetails){i?.addMessage(c.ConsoleModel.ConsoleMessage.fromException(t,p.exceptionDetails,void 0,void 0,s));return}if(!("object"in p)||!p.object)return;let a=e.debuggerModel.scriptsForSourceURL(s);if(a.length<1)return;let w=a[a.length-1].scriptId,b={type:c.ConsoleModel.FrontendMessageType.Result,url:s,parameters:[p.object],executionContextId:e.id,scriptId:w};i?.addMessage(new c.ConsoleModel.ConsoleMessage(t,"javascript","info","",b))}function _(n){return r.ParsedURL.schemeIs(n.url(),"snippet:")}function K(n){return n.type()===u.Workspace.projectTypes.FileSystem&&l.FileSystemWorkspaceBinding.FileSystemWorkspaceBinding.fileSystemType(n)===l.PlatformFileSystem.PlatformFileSystemType.SNIPPETS}function U(){let n=u.Workspace.WorkspaceImpl.instance().projectsForType(u.Workspace.projectTypes.FileSystem).find(e=>l.FileSystemWorkspaceBinding.FileSystemWorkspaceBinding.fileSystemType(e)==="snippets");if(!n)throw new Error("Unable to find workspace project for the snippets file system");return n}var j={};R(j,{SnippetsQuickOpen:()=>S});import"./../../ui/kit/kit.js";import"./../../ui/components/highlighting/highlighting.js";import*as f from"./../../core/i18n/i18n.js";import*as g from"./../../ui/legacy/components/quick_open/quick_open.js";import{html as B}from"./../../ui/lit/lit.js";var d={noSnippetsFound:"No snippets found.",run:"Run",snippet:"Snippet",runSnippet:"Run snippet"},M=f.i18n.registerUIStrings("panels/snippets/SnippetsQuickOpen.ts",d),O=f.i18n.getLocalizedString.bind(void 0,M),F=f.i18n.getLazilyComputedLocalizedString.bind(void 0,M),L,S=class n extends g.FilteredListWidget.Provider{snippets=[];static instance(e={forceNew:null}){let{forceNew:t}=e;return(!L||t)&&(L=new n),L}selectItem(e,t){e!==null&&x(this.snippets[e])}notFoundText(e){return O(d.noSnippetsFound)}attach(){this.snippets=[...U().uiSourceCodes()]}detach(){this.snippets=[]}itemScoreAt(e,t){return t.length/this.snippets[e].name().length}itemCount(){return this.snippets.length}itemKeyAt(e){return this.snippets[e].name()}renderItem(e,t){let i=this.snippets[e].name(),o=g.FilteredListWidget.FilteredListWidget.getHighlightRanges(i,t,!0);return B`
      <devtools-icon class="snippet" name="snippet"></devtools-icon>
      <div>
        <devtools-highlight type="markup" ranges=${o}>
          ${i}
        </devtools-highlight>
      </div>`}};g.FilteredListWidget.registerProvider({prefix:"!",iconName:"exclamation",provider:()=>Promise.resolve(S.instance()),helpTitle:F(d.runSnippet),titlePrefix:F(d.run),titleSuggestion:F(d.snippet),jslogContext:"snippet"});export{W as ScriptSnippetFileSystem,j as SnippetsQuickOpen};
//# sourceMappingURL=snippets.js.map
