blob: 87bca4031c4c41efdb293829bf037648ffd7553f [file] [log] [blame]
Jan Scheffler35199b92021-03-17 09:51:151// Copyright 2021 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
Blink Reformat4c46d092018-04-07 15:32:375/*
6 * Copyright (C) IBM Corp. 2009 All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 * * Neither the name of IBM Corp. nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
Tim van der Lippe8987f8f2020-01-03 15:03:1634
Tim van der Lippe021c7572021-04-19 10:49:4335import * as Common from '../../core/common/common.js';
36import * as Host from '../../core/host/host.js';
37import * as i18n from '../../core/i18n/i18n.js';
38import * as Platform from '../../core/platform/platform.js';
Jaroslav Sevcik9b05a522022-11-30 15:43:2139import * as Root from '../../core/root/root.js';
Tim van der Lippe021c7572021-04-19 10:49:4340import * as SDK from '../../core/sdk/sdk.js';
Jaroslav Sevcik9b05a522022-11-30 15:43:2141import * as Formatter from '../../models/formatter/formatter.js';
42import * as SourceMapScopes from '../../models/source_map_scopes/source_map_scopes.js';
Tim van der Lippe021c7572021-04-19 10:49:4343import * as ObjectUI from '../../ui/legacy/components/object_ui/object_ui.js';
Kriti Sapraae8f17e2021-08-18 10:28:0244// eslint-disable-next-line rulesdir/es_modules_import
45import objectValueStyles from '../../ui/legacy/components/object_ui/objectValue.css.js';
Tim van der Lippe339ad262021-04-21 12:23:3646import * as Components from '../../ui/legacy/components/utils/utils.js';
Tim van der Lippe021c7572021-04-19 10:49:4347import * as UI from '../../ui/legacy/legacy.js';
Kriti Sapraae8f17e2021-08-18 10:28:0248
49import watchExpressionsSidebarPaneStyles from './watchExpressionsSidebarPane.css.js';
50
Tim van der Lippe229a54f2021-05-14 16:59:0551import type * as Protocol from '../../generated/protocol.js';
Tim van der Lippefbbf9812020-02-13 14:43:4652
Paul Lewis39944952020-01-22 15:45:1853import {UISourceCodeFrame} from './UISourceCodeFrame.js';
54
Simon Zünd697fb0b2021-03-01 10:12:4255const UIStrings = {
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:3256 /**
57 *@description A context menu item in the Watch Expressions Sidebar Pane of the Sources panel
58 */
59 addWatchExpression: 'Add watch expression',
60 /**
61 *@description Tooltip/screen reader label of a button in the Sources panel that refreshes all watch expressions.
62 */
63 refreshWatchExpressions: 'Refresh watch expressions',
64 /**
65 *@description Empty element text content in Watch Expressions Sidebar Pane of the Sources panel
66 */
67 noWatchExpressions: 'No watch expressions',
68 /**
69 *@description A context menu item in the Watch Expressions Sidebar Pane of the Sources panel
70 */
71 deleteAllWatchExpressions: 'Delete all watch expressions',
72 /**
73 *@description A context menu item in the Watch Expressions Sidebar Pane of the Sources panel
74 */
75 addPropertyPathToWatch: 'Add property path to watch',
76 /**
77 *@description A context menu item in the Watch Expressions Sidebar Pane of the Sources panel
78 */
79 deleteWatchExpression: 'Delete watch expression',
80 /**
81 *@description Value element text content in Watch Expressions Sidebar Pane of the Sources panel
82 */
83 notAvailable: '<not available>',
84 /**
85 *@description A context menu item in the Watch Expressions Sidebar Pane of the Sources panel and Network pane request.
86 */
87 copyValue: 'Copy value',
88};
Tim van der Lippe021c7572021-04-19 10:49:4389const str_ = i18n.i18n.registerUIStrings('panels/sources/WatchExpressionsSidebarPane.ts', UIStrings);
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:3290const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
Jan Scheffler35199b92021-03-17 09:51:1591let watchExpressionsSidebarPaneInstance: WatchExpressionsSidebarPane;
Andres Olivaresfd431fb2020-12-10 15:30:0492
Jan Scheffler35199b92021-03-17 09:51:1593export class WatchExpressionsSidebarPane extends UI.ThrottledWidget.ThrottledWidget implements
94 UI.ActionRegistration.ActionDelegate, UI.Toolbar.ItemsProvider, UI.ContextMenu.Provider {
Jan Schefflera222c632021-08-13 12:46:1595 private watchExpressions: WatchExpression[];
96 private emptyElement!: HTMLElement;
97 private readonly watchExpressionsSetting: Common.Settings.Setting<string[]>;
98 private readonly addButton: UI.Toolbar.ToolbarButton;
99 private readonly refreshButton: UI.Toolbar.ToolbarButton;
100 private readonly treeOutline: ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeOutline;
101 private readonly expandController: ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeExpandController;
102 private readonly linkifier: Components.Linkifier.Linkifier;
Jan Scheffler35199b92021-03-17 09:51:15103 private constructor() {
Blink Reformat4c46d092018-04-07 15:32:37104 super(true);
Blink Reformat4c46d092018-04-07 15:32:37105
Simon Zünd0243c602020-03-10 06:51:42106 // TODO(szuend): Replace with a Set once the web test
Tim van der Lippe021c7572021-04-19 10:49:43107 // panels/sources/debugger-ui/watch-expressions-preserve-expansion.js is either converted
Jan Scheffler35199b92021-03-17 09:51:15108 // to an e2e test or no longer accesses this variable directly.
Jan Schefflera222c632021-08-13 12:46:15109 this.watchExpressions = [];
110 this.watchExpressionsSetting =
Sigurd Schneider8641b422021-07-16 06:13:35111 Common.Settings.Settings.instance().createLocalSetting<string[]>('watchExpressions', []);
Blink Reformat4c46d092018-04-07 15:32:37112
Jan Schefflera222c632021-08-13 12:46:15113 this.addButton = new UI.Toolbar.ToolbarButton(i18nString(UIStrings.addWatchExpression), 'largeicon-add');
114 this.addButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, _event => {
Tim van der Lippe2d9a95c2022-01-04 15:18:03115 void this.addButtonClicked();
Tim van der Lippe37a35ff2020-03-03 13:49:02116 });
Jan Schefflera222c632021-08-13 12:46:15117 this.refreshButton =
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32118 new UI.Toolbar.ToolbarButton(i18nString(UIStrings.refreshWatchExpressions), 'largeicon-refresh');
Jan Schefflera222c632021-08-13 12:46:15119 this.refreshButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this.update, this);
Blink Reformat4c46d092018-04-07 15:32:37120
121 this.contentElement.classList.add('watch-expressions');
Jan Schefflera222c632021-08-13 12:46:15122 this.contentElement.addEventListener('contextmenu', this.contextMenu.bind(this), false);
123 this.treeOutline = new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeOutline();
Kriti Sapraae8f17e2021-08-18 10:28:02124
Jan Schefflera222c632021-08-13 12:46:15125 this.treeOutline.setShowSelectionOnKeyboardFocus(/* show */ true);
126 this.expandController =
127 new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeExpandController(this.treeOutline);
Blink Reformat4c46d092018-04-07 15:32:37128
Tim van der Lipped1a00aa2020-08-19 16:03:56129 UI.Context.Context.instance().addFlavorChangeListener(SDK.RuntimeModel.ExecutionContext, this.update, this);
130 UI.Context.Context.instance().addFlavorChangeListener(SDK.DebuggerModel.CallFrame, this.update, this);
Jan Schefflera222c632021-08-13 12:46:15131 this.linkifier = new Components.Linkifier.Linkifier();
Blink Reformat4c46d092018-04-07 15:32:37132 this.update();
133 }
134
Jan Scheffler35199b92021-03-17 09:51:15135 static instance(): WatchExpressionsSidebarPane {
Andres Olivaresfd431fb2020-12-10 15:30:04136 if (!watchExpressionsSidebarPaneInstance) {
137 watchExpressionsSidebarPaneInstance = new WatchExpressionsSidebarPane();
138 }
139 return watchExpressionsSidebarPaneInstance;
140 }
141
Jan Scheffler35199b92021-03-17 09:51:15142 toolbarItems(): UI.Toolbar.ToolbarItem[] {
Jan Schefflera222c632021-08-13 12:46:15143 return [this.addButton, this.refreshButton];
Blink Reformat4c46d092018-04-07 15:32:37144 }
145
Jan Scheffler35199b92021-03-17 09:51:15146 focus(): void {
Jack Lynch5db2a3e2019-11-05 19:12:31147 if (this.hasFocus()) {
148 return;
149 }
Jan Schefflera222c632021-08-13 12:46:15150 if (this.watchExpressions.length > 0) {
151 this.treeOutline.forceSelect();
Jack Lynch5db2a3e2019-11-05 19:12:31152 }
153 }
154
Jan Scheffler35199b92021-03-17 09:51:15155 hasExpressions(): boolean {
Jan Schefflera222c632021-08-13 12:46:15156 return Boolean(this.watchExpressionsSetting.get().length);
Blink Reformat4c46d092018-04-07 15:32:37157 }
158
Jan Schefflera222c632021-08-13 12:46:15159 private saveExpressions(): void {
Blink Reformat4c46d092018-04-07 15:32:37160 const toSave = [];
Jan Schefflera222c632021-08-13 12:46:15161 for (let i = 0; i < this.watchExpressions.length; i++) {
162 const expression = this.watchExpressions[i].expression();
Sigurd Schneider8641b422021-07-16 06:13:35163 if (expression) {
164 toSave.push(expression);
Tim van der Lippe1d6e57a2019-09-30 11:55:34165 }
Blink Reformat4c46d092018-04-07 15:32:37166 }
167
Jan Schefflera222c632021-08-13 12:46:15168 this.watchExpressionsSetting.set(toSave);
Blink Reformat4c46d092018-04-07 15:32:37169 }
170
Jan Schefflera222c632021-08-13 12:46:15171 private async addButtonClicked(): Promise<void> {
Paul Lewis75c7d0d2020-03-19 12:17:26172 await UI.ViewManager.ViewManager.instance().showView('sources.watch');
Jan Schefflera222c632021-08-13 12:46:15173 this.emptyElement.classList.add('hidden');
174 this.createWatchExpression(null).startEditing();
Blink Reformat4c46d092018-04-07 15:32:37175 }
176
Mathias Bynensf4871ab2022-01-31 11:45:00177 async doUpdate(): Promise<void> {
Jan Schefflera222c632021-08-13 12:46:15178 this.linkifier.reset();
Blink Reformat4c46d092018-04-07 15:32:37179 this.contentElement.removeChildren();
Jan Schefflera222c632021-08-13 12:46:15180 this.treeOutline.removeChildren();
181 this.watchExpressions = [];
182 this.emptyElement = (this.contentElement.createChild('div', 'gray-info-message') as HTMLElement);
183 this.emptyElement.textContent = i18nString(UIStrings.noWatchExpressions);
184 this.emptyElement.tabIndex = -1;
185 const watchExpressionStrings = this.watchExpressionsSetting.get();
Jack Lynch338317a2020-08-19 21:59:49186 if (watchExpressionStrings.length) {
Jan Schefflera222c632021-08-13 12:46:15187 this.emptyElement.classList.add('hidden');
Jack Lynch338317a2020-08-19 21:59:49188 }
Blink Reformat4c46d092018-04-07 15:32:37189 for (let i = 0; i < watchExpressionStrings.length; ++i) {
190 const expression = watchExpressionStrings[i];
Tim van der Lippe1d6e57a2019-09-30 11:55:34191 if (!expression) {
Blink Reformat4c46d092018-04-07 15:32:37192 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34193 }
Blink Reformat4c46d092018-04-07 15:32:37194
Jan Schefflera222c632021-08-13 12:46:15195 this.createWatchExpression(expression);
Blink Reformat4c46d092018-04-07 15:32:37196 }
Blink Reformat4c46d092018-04-07 15:32:37197 }
198
Jan Schefflera222c632021-08-13 12:46:15199 private createWatchExpression(expression: string|null): WatchExpression {
200 this.contentElement.appendChild(this.treeOutline.element);
201 const watchExpression = new WatchExpression(expression, this.expandController, this.linkifier);
Benedikt Meurere02230c2021-09-09 13:57:22202 watchExpression.addEventListener(Events.ExpressionUpdated, this.watchExpressionUpdated, this);
Jan Schefflera222c632021-08-13 12:46:15203 this.treeOutline.appendChild(watchExpression.treeElement());
204 this.watchExpressions.push(watchExpression);
Blink Reformat4c46d092018-04-07 15:32:37205 return watchExpression;
206 }
207
Benedikt Meurere02230c2021-09-09 13:57:22208 private watchExpressionUpdated({data: watchExpression}: Common.EventTarget.EventTargetEvent<WatchExpression>): void {
Blink Reformat4c46d092018-04-07 15:32:37209 if (!watchExpression.expression()) {
Jan Schefflera222c632021-08-13 12:46:15210 Platform.ArrayUtilities.removeElement(this.watchExpressions, watchExpression);
211 this.treeOutline.removeChild(watchExpression.treeElement());
212 this.emptyElement.classList.toggle('hidden', Boolean(this.watchExpressions.length));
213 if (this.watchExpressions.length === 0) {
214 this.treeOutline.element.remove();
Jack Lynch5db2a3e2019-11-05 19:12:31215 }
Blink Reformat4c46d092018-04-07 15:32:37216 }
217
Jan Schefflera222c632021-08-13 12:46:15218 this.saveExpressions();
Blink Reformat4c46d092018-04-07 15:32:37219 }
220
Jan Schefflera222c632021-08-13 12:46:15221 private contextMenu(event: MouseEvent): void {
Tim van der Lippefbbf9812020-02-13 14:43:46222 const contextMenu = new UI.ContextMenu.ContextMenu(event);
Jan Schefflera222c632021-08-13 12:46:15223 this.populateContextMenu(contextMenu, event);
Tim van der Lippe2d9a95c2022-01-04 15:18:03224 void contextMenu.show();
Blink Reformat4c46d092018-04-07 15:32:37225 }
226
Jan Schefflera222c632021-08-13 12:46:15227 private populateContextMenu(contextMenu: UI.ContextMenu.ContextMenu, event: MouseEvent): void {
Blink Reformat4c46d092018-04-07 15:32:37228 let isEditing = false;
Jan Schefflera222c632021-08-13 12:46:15229 for (const watchExpression of this.watchExpressions) {
Tim van der Lippe0ad77da2020-10-01 16:38:00230 isEditing = isEditing || watchExpression.isEditing();
Tim van der Lippe1d6e57a2019-09-30 11:55:34231 }
Blink Reformat4c46d092018-04-07 15:32:37232
Tim van der Lippe1d6e57a2019-09-30 11:55:34233 if (!isEditing) {
Jan Schefflera222c632021-08-13 12:46:15234 contextMenu.debugSection().appendItem(i18nString(UIStrings.addWatchExpression), this.addButtonClicked.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37235 }
236
Jan Schefflera222c632021-08-13 12:46:15237 if (this.watchExpressions.length > 1) {
Changhao Han8eb616e2021-08-11 05:45:27238 contextMenu.debugSection().appendItem(
Jan Schefflera222c632021-08-13 12:46:15239 i18nString(UIStrings.deleteAllWatchExpressions), this.deleteAllButtonClicked.bind(this));
Changhao Han8eb616e2021-08-11 05:45:27240 }
241
Jan Schefflera222c632021-08-13 12:46:15242 const treeElement = this.treeOutline.treeElementFromEvent(event);
Jack Lynch5db2a3e2019-11-05 19:12:31243 if (!treeElement) {
Blink Reformat4c46d092018-04-07 15:32:37244 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34245 }
Jack Lynch5db2a3e2019-11-05 19:12:31246 const currentWatchExpression =
Jan Schefflera222c632021-08-13 12:46:15247 this.watchExpressions.find(watchExpression => treeElement.hasAncestorOrSelf(watchExpression.treeElement()));
Tim van der Lippe0ad77da2020-10-01 16:38:00248 if (currentWatchExpression) {
Jan Schefflera222c632021-08-13 12:46:15249 currentWatchExpression.populateContextMenu(contextMenu, event);
Tim van der Lippe0ad77da2020-10-01 16:38:00250 }
Blink Reformat4c46d092018-04-07 15:32:37251 }
252
Jan Schefflera222c632021-08-13 12:46:15253 private deleteAllButtonClicked(): void {
254 this.watchExpressions = [];
255 this.saveExpressions();
Blink Reformat4c46d092018-04-07 15:32:37256 this.update();
257 }
258
Jan Schefflera222c632021-08-13 12:46:15259 private async focusAndAddExpressionToWatch(expression: string): Promise<void> {
Benedikt Meurer61f1eef2021-03-17 10:38:29260 await UI.ViewManager.ViewManager.instance().showView('sources.watch');
Jan Schefflera222c632021-08-13 12:46:15261 this.createWatchExpression(expression);
262 this.saveExpressions();
Jack Lynch338317a2020-08-19 21:59:49263 this.update();
PhistucK065e1362018-05-05 09:51:09264 }
265
Jan Scheffler35199b92021-03-17 09:51:15266 handleAction(_context: UI.Context.Context, _actionId: string): boolean {
Tim van der Lipped1a00aa2020-08-19 16:03:56267 const frame = UI.Context.Context.instance().flavor(UISourceCodeFrame);
Tim van der Lippe1d6e57a2019-09-30 11:55:34268 if (!frame) {
Blink Reformat4c46d092018-04-07 15:32:37269 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34270 }
Tim van der Lippec1ab1122021-11-12 16:10:31271 const {state} = frame.textEditor;
272 const text = state.sliceDoc(state.selection.main.from, state.selection.main.to);
Tim van der Lippe2d9a95c2022-01-04 15:18:03273 void this.focusAndAddExpressionToWatch(text);
Blink Reformat4c46d092018-04-07 15:32:37274 return true;
275 }
276
Jan Scheffler35199b92021-03-17 09:51:15277 appendApplicableItems(event: Event, contextMenu: UI.ContextMenu.ContextMenu, target: Object): void {
Tim van der Lippefbbf9812020-02-13 14:43:46278 if (target instanceof ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement && !target.property.synthetic) {
PhistucK065e1362018-05-05 09:51:09279 contextMenu.debugSection().appendItem(
Jan Schefflera222c632021-08-13 12:46:15280 i18nString(UIStrings.addPropertyPathToWatch), () => this.focusAndAddExpressionToWatch(target.path()));
PhistucK065e1362018-05-05 09:51:09281 }
282
Tim van der Lipped1a00aa2020-08-19 16:03:56283 const frame = UI.Context.Context.instance().flavor(UISourceCodeFrame);
Tim van der Lippec1ab1122021-11-12 16:10:31284 if (!frame || frame.textEditor.state.selection.main.empty) {
Blink Reformat4c46d092018-04-07 15:32:37285 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34286 }
Blink Reformat4c46d092018-04-07 15:32:37287
288 contextMenu.debugSection().appendAction('sources.add-to-watch');
289 }
Kriti Sapraae8f17e2021-08-18 10:28:02290 wasShown(): void {
291 super.wasShown();
292 this.treeOutline.registerCSSFiles([watchExpressionsSidebarPaneStyles]);
293 this.registerCSSFiles([watchExpressionsSidebarPaneStyles, objectValueStyles]);
294 }
Tim van der Lippe8987f8f2020-01-03 15:03:16295}
Blink Reformat4c46d092018-04-07 15:32:37296
Benedikt Meurere02230c2021-09-09 13:57:22297export class WatchExpression extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
Jan Schefflera222c632021-08-13 12:46:15298 private treeElementInternal!: UI.TreeOutline.TreeElement;
299 private nameElement!: Element;
300 private valueElement!: Element;
301 private expressionInternal: string|null;
302 private readonly expandController: ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeExpandController;
303 private element: HTMLDivElement;
304 private editing: boolean;
305 private linkifier: Components.Linkifier.Linkifier;
306 private textPrompt?: ObjectUI.ObjectPropertiesSection.ObjectPropertyPrompt;
307 private result?: SDK.RemoteObject.RemoteObject|null;
308 private preventClickTimeout?: number;
Kateryna Prokopenko52a9b592021-10-20 15:06:01309 private resizeObserver?: ResizeObserver;
Jan Scheffler35199b92021-03-17 09:51:15310 constructor(
311 expression: string|null,
312 expandController: ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeExpandController,
313 linkifier: Components.Linkifier.Linkifier) {
Blink Reformat4c46d092018-04-07 15:32:37314 super();
Tim van der Lippe0ad77da2020-10-01 16:38:00315
Jan Schefflera222c632021-08-13 12:46:15316 this.expressionInternal = expression;
317 this.expandController = expandController;
318 this.element = document.createElement('div');
319 this.element.classList.add('watch-expression');
320 this.element.classList.add('monospace');
321 this.editing = false;
322 this.linkifier = linkifier;
Blink Reformat4c46d092018-04-07 15:32:37323
Jan Schefflera222c632021-08-13 12:46:15324 this.createWatchExpression();
Blink Reformat4c46d092018-04-07 15:32:37325 this.update();
326 }
327
Jan Scheffler35199b92021-03-17 09:51:15328 treeElement(): UI.TreeOutline.TreeElement {
Jan Schefflera222c632021-08-13 12:46:15329 return this.treeElementInternal;
Blink Reformat4c46d092018-04-07 15:32:37330 }
331
Jan Scheffler35199b92021-03-17 09:51:15332 expression(): string|null {
Jan Schefflera222c632021-08-13 12:46:15333 return this.expressionInternal;
Blink Reformat4c46d092018-04-07 15:32:37334 }
335
Jaroslav Sevcik9b05a522022-11-30 15:43:21336 async #evaluateExpression(executionContext: SDK.RuntimeModel.ExecutionContext, expression: string):
337 Promise<SDK.RuntimeModel.EvaluationResult> {
338 if (Root.Runtime.experiments.isEnabled('evaluateExpressionsWithSourceMaps')) {
339 const callFrame = executionContext.debuggerModel.selectedCallFrame();
340 if (callFrame) {
341 const nameMap = await SourceMapScopes.NamesResolver.allVariablesInCallFrame(callFrame);
342 try {
343 expression =
344 await Formatter.FormatterWorkerPool.formatterWorkerPool().javaScriptSubstitute(expression, nameMap);
345 } catch {
346 }
347 }
348 }
349
350 return executionContext.evaluate(
351 {
352 expression,
353 objectGroup: WatchExpression.watchObjectGroupId,
354 includeCommandLineAPI: false,
355 silent: true,
356 returnByValue: false,
357 generatePreview: false,
358 allowUnsafeEvalBlockedByCSP: undefined,
359 disableBreaks: undefined,
360 replMode: undefined,
361 throwOnSideEffect: undefined,
362 timeout: undefined,
363 },
364 /* userGesture */ false,
365 /* awaitPromise */ false);
366 }
367
Jan Scheffler35199b92021-03-17 09:51:15368 update(): void {
Tim van der Lipped1a00aa2020-08-19 16:03:56369 const currentExecutionContext = UI.Context.Context.instance().flavor(SDK.RuntimeModel.ExecutionContext);
Jan Schefflera222c632021-08-13 12:46:15370 if (currentExecutionContext && this.expressionInternal) {
Jaroslav Sevcik9b05a522022-11-30 15:43:21371 void this.#evaluateExpression(currentExecutionContext, this.expressionInternal).then(result => {
372 if ('object' in result) {
373 this.createWatchExpression(result.object, result.exceptionDetails);
374 } else {
375 this.createWatchExpression();
376 }
377 });
Sigurd Schneider53dc6482021-06-30 11:22:02378 } else {
Jan Schefflera222c632021-08-13 12:46:15379 this.createWatchExpression();
Blink Reformat4c46d092018-04-07 15:32:37380 }
381 }
382
Jan Scheffler35199b92021-03-17 09:51:15383 startEditing(): void {
Jan Schefflera222c632021-08-13 12:46:15384 this.editing = true;
385 this.treeElementInternal.setDisableSelectFocus(true);
386 this.element.removeChildren();
387 const newDiv = this.element.createChild('div');
388 newDiv.textContent = this.nameElement.textContent;
389 this.textPrompt = new ObjectUI.ObjectPropertiesSection.ObjectPropertyPrompt();
390 this.textPrompt.renderAsBlock();
391 const proxyElement = (this.textPrompt.attachAndStartEditing(newDiv, this.finishEditing.bind(this)) as HTMLElement);
392 this.treeElementInternal.listItemElement.classList.add('watch-expression-editing');
393 this.treeElementInternal.collapse();
Blink Reformat4c46d092018-04-07 15:32:37394 proxyElement.classList.add('watch-expression-text-prompt-proxy');
Jan Schefflera222c632021-08-13 12:46:15395 proxyElement.addEventListener('keydown', this.promptKeyDown.bind(this), false);
396 const selection = this.element.getComponentSelection();
Tim van der Lippe0ad77da2020-10-01 16:38:00397 if (selection) {
398 selection.selectAllChildren(newDiv);
399 }
Blink Reformat4c46d092018-04-07 15:32:37400 }
401
Jan Scheffler35199b92021-03-17 09:51:15402 isEditing(): boolean {
Jan Schefflera222c632021-08-13 12:46:15403 return Boolean(this.editing);
Blink Reformat4c46d092018-04-07 15:32:37404 }
405
Jan Schefflera222c632021-08-13 12:46:15406 private finishEditing(event: Event, canceled?: boolean): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:34407 if (event) {
Blink Reformat4c46d092018-04-07 15:32:37408 event.consume(canceled);
Tim van der Lippe1d6e57a2019-09-30 11:55:34409 }
Blink Reformat4c46d092018-04-07 15:32:37410
Jan Schefflera222c632021-08-13 12:46:15411 this.editing = false;
412 this.treeElementInternal.setDisableSelectFocus(false);
413 this.treeElementInternal.listItemElement.classList.remove('watch-expression-editing');
414 if (this.textPrompt) {
415 this.textPrompt.detach();
416 const newExpression = canceled ? this.expressionInternal : this.textPrompt.text();
417 this.textPrompt = undefined;
418 this.element.removeChildren();
419 this.updateExpression(newExpression);
Tim van der Lippe0ad77da2020-10-01 16:38:00420 }
Blink Reformat4c46d092018-04-07 15:32:37421 }
422
Jan Schefflera222c632021-08-13 12:46:15423 private dblClickOnWatchExpression(event: Event): void {
Blink Reformat4c46d092018-04-07 15:32:37424 event.consume();
Tim van der Lippe1d6e57a2019-09-30 11:55:34425 if (!this.isEditing()) {
Blink Reformat4c46d092018-04-07 15:32:37426 this.startEditing();
Tim van der Lippe1d6e57a2019-09-30 11:55:34427 }
Blink Reformat4c46d092018-04-07 15:32:37428 }
429
Jan Schefflera222c632021-08-13 12:46:15430 private updateExpression(newExpression: string|null): void {
431 if (this.expressionInternal) {
432 this.expandController.stopWatchSectionsWithId(this.expressionInternal);
Tim van der Lippe1d6e57a2019-09-30 11:55:34433 }
Kateryna Prokopenko52a9b592021-10-20 15:06:01434 this.resizeObserver?.disconnect();
Jan Schefflera222c632021-08-13 12:46:15435 this.expressionInternal = newExpression;
Blink Reformat4c46d092018-04-07 15:32:37436 this.update();
Benedikt Meurere02230c2021-09-09 13:57:22437 this.dispatchEventToListeners(Events.ExpressionUpdated, this);
Blink Reformat4c46d092018-04-07 15:32:37438 }
439
Jan Schefflera222c632021-08-13 12:46:15440 private deleteWatchExpression(event: Event): void {
Blink Reformat4c46d092018-04-07 15:32:37441 event.consume(true);
Jan Schefflera222c632021-08-13 12:46:15442 this.updateExpression(null);
Blink Reformat4c46d092018-04-07 15:32:37443 }
444
Jan Schefflera222c632021-08-13 12:46:15445 private createWatchExpression(
446 result?: SDK.RemoteObject.RemoteObject, exceptionDetails?: Protocol.Runtime.ExceptionDetails): void {
447 this.result = result || null;
Blink Reformat4c46d092018-04-07 15:32:37448
Jan Schefflera222c632021-08-13 12:46:15449 this.element.removeChildren();
450 const oldTreeElement = this.treeElementInternal;
451 this.createWatchExpressionTreeElement(result, exceptionDetails);
Jack Lynch5db2a3e2019-11-05 19:12:31452 if (oldTreeElement && oldTreeElement.parent) {
453 const root = oldTreeElement.parent;
454 const index = root.indexOfChild(oldTreeElement);
455 root.removeChild(oldTreeElement);
Jan Schefflera222c632021-08-13 12:46:15456 root.insertChild(this.treeElementInternal, index);
Jack Lynch5db2a3e2019-11-05 19:12:31457 }
Jan Schefflera222c632021-08-13 12:46:15458 this.treeElementInternal.select();
Jack Lynch5db2a3e2019-11-05 19:12:31459 }
460
Jan Schefflera222c632021-08-13 12:46:15461 private createWatchExpressionHeader(
Jan Scheffler35199b92021-03-17 09:51:15462 expressionValue?: SDK.RemoteObject.RemoteObject, exceptionDetails?: Protocol.Runtime.ExceptionDetails): Element {
Jan Schefflera222c632021-08-13 12:46:15463 const headerElement = this.element.createChild('div', 'watch-expression-header');
Tim van der Lippefbbf9812020-02-13 14:43:46464 const deleteButton = UI.Icon.Icon.create('smallicon-cross', 'watch-expression-delete-button');
Kateryna Prokopenko52a9b592021-10-20 15:06:01465 this.resizeObserver = new ResizeObserver(entries => {
466 entries.forEach(entry => {
467 // 55 serves as a width threshold here (in px)
468 if (entry.contentRect.width < 55) {
469 deleteButton.classList.remove('right-aligned');
470 deleteButton.classList.add('left-aligned');
471 } else {
472 deleteButton.classList.remove('left-aligned');
473 deleteButton.classList.add('right-aligned');
474 }
475 });
476 });
477 this.resizeObserver.observe(headerElement);
Vidal Guillermo Diazleal Ortega83edb472021-02-16 18:39:32478 UI.Tooltip.Tooltip.install(deleteButton, i18nString(UIStrings.deleteWatchExpression));
Jan Schefflera222c632021-08-13 12:46:15479 deleteButton.addEventListener('click', this.deleteWatchExpression.bind(this), false);
Blink Reformat4c46d092018-04-07 15:32:37480
Jack Lynch5db2a3e2019-11-05 19:12:31481 const titleElement = headerElement.createChild('div', 'watch-expression-title tree-element-title');
Jack Lynche1767de2020-04-22 02:43:55482 titleElement.appendChild(deleteButton);
Jan Schefflera222c632021-08-13 12:46:15483 this.nameElement =
484 ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.createNameElement(this.expressionInternal);
Tim van der Lipped7cfd142021-01-07 12:17:24485 if (Boolean(exceptionDetails) || !expressionValue) {
Jan Schefflera222c632021-08-13 12:46:15486 this.valueElement = document.createElement('span');
487 this.valueElement.classList.add('watch-expression-error');
488 this.valueElement.classList.add('value');
Blink Reformat4c46d092018-04-07 15:32:37489 titleElement.classList.add('dimmed');
Jan Schefflera222c632021-08-13 12:46:15490 this.valueElement.textContent = i18nString(UIStrings.notAvailable);
Philip Pfaffe7426b002020-11-30 15:59:22491 if (exceptionDetails !== undefined && exceptionDetails.exception !== undefined &&
492 exceptionDetails.exception.description !== undefined) {
Jan Schefflera222c632021-08-13 12:46:15493 UI.Tooltip.Tooltip.install(this.valueElement as HTMLElement, exceptionDetails.exception.description);
Philip Pfaffe7426b002020-11-30 15:59:22494 }
Blink Reformat4c46d092018-04-07 15:32:37495 } else {
Tim van der Lippefbbf9812020-02-13 14:43:46496 const propertyValue =
497 ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.createPropertyValueWithCustomSupport(
Jan Schefflera222c632021-08-13 12:46:15498 expressionValue, Boolean(exceptionDetails), false /* showPreview */, titleElement, this.linkifier);
499 this.valueElement = propertyValue.element;
Blink Reformat4c46d092018-04-07 15:32:37500 }
Tim van der Lippef49e2322020-05-01 15:03:09501 const separatorElement = document.createElement('span');
502 separatorElement.classList.add('watch-expressions-separator');
Blink Reformat4c46d092018-04-07 15:32:37503 separatorElement.textContent = ': ';
Jan Schefflera222c632021-08-13 12:46:15504 titleElement.append(this.nameElement, separatorElement, this.valueElement);
Blink Reformat4c46d092018-04-07 15:32:37505
Jack Lynch5db2a3e2019-11-05 19:12:31506 return headerElement;
507 }
Blink Reformat4c46d092018-04-07 15:32:37508
Jan Schefflera222c632021-08-13 12:46:15509 private createWatchExpressionTreeElement(
Jan Scheffler35199b92021-03-17 09:51:15510 expressionValue?: SDK.RemoteObject.RemoteObject, exceptionDetails?: Protocol.Runtime.ExceptionDetails): void {
Jan Schefflera222c632021-08-13 12:46:15511 const headerElement = this.createWatchExpressionHeader(expressionValue, exceptionDetails);
Jack Lynch5db2a3e2019-11-05 19:12:31512
513 if (!exceptionDetails && expressionValue && expressionValue.hasChildren && !expressionValue.customPreview()) {
514 headerElement.classList.add('watch-expression-object-header');
Jan Schefflera222c632021-08-13 12:46:15515 this.treeElementInternal = new ObjectUI.ObjectPropertiesSection.RootElement(expressionValue, this.linkifier);
516 this.expandController.watchSection(
517 (this.expressionInternal as string),
518 (this.treeElementInternal as ObjectUI.ObjectPropertiesSection.RootElement));
519 this.treeElementInternal.toggleOnClick = false;
520 this.treeElementInternal.listItemElement.addEventListener('click', this.onSectionClick.bind(this), false);
521 this.treeElementInternal.listItemElement.addEventListener('dblclick', this.dblClickOnWatchExpression.bind(this));
Jack Lynch5db2a3e2019-11-05 19:12:31522 } else {
Jan Schefflera222c632021-08-13 12:46:15523 headerElement.addEventListener('dblclick', this.dblClickOnWatchExpression.bind(this));
524 this.treeElementInternal = new UI.TreeOutline.TreeElement();
Jack Lynch5db2a3e2019-11-05 19:12:31525 }
Jan Schefflera222c632021-08-13 12:46:15526 this.treeElementInternal.title = this.element;
527 this.treeElementInternal.listItemElement.classList.add('watch-expression-tree-item');
528 this.treeElementInternal.listItemElement.addEventListener('keydown', event => {
Tim van der Lippebcd6b5c2021-01-13 12:31:51529 if (event.key === 'Enter' && !this.isEditing()) {
Jack Lynch5db2a3e2019-11-05 19:12:31530 this.startEditing();
531 event.consume(true);
Jaroslav Sevcik9b05a522022-11-30 15:43:21532 } else if (event.key === 'Delete' && !this.isEditing()) {
533 this.deleteWatchExpression(event);
Jack Lynch5db2a3e2019-11-05 19:12:31534 }
535 });
Blink Reformat4c46d092018-04-07 15:32:37536 }
537
Jan Schefflera222c632021-08-13 12:46:15538 private onSectionClick(event: Event): void {
Blink Reformat4c46d092018-04-07 15:32:37539 event.consume(true);
Jan Scheffler35199b92021-03-17 09:51:15540 const mouseEvent = (event as MouseEvent);
Tim van der Lippe0ad77da2020-10-01 16:38:00541 if (mouseEvent.detail === 1) {
Jan Schefflera222c632021-08-13 12:46:15542 this.preventClickTimeout = window.setTimeout(handleClick.bind(this), 333);
543 } else if (this.preventClickTimeout !== undefined) {
544 window.clearTimeout(this.preventClickTimeout);
545 this.preventClickTimeout = undefined;
Blink Reformat4c46d092018-04-07 15:32:37546 }
547
Jan Scheffler35199b92021-03-17 09:51:15548 function handleClick(this: WatchExpression): void {
Jan Schefflera222c632021-08-13 12:46:15549 if (!this.treeElementInternal) {
Blink Reformat4c46d092018-04-07 15:32:37550 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34551 }
Blink Reformat4c46d092018-04-07 15:32:37552
Jan Schefflera222c632021-08-13 12:46:15553 if (this.treeElementInternal.expanded) {
554 this.treeElementInternal.collapse();
555 } else if (!this.editing) {
556 this.treeElementInternal.expand();
Tim van der Lippe1d6e57a2019-09-30 11:55:34557 }
Blink Reformat4c46d092018-04-07 15:32:37558 }
559 }
560
Jan Schefflera222c632021-08-13 12:46:15561 private promptKeyDown(event: KeyboardEvent): void {
Jack Frankline24bc6f2022-10-07 10:36:18562 const isEscapeKey = Platform.KeyboardUtilities.isEscKey(event);
563 if (event.key === 'Enter' || isEscapeKey) {
564 this.finishEditing(event, isEscapeKey);
Tim van der Lippe1d6e57a2019-09-30 11:55:34565 }
Blink Reformat4c46d092018-04-07 15:32:37566 }
567
Jan Schefflera222c632021-08-13 12:46:15568 populateContextMenu(contextMenu: UI.ContextMenu.ContextMenu, event: Event): void {
Blink Reformat4c46d092018-04-07 15:32:37569 if (!this.isEditing()) {
570 contextMenu.editSection().appendItem(
Jan Schefflera222c632021-08-13 12:46:15571 i18nString(UIStrings.deleteWatchExpression), this.updateExpression.bind(this, null));
Blink Reformat4c46d092018-04-07 15:32:37572 }
573
Jan Schefflera222c632021-08-13 12:46:15574 if (!this.isEditing() && this.result && (this.result.type === 'number' || this.result.type === 'string')) {
Tim van der Lippefbbf9812020-02-13 14:43:46575 contextMenu.clipboardSection().appendItem(
Jan Schefflera222c632021-08-13 12:46:15576 i18nString(UIStrings.copyValue), this.copyValueButtonClicked.bind(this));
Tim van der Lippe1d6e57a2019-09-30 11:55:34577 }
Blink Reformat4c46d092018-04-07 15:32:37578
Sigurd Schneider090c3b62020-11-10 10:26:49579 const target = UI.UIUtils.deepElementFromEvent(event);
Jan Schefflera222c632021-08-13 12:46:15580 if (target && this.valueElement.isSelfOrAncestor(target) && this.result) {
581 contextMenu.appendApplicableItems(this.result);
Tim van der Lippe1d6e57a2019-09-30 11:55:34582 }
Blink Reformat4c46d092018-04-07 15:32:37583 }
584
Jan Schefflera222c632021-08-13 12:46:15585 private copyValueButtonClicked(): void {
586 Host.InspectorFrontendHost.InspectorFrontendHostInstance.copyText(this.valueElement.textContent);
Blink Reformat4c46d092018-04-07 15:32:37587 }
Jan Scheffler35199b92021-03-17 09:51:15588
Sigurd Schneider8641b422021-07-16 06:13:35589 private static readonly watchObjectGroupId = 'watch-group';
Tim van der Lippe8987f8f2020-01-03 15:03:16590}
Blink Reformat4c46d092018-04-07 15:32:37591
Benedikt Meurere02230c2021-09-09 13:57:22592const enum Events {
593 ExpressionUpdated = 'ExpressionUpdated',
Jan Scheffler35199b92021-03-17 09:51:15594}
Benedikt Meurere02230c2021-09-09 13:57:22595
596type EventTypes = {
597 [Events.ExpressionUpdated]: WatchExpression,
598};