Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | "use strict"; |
michael@0 | 7 | |
michael@0 | 8 | const SOURCE_SYNTAX_HIGHLIGHT_MAX_FILE_SIZE = 1048576; // 1 MB in bytes |
michael@0 | 9 | const SOURCE_URL_DEFAULT_MAX_LENGTH = 64; // chars |
michael@0 | 10 | const STACK_FRAMES_SOURCE_URL_MAX_LENGTH = 15; // chars |
michael@0 | 11 | const STACK_FRAMES_SOURCE_URL_TRIM_SECTION = "center"; |
michael@0 | 12 | const STACK_FRAMES_SCROLL_DELAY = 100; // ms |
michael@0 | 13 | const BREAKPOINT_LINE_TOOLTIP_MAX_LENGTH = 1000; // chars |
michael@0 | 14 | const BREAKPOINT_CONDITIONAL_POPUP_POSITION = "before_start"; |
michael@0 | 15 | const BREAKPOINT_CONDITIONAL_POPUP_OFFSET_X = 7; // px |
michael@0 | 16 | const BREAKPOINT_CONDITIONAL_POPUP_OFFSET_Y = -3; // px |
michael@0 | 17 | const RESULTS_PANEL_POPUP_POSITION = "before_end"; |
michael@0 | 18 | const RESULTS_PANEL_MAX_RESULTS = 10; |
michael@0 | 19 | const FILE_SEARCH_ACTION_MAX_DELAY = 300; // ms |
michael@0 | 20 | const GLOBAL_SEARCH_EXPAND_MAX_RESULTS = 50; |
michael@0 | 21 | const GLOBAL_SEARCH_LINE_MAX_LENGTH = 300; // chars |
michael@0 | 22 | const GLOBAL_SEARCH_ACTION_MAX_DELAY = 1500; // ms |
michael@0 | 23 | const FUNCTION_SEARCH_ACTION_MAX_DELAY = 400; // ms |
michael@0 | 24 | const SEARCH_GLOBAL_FLAG = "!"; |
michael@0 | 25 | const SEARCH_FUNCTION_FLAG = "@"; |
michael@0 | 26 | const SEARCH_TOKEN_FLAG = "#"; |
michael@0 | 27 | const SEARCH_LINE_FLAG = ":"; |
michael@0 | 28 | const SEARCH_VARIABLE_FLAG = "*"; |
michael@0 | 29 | const SEARCH_AUTOFILL = [SEARCH_GLOBAL_FLAG, SEARCH_FUNCTION_FLAG, SEARCH_TOKEN_FLAG]; |
michael@0 | 30 | const EDITOR_VARIABLE_HOVER_DELAY = 350; // ms |
michael@0 | 31 | const EDITOR_VARIABLE_POPUP_POSITION = "topcenter bottomleft"; |
michael@0 | 32 | const TOOLBAR_ORDER_POPUP_POSITION = "topcenter bottomleft"; |
michael@0 | 33 | |
michael@0 | 34 | /** |
michael@0 | 35 | * Object defining the debugger view components. |
michael@0 | 36 | */ |
michael@0 | 37 | let DebuggerView = { |
michael@0 | 38 | /** |
michael@0 | 39 | * Initializes the debugger view. |
michael@0 | 40 | * |
michael@0 | 41 | * @return object |
michael@0 | 42 | * A promise that is resolved when the view finishes initializing. |
michael@0 | 43 | */ |
michael@0 | 44 | initialize: function() { |
michael@0 | 45 | if (this._startup) { |
michael@0 | 46 | return this._startup; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | let deferred = promise.defer(); |
michael@0 | 50 | this._startup = deferred.promise; |
michael@0 | 51 | |
michael@0 | 52 | this._initializePanes(); |
michael@0 | 53 | this.Toolbar.initialize(); |
michael@0 | 54 | this.Options.initialize(); |
michael@0 | 55 | this.Filtering.initialize(); |
michael@0 | 56 | this.FilteredSources.initialize(); |
michael@0 | 57 | this.FilteredFunctions.initialize(); |
michael@0 | 58 | this.StackFrames.initialize(); |
michael@0 | 59 | this.StackFramesClassicList.initialize(); |
michael@0 | 60 | this.Sources.initialize(); |
michael@0 | 61 | this.VariableBubble.initialize(); |
michael@0 | 62 | this.Tracer.initialize(); |
michael@0 | 63 | this.WatchExpressions.initialize(); |
michael@0 | 64 | this.EventListeners.initialize(); |
michael@0 | 65 | this.GlobalSearch.initialize(); |
michael@0 | 66 | this._initializeVariablesView(); |
michael@0 | 67 | this._initializeEditor(deferred.resolve); |
michael@0 | 68 | |
michael@0 | 69 | document.title = L10N.getStr("DebuggerWindowTitle"); |
michael@0 | 70 | |
michael@0 | 71 | return deferred.promise; |
michael@0 | 72 | }, |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Destroys the debugger view. |
michael@0 | 76 | * |
michael@0 | 77 | * @return object |
michael@0 | 78 | * A promise that is resolved when the view finishes destroying. |
michael@0 | 79 | */ |
michael@0 | 80 | destroy: function() { |
michael@0 | 81 | if (this._shutdown) { |
michael@0 | 82 | return this._shutdown; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | let deferred = promise.defer(); |
michael@0 | 86 | this._shutdown = deferred.promise; |
michael@0 | 87 | |
michael@0 | 88 | this.Toolbar.destroy(); |
michael@0 | 89 | this.Options.destroy(); |
michael@0 | 90 | this.Filtering.destroy(); |
michael@0 | 91 | this.FilteredSources.destroy(); |
michael@0 | 92 | this.FilteredFunctions.destroy(); |
michael@0 | 93 | this.StackFrames.destroy(); |
michael@0 | 94 | this.StackFramesClassicList.destroy(); |
michael@0 | 95 | this.Sources.destroy(); |
michael@0 | 96 | this.VariableBubble.destroy(); |
michael@0 | 97 | this.Tracer.destroy(); |
michael@0 | 98 | this.WatchExpressions.destroy(); |
michael@0 | 99 | this.EventListeners.destroy(); |
michael@0 | 100 | this.GlobalSearch.destroy(); |
michael@0 | 101 | this._destroyPanes(); |
michael@0 | 102 | this._destroyEditor(deferred.resolve); |
michael@0 | 103 | |
michael@0 | 104 | return deferred.promise; |
michael@0 | 105 | }, |
michael@0 | 106 | |
michael@0 | 107 | /** |
michael@0 | 108 | * Initializes the UI for all the displayed panes. |
michael@0 | 109 | */ |
michael@0 | 110 | _initializePanes: function() { |
michael@0 | 111 | dumpn("Initializing the DebuggerView panes"); |
michael@0 | 112 | |
michael@0 | 113 | this._body = document.getElementById("body"); |
michael@0 | 114 | this._editorDeck = document.getElementById("editor-deck"); |
michael@0 | 115 | this._sourcesPane = document.getElementById("sources-pane"); |
michael@0 | 116 | this._instrumentsPane = document.getElementById("instruments-pane"); |
michael@0 | 117 | this._instrumentsPaneToggleButton = document.getElementById("instruments-pane-toggle"); |
michael@0 | 118 | |
michael@0 | 119 | this.showEditor = this.showEditor.bind(this); |
michael@0 | 120 | this.showBlackBoxMessage = this.showBlackBoxMessage.bind(this); |
michael@0 | 121 | this.showProgressBar = this.showProgressBar.bind(this); |
michael@0 | 122 | this.maybeShowBlackBoxMessage = this.maybeShowBlackBoxMessage.bind(this); |
michael@0 | 123 | |
michael@0 | 124 | this._onTabSelect = this._onInstrumentsPaneTabSelect.bind(this); |
michael@0 | 125 | this._instrumentsPane.tabpanels.addEventListener("select", this._onTabSelect); |
michael@0 | 126 | |
michael@0 | 127 | this._collapsePaneString = L10N.getStr("collapsePanes"); |
michael@0 | 128 | this._expandPaneString = L10N.getStr("expandPanes"); |
michael@0 | 129 | |
michael@0 | 130 | this._sourcesPane.setAttribute("width", Prefs.sourcesWidth); |
michael@0 | 131 | this._instrumentsPane.setAttribute("width", Prefs.instrumentsWidth); |
michael@0 | 132 | this.toggleInstrumentsPane({ visible: Prefs.panesVisibleOnStartup }); |
michael@0 | 133 | |
michael@0 | 134 | // Side hosts requires a different arrangement of the debugger widgets. |
michael@0 | 135 | if (gHostType == "side") { |
michael@0 | 136 | this.handleHostChanged(gHostType); |
michael@0 | 137 | } |
michael@0 | 138 | }, |
michael@0 | 139 | |
michael@0 | 140 | /** |
michael@0 | 141 | * Destroys the UI for all the displayed panes. |
michael@0 | 142 | */ |
michael@0 | 143 | _destroyPanes: function() { |
michael@0 | 144 | dumpn("Destroying the DebuggerView panes"); |
michael@0 | 145 | |
michael@0 | 146 | if (gHostType != "side") { |
michael@0 | 147 | Prefs.sourcesWidth = this._sourcesPane.getAttribute("width"); |
michael@0 | 148 | Prefs.instrumentsWidth = this._instrumentsPane.getAttribute("width"); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | this._sourcesPane = null; |
michael@0 | 152 | this._instrumentsPane = null; |
michael@0 | 153 | this._instrumentsPaneToggleButton = null; |
michael@0 | 154 | }, |
michael@0 | 155 | |
michael@0 | 156 | /** |
michael@0 | 157 | * Initializes the VariablesView instance and attaches a controller. |
michael@0 | 158 | */ |
michael@0 | 159 | _initializeVariablesView: function() { |
michael@0 | 160 | this.Variables = new VariablesView(document.getElementById("variables"), { |
michael@0 | 161 | searchPlaceholder: L10N.getStr("emptyVariablesFilterText"), |
michael@0 | 162 | emptyText: L10N.getStr("emptyVariablesText"), |
michael@0 | 163 | onlyEnumVisible: Prefs.variablesOnlyEnumVisible, |
michael@0 | 164 | searchEnabled: Prefs.variablesSearchboxVisible, |
michael@0 | 165 | eval: (variable, value) => { |
michael@0 | 166 | let string = variable.evaluationMacro(variable, value); |
michael@0 | 167 | DebuggerController.StackFrames.evaluate(string); |
michael@0 | 168 | }, |
michael@0 | 169 | lazyEmpty: true |
michael@0 | 170 | }); |
michael@0 | 171 | |
michael@0 | 172 | // Attach the current toolbox to the VView so it can link DOMNodes to |
michael@0 | 173 | // the inspector/highlighter |
michael@0 | 174 | this.Variables.toolbox = DebuggerController._toolbox; |
michael@0 | 175 | |
michael@0 | 176 | // Attach a controller that handles interfacing with the debugger protocol. |
michael@0 | 177 | VariablesViewController.attach(this.Variables, { |
michael@0 | 178 | getEnvironmentClient: aObject => gThreadClient.environment(aObject), |
michael@0 | 179 | getObjectClient: aObject => { |
michael@0 | 180 | return aObject instanceof DebuggerController.Tracer.WrappedObject |
michael@0 | 181 | ? DebuggerController.Tracer.syncGripClient(aObject.object) |
michael@0 | 182 | : gThreadClient.pauseGrip(aObject) |
michael@0 | 183 | } |
michael@0 | 184 | }); |
michael@0 | 185 | |
michael@0 | 186 | // Relay events from the VariablesView. |
michael@0 | 187 | this.Variables.on("fetched", (aEvent, aType) => { |
michael@0 | 188 | switch (aType) { |
michael@0 | 189 | case "scopes": |
michael@0 | 190 | window.emit(EVENTS.FETCHED_SCOPES); |
michael@0 | 191 | break; |
michael@0 | 192 | case "variables": |
michael@0 | 193 | window.emit(EVENTS.FETCHED_VARIABLES); |
michael@0 | 194 | break; |
michael@0 | 195 | case "properties": |
michael@0 | 196 | window.emit(EVENTS.FETCHED_PROPERTIES); |
michael@0 | 197 | break; |
michael@0 | 198 | } |
michael@0 | 199 | }); |
michael@0 | 200 | }, |
michael@0 | 201 | |
michael@0 | 202 | /** |
michael@0 | 203 | * Initializes the Editor instance. |
michael@0 | 204 | * |
michael@0 | 205 | * @param function aCallback |
michael@0 | 206 | * Called after the editor finishes initializing. |
michael@0 | 207 | */ |
michael@0 | 208 | _initializeEditor: function(aCallback) { |
michael@0 | 209 | dumpn("Initializing the DebuggerView editor"); |
michael@0 | 210 | |
michael@0 | 211 | let extraKeys = {}; |
michael@0 | 212 | bindKey("_doTokenSearch", "tokenSearchKey"); |
michael@0 | 213 | bindKey("_doGlobalSearch", "globalSearchKey", { alt: true }); |
michael@0 | 214 | bindKey("_doFunctionSearch", "functionSearchKey"); |
michael@0 | 215 | extraKeys[Editor.keyFor("jumpToLine")] = false; |
michael@0 | 216 | |
michael@0 | 217 | function bindKey(func, key, modifiers = {}) { |
michael@0 | 218 | let key = document.getElementById(key).getAttribute("key"); |
michael@0 | 219 | let shortcut = Editor.accel(key, modifiers); |
michael@0 | 220 | extraKeys[shortcut] = () => DebuggerView.Filtering[func](); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | this.editor = new Editor({ |
michael@0 | 224 | mode: Editor.modes.text, |
michael@0 | 225 | readOnly: true, |
michael@0 | 226 | lineNumbers: true, |
michael@0 | 227 | showAnnotationRuler: true, |
michael@0 | 228 | gutters: [ "breakpoints" ], |
michael@0 | 229 | extraKeys: extraKeys, |
michael@0 | 230 | contextMenu: "sourceEditorContextMenu" |
michael@0 | 231 | }); |
michael@0 | 232 | |
michael@0 | 233 | this.editor.appendTo(document.getElementById("editor")).then(() => { |
michael@0 | 234 | this.editor.extend(DebuggerEditor); |
michael@0 | 235 | this._loadingText = L10N.getStr("loadingText"); |
michael@0 | 236 | this._onEditorLoad(aCallback); |
michael@0 | 237 | }); |
michael@0 | 238 | |
michael@0 | 239 | this.editor.on("gutterClick", (ev, line) => { |
michael@0 | 240 | if (this.editor.hasBreakpoint(line)) { |
michael@0 | 241 | this.editor.removeBreakpoint(line); |
michael@0 | 242 | } else { |
michael@0 | 243 | this.editor.addBreakpoint(line); |
michael@0 | 244 | } |
michael@0 | 245 | }); |
michael@0 | 246 | }, |
michael@0 | 247 | |
michael@0 | 248 | /** |
michael@0 | 249 | * The load event handler for the source editor, also executing any necessary |
michael@0 | 250 | * post-load operations. |
michael@0 | 251 | * |
michael@0 | 252 | * @param function aCallback |
michael@0 | 253 | * Called after the editor finishes loading. |
michael@0 | 254 | */ |
michael@0 | 255 | _onEditorLoad: function(aCallback) { |
michael@0 | 256 | dumpn("Finished loading the DebuggerView editor"); |
michael@0 | 257 | |
michael@0 | 258 | DebuggerController.Breakpoints.initialize().then(() => { |
michael@0 | 259 | window.emit(EVENTS.EDITOR_LOADED, this.editor); |
michael@0 | 260 | aCallback(); |
michael@0 | 261 | }); |
michael@0 | 262 | }, |
michael@0 | 263 | |
michael@0 | 264 | /** |
michael@0 | 265 | * Destroys the Editor instance and also executes any necessary |
michael@0 | 266 | * post-unload operations. |
michael@0 | 267 | * |
michael@0 | 268 | * @param function aCallback |
michael@0 | 269 | * Called after the editor finishes destroying. |
michael@0 | 270 | */ |
michael@0 | 271 | _destroyEditor: function(aCallback) { |
michael@0 | 272 | dumpn("Destroying the DebuggerView editor"); |
michael@0 | 273 | |
michael@0 | 274 | DebuggerController.Breakpoints.destroy().then(() => { |
michael@0 | 275 | window.emit(EVENTS.EDITOR_UNLOADED, this.editor); |
michael@0 | 276 | this.editor.destroy(); |
michael@0 | 277 | this.editor = null; |
michael@0 | 278 | aCallback(); |
michael@0 | 279 | }); |
michael@0 | 280 | }, |
michael@0 | 281 | |
michael@0 | 282 | /** |
michael@0 | 283 | * Display the source editor. |
michael@0 | 284 | */ |
michael@0 | 285 | showEditor: function() { |
michael@0 | 286 | this._editorDeck.selectedIndex = 0; |
michael@0 | 287 | }, |
michael@0 | 288 | |
michael@0 | 289 | /** |
michael@0 | 290 | * Display the black box message. |
michael@0 | 291 | */ |
michael@0 | 292 | showBlackBoxMessage: function() { |
michael@0 | 293 | this._editorDeck.selectedIndex = 1; |
michael@0 | 294 | }, |
michael@0 | 295 | |
michael@0 | 296 | /** |
michael@0 | 297 | * Display the progress bar. |
michael@0 | 298 | */ |
michael@0 | 299 | showProgressBar: function() { |
michael@0 | 300 | this._editorDeck.selectedIndex = 2; |
michael@0 | 301 | }, |
michael@0 | 302 | |
michael@0 | 303 | /** |
michael@0 | 304 | * Show or hide the black box message vs. source editor depending on if the |
michael@0 | 305 | * selected source is black boxed or not. |
michael@0 | 306 | */ |
michael@0 | 307 | maybeShowBlackBoxMessage: function() { |
michael@0 | 308 | let { source } = DebuggerView.Sources.selectedItem.attachment; |
michael@0 | 309 | if (gThreadClient.source(source).isBlackBoxed) { |
michael@0 | 310 | this.showBlackBoxMessage(); |
michael@0 | 311 | } else { |
michael@0 | 312 | this.showEditor(); |
michael@0 | 313 | } |
michael@0 | 314 | }, |
michael@0 | 315 | |
michael@0 | 316 | /** |
michael@0 | 317 | * Sets the currently displayed text contents in the source editor. |
michael@0 | 318 | * This resets the mode and undo stack. |
michael@0 | 319 | * |
michael@0 | 320 | * @param string aTextContent |
michael@0 | 321 | * The source text content. |
michael@0 | 322 | */ |
michael@0 | 323 | _setEditorText: function(aTextContent = "") { |
michael@0 | 324 | this.editor.setMode(Editor.modes.text); |
michael@0 | 325 | this.editor.setText(aTextContent); |
michael@0 | 326 | this.editor.clearDebugLocation(); |
michael@0 | 327 | this.editor.clearHistory(); |
michael@0 | 328 | }, |
michael@0 | 329 | |
michael@0 | 330 | /** |
michael@0 | 331 | * Sets the proper editor mode (JS or HTML) according to the specified |
michael@0 | 332 | * content type, or by determining the type from the url or text content. |
michael@0 | 333 | * |
michael@0 | 334 | * @param string aUrl |
michael@0 | 335 | * The source url. |
michael@0 | 336 | * @param string aContentType [optional] |
michael@0 | 337 | * The source content type. |
michael@0 | 338 | * @param string aTextContent [optional] |
michael@0 | 339 | * The source text content. |
michael@0 | 340 | */ |
michael@0 | 341 | _setEditorMode: function(aUrl, aContentType = "", aTextContent = "") { |
michael@0 | 342 | // Avoid setting the editor mode for very large files. |
michael@0 | 343 | // Is this still necessary? See bug 929225. |
michael@0 | 344 | if (aTextContent.length >= SOURCE_SYNTAX_HIGHLIGHT_MAX_FILE_SIZE) { |
michael@0 | 345 | return void this.editor.setMode(Editor.modes.text); |
michael@0 | 346 | } |
michael@0 | 347 | |
michael@0 | 348 | // Use JS mode for files with .js and .jsm extensions. |
michael@0 | 349 | if (SourceUtils.isJavaScript(aUrl, aContentType)) { |
michael@0 | 350 | return void this.editor.setMode(Editor.modes.js); |
michael@0 | 351 | } |
michael@0 | 352 | |
michael@0 | 353 | // Use HTML mode for files in which the first non whitespace character is |
michael@0 | 354 | // <, regardless of extension. |
michael@0 | 355 | if (aTextContent.match(/^\s*</)) { |
michael@0 | 356 | return void this.editor.setMode(Editor.modes.html); |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | // Unknown language, use text. |
michael@0 | 360 | this.editor.setMode(Editor.modes.text); |
michael@0 | 361 | }, |
michael@0 | 362 | |
michael@0 | 363 | /** |
michael@0 | 364 | * Sets the currently displayed source text in the editor. |
michael@0 | 365 | * |
michael@0 | 366 | * You should use DebuggerView.updateEditor instead. It updates the current |
michael@0 | 367 | * caret and debug location based on a requested url and line. |
michael@0 | 368 | * |
michael@0 | 369 | * @param object aSource |
michael@0 | 370 | * The source object coming from the active thread. |
michael@0 | 371 | * @param object aFlags |
michael@0 | 372 | * Additional options for setting the source. Supported options: |
michael@0 | 373 | * - force: boolean allowing whether we can get the selected url's |
michael@0 | 374 | * text again. |
michael@0 | 375 | * @return object |
michael@0 | 376 | * A promise that is resolved after the source text has been set. |
michael@0 | 377 | */ |
michael@0 | 378 | _setEditorSource: function(aSource, aFlags={}) { |
michael@0 | 379 | // Avoid setting the same source text in the editor again. |
michael@0 | 380 | if (this._editorSource.url == aSource.url && !aFlags.force) { |
michael@0 | 381 | return this._editorSource.promise; |
michael@0 | 382 | } |
michael@0 | 383 | let transportType = gClient.localTransport ? "_LOCAL" : "_REMOTE"; |
michael@0 | 384 | let histogramId = "DEVTOOLS_DEBUGGER_DISPLAY_SOURCE" + transportType + "_MS"; |
michael@0 | 385 | let histogram = Services.telemetry.getHistogramById(histogramId); |
michael@0 | 386 | let startTime = Date.now(); |
michael@0 | 387 | |
michael@0 | 388 | let deferred = promise.defer(); |
michael@0 | 389 | |
michael@0 | 390 | this._setEditorText(L10N.getStr("loadingText")); |
michael@0 | 391 | this._editorSource = { url: aSource.url, promise: deferred.promise }; |
michael@0 | 392 | |
michael@0 | 393 | DebuggerController.SourceScripts.getText(aSource).then(([, aText, aContentType]) => { |
michael@0 | 394 | // Avoid setting an unexpected source. This may happen when switching |
michael@0 | 395 | // very fast between sources that haven't been fetched yet. |
michael@0 | 396 | if (this._editorSource.url != aSource.url) { |
michael@0 | 397 | return; |
michael@0 | 398 | } |
michael@0 | 399 | |
michael@0 | 400 | this._setEditorText(aText); |
michael@0 | 401 | this._setEditorMode(aSource.url, aContentType, aText); |
michael@0 | 402 | |
michael@0 | 403 | // Synchronize any other components with the currently displayed source. |
michael@0 | 404 | DebuggerView.Sources.selectedValue = aSource.url; |
michael@0 | 405 | DebuggerController.Breakpoints.updateEditorBreakpoints(); |
michael@0 | 406 | |
michael@0 | 407 | histogram.add(Date.now() - startTime); |
michael@0 | 408 | |
michael@0 | 409 | // Resolve and notify that a source file was shown. |
michael@0 | 410 | window.emit(EVENTS.SOURCE_SHOWN, aSource); |
michael@0 | 411 | deferred.resolve([aSource, aText, aContentType]); |
michael@0 | 412 | }, |
michael@0 | 413 | ([, aError]) => { |
michael@0 | 414 | let msg = L10N.getStr("errorLoadingText") + DevToolsUtils.safeErrorString(aError); |
michael@0 | 415 | this._setEditorText(msg); |
michael@0 | 416 | Cu.reportError(msg); |
michael@0 | 417 | dumpn(msg); |
michael@0 | 418 | |
michael@0 | 419 | // Reject and notify that there was an error showing the source file. |
michael@0 | 420 | window.emit(EVENTS.SOURCE_ERROR_SHOWN, aSource); |
michael@0 | 421 | deferred.reject([aSource, aError]); |
michael@0 | 422 | }); |
michael@0 | 423 | |
michael@0 | 424 | return deferred.promise; |
michael@0 | 425 | }, |
michael@0 | 426 | |
michael@0 | 427 | /** |
michael@0 | 428 | * Update the source editor's current caret and debug location based on |
michael@0 | 429 | * a requested url and line. |
michael@0 | 430 | * |
michael@0 | 431 | * @param string aUrl |
michael@0 | 432 | * The target source url. |
michael@0 | 433 | * @param number aLine [optional] |
michael@0 | 434 | * The target line in the source. |
michael@0 | 435 | * @param object aFlags [optional] |
michael@0 | 436 | * Additional options for showing the source. Supported options: |
michael@0 | 437 | * - charOffset: character offset for the caret or debug location |
michael@0 | 438 | * - lineOffset: line offset for the caret or debug location |
michael@0 | 439 | * - columnOffset: column offset for the caret or debug location |
michael@0 | 440 | * - noCaret: don't set the caret location at the specified line |
michael@0 | 441 | * - noDebug: don't set the debug location at the specified line |
michael@0 | 442 | * - align: string specifying whether to align the specified line |
michael@0 | 443 | * at the "top", "center" or "bottom" of the editor |
michael@0 | 444 | * - force: boolean allowing whether we can get the selected url's |
michael@0 | 445 | * text again |
michael@0 | 446 | * @return object |
michael@0 | 447 | * A promise that is resolved after the source text has been set. |
michael@0 | 448 | */ |
michael@0 | 449 | setEditorLocation: function(aUrl, aLine = 0, aFlags = {}) { |
michael@0 | 450 | // Avoid trying to set a source for a url that isn't known yet. |
michael@0 | 451 | if (!this.Sources.containsValue(aUrl)) { |
michael@0 | 452 | return promise.reject(new Error("Unknown source for the specified URL.")); |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | // If the line is not specified, default to the current frame's position, |
michael@0 | 456 | // if available and the frame's url corresponds to the requested url. |
michael@0 | 457 | if (!aLine) { |
michael@0 | 458 | let cachedFrames = DebuggerController.activeThread.cachedFrames; |
michael@0 | 459 | let currentDepth = DebuggerController.StackFrames.currentFrameDepth; |
michael@0 | 460 | let frame = cachedFrames[currentDepth]; |
michael@0 | 461 | if (frame && frame.where.url == aUrl) { |
michael@0 | 462 | aLine = frame.where.line; |
michael@0 | 463 | } |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | let sourceItem = this.Sources.getItemByValue(aUrl); |
michael@0 | 467 | let sourceForm = sourceItem.attachment.source; |
michael@0 | 468 | |
michael@0 | 469 | // Make sure the requested source client is shown in the editor, then |
michael@0 | 470 | // update the source editor's caret position and debug location. |
michael@0 | 471 | return this._setEditorSource(sourceForm, aFlags).then(([,, aContentType]) => { |
michael@0 | 472 | // Record the contentType learned from fetching |
michael@0 | 473 | sourceForm.contentType = aContentType; |
michael@0 | 474 | // Line numbers in the source editor should start from 1. If invalid |
michael@0 | 475 | // or not specified, then don't do anything. |
michael@0 | 476 | if (aLine < 1) { |
michael@0 | 477 | window.emit(EVENTS.EDITOR_LOCATION_SET); |
michael@0 | 478 | return; |
michael@0 | 479 | } |
michael@0 | 480 | if (aFlags.charOffset) { |
michael@0 | 481 | aLine += this.editor.getPosition(aFlags.charOffset).line; |
michael@0 | 482 | } |
michael@0 | 483 | if (aFlags.lineOffset) { |
michael@0 | 484 | aLine += aFlags.lineOffset; |
michael@0 | 485 | } |
michael@0 | 486 | if (!aFlags.noCaret) { |
michael@0 | 487 | let location = { line: aLine -1, ch: aFlags.columnOffset || 0 }; |
michael@0 | 488 | this.editor.setCursor(location, aFlags.align); |
michael@0 | 489 | } |
michael@0 | 490 | if (!aFlags.noDebug) { |
michael@0 | 491 | this.editor.setDebugLocation(aLine - 1); |
michael@0 | 492 | } |
michael@0 | 493 | window.emit(EVENTS.EDITOR_LOCATION_SET); |
michael@0 | 494 | }).then(null, console.error); |
michael@0 | 495 | }, |
michael@0 | 496 | |
michael@0 | 497 | /** |
michael@0 | 498 | * Gets the visibility state of the instruments pane. |
michael@0 | 499 | * @return boolean |
michael@0 | 500 | */ |
michael@0 | 501 | get instrumentsPaneHidden() |
michael@0 | 502 | this._instrumentsPane.hasAttribute("pane-collapsed"), |
michael@0 | 503 | |
michael@0 | 504 | /** |
michael@0 | 505 | * Gets the currently selected tab in the instruments pane. |
michael@0 | 506 | * @return string |
michael@0 | 507 | */ |
michael@0 | 508 | get instrumentsPaneTab() |
michael@0 | 509 | this._instrumentsPane.selectedTab.id, |
michael@0 | 510 | |
michael@0 | 511 | /** |
michael@0 | 512 | * Sets the instruments pane hidden or visible. |
michael@0 | 513 | * |
michael@0 | 514 | * @param object aFlags |
michael@0 | 515 | * An object containing some of the following properties: |
michael@0 | 516 | * - visible: true if the pane should be shown, false to hide |
michael@0 | 517 | * - animated: true to display an animation on toggle |
michael@0 | 518 | * - delayed: true to wait a few cycles before toggle |
michael@0 | 519 | * - callback: a function to invoke when the toggle finishes |
michael@0 | 520 | * @param number aTabIndex [optional] |
michael@0 | 521 | * The index of the intended selected tab in the details pane. |
michael@0 | 522 | */ |
michael@0 | 523 | toggleInstrumentsPane: function(aFlags, aTabIndex) { |
michael@0 | 524 | let pane = this._instrumentsPane; |
michael@0 | 525 | let button = this._instrumentsPaneToggleButton; |
michael@0 | 526 | |
michael@0 | 527 | ViewHelpers.togglePane(aFlags, pane); |
michael@0 | 528 | |
michael@0 | 529 | if (aFlags.visible) { |
michael@0 | 530 | button.removeAttribute("pane-collapsed"); |
michael@0 | 531 | button.setAttribute("tooltiptext", this._collapsePaneString); |
michael@0 | 532 | } else { |
michael@0 | 533 | button.setAttribute("pane-collapsed", ""); |
michael@0 | 534 | button.setAttribute("tooltiptext", this._expandPaneString); |
michael@0 | 535 | } |
michael@0 | 536 | |
michael@0 | 537 | if (aTabIndex !== undefined) { |
michael@0 | 538 | pane.selectedIndex = aTabIndex; |
michael@0 | 539 | } |
michael@0 | 540 | }, |
michael@0 | 541 | |
michael@0 | 542 | /** |
michael@0 | 543 | * Sets the instruments pane visible after a short period of time. |
michael@0 | 544 | * |
michael@0 | 545 | * @param function aCallback |
michael@0 | 546 | * A function to invoke when the toggle finishes. |
michael@0 | 547 | */ |
michael@0 | 548 | showInstrumentsPane: function(aCallback) { |
michael@0 | 549 | DebuggerView.toggleInstrumentsPane({ |
michael@0 | 550 | visible: true, |
michael@0 | 551 | animated: true, |
michael@0 | 552 | delayed: true, |
michael@0 | 553 | callback: aCallback |
michael@0 | 554 | }, 0); |
michael@0 | 555 | }, |
michael@0 | 556 | |
michael@0 | 557 | /** |
michael@0 | 558 | * Handles a tab selection event on the instruments pane. |
michael@0 | 559 | */ |
michael@0 | 560 | _onInstrumentsPaneTabSelect: function() { |
michael@0 | 561 | if (this._instrumentsPane.selectedTab.id == "events-tab") { |
michael@0 | 562 | DebuggerController.Breakpoints.DOM.scheduleEventListenersFetch(); |
michael@0 | 563 | } |
michael@0 | 564 | }, |
michael@0 | 565 | |
michael@0 | 566 | /** |
michael@0 | 567 | * Handles a host change event issued by the parent toolbox. |
michael@0 | 568 | * |
michael@0 | 569 | * @param string aType |
michael@0 | 570 | * The host type, either "bottom", "side" or "window". |
michael@0 | 571 | */ |
michael@0 | 572 | handleHostChanged: function(aType) { |
michael@0 | 573 | let newLayout = ""; |
michael@0 | 574 | |
michael@0 | 575 | if (aType == "side") { |
michael@0 | 576 | newLayout = "vertical"; |
michael@0 | 577 | this._enterVerticalLayout(); |
michael@0 | 578 | } else { |
michael@0 | 579 | newLayout = "horizontal"; |
michael@0 | 580 | this._enterHorizontalLayout(); |
michael@0 | 581 | } |
michael@0 | 582 | |
michael@0 | 583 | this._hostType = aType; |
michael@0 | 584 | this._body.setAttribute("layout", newLayout); |
michael@0 | 585 | window.emit(EVENTS.LAYOUT_CHANGED, newLayout); |
michael@0 | 586 | }, |
michael@0 | 587 | |
michael@0 | 588 | /** |
michael@0 | 589 | * Switches the debugger widgets to a horizontal layout. |
michael@0 | 590 | */ |
michael@0 | 591 | _enterVerticalLayout: function() { |
michael@0 | 592 | let normContainer = document.getElementById("debugger-widgets"); |
michael@0 | 593 | let vertContainer = document.getElementById("vertical-layout-panes-container"); |
michael@0 | 594 | |
michael@0 | 595 | // Move the soruces and instruments panes in a different container. |
michael@0 | 596 | let splitter = document.getElementById("sources-and-instruments-splitter"); |
michael@0 | 597 | vertContainer.insertBefore(this._sourcesPane, splitter); |
michael@0 | 598 | vertContainer.appendChild(this._instrumentsPane); |
michael@0 | 599 | |
michael@0 | 600 | // Make sure the vertical layout container's height doesn't repeatedly |
michael@0 | 601 | // grow or shrink based on the displayed sources, variables etc. |
michael@0 | 602 | vertContainer.setAttribute("height", |
michael@0 | 603 | vertContainer.getBoundingClientRect().height); |
michael@0 | 604 | }, |
michael@0 | 605 | |
michael@0 | 606 | /** |
michael@0 | 607 | * Switches the debugger widgets to a vertical layout. |
michael@0 | 608 | */ |
michael@0 | 609 | _enterHorizontalLayout: function() { |
michael@0 | 610 | let normContainer = document.getElementById("debugger-widgets"); |
michael@0 | 611 | let vertContainer = document.getElementById("vertical-layout-panes-container"); |
michael@0 | 612 | |
michael@0 | 613 | // The sources and instruments pane need to be inserted at their |
michael@0 | 614 | // previous locations in their normal container. |
michael@0 | 615 | let splitter = document.getElementById("sources-and-editor-splitter"); |
michael@0 | 616 | normContainer.insertBefore(this._sourcesPane, splitter); |
michael@0 | 617 | normContainer.appendChild(this._instrumentsPane); |
michael@0 | 618 | |
michael@0 | 619 | // Revert to the preferred sources and instruments widths, because |
michael@0 | 620 | // they flexed in the vertical layout. |
michael@0 | 621 | this._sourcesPane.setAttribute("width", Prefs.sourcesWidth); |
michael@0 | 622 | this._instrumentsPane.setAttribute("width", Prefs.instrumentsWidth); |
michael@0 | 623 | }, |
michael@0 | 624 | |
michael@0 | 625 | /** |
michael@0 | 626 | * Handles any initialization on a tab navigation event issued by the client. |
michael@0 | 627 | */ |
michael@0 | 628 | handleTabNavigation: function() { |
michael@0 | 629 | dumpn("Handling tab navigation in the DebuggerView"); |
michael@0 | 630 | |
michael@0 | 631 | this.Filtering.clearSearch(); |
michael@0 | 632 | this.FilteredSources.clearView(); |
michael@0 | 633 | this.FilteredFunctions.clearView(); |
michael@0 | 634 | this.GlobalSearch.clearView(); |
michael@0 | 635 | this.StackFrames.empty(); |
michael@0 | 636 | this.Sources.empty(); |
michael@0 | 637 | this.Variables.empty(); |
michael@0 | 638 | this.EventListeners.empty(); |
michael@0 | 639 | |
michael@0 | 640 | if (this.editor) { |
michael@0 | 641 | this.editor.setMode(Editor.modes.text); |
michael@0 | 642 | this.editor.setText(""); |
michael@0 | 643 | this.editor.clearHistory(); |
michael@0 | 644 | this._editorSource = {}; |
michael@0 | 645 | } |
michael@0 | 646 | |
michael@0 | 647 | this.Sources.emptyText = L10N.getStr("loadingSourcesText"); |
michael@0 | 648 | }, |
michael@0 | 649 | |
michael@0 | 650 | _startup: null, |
michael@0 | 651 | _shutdown: null, |
michael@0 | 652 | Toolbar: null, |
michael@0 | 653 | Options: null, |
michael@0 | 654 | Filtering: null, |
michael@0 | 655 | FilteredSources: null, |
michael@0 | 656 | FilteredFunctions: null, |
michael@0 | 657 | GlobalSearch: null, |
michael@0 | 658 | StackFrames: null, |
michael@0 | 659 | Sources: null, |
michael@0 | 660 | Tracer: null, |
michael@0 | 661 | Variables: null, |
michael@0 | 662 | VariableBubble: null, |
michael@0 | 663 | WatchExpressions: null, |
michael@0 | 664 | EventListeners: null, |
michael@0 | 665 | editor: null, |
michael@0 | 666 | _editorSource: {}, |
michael@0 | 667 | _loadingText: "", |
michael@0 | 668 | _body: null, |
michael@0 | 669 | _editorDeck: null, |
michael@0 | 670 | _sourcesPane: null, |
michael@0 | 671 | _instrumentsPane: null, |
michael@0 | 672 | _instrumentsPaneToggleButton: null, |
michael@0 | 673 | _collapsePaneString: "", |
michael@0 | 674 | _expandPaneString: "" |
michael@0 | 675 | }; |
michael@0 | 676 | |
michael@0 | 677 | /** |
michael@0 | 678 | * A custom items container, used for displaying views like the |
michael@0 | 679 | * FilteredSources, FilteredFunctions etc., inheriting the generic WidgetMethods. |
michael@0 | 680 | */ |
michael@0 | 681 | function ResultsPanelContainer() { |
michael@0 | 682 | } |
michael@0 | 683 | |
michael@0 | 684 | ResultsPanelContainer.prototype = Heritage.extend(WidgetMethods, { |
michael@0 | 685 | /** |
michael@0 | 686 | * Sets the anchor node for this container panel. |
michael@0 | 687 | * @param nsIDOMNode aNode |
michael@0 | 688 | */ |
michael@0 | 689 | set anchor(aNode) { |
michael@0 | 690 | this._anchor = aNode; |
michael@0 | 691 | |
michael@0 | 692 | // If the anchor node is not null, create a panel to attach to the anchor |
michael@0 | 693 | // when showing the popup. |
michael@0 | 694 | if (aNode) { |
michael@0 | 695 | if (!this._panel) { |
michael@0 | 696 | this._panel = document.createElement("panel"); |
michael@0 | 697 | this._panel.id = "results-panel"; |
michael@0 | 698 | this._panel.setAttribute("level", "top"); |
michael@0 | 699 | this._panel.setAttribute("noautofocus", "true"); |
michael@0 | 700 | this._panel.setAttribute("consumeoutsideclicks", "false"); |
michael@0 | 701 | document.documentElement.appendChild(this._panel); |
michael@0 | 702 | } |
michael@0 | 703 | if (!this.widget) { |
michael@0 | 704 | this.widget = new SimpleListWidget(this._panel); |
michael@0 | 705 | this.autoFocusOnFirstItem = false; |
michael@0 | 706 | this.autoFocusOnSelection = false; |
michael@0 | 707 | this.maintainSelectionVisible = false; |
michael@0 | 708 | } |
michael@0 | 709 | } |
michael@0 | 710 | // Cleanup the anchor and remove the previously created panel. |
michael@0 | 711 | else { |
michael@0 | 712 | this._panel.remove(); |
michael@0 | 713 | this._panel = null; |
michael@0 | 714 | this.widget = null; |
michael@0 | 715 | } |
michael@0 | 716 | }, |
michael@0 | 717 | |
michael@0 | 718 | /** |
michael@0 | 719 | * Gets the anchor node for this container panel. |
michael@0 | 720 | * @return nsIDOMNode |
michael@0 | 721 | */ |
michael@0 | 722 | get anchor() { |
michael@0 | 723 | return this._anchor; |
michael@0 | 724 | }, |
michael@0 | 725 | |
michael@0 | 726 | /** |
michael@0 | 727 | * Sets the container panel hidden or visible. It's hidden by default. |
michael@0 | 728 | * @param boolean aFlag |
michael@0 | 729 | */ |
michael@0 | 730 | set hidden(aFlag) { |
michael@0 | 731 | if (aFlag) { |
michael@0 | 732 | this._panel.hidden = true; |
michael@0 | 733 | this._panel.hidePopup(); |
michael@0 | 734 | } else { |
michael@0 | 735 | this._panel.hidden = false; |
michael@0 | 736 | this._panel.openPopup(this._anchor, this.position, this.left, this.top); |
michael@0 | 737 | } |
michael@0 | 738 | }, |
michael@0 | 739 | |
michael@0 | 740 | /** |
michael@0 | 741 | * Gets this container's visibility state. |
michael@0 | 742 | * @return boolean |
michael@0 | 743 | */ |
michael@0 | 744 | get hidden() |
michael@0 | 745 | this._panel.state == "closed" || |
michael@0 | 746 | this._panel.state == "hiding", |
michael@0 | 747 | |
michael@0 | 748 | /** |
michael@0 | 749 | * Removes all items from this container and hides it. |
michael@0 | 750 | */ |
michael@0 | 751 | clearView: function() { |
michael@0 | 752 | this.hidden = true; |
michael@0 | 753 | this.empty(); |
michael@0 | 754 | }, |
michael@0 | 755 | |
michael@0 | 756 | /** |
michael@0 | 757 | * Selects the next found item in this container. |
michael@0 | 758 | * Does not change the currently focused node. |
michael@0 | 759 | */ |
michael@0 | 760 | selectNext: function() { |
michael@0 | 761 | let nextIndex = this.selectedIndex + 1; |
michael@0 | 762 | if (nextIndex >= this.itemCount) { |
michael@0 | 763 | nextIndex = 0; |
michael@0 | 764 | } |
michael@0 | 765 | this.selectedItem = this.getItemAtIndex(nextIndex); |
michael@0 | 766 | }, |
michael@0 | 767 | |
michael@0 | 768 | /** |
michael@0 | 769 | * Selects the previously found item in this container. |
michael@0 | 770 | * Does not change the currently focused node. |
michael@0 | 771 | */ |
michael@0 | 772 | selectPrev: function() { |
michael@0 | 773 | let prevIndex = this.selectedIndex - 1; |
michael@0 | 774 | if (prevIndex < 0) { |
michael@0 | 775 | prevIndex = this.itemCount - 1; |
michael@0 | 776 | } |
michael@0 | 777 | this.selectedItem = this.getItemAtIndex(prevIndex); |
michael@0 | 778 | }, |
michael@0 | 779 | |
michael@0 | 780 | /** |
michael@0 | 781 | * Customization function for creating an item's UI. |
michael@0 | 782 | * |
michael@0 | 783 | * @param string aLabel |
michael@0 | 784 | * The item's label string. |
michael@0 | 785 | * @param string aBeforeLabel |
michael@0 | 786 | * An optional string shown before the label. |
michael@0 | 787 | * @param string aBelowLabel |
michael@0 | 788 | * An optional string shown underneath the label. |
michael@0 | 789 | */ |
michael@0 | 790 | _createItemView: function(aLabel, aBelowLabel, aBeforeLabel) { |
michael@0 | 791 | let container = document.createElement("vbox"); |
michael@0 | 792 | container.className = "results-panel-item"; |
michael@0 | 793 | |
michael@0 | 794 | let firstRowLabels = document.createElement("hbox"); |
michael@0 | 795 | let secondRowLabels = document.createElement("hbox"); |
michael@0 | 796 | |
michael@0 | 797 | if (aBeforeLabel) { |
michael@0 | 798 | let beforeLabelNode = document.createElement("label"); |
michael@0 | 799 | beforeLabelNode.className = "plain results-panel-item-label-before"; |
michael@0 | 800 | beforeLabelNode.setAttribute("value", aBeforeLabel); |
michael@0 | 801 | firstRowLabels.appendChild(beforeLabelNode); |
michael@0 | 802 | } |
michael@0 | 803 | |
michael@0 | 804 | let labelNode = document.createElement("label"); |
michael@0 | 805 | labelNode.className = "plain results-panel-item-label"; |
michael@0 | 806 | labelNode.setAttribute("value", aLabel); |
michael@0 | 807 | firstRowLabels.appendChild(labelNode); |
michael@0 | 808 | |
michael@0 | 809 | if (aBelowLabel) { |
michael@0 | 810 | let belowLabelNode = document.createElement("label"); |
michael@0 | 811 | belowLabelNode.className = "plain results-panel-item-label-below"; |
michael@0 | 812 | belowLabelNode.setAttribute("value", aBelowLabel); |
michael@0 | 813 | secondRowLabels.appendChild(belowLabelNode); |
michael@0 | 814 | } |
michael@0 | 815 | |
michael@0 | 816 | container.appendChild(firstRowLabels); |
michael@0 | 817 | container.appendChild(secondRowLabels); |
michael@0 | 818 | |
michael@0 | 819 | return container; |
michael@0 | 820 | }, |
michael@0 | 821 | |
michael@0 | 822 | _anchor: null, |
michael@0 | 823 | _panel: null, |
michael@0 | 824 | position: RESULTS_PANEL_POPUP_POSITION, |
michael@0 | 825 | left: 0, |
michael@0 | 826 | top: 0 |
michael@0 | 827 | }); |