blob: 01d3669b5e507a7f01af00db77c1ce1ab5d2edc6 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// 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 Scheffler35199b92021-03-17 09:51:155/* eslint-disable rulesdir/no_underscored_properties */
6
Tim van der Lippe76961572021-04-06 10:48:077import * as Common from '../core/common/common.js';
Tim van der Lippebb352e62021-04-01 17:57:288import * as i18n from '../core/i18n/i18n.js';
Tim van der Lippefbbf9812020-02-13 14:43:469import * as QuickOpen from '../quick_open/quick_open.js';
Tim van der Lippeaa61faf2021-04-07 15:32:0710import * as UI from '../ui/legacy/legacy.js';
Tim van der Lippefbbf9812020-02-13 14:43:4611import * as Workspace from '../workspace/workspace.js'; // eslint-disable-line no-unused-vars
12
Paul Lewis39944952020-01-22 15:45:1813import {SourcesView} from './SourcesView.js';
14import {UISourceCodeFrame} from './UISourceCodeFrame.js'; // eslint-disable-line no-unused-vars
15
Simon Zünd697fb0b2021-03-01 10:12:4216const UIStrings = {
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:3217 /**
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 Ortega83edb472021-02-16 18:39:3255};
Jan Scheffler35199b92021-03-17 09:51:1556const str_ = i18n.i18n.registerUIStrings('sources/GoToLineQuickOpen.ts', UIStrings);
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:3257const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
Andres Olivares5853daa2021-02-17 22:43:2658
Jan Scheffler35199b92021-03-17 09:51:1559let goToLineQuickOpenInstance: GoToLineQuickOpen;
Paul Lewis39944952020-01-22 15:45:1860export class GoToLineQuickOpen extends QuickOpen.FilteredListWidget.Provider {
Jan Scheffler35199b92021-03-17 09:51:1561 static instance(opts: {
62 forceNew: boolean|null,
63 } = {forceNew: null}): GoToLineQuickOpen {
Andres Olivares5853daa2021-02-17 22:43:2664 const {forceNew} = opts;
65 if (!goToLineQuickOpenInstance || forceNew) {
66 goToLineQuickOpenInstance = new GoToLineQuickOpen();
67 }
68
69 return goToLineQuickOpenInstance;
70 }
71
Jan Scheffler35199b92021-03-17 09:51:1572 selectItem(itemIndex: number|null, promptValue: string): void {
Blink Reformat4c46d092018-04-07 15:32:3773 const uiSourceCode = this._currentUISourceCode();
Tim van der Lippe1d6e57a2019-09-30 11:55:3474 if (!uiSourceCode) {
Blink Reformat4c46d092018-04-07 15:32:3775 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3476 }
Blink Reformat4c46d092018-04-07 15:32:3777 const position = this._parsePosition(promptValue);
Tim van der Lippe1d6e57a2019-09-30 11:55:3478 if (!position) {
Blink Reformat4c46d092018-04-07 15:32:3779 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3480 }
Blink Reformat4c46d092018-04-07 15:32:3781 Common.Revealer.reveal(uiSourceCode.uiLocation(position.line - 1, position.column - 1));
82 }
83
Jan Scheffler35199b92021-03-17 09:51:1584 notFoundText(query: string): string {
Tim van der Lippe1d6e57a2019-09-30 11:55:3485 if (!this._currentUISourceCode()) {
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:3286 return i18nString(UIStrings.noFileSelected);
Tim van der Lippe1d6e57a2019-09-30 11:55:3487 }
Blink Reformat4c46d092018-04-07 15:32:3788 const position = this._parsePosition(query);
Philip Pfaffe4da2da82020-11-30 10:13:1289 const sourceFrame = this._currentSourceFrame();
Kalon Hinds6b240922019-08-01 02:44:3190 if (!position) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3491 if (!sourceFrame) {
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:3292 return i18nString(UIStrings.typeANumberToGoToThatLine);
Tim van der Lippe1d6e57a2019-09-30 11:55:3493 }
Philip Pfaffe4da2da82020-11-30 10:13:1294 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 Ortega83edb472021-02-16 18:39:3299 return i18nString(UIStrings.currentPositionXsTypeAnOffset, {PH1: '0'.padStart(bytecodeOffsetDigits, '0')});
Philip Pfaffe4da2da82020-11-30 10:13:12100 }
Kalon Hinds6b240922019-08-01 02:44:31101 const linesCount = sourceFrame.textEditor.linesCount;
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32102 return i18nString(UIStrings.currentLineSTypeALineNumber, {PH1: currentLineNumber, PH2: linesCount});
Kalon Hinds6b240922019-08-01 02:44:31103 }
Philip Pfaffe4da2da82020-11-30 10:13:12104
105 if (sourceFrame && sourceFrame.wasmDisassembly) {
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32106 return i18nString(UIStrings.goToOffsetXs, {PH1: (position.column - 1).toString(16)});
Philip Pfaffe4da2da82020-11-30 10:13:12107 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34108 if (position.column && position.column > 1) {
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32109 return i18nString(UIStrings.goToLineSAndColumnS, {PH1: position.line, PH2: position.column});
Tim van der Lippe1d6e57a2019-09-30 11:55:34110 }
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32111 return i18nString(UIStrings.goToLineS, {PH1: position.line});
Blink Reformat4c46d092018-04-07 15:32:37112 }
113
Jan Scheffler35199b92021-03-17 09:51:15114 _parsePosition(query: string): {
115 line: number,
116 column: number,
117 }|null {
Philip Pfaffe4da2da82020-11-30 10:13:12118 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 Reformat4c46d092018-04-07 15:32:37129 const parts = query.match(/([0-9]+)(\:[0-9]*)?/);
Tim van der Lippe1d6e57a2019-09-30 11:55:34130 if (!parts || !parts[0] || parts[0].length !== query.length) {
Blink Reformat4c46d092018-04-07 15:32:37131 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34132 }
Blink Reformat4c46d092018-04-07 15:32:37133 const line = parseInt(parts[1], 10);
Simon Zünd1d49ebb2020-09-11 07:46:55134 let column = 0;
Tim van der Lippe1d6e57a2019-09-30 11:55:34135 if (parts[2]) {
Blink Reformat4c46d092018-04-07 15:32:37136 column = parseInt(parts[2].substring(1), 10);
Tim van der Lippe1d6e57a2019-09-30 11:55:34137 }
Blink Reformat4c46d092018-04-07 15:32:37138 return {line: Math.max(line | 0, 1), column: Math.max(column | 0, 1)};
139 }
140
Jan Scheffler35199b92021-03-17 09:51:15141 _currentUISourceCode(): Workspace.UISourceCode.UISourceCode|null {
Tim van der Lipped1a00aa2020-08-19 16:03:56142 const sourcesView = UI.Context.Context.instance().flavor(SourcesView);
Tim van der Lippe1d6e57a2019-09-30 11:55:34143 if (!sourcesView) {
Blink Reformat4c46d092018-04-07 15:32:37144 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34145 }
Blink Reformat4c46d092018-04-07 15:32:37146 return sourcesView.currentUISourceCode();
147 }
Kalon Hinds6b240922019-08-01 02:44:31148
Jan Scheffler35199b92021-03-17 09:51:15149 _currentSourceFrame(): UISourceCodeFrame|null {
Tim van der Lipped1a00aa2020-08-19 16:03:56150 const sourcesView = UI.Context.Context.instance().flavor(SourcesView);
Tim van der Lippe1d6e57a2019-09-30 11:55:34151 if (!sourcesView) {
Kalon Hinds6b240922019-08-01 02:44:31152 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34153 }
Kalon Hinds6b240922019-08-01 02:44:31154 return sourcesView.currentSourceFrame();
155 }
Tim van der Lippe8987f8f2020-01-03 15:03:16156}