Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 5 | /* eslint-disable rulesdir/no_underscored_properties */ |
| 6 | |
Tim van der Lippe | 7696157 | 2021-04-06 10:48:07 | [diff] [blame] | 7 | import * as Common from '../core/common/common.js'; |
Tim van der Lippe | bb352e6 | 2021-04-01 17:57:28 | [diff] [blame] | 8 | import * as i18n from '../core/i18n/i18n.js'; |
Tim van der Lippe | fbbf981 | 2020-02-13 14:43:46 | [diff] [blame] | 9 | import * as QuickOpen from '../quick_open/quick_open.js'; |
Tim van der Lippe | aa61faf | 2021-04-07 15:32:07 | [diff] [blame^] | 10 | import * as UI from '../ui/legacy/legacy.js'; |
Tim van der Lippe | fbbf981 | 2020-02-13 14:43:46 | [diff] [blame] | 11 | import * as Workspace from '../workspace/workspace.js'; // eslint-disable-line no-unused-vars |
| 12 | |
Paul Lewis | 3994495 | 2020-01-22 15:45:18 | [diff] [blame] | 13 | import {SourcesView} from './SourcesView.js'; |
| 14 | import {UISourceCodeFrame} from './UISourceCodeFrame.js'; // eslint-disable-line no-unused-vars |
| 15 | |
Simon Zünd | 697fb0b | 2021-03-01 10:12:42 | [diff] [blame] | 16 | const UIStrings = { |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 17 | /** |
| 18 | *@description Text in Go To Line Quick Open of the Sources panel |
| 19 | */ |
| 20 | noFileSelected: 'No file selected.', |
| 21 | /** |
| 22 | *@description Text in Go To Line Quick Open of the Sources panel |
| 23 | */ |
| 24 | typeANumberToGoToThatLine: 'Type a number to go to that line.', |
| 25 | /** |
| 26 | *@description Text in Go To Line Quick Open of the Sources panel |
| 27 | *@example {abc} PH1 |
| 28 | *@example {000} PH2 |
| 29 | *@example {bbb} PH3 |
| 30 | */ |
| 31 | currentPositionXsTypeAnOffset: |
| 32 | 'Current position: 0x{PH1}. Type an offset between 0x{PH2} and 0x{PH3} to navigate to.', |
| 33 | /** |
| 34 | *@description Text in the GoToLine dialog of the Sources pane that describes the current line number, file line number range, and use of the GoToLine dialog |
| 35 | *@example {1} PH1 |
| 36 | *@example {100} PH2 |
| 37 | */ |
| 38 | currentLineSTypeALineNumber: 'Current line: {PH1}. Type a line number between 1 and {PH2} to navigate to.', |
| 39 | /** |
| 40 | *@description Text in Go To Line Quick Open of the Sources panel |
| 41 | *@example {abc} PH1 |
| 42 | */ |
| 43 | goToOffsetXs: 'Go to offset 0x{PH1}.', |
| 44 | /** |
| 45 | *@description Text in Go To Line Quick Open of the Sources panel |
| 46 | *@example {2} PH1 |
| 47 | *@example {2} PH2 |
| 48 | */ |
| 49 | goToLineSAndColumnS: 'Go to line {PH1} and column {PH2}.', |
| 50 | /** |
| 51 | *@description Text in Go To Line Quick Open of the Sources panel |
| 52 | *@example {2} PH1 |
| 53 | */ |
| 54 | goToLineS: 'Go to line {PH1}.', |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 55 | }; |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 56 | const str_ = i18n.i18n.registerUIStrings('sources/GoToLineQuickOpen.ts', UIStrings); |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 57 | const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
Andres Olivares | 5853daa | 2021-02-17 22:43:26 | [diff] [blame] | 58 | |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 59 | let goToLineQuickOpenInstance: GoToLineQuickOpen; |
Paul Lewis | 3994495 | 2020-01-22 15:45:18 | [diff] [blame] | 60 | export class GoToLineQuickOpen extends QuickOpen.FilteredListWidget.Provider { |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 61 | static instance(opts: { |
| 62 | forceNew: boolean|null, |
| 63 | } = {forceNew: null}): GoToLineQuickOpen { |
Andres Olivares | 5853daa | 2021-02-17 22:43:26 | [diff] [blame] | 64 | const {forceNew} = opts; |
| 65 | if (!goToLineQuickOpenInstance || forceNew) { |
| 66 | goToLineQuickOpenInstance = new GoToLineQuickOpen(); |
| 67 | } |
| 68 | |
| 69 | return goToLineQuickOpenInstance; |
| 70 | } |
| 71 | |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 72 | selectItem(itemIndex: number|null, promptValue: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 73 | const uiSourceCode = this._currentUISourceCode(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 74 | if (!uiSourceCode) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 75 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 76 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | const position = this._parsePosition(promptValue); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 78 | if (!position) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 79 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 80 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 81 | Common.Revealer.reveal(uiSourceCode.uiLocation(position.line - 1, position.column - 1)); |
| 82 | } |
| 83 | |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 84 | notFoundText(query: string): string { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 85 | if (!this._currentUISourceCode()) { |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 86 | return i18nString(UIStrings.noFileSelected); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 87 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 88 | const position = this._parsePosition(query); |
Philip Pfaffe | 4da2da8 | 2020-11-30 10:13:12 | [diff] [blame] | 89 | const sourceFrame = this._currentSourceFrame(); |
Kalon Hinds | 6b24092 | 2019-08-01 02:44:31 | [diff] [blame] | 90 | if (!position) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 91 | if (!sourceFrame) { |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 92 | return i18nString(UIStrings.typeANumberToGoToThatLine); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 93 | } |
Philip Pfaffe | 4da2da8 | 2020-11-30 10:13:12 | [diff] [blame] | 94 | const disassembly = sourceFrame.wasmDisassembly; |
| 95 | const currentLineNumber = sourceFrame.textEditor.currentLineNumber(); |
| 96 | if (disassembly) { |
| 97 | const lastBytecodeOffset = disassembly.lineNumberToBytecodeOffset(disassembly.lineNumbers - 1); |
| 98 | const bytecodeOffsetDigits = lastBytecodeOffset.toString(16).length; |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 99 | return i18nString(UIStrings.currentPositionXsTypeAnOffset, {PH1: '0'.padStart(bytecodeOffsetDigits, '0')}); |
Philip Pfaffe | 4da2da8 | 2020-11-30 10:13:12 | [diff] [blame] | 100 | } |
Kalon Hinds | 6b24092 | 2019-08-01 02:44:31 | [diff] [blame] | 101 | const linesCount = sourceFrame.textEditor.linesCount; |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 102 | return i18nString(UIStrings.currentLineSTypeALineNumber, {PH1: currentLineNumber, PH2: linesCount}); |
Kalon Hinds | 6b24092 | 2019-08-01 02:44:31 | [diff] [blame] | 103 | } |
Philip Pfaffe | 4da2da8 | 2020-11-30 10:13:12 | [diff] [blame] | 104 | |
| 105 | if (sourceFrame && sourceFrame.wasmDisassembly) { |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 106 | return i18nString(UIStrings.goToOffsetXs, {PH1: (position.column - 1).toString(16)}); |
Philip Pfaffe | 4da2da8 | 2020-11-30 10:13:12 | [diff] [blame] | 107 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 108 | if (position.column && position.column > 1) { |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 109 | return i18nString(UIStrings.goToLineSAndColumnS, {PH1: position.line, PH2: position.column}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 110 | } |
Vidal Guillermo Diazleal Ortega | 83edb47 | 2021-02-16 18:39:32 | [diff] [blame] | 111 | return i18nString(UIStrings.goToLineS, {PH1: position.line}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 112 | } |
| 113 | |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 114 | _parsePosition(query: string): { |
| 115 | line: number, |
| 116 | column: number, |
| 117 | }|null { |
Philip Pfaffe | 4da2da8 | 2020-11-30 10:13:12 | [diff] [blame] | 118 | const sourceFrame = this._currentSourceFrame(); |
| 119 | if (sourceFrame && sourceFrame.wasmDisassembly) { |
| 120 | const parts = query.match(/0x([0-9a-fA-F]+)/); |
| 121 | if (!parts || !parts[0] || parts[0].length !== query.length) { |
| 122 | return null; |
| 123 | } |
| 124 | |
| 125 | const column = parseInt(parts[0], 16) + 1; |
| 126 | return {line: 0, column}; |
| 127 | } |
| 128 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 129 | const parts = query.match(/([0-9]+)(\:[0-9]*)?/); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 130 | if (!parts || !parts[0] || parts[0].length !== query.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 131 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 132 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 133 | const line = parseInt(parts[1], 10); |
Simon Zünd | 1d49ebb | 2020-09-11 07:46:55 | [diff] [blame] | 134 | let column = 0; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 135 | if (parts[2]) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 136 | column = parseInt(parts[2].substring(1), 10); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 137 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 138 | return {line: Math.max(line | 0, 1), column: Math.max(column | 0, 1)}; |
| 139 | } |
| 140 | |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 141 | _currentUISourceCode(): Workspace.UISourceCode.UISourceCode|null { |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 142 | const sourcesView = UI.Context.Context.instance().flavor(SourcesView); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 143 | if (!sourcesView) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 144 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 145 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 146 | return sourcesView.currentUISourceCode(); |
| 147 | } |
Kalon Hinds | 6b24092 | 2019-08-01 02:44:31 | [diff] [blame] | 148 | |
Jan Scheffler | 35199b9 | 2021-03-17 09:51:15 | [diff] [blame] | 149 | _currentSourceFrame(): UISourceCodeFrame|null { |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 150 | const sourcesView = UI.Context.Context.instance().flavor(SourcesView); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 151 | if (!sourcesView) { |
Kalon Hinds | 6b24092 | 2019-08-01 02:44:31 | [diff] [blame] | 152 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 153 | } |
Kalon Hinds | 6b24092 | 2019-08-01 02:44:31 | [diff] [blame] | 154 | return sourcesView.currentSourceFrame(); |
| 155 | } |
Tim van der Lippe | 8987f8f | 2020-01-03 15:03:16 | [diff] [blame] | 156 | } |