import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as QuickOpen from"../quick_open/quick_open.js";import*as UI from"../ui/ui.js";import{SourcesView}from"./SourcesView.js";const UIStrings={noFileSelected:"No file selected.",typeANumberToGoToThatLine:"Type a number to go to that line.",currentPositionXsTypeAnOffset:"Current position: 0x{PH1}. Type an offset between 0x{PH2} and 0x{PH3} to navigate to.",currentLineSTypeALineNumber:"Current line: {PH1}. Type a line number between 1 and {PH2} to navigate to.",goToOffsetXs:"Go to offset 0x{PH1}.",goToLineSAndColumnS:"Go to line {PH1} and column {PH2}.",goToLineS:"Go to line {PH1}.",goToLine:"Go to line"},str_=i18n.i18n.registerUIStrings("sources/GoToLineQuickOpen.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_),i18nLazyString=i18n.i18n.getLazilyComputedLocalizedString.bind(void 0,str_);let goToLineQuickOpenInstance;export class GoToLineQuickOpen extends QuickOpen.FilteredListWidget.Provider{static instance(e={forceNew:null}){const{forceNew:n}=e;return goToLineQuickOpenInstance&&!n||(goToLineQuickOpenInstance=new GoToLineQuickOpen),goToLineQuickOpenInstance}selectItem(e,n){const t=this._currentUISourceCode();if(!t)return;const i=this._parsePosition(n);i&&Common.Revealer.reveal(t.uiLocation(i.line-1,i.column-1))}notFoundText(e){if(!this._currentUISourceCode())return i18nString(UIStrings.noFileSelected);const n=this._parsePosition(e),t=this._currentSourceFrame();if(!n){if(!t)return i18nString(UIStrings.typeANumberToGoToThatLine);const e=t.wasmDisassembly,n=t.textEditor.currentLineNumber();if(e){const n=e.lineNumberToBytecodeOffset(e.lineNumbers-1).toString(16).length;return i18nString(UIStrings.currentPositionXsTypeAnOffset,{PH1:"0".padStart(n,"0")})}const i=t.textEditor.linesCount;return i18nString(UIStrings.currentLineSTypeALineNumber,{PH1:n,PH2:i})}return t&&t.wasmDisassembly?i18nString(UIStrings.goToOffsetXs,{PH1:(n.column-1).toString(16)}):n.column&&n.column>1?i18nString(UIStrings.goToLineSAndColumnS,{PH1:n.line,PH2:n.column}):i18nString(UIStrings.goToLineS,{PH1:n.line})}_parsePosition(e){const n=this._currentSourceFrame();if(n&&n.wasmDisassembly){const n=e.match(/0x([0-9a-fA-F]+)/);if(!n||!n[0]||n[0].length!==e.length)return null;return{line:0,column:parseInt(n[0],16)+1}}const t=e.match(/([0-9]+)(\:[0-9]*)?/);if(!t||!t[0]||t[0].length!==e.length)return null;const i=parseInt(t[1],10);let o=0;return t[2]&&(o=parseInt(t[2].substring(1),10)),{line:Math.max(0|i,1),column:Math.max(0|o,1)}}_currentUISourceCode(){const e=UI.Context.Context.instance().flavor(SourcesView);return e?e.currentUISourceCode():null}_currentSourceFrame(){const e=UI.Context.Context.instance().flavor(SourcesView);return e?e.currentSourceFrame():null}}QuickOpen.FilteredListWidget.registerProvider({prefix:":",title:i18nLazyString(UIStrings.goToLine),provider:GoToLineQuickOpen.instance});